Use Brige library and Android-State library to avoid TransactionTooLargeException during state saving and restoration. Minor bugs fixed.

This commit is contained in:
Alex Ning 2019-08-01 00:29:29 +08:00
parent dc23a30220
commit 47515b87d0
10 changed files with 66 additions and 46 deletions

Binary file not shown.

Binary file not shown.

View File

@ -76,4 +76,7 @@ dependencies {
implementation 'org.greenrobot:eventbus:3.1.1' implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.libRG:customtextview:2.2' implementation 'com.libRG:customtextview:2.2'
implementation 'com.github.Deishelon:RoundedBottomSheet:1.0.1' implementation 'com.github.Deishelon:RoundedBottomSheet:1.0.1'
implementation 'com.github.livefront:bridge:v1.2.0'
implementation 'com.evernote:android-state:1.4.1'
annotationProcessor 'com.evernote:android-state-processor:1.4.1'
} }

View File

@ -23,8 +23,8 @@ class FetchComment {
void onFetchMoreCommentFailed(); void onFetchMoreCommentFailed();
} }
static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article, static void fetchComments(Retrofit retrofit, String subredditNamePrefixed, String article,
Locale locale, FetchCommentListener fetchCommentListener) { Locale locale, FetchCommentListener fetchCommentListener) {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> comments = api.getComments(subredditNamePrefixed, article); Call<String> comments = api.getComments(subredditNamePrefixed, article);
comments.enqueue(new Callback<String>() { comments.enqueue(new Callback<String>() {

View File

@ -1,6 +1,14 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Application; import android.app.Application;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.evernote.android.state.StateSaver;
import com.livefront.bridge.Bridge;
import com.livefront.bridge.SavedStateHandler;
public class Infinity extends Application { public class Infinity extends Application {
private AppComponent mAppComponent; private AppComponent mAppComponent;
@ -12,6 +20,18 @@ public class Infinity extends Application {
mAppComponent = DaggerAppComponent.builder() mAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this)) .appModule(new AppModule(this))
.build(); .build();
Bridge.initialize(getApplicationContext(), new SavedStateHandler() {
@Override
public void saveInstanceState(@NonNull Object target, @NonNull Bundle state) {
StateSaver.saveInstanceState(target, state);
}
@Override
public void restoreInstanceState(@NonNull Object target, @Nullable Bundle state) {
StateSaver.restoreInstanceState(target, state);
}
});
} }
public AppComponent getmAppComponent() { public AppComponent getmAppComponent() {

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -23,7 +24,9 @@ import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager; import com.bumptech.glide.RequestManager;
import com.evernote.android.state.State;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.livefront.bridge.Bridge;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.Subscribe;
@ -54,24 +57,24 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private Locale mLocale; private Locale mLocale;
private int orientation; private int orientation;
private static final String ORIENTATION_STATE = "OS";
private static final String POST_STATE = "PS";
private static final String IS_REFRESHING_STATE = "IRS";
private static final String IS_LOADING_MORE_CHILDREN_STATE = "ILMCS";
private static final String COMMENTS_STATE = "CS";
private static final String HAS_MORE_CHILDREN_STATE = "HMCS";
private static final String MORE_CHILDREN_LIST_STATE = "MCLS";
private static final String MORE_CHILDREN_STARTING_INDEX_STATE = "MCSIS";
private Post mPost;
private int postListPosition = -1; private int postListPosition = -1;
private boolean isLoadingMoreChildren = false; @State
private boolean isRefreshing = false; Post mPost;
private ArrayList<String> children; @State
private int mChildrenStartingIndex = 0; boolean isLoadingMoreChildren = false;
private boolean loadMoreChildrenSuccess = true; @State
private boolean hasMoreChildren; boolean isRefreshing = false;
@State
ArrayList<CommentData> comments;
@State
ArrayList<String> children;
@State
int mChildrenStartingIndex = 0;
@State
boolean loadMoreChildrenSuccess = true;
@State
boolean hasMoreChildren;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private CommentAndPostRecyclerViewAdapter mAdapter; private CommentAndPostRecyclerViewAdapter mAdapter;
@ -99,6 +102,8 @@ public class ViewPostDetailActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_post_detail); setContentView(R.layout.activity_view_post_detail);
Bridge.restoreInstanceState(this, savedInstanceState);
ButterKnife.bind(this); ButterKnife.bind(this);
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
@ -114,12 +119,10 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mLinearLayoutManager = new LinearLayoutManager(this); mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager); mRecyclerView.setLayoutManager(mLinearLayoutManager);
if(savedInstanceState == null) { orientation = getResources().getConfiguration().orientation;
orientation = getResources().getConfiguration().orientation;
if(mPost == null) {
mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA);
} else {
orientation = savedInstanceState.getInt(ORIENTATION_STATE);
mPost = savedInstanceState.getParcelable(POST_STATE);
} }
if(mPost == null) { if(mPost == null) {
@ -145,24 +148,19 @@ public class ViewPostDetailActivity extends AppCompatActivity {
}); });
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
if(savedInstanceState != null) { if(comments == null) {
isRefreshing = savedInstanceState.getBoolean(IS_REFRESHING_STATE); fetchComments();
} else {
if(isRefreshing) { if(isRefreshing) {
isRefreshing = false; isRefreshing = false;
refresh(); refresh();
} else { } else {
mAdapter.addComments(savedInstanceState.getParcelableArrayList(COMMENTS_STATE), mAdapter.addComments(comments, hasMoreChildren);
savedInstanceState.getBoolean(HAS_MORE_CHILDREN_STATE));
isLoadingMoreChildren = savedInstanceState.getBoolean(IS_LOADING_MORE_CHILDREN_STATE);
children = savedInstanceState.getStringArrayList(MORE_CHILDREN_LIST_STATE);
mChildrenStartingIndex = savedInstanceState.getInt(MORE_CHILDREN_STARTING_INDEX_STATE);
if(isLoadingMoreChildren) { if(isLoadingMoreChildren) {
isLoadingMoreChildren = false; isLoadingMoreChildren = false;
fetchMoreComments(); fetchMoreComments();
} }
} }
} else {
fetchComment();
} }
} }
@ -263,16 +261,20 @@ public class ViewPostDetailActivity extends AppCompatActivity {
}); });
} }
private void fetchComment() { private void fetchComments() {
mAdapter.initiallyLoading(); mAdapter.initiallyLoading();
FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), FetchComment.fetchComments(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(),
mLocale, new FetchComment.FetchCommentListener() { mLocale, new FetchComment.FetchCommentListener() {
@Override @Override
public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> children) { String parentId, ArrayList<String> children) {
ViewPostDetailActivity.this.children = children; ViewPostDetailActivity.this.children = children;
comments = expandedComments;
if(comments != null) {
Log.i("thisis ", "not null");
}
hasMoreChildren = children.size() != 0; hasMoreChildren = children.size() != 0;
mAdapter.addComments(expandedComments, hasMoreChildren); mAdapter.addComments(expandedComments, hasMoreChildren);
@ -337,7 +339,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mFetchPostInfoLinearLayout.setVisibility(View.GONE); mFetchPostInfoLinearLayout.setVisibility(View.GONE);
mGlide.clear(mFetchPostInfoImageView); mGlide.clear(mFetchPostInfoImageView);
fetchComment(); fetchComments();
String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
@ -425,14 +427,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
@Override @Override
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt(ORIENTATION_STATE, orientation); Bridge.saveInstanceState(this, outState);
outState.putParcelable(POST_STATE, mPost);
outState.putBoolean(IS_REFRESHING_STATE, isRefreshing);
outState.putBoolean(IS_LOADING_MORE_CHILDREN_STATE, isLoadingMoreChildren);
outState.putParcelableArrayList(COMMENTS_STATE, mAdapter.getVisibleComments());
outState.putBoolean(HAS_MORE_CHILDREN_STATE, hasMoreChildren);
outState.putStringArrayList(MORE_CHILDREN_LIST_STATE, children);
outState.putInt(MORE_CHILDREN_STARTING_INDEX_STATE, mChildrenStartingIndex);
} }
@Override @Override
@ -448,6 +443,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
protected void onDestroy() { protected void onDestroy() {
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);
super.onDestroy(); super.onDestroy();
Bridge.clear(this);
if(mLoadSubredditIconAsyncTask != null) { if(mLoadSubredditIconAsyncTask != null) {
mLoadSubredditIconAsyncTask.cancel(true); mLoadSubredditIconAsyncTask.cancel(true);
} }

View File

@ -6,7 +6,7 @@
<item <item
android:id="@+id/action_sort_main_activity" android:id="@+id/action_sort_main_activity"
android:orderInCategory="1" android:orderInCategory="1"
android:title="@string/action_search" android:title="@string/action_sort"
android:icon="@drawable/ic_outline_sort_24px" android:icon="@drawable/ic_outline_sort_24px"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />

View File

@ -4,7 +4,7 @@
<item <item
android:id="@+id/action_sort_search_result_activity" android:id="@+id/action_sort_search_result_activity"
android:orderInCategory="1" android:orderInCategory="1"
android:title="@string/action_search" android:title="@string/action_sort"
android:icon="@drawable/ic_outline_sort_24px" android:icon="@drawable/ic_outline_sort_24px"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />

View File

@ -6,7 +6,7 @@
<item <item
android:id="@+id/action_sort_view_subreddit_detail_activity" android:id="@+id/action_sort_view_subreddit_detail_activity"
android:orderInCategory="1" android:orderInCategory="1"
android:title="@string/action_search" android:title="@string/action_sort"
android:icon="@drawable/ic_outline_sort_24px" android:icon="@drawable/ic_outline_sort_24px"
app:showAsAction="ifRoom" /> app:showAsAction="ifRoom" />

View File

@ -22,6 +22,7 @@
<string name="action_start_lazy_mode">Start Lazy Mode</string> <string name="action_start_lazy_mode">Start Lazy Mode</string>
<string name="action_stop_lazy_mode">Stop Lazy Mode</string> <string name="action_stop_lazy_mode">Stop Lazy Mode</string>
<string name="action_send">Send</string> <string name="action_send">Send</string>
<string name="action_sort">Sort</string>
<string name="tap_to_retry">Error loading image. Tap to retry.</string> <string name="tap_to_retry">Error loading image. Tap to retry.</string>
<string name="load_posts_error">Error loading posts.\nTap to retry.</string> <string name="load_posts_error">Error loading posts.\nTap to retry.</string>