Viewing rules after pressing rules button in PostXXXActivity is available. Extend MarkwonView to support opening reddit subreddit and user links.

This commit is contained in:
Alex Ning 2019-07-18 12:00:46 +08:00
parent 42497bb247
commit 611d6a90ec
23 changed files with 443 additions and 59 deletions

Binary file not shown.

Binary file not shown.

View File

@ -19,6 +19,10 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"> android:usesCleartextTraffic="true">
<activity
android:name=".RulesActivity"
android:label="@string/rules_activity_label"
android:parentActivityName=".MainActivity" />
<activity <activity
android:name=".PostVideoActivity" android:name=".PostVideoActivity"
android:label="@string/post_video_activity_label" android:label="@string/post_video_activity_label"

View File

@ -0,0 +1,49 @@
package CustomView;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.CustomTabsIntent;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.ViewUserDetailActivity;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.view.MarkwonView;
public class CustomMarkwonView extends MarkwonView {
public CustomMarkwonView(Context context) {
super(context);
}
public CustomMarkwonView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setMarkdown(@Nullable String markdown, Context conte) {
SpannableConfiguration configuration = SpannableConfiguration.builder(conte).linkResolver((view, link) -> {
if(link.startsWith("/u/") || link.startsWith("u/")) {
Intent intent = new Intent(conte, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3));
conte.startActivity(intent);
} else if(link.startsWith("/r/") || link.startsWith("r/")) {
Intent intent = new Intent(conte, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3));
conte.startActivity(intent);
} else {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list
builder.addDefaultShareMenuItem();
builder.setToolbarColor(conte.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(conte, Uri.parse(link));
}
}).build();
super.setMarkdown(configuration, markdown);
}
}

View File

@ -23,4 +23,5 @@ interface AppComponent {
void inject(PostImageActivity postImageActivity); void inject(PostImageActivity postImageActivity);
void inject(PostVideoActivity postVideoActivity); void inject(PostVideoActivity postVideoActivity);
void inject(FlairBottomSheetFragment flairBottomSheetFragment); void inject(FlairBottomSheetFragment flairBottomSheetFragment);
void inject(RulesActivity rulesActivity);
} }

View File

