diff options
Diffstat (limited to 'app/src/main/java/ml')
4 files changed, 120 insertions, 11 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java index 936f4d48..8f9d9fc4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSidebarActivity.java @@ -11,6 +11,7 @@ import android.text.Spanned; import android.text.style.SuperscriptSpan; import android.text.util.Linkify; import android.util.TypedValue; +import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -25,12 +26,14 @@ import androidx.core.content.ContextCompat; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.appbar.AppBarLayout; import org.commonmark.ext.gfm.tables.TableBlock; import javax.inject.Inject; +import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; @@ -45,11 +48,15 @@ import io.noties.markwon.recycler.table.TableEntryPlugin; import io.noties.markwon.simple.ext.SimpleExtPlugin; import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubredditDataAsyncTask; +import ml.docilealligator.infinityforreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; +import retrofit2.Retrofit; public class ViewSidebarActivity extends BaseActivity { @@ -58,12 +65,18 @@ public class ViewSidebarActivity extends BaseActivity { AppBarLayout appBarLayout; @BindView(R.id.toolbar_view_sidebar_activity) Toolbar toolbar; + @BindView(R.id.swipe_refresh_layout_view_sidebar_activity) + SwipeRefreshLayout swipeRefreshLayout; @BindView(R.id.markdown_recycler_view_view_sidebar_activity) RecyclerView markdownRecyclerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject SharedPreferences mSharedPreferences; + private String subredditName; private SubredditViewModel mSubredditViewModel; @Override @@ -127,7 +140,7 @@ public class ViewSidebarActivity extends BaseActivity { } } - String subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); + subredditName = getIntent().getStringExtra(EXTRA_SUBREDDIT_NAME); if (subredditName == null) { Toast.makeText(this, R.string.error_getting_subreddit_name, Toast.LENGTH_SHORT).show(); finish(); @@ -186,8 +199,39 @@ public class ViewSidebarActivity extends BaseActivity { if (subredditData != null) { markwonAdapter.setMarkdown(markwon, subredditData.getSidebarDescription()); markwonAdapter.notifyDataSetChanged(); + } else { + fetchSubredditData(); } }); + + TypedValue typedValue = new TypedValue(); + getTheme().resolveAttribute(R.attr.cardViewBackgroundColor, typedValue, true); + swipeRefreshLayout.setProgressBackgroundColorSchemeColor(typedValue.data); + swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent); + swipeRefreshLayout.setOnRefreshListener(this::fetchSubredditData); + } + + private void fetchSubredditData() { + swipeRefreshLayout.setRefreshing(true); + FetchSubredditData.fetchSubredditData(mRetrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { + swipeRefreshLayout.setRefreshing(false); + new InsertSubredditDataAsyncTask(mRedditDataRoomDatabase, subredditData, () -> swipeRefreshLayout.setRefreshing(false)).execute(); + } + + @Override + public void onFetchSubredditDataFail() { + swipeRefreshLayout.setRefreshing(false); + Toast.makeText(ViewSidebarActivity.this, R.string.cannot_fetch_sidebar, Toast.LENGTH_SHORT).show(); + } + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.view_sidebar_activity, menu); + return true; } @Override @@ -195,6 +239,11 @@ public class ViewSidebarActivity extends BaseActivity { if (item.getItemId() == android.R.id.home) { finish(); return true; + } else if (item.getItemId() == R.id.action_refresh_view_sidebar_activity) { + if (!swipeRefreshLayout.isRefreshing()) { + fetchSubredditData(); + } + return true; } return false; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 1fcb81de..62ab002e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.view.KeyEvent; @@ -44,6 +43,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; import ml.docilealligator.infinityforreddit.AsyncTask.CheckIsSubscribedToSubredditAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubredditDataAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; @@ -61,7 +61,6 @@ import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; -import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import ml.docilealligator.infinityforreddit.SubredditSubscription; @@ -662,7 +661,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp ((FragmentCommunicator) mFragment).changeNSFW(changeNSFWEvent.nsfw); } - private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { + /*private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { private SubredditDao mSubredditDao; private SubredditData subredditData; @@ -688,5 +687,5 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp interface InsertSubredditDataAsyncTaskListener { void insertSuccess(); } - } + }*/ } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index fb285316..ea37f174 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -104,7 +104,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy private Retrofit mOauthRetrofit; private RedditDataRoomDatabase mRedditDataRoomDatabase; private RequestManager mGlide; - private Markwon mMarkwon; + private Markwon mPostDetailMarkwon; + private Markwon mCommentMarkwon; private final MarkwonAdapter mMarkwonAdapter; private String mAccessToken; private String mAccountName; @@ -138,7 +139,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy mRedditDataRoomDatabase = redditDataRoomDatabase; mGlide = glide; int markdownColor = ContextCompat.getColor(activity, R.color.defaultTextColor); - mMarkwon = Markwon.builder(mActivity) + mPostDetailMarkwon = Markwon.builder(mActivity) .usePlugin(new AbstractMarkwonPlugin() { @Override public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { @@ -169,12 +170,37 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ) .usePlugin(TableEntryPlugin.create(mActivity)) .build(); + mCommentMarkwon = Markwon.builder(mActivity) + .usePlugin(new AbstractMarkwonPlugin() { + @Override + public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) { + builder.linkResolver((view, link) -> { + Intent intent = new Intent(mActivity, LinkResolverActivity.class); + Uri uri = Uri.parse(link); + if (uri.getScheme() == null && uri.getHost() == null) { + intent.setData(LinkResolverActivity.getRedditUriByPath(link)); + } else { + intent.setData(uri); + } + mActivity.startActivity(intent); + }).urlProcessor(new UrlProcessorRelativeToAbsolute("https://www.reddit.com")); + } + }) + .usePlugin(StrikethroughPlugin.create()) + .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) + .usePlugin(SimpleExtPlugin.create(plugin -> + plugin.addExtension(1, '^', (configuration, props) -> { + return new SuperscriptSpan(); + }) + ) + ) + .build(); mMarkwonAdapter = 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(); - mMarkwonAdapter.setMarkdown(mMarkwon, ""); + mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, ""); mAccessToken = accessToken; mAccountName = accountName; mPost = post; @@ -528,7 +554,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy }); ((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager); ((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter); - mMarkwonAdapter.setMarkdown(mMarkwon, mPost.getSelfText()); + mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, mPost.getSelfText()); mMarkwonAdapter.notifyDataSetChanged(); } @@ -562,7 +588,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy }); ((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager); ((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter); - mMarkwonAdapter.setMarkdown(mMarkwon, mPost.getSelfText()); + mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, mPost.getSelfText()); mMarkwonAdapter.notifyDataSetChanged(); } break; @@ -681,7 +707,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); } - mMarkwon.setMarkdown(((CommentViewHolder) holder).commentMarkdownView, comment.getCommentContent()); + mCommentMarkwon.setMarkdown(((CommentViewHolder) holder).commentMarkdownView, comment.getCommentContent()); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType())); ((CommentViewHolder) holder).itemView.setPadding(comment.getDepth() * 8, 0, 0, 0); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java new file mode 100644 index 00000000..8f7bb88e --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java @@ -0,0 +1,35 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao; +import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; + +public class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { + private SubredditDao mSubredditDao; + private SubredditData subredditData; + private InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener; + + public InsertSubredditDataAsyncTask(RedditDataRoomDatabase db, SubredditData subredditData, + InsertSubredditDataAsyncTaskListener insertSubredditDataAsyncTaskListener) { + mSubredditDao = db.subredditDao(); + this.subredditData = subredditData; + this.insertSubredditDataAsyncTaskListener = insertSubredditDataAsyncTaskListener; + } + + @Override + protected Void doInBackground(final Void... params) { + mSubredditDao.insert(subredditData); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + insertSubredditDataAsyncTaskListener.insertSuccess(); + } + + public interface InsertSubredditDataAsyncTaskListener { + void insertSuccess(); + } +} |