From 38caf7365a38b183328d23384eff98505424c796 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 5 Sep 2021 11:11:01 +0800 Subject: Fix a bug which causes the ViewModel not retain its state after orientation change. --- app/build.gradle | 2 ++ .../activities/Paging3TestActivity.java | 9 +++-- .../post/PostPaging3PagingSource.java | 40 ++-------------------- .../post/PostPaging3Repository.java | 12 ++++--- .../post/PostPaging3ViewModel.java | 24 ++++++++++--- 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9a80bb9c..4f792b29 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,6 +51,8 @@ dependencies { def lifecycleVersion = "2.2.0" implementation "androidx.lifecycle:lifecycle-livedata:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycleVersion" + implementation "androidx.lifecycle:lifecycle-runtime:$lifecycleVersion" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion" annotationProcessor "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion" implementation 'androidx.paging:paging-runtime:3.0.1' diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/Paging3TestActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/Paging3TestActivity.java index e3e7feb0..857fc6c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/Paging3TestActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/Paging3TestActivity.java @@ -75,6 +75,7 @@ public class Paging3TestActivity extends BaseActivity { @Inject Executor mExecutor; public PostPaging3ViewModel viewModel; + private Paging3TestAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { @@ -103,7 +104,7 @@ public class Paging3TestActivity extends BaseActivity { } RecyclerView recyclerView = findViewById(R.id.recycler_view); - Paging3TestAdapter adapter = new Paging3TestAdapter(this, new PostFragment(), mExecutor, + adapter = new Paging3TestAdapter(this, new PostFragment(), mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, @@ -141,12 +142,14 @@ public class Paging3TestActivity extends BaseActivity { }); recyclerView.setAdapter(adapter); - viewModel = new ViewModelProvider(this, new PostPaging3ViewModel.Factory(mExecutor, mPaging3Retrofit, + viewModel = new ViewModelProvider(this, new PostPaging3ViewModel.Factory(getLifecycle(), mExecutor, mPaging3Retrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, null, null, postType, sortType, null, null, null, null, null)).get(PostPaging3ViewModel.class); - viewModel.getPosts().observe(this, postPagingData -> adapter.submitData(getLifecycle(), postPagingData)); + viewModel.getPosts().observe(this, postPagingData -> { + adapter.submitData(getLifecycle(), postPagingData); + }); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java index cdb0c4b4..42175bcb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3PagingSource.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; +import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; @@ -76,47 +77,10 @@ public class PostPaging3PagingSource extends ListenableFuturePagingSource loadParams, @NonNull Continuation> continuation) { - RedditAPI api = retrofit.create(RedditAPI.class); - Call bestPost; - if(sortType.getTime() != null) { - bestPost = api.getBestPosts(sortType.getType().value, sortType.getTime().value, loadParams.getKey(), - APIUtils.getOAuthHeader(accessToken)); - } else { - bestPost = api.getBestPosts(sortType.getType().value, loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); - } - - try { - Response response = bestPost.execute(); - if (response.isSuccessful()) { - String responseString = response.body(); - LinkedHashSet newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, readPostList); - String lastItem = ParsePost.getLastItem(responseString); - if (newPosts == null) { - return new LoadResult.Error<>(new Exception("Error parsing more posts")); - } else { - int currentPostsSize = postLinkedHashSet.size(); - postLinkedHashSet.addAll(newPosts); - if (currentPostsSize == postLinkedHashSet.size()) { - return new LoadResult.Page<>(new ArrayList<>(), null, lastItem); - } else { - return new LoadResult.Page<>(new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()), null, lastItem); - } - } - } else { - return new LoadResult.Error<>(new Exception("Response failed")); - } - } catch (IOException e) { - e.printStackTrace(); - return new LoadResult.Error<>(e); - } - }*/ - @NonNull @Override public ListenableFuture> loadFuture(@NonNull LoadParams loadParams) { + Log.i("asfsaf", "s" + loadParams.getKey()); RedditAPI api = retrofit.create(RedditAPI.class); ListenableFuture> bestPost; if(sortType.getTime() != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3Repository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3Repository.java index a761cd7e..90c691d4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3Repository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3Repository.java @@ -34,6 +34,7 @@ public class PostPaging3Repository { private String userWhere; private String multiRedditPath; private LinkedHashSet postLinkedHashSet; + private PostPaging3PagingSource paging3PagingSource; public PostPaging3Repository(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, @@ -57,15 +58,16 @@ public class PostPaging3Repository { this.userWhere = userWhere; this.multiRedditPath = multiRedditPath; this.postLinkedHashSet = postLinkedHashSet; + paging3PagingSource = new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, sharedPreferences, + postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); } public LiveData> getPostsLiveData() { - return PagingLiveData.getLiveData(new Pager<>(new PagingConfig(25, 50, false), - this::returnPagingSoruce)); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); + return PagingLiveData.getLiveData(pager); } - private PostPaging3PagingSource returnPagingSoruce() { - return new PostPaging3PagingSource(executor, retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); + public PostPaging3PagingSource returnPagingSoruce() { + return paging3PagingSource; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3ViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3ViewModel.java index 662c804c..e869cd6c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3ViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPaging3ViewModel.java @@ -3,10 +3,13 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; import androidx.annotation.NonNull; +import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelKt; import androidx.lifecycle.ViewModelProvider; +import androidx.paging.Pager; +import androidx.paging.PagingConfig; import androidx.paging.PagingData; import androidx.paging.PagingLiveData; @@ -23,8 +26,10 @@ public class PostPaging3ViewModel extends ViewModel { private PostPaging3Repository repository; private LiveData> posts; + private PostPaging3PagingSource paging3PagingSource; + private Lifecycle lifecycle; - public PostPaging3ViewModel(Executor executor, Retrofit retrofit, String accessToken, String accountName, + public PostPaging3ViewModel(Lifecycle lifecycle, Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, String query, String trendingSource, int postType, @@ -32,14 +37,24 @@ public class PostPaging3ViewModel extends ViewModel { String userWhere, String multiRedditPath, LinkedHashSet postLinkedHashSet) { repository = new PostPaging3Repository(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditOrUserName, query, trendingSource, postType, sortType, postFilter, readPostList, userWhere, multiRedditPath, postLinkedHashSet); + paging3PagingSource = repository.returnPagingSoruce(); + Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); + posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); } public LiveData> getPosts() { - posts = PagingLiveData.cachedIn(repository.getPostsLiveData(), ViewModelKt.getViewModelScope(this)); + /*Pager pager = new Pager<>(new PagingConfig(25, 25, false), this::returnPagingSoruce); + posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), ViewModelKt.getViewModelScope(this)); + posts = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), lifecycle);*/ return posts; } + private PostPaging3PagingSource returnPagingSoruce() { + return paging3PagingSource; + } + public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Lifecycle lifecycle; private Executor executor; private Retrofit retrofit; private String accessToken; @@ -57,11 +72,12 @@ public class PostPaging3ViewModel extends ViewModel { private String multiRedditPath; private LinkedHashSet postLinkedHashSet; - public Factory(Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, + public Factory(Lifecycle lifecycle, Executor executor, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, String query, String trendingSource, int postType, SortType sortType, PostFilter postFilter, List readPostList, String userWhere, String multiRedditPath, LinkedHashSet postLinkedHashSet) { + this.lifecycle = lifecycle; this.executor = executor; this.retrofit = retrofit; this.accessToken = accessToken; @@ -83,7 +99,7 @@ public class PostPaging3ViewModel extends ViewModel { @NonNull @Override public T create(@NonNull Class modelClass) { - return (T) new PostPaging3ViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostPaging3ViewModel(lifecycle, executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditOrUserName, query, trendingSource, postType, sortType, postFilter, readPostList, userWhere, multiRedditPath, postLinkedHashSet); } -- cgit v1.2.3