@ -39,6 +39,7 @@ import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import CustomView.AspectRatioGifImageView; import CustomView.AspectRatioGifImageView;
import CustomView.CustomMarkwonView;
import SubredditDatabase.SubredditRoomDatabase; import SubredditDatabase.SubredditRoomDatabase;
import User.UserRoomDatabase; import User.UserRoomDatabase;
import butterknife.BindView; import butterknife.BindView;
@ -46,10 +47,8 @@ import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.view.MarkwonView;
class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_POST_DETAIL = 0; private static final int VIEW_TYPE_POST_DETAIL = 0;
private static final int VIEW_TYPE_FIRST_LOADING = 1; private static final int VIEW_TYPE_FIRST_LOADING = 1;
private static final int VIEW_TYPE_FIRST_LOADING_FAILED = 2; private static final int VIEW_TYPE_FIRST_LOADING_FAILED = 2;
@ -80,10 +79,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
void retryFetchingMoreComments(); void retryFetchingMoreComments();
} }
CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, RequestManager glide, CommentAndPostRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, RequestManager glide,
SharedPreferences sharedPreferences, Post post, String subredditNamePrefixed, SharedPreferences sharedPreferences, Post post, String subredditNamePrefixed,
Locale locale, LoadSubredditIconAsyncTask loadSubredditIconAsyncTask, Locale locale, LoadSubredditIconAsyncTask loadSubredditIconAsyncTask,
CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) {
mActivity = activity; mActivity = activity;
mRetrofit = retrofit; mRetrofit = retrofit;
mOauthRetrofit = oauthRetrofit; mOauthRetrofit = oauthRetrofit;
@ -372,7 +371,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if(!mPost.getSelfText().equals("")) { if(!mPost.getSelfText().equals("")) {
((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE); ((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(getCustomSpannableConfiguration(), mPost.getSelfText()); ((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(mPost.getSelfText(), mActivity);
} }
((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setVisibility(View.VISIBLE); ((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setOnClickListener(view -> { ((PostDetailViewHolder) holder).mNoPreviewLinkImageView.setOnClickListener(view -> {
@ -389,7 +388,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if(!mPost.getSelfText().equals("")) { if(!mPost.getSelfText().equals("")) {
((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE); ((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE);
((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(getCustomSpannableConfiguration(), mPost.getSelfText()); ((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(mPost.getSelfText(), mActivity);
} }
break; break;
} }
@ -401,26 +400,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); ((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime());
SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mActivity).linkResolver((view, link) -> { ((CommentViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mActivity);
if (link.startsWith("/u/") || link.startsWith("u/")) {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3));
mActivity.startActivity(intent);
} else if (link.startsWith("/r/") || link.startsWith("r/")) {
Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3));
mActivity.startActivity(intent);
} else {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mActivity, Uri.parse(link));
}
}).build();
((CommentViewHolder) holder).commentMarkdownView.setMarkdown(spannableConfiguration, comment.getCommentContent());
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore()));
ViewGroup.LayoutParams params = ((CommentViewHolder) holder).verticalBlock.getLayoutParams(); ViewGroup.LayoutParams params = ((CommentViewHolder) holder).verticalBlock.getLayoutParams();
@ -497,27 +477,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
} }
private SpannableConfiguration getCustomSpannableConfiguration() {
return SpannableConfiguration.builder(mActivity).linkResolver((view, link) -> {
if(link.startsWith("/u/") || link.startsWith("u/")) {
Intent intent = new Intent(mActivity, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3));
mActivity.startActivity(intent);
} else if(link.startsWith("/r/") || link.startsWith("r/")) {
Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3));
mActivity.startActivity(intent);
} else {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mActivity.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mActivity, Uri.parse(link));
}
}).build();
}
void updatePost(Post post) { void updatePost(Post post) {
mPost = post; mPost = post;
notifyItemChanged(0); notifyItemChanged(0);
@ -689,7 +648,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
@BindView(R.id.subreddit_text_view_item_post_detail) TextView mSubredditTextView; @BindView(R.id.subreddit_text_view_item_post_detail) TextView mSubredditTextView;
@BindView(R.id.post_time_text_view_item_post_detail) TextView mPostTimeTextView; @BindView(R.id.post_time_text_view_item_post_detail) TextView mPostTimeTextView;
@BindView(R.id.title_text_view_item_post_detail) TextView mTitleTextView; @BindView(R.id.title_text_view_item_post_detail) TextView mTitleTextView;
@BindView(R.id.content_markdown_view_item_post_detail) MarkwonView mContentMarkdownView; @BindView(R.id.content_markdown_view_item_post_detail) CustomMarkwonView mContentMarkdownView;
@BindView(R.id.type_text_view_item_post_detail) Chip mTypeChip; @BindView(R.id.type_text_view_item_post_detail) Chip mTypeChip;
@BindView(R.id.gilded_image_view_item_post_detail) ImageView mGildedImageView; @BindView(R.id.gilded_image_view_item_post_detail) ImageView mGildedImageView;
@BindView(R.id.gilded_number_text_view_item_post_detail) TextView mGildedNumberTextView; @BindView(R.id.gilded_number_text_view_item_post_detail) TextView mGildedNumberTextView;
@ -848,7 +807,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
class CommentViewHolder extends RecyclerView.ViewHolder { class CommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView;
@BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView;
@BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView; @BindView(R.id.comment_markdown_view_item_post_comment) CustomMarkwonView commentMarkdownView;
@BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton;
@BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView;
@BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton;

View File

@ -76,4 +76,7 @@ public class JSONUtils {
static final String VALUE_KEY = "value"; static final String VALUE_KEY = "value";
static final String TEXT_KEY = "text"; static final String TEXT_KEY = "text";
static final String SPOILER_KEY = "spoiler"; static final String SPOILER_KEY = "spoiler";
static final String RULES_KEY = "rules";
static final String SHORT_NAME_KEY = "short_name";
static final String DESCRIPTION_HTML_KEY = "description_html";
} }

View File

@ -179,6 +179,20 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
rulesButton.setOnClickListener(view -> {
if(subredditName == null) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(this, RulesActivity.class);
if(subredditIsUser) {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName);
}
startActivity(intent);
}
});
flairTextView.setOnClickListener(view -> { flairTextView.setOnClickListener(view -> {
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();

View File

@ -150,6 +150,20 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
rulesButton.setOnClickListener(view -> {
if(subredditName == null) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(this, RulesActivity.class);
if(subredditIsUser) {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName);
}
startActivity(intent);
}
});
flairTextView.setOnClickListener(view -> { flairTextView.setOnClickListener(view -> {
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();

View File

@ -151,6 +151,20 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
rulesButton.setOnClickListener(view -> {
if(subredditName == null) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(this, RulesActivity.class);
if(subredditIsUser) {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName);
}
startActivity(intent);
}
});
flairTextView.setOnClickListener(view -> { flairTextView.setOnClickListener(view -> {
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();

View File

@ -184,6 +184,20 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS
startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE); startActivityForResult(intent, SUBREDDIT_SELECTION_REQUEST_CODE);
}); });
rulesButton.setOnClickListener(view -> {
if(subredditName == null) {
Snackbar.make(coordinatorLayout, R.string.select_a_subreddit, Snackbar.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(this, RulesActivity.class);
if(subredditIsUser) {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, "u_" + subredditName);
} else {
intent.putExtra(RulesActivity.EXTRA_SUBREDDIT_NAME, subredditName);
}
startActivity(intent);
}
});
flairTextView.setOnClickListener(view -> { flairTextView.setOnClickListener(view -> {
if(flair == null) { if(flair == null) {
flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); flairSelectionBottomSheetFragment = new FlairBottomSheetFragment();

View File

@ -95,6 +95,9 @@ public interface RedditAPI {
@GET("r/{subredditName}/api/link_flair.json?raw_json=1") @GET("r/{subredditName}/api/link_flair.json?raw_json=1")
Call<String> getFlairs(@HeaderMap Map<String, String> headers, @Path("subredditName") String subredditName); Call<String> getFlairs(@HeaderMap Map<String, String> headers, @Path("subredditName") String subredditName);
@GET("/r/{subredditName}/about/rules.json?raw_json=1")
Call<String> getRules(@Path("subredditName") String subredditName);
@Multipart @Multipart
@POST(".") @POST(".")
Call<String> uploadMediaToAWS(@PartMap()Map<String, RequestBody> params, @Part() MultipartBody.Part file); Call<String> uploadMediaToAWS(@PartMap()Map<String, RequestBody> params, @Part() MultipartBody.Part file);

View File

@ -0,0 +1,19 @@
package ml.docilealligator.infinityforreddit;
class Rule {
private String shortName;
private String descriptionHtml;
Rule(String shortName, String descriptionHtml) {
this.shortName = shortName;
this.descriptionHtml = descriptionHtml;
}
String getShortName() {
return shortName;
}
String getDescriptionHtml() {
return descriptionHtml;
}
}

View File

@ -0,0 +1,170 @@
package ml.docilealligator.infinityforreddit;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class RulesActivity extends AppCompatActivity {
static final String EXTRA_SUBREDDIT_NAME = "ESN";
@BindView(R.id.progress_bar_rules_activity) ProgressBar progressBar;
@BindView(R.id.recycler_view_rules_activity) RecyclerView recyclerView;
@BindView(R.id.error_text_view_rules_activity) TextView errorTextView;
private String mSubredditName;
private RulesRecyclerViewAdapter mAdapter;
@Inject
@Named("no_oauth")
Retrofit mRetrofit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
ButterKnife.bind(this);
((Infinity) getApplication()).getmAppComponent().inject(this);
ActionBar actionBar = getSupportActionBar();
Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp);
actionBar.setHomeAsUpIndicator(upArrow);
mSubredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new RulesRecyclerViewAdapter(this);
recyclerView.setAdapter(mAdapter);
fetchRules();
}
private void fetchRules() {
progressBar.setVisibility(View.VISIBLE);
errorTextView.setVisibility(View.GONE);
RedditAPI api = mRetrofit.create(RedditAPI.class);
Call<String> rulesCall = api.getRules(mSubredditName);
rulesCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) {
new ParseRulesAsyncTask(response.body(), new ParseRulesAsyncTask.ParseRulesAsyncTaskListener() {
@Override
public void parseSuccessful(ArrayList<Rule> rules) {
progressBar.setVisibility(View.GONE);
if(rules == null || rules.size() == 0) {
errorTextView.setVisibility(View.VISIBLE);
errorTextView.setText(R.string.no_rule);
errorTextView.setOnClickListener(view -> {});
}
mAdapter.changeDataset(rules);
}
@Override
public void parseFailed() {
displayError();
}
}).execute();
} else {
displayError();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
displayError();
}
});
}
private void displayError() {
progressBar.setVisibility(View.GONE);
errorTextView.setVisibility(View.VISIBLE);
errorTextView.setText(R.string.error_loading_rules);
errorTextView.setOnClickListener(view -> fetchRules());
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home) {
finish();
return true;
}
return false;
}
private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> {
private String response;
private ParseRulesAsyncTaskListener parseRulesAsyncTaskListener;
interface ParseRulesAsyncTaskListener {
void parseSuccessful(ArrayList<Rule> rules);
void parseFailed();
}
ParseRulesAsyncTask(String response, ParseRulesAsyncTaskListener parseRulesAsyncTaskListener) {
this.response = response;
this.parseRulesAsyncTaskListener = parseRulesAsyncTaskListener;
}
@Override
protected ArrayList<Rule> doInBackground(Void... voids) {
try {
JSONArray rulesArray = new JSONObject(response).getJSONArray(JSONUtils.RULES_KEY);
ArrayList<Rule> rules = new ArrayList<>();
for(int i = 0; i < rulesArray.length(); i++) {
String shortName = rulesArray.getJSONObject(i).getString(JSONUtils.SHORT_NAME_KEY);
String descriptionHtml = null;
if(rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_HTML_KEY)) {
descriptionHtml = rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_HTML_KEY);
}
rules.add(new Rule(shortName, descriptionHtml));
}
return rules;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(ArrayList<Rule> rules) {
if(rules != null) {
parseRulesAsyncTaskListener.parseSuccessful(rules);
} else {
parseRulesAsyncTaskListener.parseFailed();
}
}
}
}

View File

@ -0,0 +1,67 @@
package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import CustomView.CustomMarkwonView;
import butterknife.BindView;
import butterknife.ButterKnife;
class RulesRecyclerViewAdapter extends RecyclerView.Adapter<RulesRecyclerViewAdapter.RuleViewHolder> {
private Context context;
private ArrayList<Rule> rules;
RulesRecyclerViewAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public RuleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new RuleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rule, parent, false));
}
@Override
public void onBindViewHolder(@NonNull RuleViewHolder holder, int position) {
holder.shortNameTextView.setText(rules.get(holder.getAdapterPosition()).getShortName());
if(rules.get(holder.getAdapterPosition()).getDescriptionHtml() == null) {
holder.descriptionMarkwonView.setVisibility(View.GONE);
} else {
holder.descriptionMarkwonView.setMarkdown(rules.get(holder.getAdapterPosition()).getDescriptionHtml(), context);
}
}
@Override
public int getItemCount() {
return rules == null ? 0 : rules.size();
}
@Override
public void onViewRecycled(@NonNull RuleViewHolder holder) {
super.onViewRecycled(holder);
holder.descriptionMarkwonView.setVisibility(View.VISIBLE);
}
void changeDataset(ArrayList<Rule> rules) {
this.rules = rules;
notifyDataSetChanged();
}
class RuleViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.short_name_text_view_item_rule) TextView shortNameTextView;
@BindView(R.id.description_markwon_view_item_rule) CustomMarkwonView descriptionMarkwonView;
RuleViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}

View File

@ -65,7 +65,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private boolean hasMoreChildren; private boolean hasMoreChildren;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private CommentRecyclerViewAdapter mAdapter; private CommentAndPostRecyclerViewAdapter mAdapter;
private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask;
@BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout;
@ -109,10 +109,10 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mPost = savedInstanceState.getParcelable(POST_STATE); mPost = savedInstanceState.getParcelable(POST_STATE);
} }
mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mGlide, mSharedPreferences, mPost, mOauthRetrofit, mGlide, mSharedPreferences, mPost,
mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask,
new CommentRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override @Override
public void updatePost(Post post) { public void updatePost(Post post) {
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));

View File

@ -45,7 +45,7 @@ import retrofit2.Retrofit;
public class ViewSubredditDetailActivity extends AppCompatActivity { public class ViewSubredditDetailActivity extends AppCompatActivity {
static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN"; public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK"; private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";

View File

@ -42,7 +42,7 @@ import retrofit2.Retrofit;
public class ViewUserDetailActivity extends AppCompatActivity { public class ViewUserDetailActivity extends AppCompatActivity {
static final String EXTRA_USER_NAME_KEY = "EUNK"; public static final String EXTRA_USER_NAME_KEY = "EUNK";
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK"; private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String IS_IN_LAZY_MODE_STATE = "IILMS";

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RulesActivity">
<ProgressBar
android:id="@+id/progress_bar_rules_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_rules_activity"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/error_text_view_rules_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -42,7 +42,7 @@
</LinearLayout> </LinearLayout>
<ru.noties.markwon.view.MarkwonView <CustomView.CustomMarkwonView
android:id="@+id/comment_markdown_view_item_post_comment" android:id="@+id/comment_markdown_view_item_post_comment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -63,7 +63,7 @@
android:textColor="@color/primaryTextColor" android:textColor="@color/primaryTextColor"
android:textSize="18sp" /> android:textSize="18sp" />
<ru.noties.markwon.view.MarkwonView <CustomView.CustomMarkwonView
android:id="@+id/content_markdown_view_item_post_detail" android:id="@+id/content_markdown_view_item_post_detail"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/short_name_text_view_item_rule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="@color/primaryTextColor"
android:textSize="16sp" />
<CustomView.CustomMarkwonView
android:id="@+id/description_markwon_view_item_rule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingStart="16dp"
android:paddingEnd="16dp" />
</LinearLayout>

View File

@ -9,6 +9,7 @@
<string name="post_link_activity_label">Link Post</string> <string name="post_link_activity_label">Link Post</string>
<string name="post_image_activity_label">Image Post</string> <string name="post_image_activity_label">Image Post</string>
<string name="post_video_activity_label">Video Post</string> <string name="post_video_activity_label">Video Post</string>
<string name="rules_activity_label">Rules</string>
<string name="navigation_drawer_open">Open navigation drawer</string> <string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string> <string name="navigation_drawer_close">Close navigation drawer</string>
@ -124,4 +125,7 @@
<string name="no_flair">No flair</string> <string name="no_flair">No flair</string>
<string name="error_loading_flairs">Error loading flairs.\nTap to retry.</string> <string name="error_loading_flairs">Error loading flairs.\nTap to retry.</string>
<string name="no_rule">No rule</string>
<string name="error_loading_rules">Error loading rules.\nTap to retry.</string>
</resources> </resources>