Fetch subreddit info in ViewSidebarActivity.

This commit is contained in:
Alex Ning 2019-12-31 15:11:12 +08:00
parent 286ab8ebca
commit e4ce71bf2a
7 changed files with 143 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import android.text.Spanned;
import android.text.style.SuperscriptSpan; import android.text.style.SuperscriptSpan;
import android.text.util.Linkify; import android.text.util.Linkify;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -25,12 +26,14 @@ import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import org.commonmark.ext.gfm.tables.TableBlock; import org.commonmark.ext.gfm.tables.TableBlock;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; 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.simple.ext.SimpleExtPlugin;
import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute; import io.noties.markwon.urlprocessor.UrlProcessorRelativeToAbsolute;
import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; 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.Infinity;
import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel;
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
import retrofit2.Retrofit;
public class ViewSidebarActivity extends BaseActivity { public class ViewSidebarActivity extends BaseActivity {
@ -58,12 +65,18 @@ public class ViewSidebarActivity extends BaseActivity {
AppBarLayout appBarLayout; AppBarLayout appBarLayout;
@BindView(R.id.toolbar_view_sidebar_activity) @BindView(R.id.toolbar_view_sidebar_activity)
Toolbar toolbar; Toolbar toolbar;
@BindView(R.id.swipe_refresh_layout_view_sidebar_activity)
SwipeRefreshLayout swipeRefreshLayout;
@BindView(R.id.markdown_recycler_view_view_sidebar_activity) @BindView(R.id.markdown_recycler_view_view_sidebar_activity)
RecyclerView markdownRecyclerView; RecyclerView markdownRecyclerView;
@Inject @Inject
@Named("no_oauth")
Retrofit mRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject @Inject
SharedPreferences mSharedPreferences; SharedPreferences mSharedPreferences;
private String subredditName;
private SubredditViewModel mSubredditViewModel; private SubredditViewModel mSubredditViewModel;
@Override @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) { if (subredditName == null) {
Toast.makeText(this, R.string.error_getting_subreddit_name, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.error_getting_subreddit_name, Toast.LENGTH_SHORT).show();
finish(); finish();
@ -186,8 +199,39 @@ public class ViewSidebarActivity extends BaseActivity {
if (subredditData != null) { if (subredditData != null) {
markwonAdapter.setMarkdown(markwon, subredditData.getSidebarDescription()); markwonAdapter.setMarkdown(markwon, subredditData.getSidebarDescription());
markwonAdapter.notifyDataSetChanged(); 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 @Override
@ -195,6 +239,11 @@ public class ViewSidebarActivity extends BaseActivity {
if (item.getItemId() == android.R.id.home) { if (item.getItemId() == android.R.id.home) {
finish(); finish();
return true; return true;
} else if (item.getItemId() == R.id.action_refresh_view_sidebar_activity) {
if (!swipeRefreshLayout.isRefreshing()) {
fetchSubredditData();
}
return true;
} }
return false; return false;
} }

View File

@ -4,7 +4,6 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -44,6 +43,7 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener;
import ml.docilealligator.infinityforreddit.AsyncTask.CheckIsSubscribedToSubredditAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.CheckIsSubscribedToSubredditAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubredditDataAsyncTask;
import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent;
import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent;
@ -61,7 +61,6 @@ import ml.docilealligator.infinityforreddit.ReadMessage;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditDao;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditData;
import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel; import ml.docilealligator.infinityforreddit.SubredditDatabase.SubredditViewModel;
import ml.docilealligator.infinityforreddit.SubredditSubscription; import ml.docilealligator.infinityforreddit.SubredditSubscription;
@ -662,7 +661,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
((FragmentCommunicator) mFragment).changeNSFW(changeNSFWEvent.nsfw); ((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 SubredditDao mSubredditDao;
private SubredditData subredditData; private SubredditData subredditData;
@ -688,5 +687,5 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
interface InsertSubredditDataAsyncTaskListener { interface InsertSubredditDataAsyncTaskListener {
void insertSuccess(); void insertSuccess();
} }
} }*/
} }

View File

@ -104,7 +104,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
private Retrofit mOauthRetrofit; private Retrofit mOauthRetrofit;
private RedditDataRoomDatabase mRedditDataRoomDatabase; private RedditDataRoomDatabase mRedditDataRoomDatabase;
private RequestManager mGlide; private RequestManager mGlide;
private Markwon mMarkwon; private Markwon mPostDetailMarkwon;
private Markwon mCommentMarkwon;
private final MarkwonAdapter mMarkwonAdapter; private final MarkwonAdapter mMarkwonAdapter;
private String mAccessToken; private String mAccessToken;
private String mAccountName; private String mAccountName;
@ -138,7 +139,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mRedditDataRoomDatabase = redditDataRoomDatabase; mRedditDataRoomDatabase = redditDataRoomDatabase;
mGlide = glide; mGlide = glide;
int markdownColor = ContextCompat.getColor(activity, R.color.defaultTextColor); int markdownColor = ContextCompat.getColor(activity, R.color.defaultTextColor);
mMarkwon = Markwon.builder(mActivity) mPostDetailMarkwon = Markwon.builder(mActivity)
.usePlugin(new AbstractMarkwonPlugin() { .usePlugin(new AbstractMarkwonPlugin() {
@Override @Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
@ -169,12 +170,37 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
) )
.usePlugin(TableEntryPlugin.create(mActivity)) .usePlugin(TableEntryPlugin.create(mActivity))
.build(); .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) mMarkwonAdapter = MarkwonAdapter.builder(R.layout.adapter_default_entry, R.id.text)
.include(TableBlock.class, TableEntry.create(builder -> builder .include(TableBlock.class, TableEntry.create(builder -> builder
.tableLayout(R.layout.adapter_table_block, R.id.table_layout) .tableLayout(R.layout.adapter_table_block, R.id.table_layout)
.textLayoutIsRoot(R.layout.view_table_entry_cell))) .textLayoutIsRoot(R.layout.view_table_entry_cell)))
.build(); .build();
mMarkwonAdapter.setMarkdown(mMarkwon, ""); mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, "");
mAccessToken = accessToken; mAccessToken = accessToken;
mAccountName = accountName; mAccountName = accountName;
mPost = post; mPost = post;
@ -528,7 +554,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
}); });
((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager); ((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager);
((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter); ((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter);
mMarkwonAdapter.setMarkdown(mMarkwon, mPost.getSelfText()); mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, mPost.getSelfText());
mMarkwonAdapter.notifyDataSetChanged(); mMarkwonAdapter.notifyDataSetChanged();
} }
@ -562,7 +588,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
}); });
((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager); ((PostDetailViewHolder) holder).mContentMarkdownView.setLayoutManager(linearLayoutManager);
((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter); ((PostDetailViewHolder) holder).mContentMarkdownView.setAdapter(mMarkwonAdapter);
mMarkwonAdapter.setMarkdown(mMarkwon, mPost.getSelfText()); mMarkwonAdapter.setMarkdown(mPostDetailMarkwon, mPost.getSelfText());
mMarkwonAdapter.notifyDataSetChanged(); mMarkwonAdapter.notifyDataSetChanged();
} }
break; break;
@ -681,7 +707,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
((CommentViewHolder) holder).commentTimeTextView.setText(comment.getCommentTime()); ((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).scoreTextView.setText(Integer.toString(comment.getScore() + comment.getVoteType()));
((CommentViewHolder) holder).itemView.setPadding(comment.getDepth() * 8, 0, 0, 0); ((CommentViewHolder) holder).itemView.setPadding(comment.getDepth() * 8, 0, 0, 0);

View File

@ -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();
}
}

View File

@ -32,12 +32,19 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout_view_sidebar_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/markdown_recycler_view_view_sidebar_activity" android:id="@+id/markdown_recycler_view_view_sidebar_activity"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp" android:padding="16dp"
android:clipToPadding="false" android:clipToPadding="false" />
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh_view_sidebar_activity"
android:orderInCategory="1"
android:title="@string/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="ifRoom" />
</menu>

View File

@ -92,6 +92,7 @@
<string name="online_subscribers_number_detail">Online: %1$d</string> <string name="online_subscribers_number_detail">Online: %1$d</string>
<string name="cannot_fetch_subreddit_info">Cannot fetch subreddit info</string> <string name="cannot_fetch_subreddit_info">Cannot fetch subreddit info</string>
<string name="cannot_fetch_user_info">Cannot fetch user info</string> <string name="cannot_fetch_user_info">Cannot fetch user info</string>
<string name="cannot_fetch_sidebar">Cannot fetch sidebar</string>
<string name="gilded_count">x%1$d</string> <string name="gilded_count">x%1$d</string>
<string name="title_activity_view_user_detail">ViewUserDetailActivity</string> <string name="title_activity_view_user_detail">ViewUserDetailActivity</string>