From 47515b87d0490f75dfb0705330f791035024533a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 1 Aug 2019 00:29:29 +0800 Subject: Use Brige library and Android-State library to avoid TransactionTooLargeException during state saving and restoration. Minor bugs fixed. --- .../infinityforreddit/FetchComment.java | 4 +- .../infinityforreddit/Infinity.java | 20 ++++++ .../infinityforreddit/ViewPostDetailActivity.java | 78 ++++++++++------------ app/src/main/res/menu/main_activity.xml | 2 +- app/src/main/res/menu/search_result_activity.xml | 2 +- .../res/menu/view_subreddit_detail_activity.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 7 files changed, 63 insertions(+), 46 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index fb1aec1f..cec412fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -23,8 +23,8 @@ class FetchComment { void onFetchMoreCommentFailed(); } - static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article, - Locale locale, FetchCommentListener fetchCommentListener) { + static void fetchComments(Retrofit retrofit, String subredditNamePrefixed, String article, + Locale locale, FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call comments = api.getComments(subredditNamePrefixed, article); comments.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java index 8137355b..24345789 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Infinity.java @@ -1,6 +1,14 @@ package ml.docilealligator.infinityforreddit; 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 { private AppComponent mAppComponent; @@ -12,6 +20,18 @@ public class Infinity extends Application { mAppComponent = DaggerAppComponent.builder() .appModule(new AppModule(this)) .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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 903786d0..87c4a380 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -23,7 +24,9 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import com.evernote.android.state.State; import com.google.android.material.snackbar.Snackbar; +import com.livefront.bridge.Bridge; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -54,24 +57,24 @@ public class ViewPostDetailActivity extends AppCompatActivity { private Locale mLocale; 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 boolean isLoadingMoreChildren = false; - private boolean isRefreshing = false; - private ArrayList children; - private int mChildrenStartingIndex = 0; - private boolean loadMoreChildrenSuccess = true; - private boolean hasMoreChildren; + @State + Post mPost; + @State + boolean isLoadingMoreChildren = false; + @State + boolean isRefreshing = false; + @State + ArrayList comments; + @State + ArrayList children; + @State + int mChildrenStartingIndex = 0; + @State + boolean loadMoreChildrenSuccess = true; + @State + boolean hasMoreChildren; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; @@ -99,6 +102,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_post_detail); + Bridge.restoreInstanceState(this, savedInstanceState); + ButterKnife.bind(this); EventBus.getDefault().register(this); @@ -114,12 +119,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { mLinearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); - if(savedInstanceState == null) { - orientation = getResources().getConfiguration().orientation; + orientation = getResources().getConfiguration().orientation; + + if(mPost == null) { mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); - } else { - orientation = savedInstanceState.getInt(ORIENTATION_STATE); - mPost = savedInstanceState.getParcelable(POST_STATE); } if(mPost == null) { @@ -145,24 +148,19 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); mRecyclerView.setAdapter(mAdapter); - if(savedInstanceState != null) { - isRefreshing = savedInstanceState.getBoolean(IS_REFRESHING_STATE); + if(comments == null) { + fetchComments(); + } else { if(isRefreshing) { isRefreshing = false; refresh(); } else { - mAdapter.addComments(savedInstanceState.getParcelableArrayList(COMMENTS_STATE), - 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); + mAdapter.addComments(comments, hasMoreChildren); if(isLoadingMoreChildren) { isLoadingMoreChildren = false; fetchMoreComments(); } } - } else { - fetchComment(); } } @@ -263,16 +261,20 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); } - private void fetchComment() { + private void fetchComments() { mAdapter.initiallyLoading(); - FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), + FetchComment.fetchComments(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), mLocale, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { ViewPostDetailActivity.this.children = children; + comments = expandedComments; + if(comments != null) { + Log.i("thisis ", "not null"); + } hasMoreChildren = children.size() != 0; mAdapter.addComments(expandedComments, hasMoreChildren); @@ -337,7 +339,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mFetchPostInfoLinearLayout.setVisibility(View.GONE); mGlide.clear(mFetchPostInfoImageView); - fetchComment(); + fetchComments(); String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); @@ -425,14 +427,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - outState.putInt(ORIENTATION_STATE, orientation); - 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); + Bridge.saveInstanceState(this, outState); } @Override @@ -448,6 +443,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); + Bridge.clear(this); if(mLoadSubredditIconAsyncTask != null) { mLoadSubredditIconAsyncTask.cancel(true); } diff --git a/app/src/main/res/menu/main_activity.xml b/app/src/main/res/menu/main_activity.xml index 43cb0785..41d4f28b 100644 --- a/app/src/main/res/menu/main_activity.xml +++ b/app/src/main/res/menu/main_activity.xml @@ -6,7 +6,7 @@ diff --git a/app/src/main/res/menu/search_result_activity.xml b/app/src/main/res/menu/search_result_activity.xml index b4db6c93..0fc7a1fe 100644 --- a/app/src/main/res/menu/search_result_activity.xml +++ b/app/src/main/res/menu/search_result_activity.xml @@ -4,7 +4,7 @@ diff --git a/app/src/main/res/menu/view_subreddit_detail_activity.xml b/app/src/main/res/menu/view_subreddit_detail_activity.xml index 9dcdb633..a8912ab2 100644 --- a/app/src/main/res/menu/view_subreddit_detail_activity.xml +++ b/app/src/main/res/menu/view_subreddit_detail_activity.xml @@ -6,7 +6,7 @@ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8be8345e..09bb103a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -22,6 +22,7 @@ Start Lazy Mode Stop Lazy Mode Send + Sort Error loading image. Tap to retry. Error loading posts.\nTap to retry. -- cgit v1.2.3