From aa8d0c49c18cf2e6b44f4ee9424742862f101825 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 11 Nov 2020 18:02:52 +0800 Subject: Add an option to preview markdown in several activitys. --- .../Activity/CommentActivity.java | 104 ++++---- .../Activity/CommentFullMarkdownActivity.java | 266 --------------------- .../Activity/EditCommentActivity.java | 6 +- .../Activity/EditPostActivity.java | 6 +- .../Activity/FullMarkdownActivity.java | 266 +++++++++++++++++++++ .../Activity/PostTextActivity.java | 88 +++---- .../MarkdownBottomBarRecyclerViewAdapter.java | 3 + .../infinityforreddit/AppComponent.java | 4 +- .../CommentMoreBottomSheetFragment.java | 8 +- 9 files changed, 385 insertions(+), 366 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FullMarkdownActivity.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java index 66043919..31e0a35f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentActivity.java @@ -327,60 +327,64 @@ public class CommentActivity extends BaseActivity { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - onBackPressed(); - return true; - case R.id.action_send_comment_activity: - if (!isSubmitting) { - isSubmitting = true; - if (commentEditText.getText() == null || commentEditText.getText().toString().equals("")) { - isSubmitting = false; - Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show(); - return true; - } + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + onBackPressed(); + return true; + } else if (itemId == R.id.action_preview_comment_activity) { + Intent intent = new Intent(this, FullMarkdownActivity.class); + intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, commentEditText.getText().toString()); + startActivity(intent); + } else if (itemId == R.id.action_send_comment_activity) { + if (!isSubmitting) { + isSubmitting = true; + if (commentEditText.getText() == null || commentEditText.getText().toString().equals("")) { + isSubmitting = false; + Snackbar.make(coordinatorLayout, R.string.comment_content_required, Snackbar.LENGTH_SHORT).show(); + return true; + } - item.setEnabled(false); - item.getIcon().setAlpha(130); - Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE); - sendingSnackbar.show(); - - SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth, - getResources().getConfiguration().locale, mOauthRetrofit, - mAccessToken, - new SendComment.SendCommentListener() { - @Override - public void sendCommentSuccess(Comment comment) { - isSubmitting = false; - item.setEnabled(true); - item.getIcon().setAlpha(255); - Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show(); - Intent returnIntent = new Intent(); - returnIntent.putExtra(RETURN_EXTRA_COMMENT_DATA_KEY, comment); - returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname); - if (isReplying) { - returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); - } - setResult(RESULT_OK, returnIntent); - finish(); + item.setEnabled(false); + item.getIcon().setAlpha(130); + Snackbar sendingSnackbar = Snackbar.make(coordinatorLayout, R.string.sending_comment, Snackbar.LENGTH_INDEFINITE); + sendingSnackbar.show(); + + SendComment.sendComment(commentEditText.getText().toString(), parentFullname, parentDepth, + getResources().getConfiguration().locale, mOauthRetrofit, + mAccessToken, + new SendComment.SendCommentListener() { + @Override + public void sendCommentSuccess(Comment comment) { + isSubmitting = false; + item.setEnabled(true); + item.getIcon().setAlpha(255); + Toast.makeText(CommentActivity.this, R.string.send_comment_success, Toast.LENGTH_SHORT).show(); + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_COMMENT_DATA_KEY, comment); + returnIntent.putExtra(EXTRA_PARENT_FULLNAME_KEY, parentFullname); + if (isReplying) { + returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition); } + setResult(RESULT_OK, returnIntent); + finish(); + } + + @Override + public void sendCommentFailed(@Nullable String errorMessage) { + isSubmitting = false; + sendingSnackbar.dismiss(); + item.setEnabled(true); + item.getIcon().setAlpha(255); - @Override - public void sendCommentFailed(@Nullable String errorMessage) { - isSubmitting = false; - sendingSnackbar.dismiss(); - item.setEnabled(true); - item.getIcon().setAlpha(255); - - if (errorMessage == null || !errorMessage.equals("")) { - Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); - } else { - Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); - } + if (errorMessage == null || !errorMessage.equals("")) { + Snackbar.make(coordinatorLayout, R.string.send_comment_failed, Snackbar.LENGTH_SHORT).show(); + } else { + Snackbar.make(coordinatorLayout, errorMessage, Snackbar.LENGTH_SHORT).show(); } - }); - } - return true; + } + }); + } + return true; } return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java deleted file mode 100644 index 9d8f1f6f..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/CommentFullMarkdownActivity.java +++ /dev/null @@ -1,266 +0,0 @@ -package ml.docilealligator.infinityforreddit.Activity; - -import android.content.Intent; -import android.content.SharedPreferences; -import android.graphics.Color; -import android.net.Uri; -import android.os.Build; -import android.os.Bundle; -import android.text.SpannableStringBuilder; -import android.text.Spanned; -import android.text.TextPaint; -import android.text.style.ClickableSpan; -import android.text.style.SuperscriptSpan; -import android.text.util.Linkify; -import android.view.MenuItem; -import android.view.View; -import android.view.Window; -import android.view.WindowManager; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.appcompat.widget.Toolbar; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import com.google.android.material.appbar.AppBarLayout; -import com.google.android.material.appbar.CollapsingToolbarLayout; -import com.r0adkll.slidr.Slidr; -import com.r0adkll.slidr.model.SlidrInterface; - -import org.commonmark.ext.gfm.tables.TableBlock; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.inject.Inject; -import javax.inject.Named; - -import butterknife.BindView; -import butterknife.ButterKnife; -import io.noties.markwon.AbstractMarkwonPlugin; -import io.noties.markwon.Markwon; -import io.noties.markwon.MarkwonConfiguration; -import io.noties.markwon.core.MarkwonTheme; -import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; -import io.noties.markwon.linkify.LinkifyPlugin; -import io.noties.markwon.recycler.MarkwonAdapter; -import io.noties.markwon.recycler.table.TableEntry; -import io.noties.markwon.recycler.table.TableEntryPlugin; -import io.noties.markwon.simple.ext.SimpleExtPlugin; -import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; -import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; - -public class CommentFullMarkdownActivity extends BaseActivity { - - public static final String EXTRA_COMMENT_MARKDOWN = "ECM"; - public static final String EXTRA_IS_NSFW = "EIN"; - - @BindView(R.id.coordinator_layout_comment_full_markdown_activity) - CoordinatorLayout coordinatorLayout; - @BindView(R.id.appbar_layout_comment_full_markdown_activity) - AppBarLayout appBarLayout; - @BindView(R.id.collapsing_toolbar_layout_comment_full_markdown_activity) - CollapsingToolbarLayout collapsingToolbarLayout; - @BindView(R.id.toolbar_comment_full_markdown_activity) - Toolbar toolbar; - @BindView(R.id.content_markdown_view_comment_full_markdown_activity) - RecyclerView markdownRecyclerView; - @Inject - @Named("default") - SharedPreferences mSharedPreferences; - @Inject - CustomThemeWrapper mCustomThemeWrapper; - private SlidrInterface mSlidrInterface; - - @Override - protected void onCreate(Bundle savedInstanceState) { - ((Infinity) getApplication()).getAppComponent().inject(this); - - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_comment_full_markdown); - - ButterKnife.bind(this); - - applyCustomTheme(); - - setSupportActionBar(toolbar); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - setTitle(" "); - - if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { - mSlidrInterface = Slidr.attach(this); - } - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - Window window = getWindow(); - - if (isChangeStatusBarIconColor()) { - addOnOffsetChangedListener(appBarLayout); - } - - if (isImmersiveInterface()) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | - View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | - View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); - } else { - window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); - } - adjustToolbar(toolbar); - markdownRecyclerView.setPadding(0, 0, 0, getNavBarHeight()); - } - } - - String commentMarkdown = getIntent().getStringExtra(EXTRA_COMMENT_MARKDOWN); - boolean isNsfw = getIntent().getBooleanExtra(EXTRA_IS_NSFW, false); - int markdownColor = mCustomThemeWrapper.getCommentColor(); - int spoilerBackgroundColor = markdownColor | 0xFF000000; - int linkColor = mCustomThemeWrapper.getLinkColor(); - Markwon markwon = Markwon.builder(this) - .usePlugin(new AbstractMarkwonPlugin() { - @NonNull - @Override - public String processMarkdown(@NonNull String markdown) { - StringBuilder markdownStringBuilder = new StringBuilder(markdown); - Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); - Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); - while (matcher.find()) { - markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); - } - return super.processMarkdown(markdownStringBuilder.toString()); - } - - @Override - public void afterSetText(@NonNull TextView textView) { - textView.setHighlightColor(Color.TRANSPARENT); - SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString()); - Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); - Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); - int start = 0; - boolean find = false; - while (matcher.find(start)) { - find = true; - markdownStringBuilder.delete(matcher.end() - 2, matcher.end()); - markdownStringBuilder.delete(matcher.start(), matcher.start() + 2); - ClickableSpan clickableSpan = new ClickableSpan() { - private boolean isShowing = false; - @Override - public void updateDrawState(@NonNull TextPaint ds) { - if (isShowing) { - super.updateDrawState(ds); - ds.setColor(markdownColor); - } else { - ds.bgColor = spoilerBackgroundColor; - ds.setColor(markdownColor); - } - ds.setUnderlineText(false); - } - - @Override - public void onClick(@NonNull View view) { - isShowing = !isShowing; - view.invalidate(); - } - }; - markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - start = matcher.end() - 4; - } - if (find) { - textView.setText(markdownStringBuilder); - } - } - - @Override - public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { - textView.setTextColor(markdownColor); - } - - @Override - public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { - builder.linkResolver((view, link) -> { - Intent intent = new Intent(CommentFullMarkdownActivity.this, LinkResolverActivity.class); - Uri uri = Uri.parse(link); - if (uri.getScheme() == null && uri.getHost() == null) { - intent.setData(LinkResolverActivity.getRedditUriByPath(link)); - } else { - intent.setData(uri); - } - intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, isNsfw); - startActivity(intent); - }); - } - - @Override - public void configureTheme(@NonNull MarkwonTheme.Builder builder) { - builder.linkColor(linkColor); - } - }) - .usePlugin(StrikethroughPlugin.create()) - .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) - .usePlugin(SimpleExtPlugin.create(plugin -> - plugin.addExtension(1, '^', (configuration, props) -> { - return new SuperscriptSpan(); - }) - ) - ) - .usePlugin(TableEntryPlugin.create(this)) - .build(); - - MarkwonAdapter markwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) - .include(TableBlock.class, TableEntry.create(builder -> builder - .tableLayout(R.layout.adapter_table_block, R.id.table_layout) - .textLayoutIsRoot(R.layout.view_table_entry_cell))) - .build(); - LinearLayoutManager linearLayoutManager = new MarkwonLinearLayoutManager(this, new MarkwonLinearLayoutManager.HorizontalScrollViewScrolledListener() { - @Override - public void onScrolledLeft() { - if (mSlidrInterface != null) { - mSlidrInterface.lock(); - } - } - - @Override - public void onScrolledRight() { - if (mSlidrInterface != null) { - mSlidrInterface.unlock(); - } - } - }); - markdownRecyclerView.setLayoutManager(linearLayoutManager); - markdownRecyclerView.setAdapter(markwonAdapter); - markwonAdapter.setMarkdown(markwon, commentMarkdown); - markwonAdapter.notifyDataSetChanged(); - } - - @Override - public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - return true; - } - - return false; - } - - @Override - protected SharedPreferences getDefaultSharedPreferences() { - return mSharedPreferences; - } - - @Override - protected CustomThemeWrapper getCustomThemeWrapper() { - return mCustomThemeWrapper; - } - - @Override - protected void applyCustomTheme() { - coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getCommentBackgroundColor()); - applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); - } -} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java index b18618cf..14c875be 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditCommentActivity.java @@ -156,7 +156,11 @@ public class EditCommentActivity extends BaseActivity { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == R.id.action_send_edit_comment_activity) { + if (item.getItemId() == R.id.action_preview_edit_comment_activity) { + Intent intent = new Intent(this, FullMarkdownActivity.class); + intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); + startActivity(intent); + } else if (item.getItemId() == R.id.action_send_edit_comment_activity) { if (!isSubmitting) { isSubmitting = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java index 18e997ed..a773a386 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/EditPostActivity.java @@ -167,7 +167,11 @@ public class EditPostActivity extends BaseActivity { @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == R.id.action_send_edit_post_activity) { + if (item.getItemId() == R.id.action_preview_edit_post_activity) { + Intent intent = new Intent(this, FullMarkdownActivity.class); + intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); + startActivity(intent); + } else if (item.getItemId() == R.id.action_send_edit_post_activity) { if (!isSubmitting) { isSubmitting = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FullMarkdownActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FullMarkdownActivity.java new file mode 100644 index 00000000..5b262503 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FullMarkdownActivity.java @@ -0,0 +1,266 @@ +package ml.docilealligator.infinityforreddit.Activity; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.text.SpannableStringBuilder; +import android.text.Spanned; +import android.text.TextPaint; +import android.text.style.ClickableSpan; +import android.text.style.SuperscriptSpan; +import android.text.util.Linkify; +import android.view.MenuItem; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.appbar.CollapsingToolbarLayout; +import com.r0adkll.slidr.Slidr; +import com.r0adkll.slidr.model.SlidrInterface; + +import org.commonmark.ext.gfm.tables.TableBlock; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import io.noties.markwon.AbstractMarkwonPlugin; +import io.noties.markwon.Markwon; +import io.noties.markwon.MarkwonConfiguration; +import io.noties.markwon.core.MarkwonTheme; +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin; +import io.noties.markwon.linkify.LinkifyPlugin; +import io.noties.markwon.recycler.MarkwonAdapter; +import io.noties.markwon.recycler.table.TableEntry; +import io.noties.markwon.recycler.table.TableEntryPlugin; +import io.noties.markwon.simple.ext.SimpleExtPlugin; +import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class FullMarkdownActivity extends BaseActivity { + + public static final String EXTRA_COMMENT_MARKDOWN = "ECM"; + public static final String EXTRA_IS_NSFW = "EIN"; + + @BindView(R.id.coordinator_layout_comment_full_markdown_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_comment_full_markdown_activity) + AppBarLayout appBarLayout; + @BindView(R.id.collapsing_toolbar_layout_comment_full_markdown_activity) + CollapsingToolbarLayout collapsingToolbarLayout; + @BindView(R.id.toolbar_comment_full_markdown_activity) + Toolbar toolbar; + @BindView(R.id.content_markdown_view_comment_full_markdown_activity) + RecyclerView markdownRecyclerView; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + private SlidrInterface mSlidrInterface; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_comment_full_markdown); + + ButterKnife.bind(this); + + applyCustomTheme(); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + setTitle(" "); + + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { + mSlidrInterface = Slidr.attach(this); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Window window = getWindow(); + + if (isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + if (isImmersiveInterface()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + } else { + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + } + adjustToolbar(toolbar); + markdownRecyclerView.setPadding(0, 0, 0, getNavBarHeight()); + } + } + + String commentMarkdown = getIntent().getStringExtra(EXTRA_COMMENT_MARKDOWN); + boolean isNsfw = getIntent().getBooleanExtra(EXTRA_IS_NSFW, false); + int markdownColor = mCustomThemeWrapper.getCommentColor(); + int spoilerBackgroundColor = markdownColor | 0xFF000000; + int linkColor = mCustomThemeWrapper.getLinkColor(); + Markwon markwon = Markwon.builder(this) + .usePlugin(new AbstractMarkwonPlugin() { + @NonNull + @Override + public String processMarkdown(@NonNull String markdown) { + StringBuilder markdownStringBuilder = new StringBuilder(markdown); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); + Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); + while (matcher.find()) { + markdownStringBuilder.replace(matcher.start(), matcher.start() + 1, ">"); + } + return super.processMarkdown(markdownStringBuilder.toString()); + } + + @Override + public void afterSetText(@NonNull TextView textView) { + textView.setHighlightColor(Color.TRANSPARENT); + SpannableStringBuilder markdownStringBuilder = new SpannableStringBuilder(textView.getText().toString()); + Pattern spoilerPattern = Pattern.compile(">![\\S\\s]*?!<"); + Matcher matcher = spoilerPattern.matcher(markdownStringBuilder); + int start = 0; + boolean find = false; + while (matcher.find(start)) { + find = true; + markdownStringBuilder.delete(matcher.end() - 2, matcher.end()); + markdownStringBuilder.delete(matcher.start(), matcher.start() + 2); + ClickableSpan clickableSpan = new ClickableSpan() { + private boolean isShowing = false; + @Override + public void updateDrawState(@NonNull TextPaint ds) { + if (isShowing) { + super.updateDrawState(ds); + ds.setColor(markdownColor); + } else { + ds.bgColor = spoilerBackgroundColor; + ds.setColor(markdownColor); + } + ds.setUnderlineText(false); + } + + @Override + public void onClick(@NonNull View view) { + isShowing = !isShowing; + view.invalidate(); + } + }; + markdownStringBuilder.setSpan(clickableSpan, matcher.start(), matcher.end() - 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + start = matcher.end() - 4; + } + if (find) { + textView.setText(markdownStringBuilder); + } + } + + @Override + public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { + textView.setTextColor(markdownColor); + } + + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(FullMarkdownActivity.this, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + intent.putExtra(LinkResolverActivity.EXTRA_IS_NSFW, isNsfw); + startActivity(intent); + }); + } + + @Override + public void configureTheme(@NonNull MarkwonTheme.Builder builder) { + builder.linkColor(linkColor); + } + }) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(SimpleExtPlugin.create(plugin -> + plugin.addExtension(1, '^', (configuration, props) -> { + return new SuperscriptSpan(); + }) + ) + ) + .usePlugin(TableEntryPlugin.create(this)) + .build(); + + MarkwonAdapter markwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text) + .include(TableBlock.class, TableEntry.create(builder -> builder + .tableLayout(R.layout.adapter_table_block, R.id.table_layout) + .textLayoutIsRoot(R.layout.view_table_entry_cell))) + .build(); + LinearLayoutManager linearLayoutManager = new MarkwonLinearLayoutManager(this, new MarkwonLinearLayoutManager.HorizontalScrollViewScrolledListener() { + @Override + public void onScrolledLeft() { + if (mSlidrInterface != null) { + mSlidrInterface.lock(); + } + } + + @Override + public void onScrolledRight() { + if (mSlidrInterface != null) { + mSlidrInterface.unlock(); + } + } + }); + markdownRecyclerView.setLayoutManager(linearLayoutManager); + markdownRecyclerView.setAdapter(markwonAdapter); + markwonAdapter.setMarkdown(markwon, commentMarkdown); + markwonAdapter.notifyDataSetChanged(); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + + return false; + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java index de7bc72c..1ebdab2c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/PostTextActivity.java @@ -414,57 +414,61 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - if (isPosting) { - promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); - return true; - } else { - if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { - promptAlertDialog(R.string.discard, R.string.discard_detail); - return true; - } - } - finish(); + int itemId = item.getItemId(); + if (itemId == android.R.id.home) { + if (isPosting) { + promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_submit_post_detail); return true; - case R.id.action_send_post_text_activity: - if (!subredditSelected) { - Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); - return true; - } - - if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { - Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + } else { + if (!titleEditText.getText().toString().equals("") || !contentEditText.getText().toString().equals("")) { + promptAlertDialog(R.string.discard, R.string.discard_detail); return true; } + } + finish(); + return true; + } else if (itemId == R.id.action_preview_post_text_activity) { + Intent intent = new Intent(this, FullMarkdownActivity.class); + intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, contentEditText.getText().toString()); + startActivity(intent); + } else if (itemId == R.id.action_send_post_text_activity) { + if (!subredditSelected) { + Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show(); + return true; + } - isPosting = true; + if (titleEditText.getText() == null || titleEditText.getText().toString().equals("")) { + Snackbar.make(coordinatorLayout, R.string.title_required, Snackbar.LENGTH_SHORT).show(); + return true; + } - item.setEnabled(false); - item.getIcon().setAlpha(130); + isPosting = true; - mPostingSnackbar.show(); + item.setEnabled(false); + item.getIcon().setAlpha(130); - String subredditName; - if (subredditIsUser) { - subredditName = "u_" + subredditNameTextView.getText().toString(); - } else { - subredditName = subredditNameTextView.getText().toString(); - } + mPostingSnackbar.show(); - Intent intent = new Intent(this, SubmitPostService.class); - intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); - intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); - intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); - intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); - intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); - intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); - intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); - intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); - startService(intent); + String subredditName; + if (subredditIsUser) { + subredditName = "u_" + subredditNameTextView.getText().toString(); + } else { + subredditName = subredditNameTextView.getText().toString(); + } - return true; + Intent intent = new Intent(this, SubmitPostService.class); + intent.putExtra(SubmitPostService.EXTRA_ACCESS_TOKEN, mAccessToken); + intent.putExtra(SubmitPostService.EXTRA_SUBREDDIT_NAME, subredditName); + intent.putExtra(SubmitPostService.EXTRA_TITLE, titleEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_CONTENT, contentEditText.getText().toString()); + intent.putExtra(SubmitPostService.EXTRA_KIND, APIUtils.KIND_SELF); + intent.putExtra(SubmitPostService.EXTRA_FLAIR, flair); + intent.putExtra(SubmitPostService.EXTRA_IS_SPOILER, isSpoiler); + intent.putExtra(SubmitPostService.EXTRA_IS_NSFW, isNSFW); + intent.putExtra(SubmitPostService.EXTRA_POST_TYPE, SubmitPostService.EXTRA_POST_TEXT_OR_LINK); + startService(intent); + + return true; } return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java index 73d149b1..e8d2362a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MarkdownBottomBarRecyclerViewAdapter.java @@ -134,6 +134,9 @@ public class MarkdownBottomBarRecyclerViewAdapter extends RecyclerView.Adapter { String text = textEditText.getText().toString(); String link = linkEditText.getText().toString(); + if (text.equals("")) { + text = link; + } commentEditText.getText().replace(Math.min(start, end), Math.max(start, end), "[" + text + "](" + link + ")", 0, "[]()".length() + text.length() + link.length()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 6fc0c4b8..8fc499cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -6,7 +6,7 @@ import dagger.Component; import ml.docilealligator.infinityforreddit.Activity.AccountPostsActivity; import ml.docilealligator.infinityforreddit.Activity.AccountSavedThingActivity; import ml.docilealligator.infinityforreddit.Activity.CommentActivity; -import ml.docilealligator.infinityforreddit.Activity.CommentFullMarkdownActivity; +import ml.docilealligator.infinityforreddit.Activity.FullMarkdownActivity; import ml.docilealligator.infinityforreddit.Activity.CreateMultiRedditActivity; import ml.docilealligator.infinityforreddit.Activity.CustomThemeListingActivity; import ml.docilealligator.infinityforreddit.Activity.CustomThemePreviewActivity; @@ -208,7 +208,7 @@ public interface AppComponent { void inject(SubmitCrosspostActivity submitCrosspostActivity); - void inject(CommentFullMarkdownActivity commentFullMarkdownActivity); + void inject(FullMarkdownActivity fullMarkdownActivity); void inject(SelectUserFlairActivity selectUserFlairActivity); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java index e105ff80..198a9965 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/CommentMoreBottomSheetFragment.java @@ -21,7 +21,7 @@ import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; import butterknife.BindView; import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.Activity.CommentFullMarkdownActivity; +import ml.docilealligator.infinityforreddit.Activity.FullMarkdownActivity; import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity; import ml.docilealligator.infinityforreddit.Activity.GiveAwardActivity; import ml.docilealligator.infinityforreddit.Activity.ReportActivity; @@ -155,9 +155,9 @@ public class CommentMoreBottomSheetFragment extends RoundedBottomSheetDialogFrag }); viewFullMarkdownTextView.setOnClickListener(view -> { - Intent intent = new Intent(activity, CommentFullMarkdownActivity.class); - intent.putExtra(CommentFullMarkdownActivity.EXTRA_IS_NSFW, bundle.getBoolean(EXTRA_IS_NSFW, false)); - intent.putExtra(CommentFullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, bundle.getString(EXTRA_COMMENT_MARKDOWN, "")); + Intent intent = new Intent(activity, FullMarkdownActivity.class); + intent.putExtra(FullMarkdownActivity.EXTRA_IS_NSFW, bundle.getBoolean(EXTRA_IS_NSFW, false)); + intent.putExtra(FullMarkdownActivity.EXTRA_COMMENT_MARKDOWN, bundle.getString(EXTRA_COMMENT_MARKDOWN, "")); activity.startActivity(intent); dismiss(); -- cgit v1.2.3