Fix markdown in rules screen (#1050)

* Display tables in rules screen

* Parse spoiler and headings in rules screen
This commit is contained in:
Sergei Kozelko 2022-09-09 08:34:00 +03:00 committed by GitHub
parent 401dba6d83
commit e0cf4ec485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 18 deletions

View File

@ -20,6 +20,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrInterface;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -87,8 +88,9 @@ public class RulesActivity extends BaseActivity {
applyCustomTheme(); applyCustomTheme();
SlidrInterface slidrInterface = null;
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) {
Slidr.attach(this); slidrInterface = Slidr.attach(this);
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
@ -119,7 +121,7 @@ public class RulesActivity extends BaseActivity {
mSubredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME); mSubredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME);
mAdapter = new RulesRecyclerViewAdapter(this, mCustomThemeWrapper); mAdapter = new RulesRecyclerViewAdapter(this, mCustomThemeWrapper, slidrInterface);
recyclerView.setAdapter(mAdapter); recyclerView.setAdapter(mAdapter);
FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() { FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {

View File

@ -11,8 +11,13 @@ import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import com.r0adkll.slidr.model.SlidrInterface;
import org.commonmark.ext.gfm.tables.TableBlock;
import java.util.ArrayList; import java.util.ArrayList;
import butterknife.BindView; import butterknife.BindView;
@ -30,6 +35,9 @@ import io.noties.markwon.inlineparser.HtmlInlineProcessor;
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin; import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
import io.noties.markwon.linkify.LinkifyPlugin; import io.noties.markwon.linkify.LinkifyPlugin;
import io.noties.markwon.movement.MovementMethodPlugin; import io.noties.markwon.movement.MovementMethodPlugin;
import io.noties.markwon.recycler.MarkwonAdapter;
import io.noties.markwon.recycler.table.TableEntry;
import io.noties.markwon.recycler.table.TableEntryPlugin;
import me.saket.bettermovementmethod.BetterLinkMovementMethod; import me.saket.bettermovementmethod.BetterLinkMovementMethod;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.Rule; import ml.docilealligator.infinityforreddit.Rule;
@ -37,18 +45,28 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity; import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.UrlMenuBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
import ml.docilealligator.infinityforreddit.customviews.MarkwonLinearLayoutManager;
import ml.docilealligator.infinityforreddit.markdown.RedditHeadingPlugin;
import ml.docilealligator.infinityforreddit.markdown.SpoilerParserPlugin;
import ml.docilealligator.infinityforreddit.markdown.SuperscriptInlineProcessor; import ml.docilealligator.infinityforreddit.markdown.SuperscriptInlineProcessor;
import ml.docilealligator.infinityforreddit.utils.Utils; import ml.docilealligator.infinityforreddit.utils.Utils;
public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> { public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> {
private BaseActivity activity; private BaseActivity activity;
private Markwon markwon; private Markwon markwon;
@Nullable
private final SlidrInterface slidrInterface;
private ArrayList<Rule> rules; private ArrayList<Rule> rules;
private int mPrimaryTextColor; private int mPrimaryTextColor;
private int mSecondaryTextColor;
public RulesRecyclerViewAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper) { public RulesRecyclerViewAdapter(@NonNull BaseActivity activity,
@NonNull CustomThemeWrapper customThemeWrapper,
@Nullable SlidrInterface slidrInterface) {
this.activity = activity; this.activity = activity;
this.slidrInterface = slidrInterface;
mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor();
int spoilerBackgroundColor = mPrimaryTextColor | 0xFF000000;
markwon = Markwon.builder(activity) markwon = Markwon.builder(activity)
.usePlugin(MarkwonInlineParserPlugin.create(plugin -> { .usePlugin(MarkwonInlineParserPlugin.create(plugin -> {
plugin.excludeInlineProcessor(AutolinkInlineProcessor.class); plugin.excludeInlineProcessor(AutolinkInlineProcessor.class);
@ -101,10 +119,11 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
return true; return true;
}))) })))
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(SpoilerParserPlugin.create(mPrimaryTextColor, spoilerBackgroundColor))
.usePlugin(RedditHeadingPlugin.create())
.usePlugin(StrikethroughPlugin.create()) .usePlugin(StrikethroughPlugin.create())
.usePlugin(TableEntryPlugin.create(activity))
.build(); .build();
mPrimaryTextColor = customThemeWrapper.getPrimaryTextColor();
mSecondaryTextColor = customThemeWrapper.getSecondaryTextColor();
} }
@NonNull @NonNull
@ -115,11 +134,14 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
@Override @Override
public void onBindViewHolder(@NonNull RuleViewHolder holder, int position) { public void onBindViewHolder(@NonNull RuleViewHolder holder, int position) {
holder.shortNameTextView.setText(rules.get(holder.getBindingAdapterPosition()).getShortName()); Rule rule = rules.get(holder.getBindingAdapterPosition());
if (rules.get(holder.getBindingAdapterPosition()).getDescriptionHtml() == null) { holder.shortNameTextView.setText(rule.getShortName());
if (rule.getDescriptionHtml() == null) {
holder.descriptionMarkwonView.setVisibility(View.GONE); holder.descriptionMarkwonView.setVisibility(View.GONE);
} else { } else {
markwon.setMarkdown(holder.descriptionMarkwonView, rules.get(holder.getBindingAdapterPosition()).getDescriptionHtml()); holder.markwonAdapter.setMarkdown(markwon, rule.getDescriptionHtml());
//noinspection NotifyDatasetChanged
holder.markwonAdapter.notifyDataSetChanged();
} }
} }
@ -143,18 +165,41 @@ public class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecycler
@BindView(R.id.short_name_text_view_item_rule) @BindView(R.id.short_name_text_view_item_rule)
TextView shortNameTextView; TextView shortNameTextView;
@BindView(R.id.description_markwon_view_item_rule) @BindView(R.id.description_markwon_view_item_rule)
TextView descriptionMarkwonView; RecyclerView descriptionMarkwonView;
@NonNull
final MarkwonAdapter markwonAdapter;
RuleViewHolder(@NonNull View itemView) { RuleViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
ButterKnife.bind(this, itemView); ButterKnife.bind(this, itemView);
shortNameTextView.setTextColor(mPrimaryTextColor); shortNameTextView.setTextColor(mPrimaryTextColor);
descriptionMarkwonView.setTextColor(mSecondaryTextColor);
if (activity.typeface != null) { if (activity.typeface != null) {
shortNameTextView.setTypeface(activity.typeface); shortNameTextView.setTypeface(activity.typeface);
descriptionMarkwonView.setTypeface(activity.typeface);
} }
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();
LinearLayoutManagerBugFixed linearLayoutManager = new MarkwonLinearLayoutManager(activity,
new MarkwonLinearLayoutManager.HorizontalScrollViewScrolledListener() {
@Override
public void onScrolledLeft() {
if (slidrInterface != null) {
slidrInterface.lock();
}
}
@Override
public void onScrolledRight() {
if (slidrInterface != null) {
slidrInterface.unlock();
}
}
});
descriptionMarkwonView.setLayoutManager(linearLayoutManager);
descriptionMarkwonView.setAdapter(markwonAdapter);
} }
} }
} }

View File

@ -11,14 +11,12 @@
android:textSize="?attr/title_font_16" android:textSize="?attr/title_font_16"
android:fontFamily="?attr/font_family" /> android:fontFamily="?attr/font_family" />
<TextView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/description_markwon_view_item_rule" android:id="@+id/description_markwon_view_item_rule"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingBottom="16dp" android:paddingBottom="8dp"
android:paddingStart="16dp" android:paddingStart="8dp"
android:paddingEnd="16dp" android:paddingEnd="8dp"/>
android:textSize="?attr/content_font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout> </LinearLayout>