diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-07-31 16:29:29 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-07-31 16:29:29 +0000 |
commit | 47515b87d0490f75dfb0705330f791035024533a (patch) | |
tree | e21178cfaa1e4a1198d79b5a08f5061c2fd46e91 /app | |
parent | dc23a302201985dd637b68ce40345090c59e92fe (diff) | |
download | infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar.gz infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar.bz2 infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar.lz infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar.xz infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.tar.zst infinity-for-reddit-47515b87d0490f75dfb0705330f791035024533a.zip |
Use Brige library and Android-State library to avoid TransactionTooLargeException during state saving and restoration. Minor bugs fixed.
Diffstat (limited to 'app')
8 files changed, 66 insertions, 46 deletions
diff --git a/app/build.gradle b/app/build.gradle index 83263b79..b172df20 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -76,4 +76,7 @@ dependencies { implementation 'org.greenrobot:eventbus:3.1.1' implementation 'com.libRG:customtextview:2.2' 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' } 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<String> comments = api.getComments(subredditNamePrefixed, article); comments.enqueue(new Callback<String>() { 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<String> 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<CommentData> comments; + @State + ArrayList<String> 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<CommentData> expandedComments, String parentId, ArrayList<String> 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 @@ <item android:id="@+id/action_sort_main_activity" android:orderInCategory="1" - android:title="@string/action_search" + android:title="@string/action_sort" android:icon="@drawable/ic_outline_sort_24px" app:showAsAction="ifRoom" /> 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 @@ <item android:id="@+id/action_sort_search_result_activity" android:orderInCategory="1" - android:title="@string/action_search" + android:title="@string/action_sort" android:icon="@drawable/ic_outline_sort_24px" app:showAsAction="ifRoom" /> 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 @@ <item android:id="@+id/action_sort_view_subreddit_detail_activity" android:orderInCategory="1" - android:title="@string/action_search" + android:title="@string/action_sort" android:icon="@drawable/ic_outline_sort_24px" app:showAsAction="ifRoom" /> 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 @@ <string name="action_start_lazy_mode">Start Lazy Mode</string> <string name="action_stop_lazy_mode">Stop Lazy Mode</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="load_posts_error">Error loading posts.\nTap to retry.</string> |