From e4ce71bf2a15232bacbb8eef438b35dcd5a8e08a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 31 Dec 2019 15:11:12 +0800 Subject: [PATCH] Fetch subreddit info in ViewSidebarActivity. --- .../Activity/ViewSidebarActivity.java | 51 ++++++++++++++++++- .../Activity/ViewSubredditDetailActivity.java | 7 ++- .../CommentAndPostRecyclerViewAdapter.java | 38 +++++++++++--- .../InsertSubredditDataAsyncTask.java | 35 +++++++++++++ .../main/res/layout/activity_view_sidebar.xml | 17 +++++-- .../main/res/menu/view_sidebar_activity.xml | 10 ++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 143 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubredditDataAsyncTask.java create mode 100644 app/src/main/res/menu/view_sidebar_activity.xml 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 { + /*private static class InsertSubredditDataAsyncTask extends AsyncTask { 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 { + 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 { + 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(); + } +} diff --git a/app/src/main/res/layout/activity_view_sidebar.xml b/app/src/main/res/layout/activity_view_sidebar.xml index 31ed1529..169afaa6 100644 --- a/app/src/main/res/layout/activity_view_sidebar.xml +++ b/app/src/main/res/layout/activity_view_sidebar.xml @@ -32,12 +32,19 @@ - + app:layout_behavior="@string/appbar_scrolling_view_behavior"> + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/view_sidebar_activity.xml b/app/src/main/res/menu/view_sidebar_activity.xml new file mode 100644 index 00000000..3bb65dee --- /dev/null +++ b/app/src/main/res/menu/view_sidebar_activity.xml @@ -0,0 +1,10 @@ + + + + \ 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 fd022e0c..64cbedf9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -92,6 +92,7 @@ Online: %1$d Cannot fetch subreddit info Cannot fetch user info + Cannot fetch sidebar x%1$d ViewUserDetailActivity