diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-07-18 04:00:46 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-07-18 04:00:46 +0000 |
commit | 611d6a90ec39707b4d4161da1eb5b19249e670c4 (patch) | |
tree | c5df1e52265e0593ae936be2242727804b886834 /app/src | |
parent | 42497bb2475bbd0edaff79a6f1aaf99104e1b84b (diff) | |
download | infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar.gz infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar.bz2 infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar.lz infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar.xz infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.tar.zst infinity-for-reddit-611d6a90ec39707b4d4161da1eb5b19249e670c4.zip |
Viewing rules after pressing rules button in PostXXXActivity is available. Extend MarkwonView to support opening reddit subreddit and user links.
Diffstat (limited to 'app/src')
21 files changed, 443 insertions, 59 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 11a36158..24a79054 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,10 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <activity + android:name=".RulesActivity" + android:label="@string/rules_activity_label" + android:parentActivityName=".MainActivity" /> + <activity android:name=".PostVideoActivity" android:label="@string/post_video_activity_label" android:parentActivityName=".MainActivity" diff --git a/app/src/main/java/CustomView/CustomMarkwonView.java b/app/src/main/java/CustomView/CustomMarkwonView.java new file mode 100644 index 00000000..c0d59f05 --- /dev/null +++ b/app/src/main/java/CustomView/CustomMarkwonView.java @@ -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); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index da7da984..87c4f254 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -23,4 +23,5 @@ interface AppComponent { void inject(PostImageActivity postImageActivity); void inject(PostVideoActivity postVideoActivity); void inject(FlairBottomSheetFragment flairBottomSheetFragment); + void inject(RulesActivity rulesActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 32b023be..3bb3ef10 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -39,6 +39,7 @@ import java.util.ArrayList; import java.util.Locale; import CustomView.AspectRatioGifImageView; +import CustomView.CustomMarkwonView; import SubredditDatabase.SubredditRoomDatabase; import User.UserRoomDatabase; import butterknife.BindView; @@ -46,10 +47,8 @@ import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; 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_FIRST_LOADING = 1; private static final int VIEW_TYPE_FIRST_LOADING_FAILED = 2; @@ -80,10 +79,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH void retryFetchingMoreComments(); } - CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, RequestManager glide, - SharedPreferences sharedPreferences, Post post, String subredditNamePrefixed, - Locale locale, LoadSubredditIconAsyncTask loadSubredditIconAsyncTask, - CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { + CommentAndPostRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, RequestManager glide, + SharedPreferences sharedPreferences, Post post, String subredditNamePrefixed, + Locale locale, LoadSubredditIconAsyncTask loadSubredditIconAsyncTask, + CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { mActivity = activity; mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; @@ -372,7 +371,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH if(!mPost.getSelfText().equals("")) { ((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.setOnClickListener(view -> { @@ -389,7 +388,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH if(!mPost.getSelfText().equals("")) { ((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE); - ((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(getCustomSpannableConfiguration(), mPost.getSelfText()); + ((PostDetailViewHolder) holder).mContentMarkdownView.setMarkdown(mPost.getSelfText(), mActivity); } break; } @@ -401,26 +400,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH ((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); - SpannableConfiguration spannableConfiguration = 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(); - - ((CommentViewHolder) holder).commentMarkdownView.setMarkdown(spannableConfiguration, comment.getCommentContent()); + ((CommentViewHolder) holder).commentMarkdownView.setMarkdown(comment.getCommentContent(), mActivity); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore())); 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) { mPost = post; 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.post_time_text_view_item_post_detail) TextView mPostTimeTextView; @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.gilded_image_view_item_post_detail) ImageView mGildedImageView; @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 { @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_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.score_text_view_item_post_comment) TextView scoreTextView; @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index 3feda5cf..7f92697a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -76,4 +76,7 @@ public class JSONUtils { static final String VALUE_KEY = "value"; static final String TEXT_KEY = "text"; 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"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 1ba869c3..0d8a258c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -179,6 +179,20 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS 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 -> { if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 7260c5af..c0067e88 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -150,6 +150,20 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh 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 -> { if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index c943e88e..828f556c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -151,6 +151,20 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh 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 -> { if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 7432a0b7..23214037 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -184,6 +184,20 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS 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 -> { if(flair == null) { flairSelectionBottomSheetFragment = new FlairBottomSheetFragment(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index b4dc3d97..75e99f7e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -95,6 +95,9 @@ public interface RedditAPI { @GET("r/{subredditName}/api/link_flair.json?raw_json=1") 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 @POST(".") Call<String> uploadMediaToAWS(@PartMap()Map<String, RequestBody> params, @Part() MultipartBody.Part file); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Rule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Rule.java new file mode 100644 index 00000000..89b4d26d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Rule.java @@ -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; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java new file mode 100644 index 00000000..18584546 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java @@ -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(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java new file mode 100644 index 00000000..941b0316 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesRecyclerViewAdapter.java @@ -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); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 9b8529ed..f605666c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -65,7 +65,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private boolean hasMoreChildren; private LinearLayoutManager mLinearLayoutManager; - private CommentRecyclerViewAdapter mAdapter; + private CommentAndPostRecyclerViewAdapter mAdapter; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; @@ -109,10 +109,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { mPost = savedInstanceState.getParcelable(POST_STATE); } - mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, + mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mGlide, mSharedPreferences, mPost, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, - new CommentRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { + new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index 3433cfb8..5bc0c163 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -45,7 +45,7 @@ import retrofit2.Retrofit; 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 IS_IN_LAZY_MODE_STATE = "IILMS"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index e9ab3123..64d7cdc2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -42,7 +42,7 @@ import retrofit2.Retrofit; 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 IS_IN_LAZY_MODE_STATE = "IILMS"; diff --git a/app/src/main/res/layout/activity_rules.xml b/app/src/main/res/layout/activity_rules.xml new file mode 100644 index 00000000..6678f13c --- /dev/null +++ b/app/src/main/res/layout/activity_rules.xml @@ -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>
\ No newline at end of file diff --git a/app/src/main/res/layout/item_comment.xml b/app/src/main/res/layout/item_comment.xml index 088943f7..d03b0bad 100644 --- a/app/src/main/res/layout/item_comment.xml +++ b/app/src/main/res/layout/item_comment.xml @@ -42,7 +42,7 @@ </LinearLayout> - <ru.noties.markwon.view.MarkwonView + <CustomView.CustomMarkwonView android:id="@+id/comment_markdown_view_item_post_comment" android:layout_width="match_parent" android:layout_height="wrap_content" diff --git a/app/src/main/res/layout/item_post_detail.xml b/app/src/main/res/layout/item_post_detail.xml index babdcdbf..fc48202e 100644 --- a/app/src/main/res/layout/item_post_detail.xml +++ b/app/src/main/res/layout/item_post_detail.xml @@ -63,7 +63,7 @@ android:textColor="@color/primaryTextColor" android:textSize="18sp" /> - <ru.noties.markwon.view.MarkwonView + <CustomView.CustomMarkwonView android:id="@+id/content_markdown_view_item_post_detail" android:layout_width="match_parent" android:layout_height="wrap_content" diff --git a/app/src/main/res/layout/item_rule.xml b/app/src/main/res/layout/item_rule.xml new file mode 100644 index 00000000..1124a548 --- /dev/null +++ b/app/src/main/res/layout/item_rule.xml @@ -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>
\ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d76df6b2..005aa048 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ <string name="post_link_activity_label">Link Post</string> <string name="post_image_activity_label">Image 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_close">Close navigation drawer</string> @@ -124,4 +125,7 @@ <string name="no_flair">No flair</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> |