diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-06-23 11:20:37 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-06-23 11:20:37 +0000 |
commit | 260bd4543388c68cc73cabf17ecb559bfb414257 (patch) | |
tree | bb3bce674ee792623b6295440cbcb053af5711fa /app/src/main/java | |
parent | 866db8c68120d3a39923874e779cd976854a277e (diff) | |
download | infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar.gz infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar.bz2 infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar.lz infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar.xz infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.tar.zst infinity-for-reddit-260bd4543388c68cc73cabf17ecb559bfb414257.zip |
Start replacing AsyncTask with Executor in ParsePost.
Diffstat (limited to '')
6 files changed, 166 insertions, 66 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index f7d04c3d..4aa058d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -1093,27 +1093,32 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void initializeAndBindPostViewModel(String accessToken) { if (postType == PostDataSource.TYPE_SEARCH) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } @@ -1124,28 +1129,33 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void initializeAndBindPostViewModelForAnonymous(String concatenatedSubredditNames) { //For anonymous user if (postType == PostDataSource.TYPE_SEARCH) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mExecutor, + new Handler(), mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { //Anonymous Front Page - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, + new Handler(), mRetrofit, mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)).get(PostViewModel.class); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index f3b09d5e..e7c6c6a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit.post; import android.net.Uri; import android.os.AsyncTask; +import android.os.Handler; import android.text.Html; import org.json.JSONArray; @@ -12,6 +13,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.concurrent.Executor; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,9 +27,58 @@ import ml.docilealligator.infinityforreddit.utils.Utils; */ public class ParsePost { - public static void parsePosts(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList, + public static void parsePosts(Executor executor, Handler handler, String response, int nPosts, + PostFilter postFilter, List<ReadPost> readPostList, ParsePostsListingListener parsePostsListingListener) { - new ParsePostDataAsyncTask(response, nPosts, postFilter, readPostList, parsePostsListingListener).execute(); + executor.execute(() -> { + boolean parseFailed = false; + LinkedHashSet<Post> newPosts = new LinkedHashSet<>(); + String lastItem = null; + try { + JSONObject jsonResponse = new JSONObject(response); + JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); + + //Posts listing + int size; + if (nPosts < 0 || nPosts > allData.length()) { + size = allData.length(); + } else { + size = nPosts; + } + + HashSet<ReadPost> readPostHashSet = null; + if (readPostList != null) { + readPostHashSet = new HashSet<>(readPostList); + } + for (int i = 0; i < size; i++) { + try { + if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) { + JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); + Post post = parseBasicData(data); + if (readPostHashSet != null && readPostHashSet.contains(ReadPost.convertPost(post))) { + post.markAsRead(false); + } + if (PostFilter.isPostAllowed(post, postFilter)) { + newPosts.add(post); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + } catch (JSONException e) { + e.printStackTrace(); + parseFailed = true; + } + + if (!parseFailed) { + String finalLastItem = lastItem; + handler.post(() -> parsePostsListingListener.onParsePostsListingSuccess(newPosts, finalLastItem)); + } else { + handler.post(parsePostsListingListener::onParsePostsListingFail); + } + }); } public static void parsePost(String response, ParsePostListener parsePostListener) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java index 8c567809..9c8490ef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; +import android.os.Handler; import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; @@ -9,6 +10,7 @@ import androidx.paging.PageKeyedDataSource; import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; +import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; @@ -37,6 +39,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { public static final String USER_WHERE_SAVED = "saved"; public static final String USER_WHERE_GILDED = "gilded"; + private Executor executor; + private Handler handler; private Retrofit retrofit; private String accessToken; private String accountName; @@ -59,10 +63,12 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private LoadParams<String> params; private LoadCallback<String, Post> callback; - PostDataSource(Retrofit retrofit, String accessToken, String accountName, + PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -78,10 +84,12 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, + PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String path, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -118,10 +126,12 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, + PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -139,10 +149,12 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { postLinkedHashSet = new LinkedHashSet<>(); } - PostDataSource(Retrofit retrofit, String accessToken, String accountName, + PostDataSource(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditOrUserName, String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -254,7 +266,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -317,7 +329,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -377,7 +389,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -451,7 +463,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -512,7 +524,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -583,7 +595,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -666,7 +678,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -757,7 +769,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -817,7 +829,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -888,7 +900,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -938,7 +950,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, null, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, null, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -1002,7 +1014,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, null, + ParsePost.parsePosts(executor, handler, response.body(), -1, postFilter, null, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java index da99a508..ff4088b7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; +import android.os.Handler; import android.util.Log; import androidx.annotation.NonNull; @@ -8,13 +9,16 @@ import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; import java.util.List; +import java.util.concurrent.Executor; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; import retrofit2.Retrofit; class PostDataSourceFactory extends DataSource.Factory { + private Executor executor; + private Handler handler; private Retrofit retrofit; private String accessToken; private String accountName; @@ -31,10 +35,12 @@ class PostDataSourceFactory extends DataSource.Factory { private PostDataSource postDataSource; private MutableLiveData<PostDataSource> postDataSourceLiveData; - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, + PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -47,10 +53,12 @@ class PostDataSourceFactory extends DataSource.Factory { this.readPostList = readPostList; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, + PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -64,10 +72,12 @@ class PostDataSourceFactory extends DataSource.Factory { this.readPostList = readPostList; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, + PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -82,10 +92,12 @@ class PostDataSourceFactory extends DataSource.Factory { this.readPostList = readPostList; } - PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, + PostDataSourceFactory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -104,24 +116,24 @@ class PostDataSourceFactory extends DataSource.Factory { @Override public DataSource<String, Post> create() { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, + postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, + postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { Log.i("asdasfd", "s5 " + (postFilter == null)); - postDataSource = new PostDataSource(retrofit, accessToken, accountName, + postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { - postDataSource = new PostDataSource(retrofit, null, null, + postDataSource = new PostDataSource(executor, handler, retrofit, null, null, sharedPreferences, null, name, postType, sortType, postFilter, null); } else { - postDataSource = new PostDataSource(retrofit, accessToken, accountName, + postDataSource = new PostDataSource(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, userWhere, readPostList); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java index 45001fbf..3649f430 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -1,6 +1,7 @@ package ml.docilealligator.infinityforreddit.post; import android.content.SharedPreferences; +import android.os.Handler; import androidx.annotation.NonNull; import androidx.core.util.Pair; @@ -14,6 +15,7 @@ import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; import java.util.List; +import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; @@ -31,10 +33,10 @@ public class PostViewModel extends ViewModel { private MutableLiveData<PostFilter> postFilterLiveData; private SortTypeAndPostFilterLiveData sortTypeAndPostFilterLiveData; - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, + public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences cache, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, + postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, sharedPreferences, cache, postType, sortType, postFilter, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), @@ -64,11 +66,11 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, + public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, + postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, sharedPreferences, cache, subredditName, postType, sortType, postFilter, readPostList); @@ -99,11 +101,11 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, + public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences cache, String username, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, + postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, sharedPreferences, cache, username, postType, sortType, postFilter, where, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), @@ -133,10 +135,10 @@ public class PostViewModel extends ViewModel { }); } - public PostViewModel(Retrofit retrofit, String accessToken, String accountName, + public PostViewModel(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, + postDataSourceFactory = new PostDataSourceFactory(executor, handler, retrofit, accessToken, accountName, sharedPreferences, cache, subredditName, query, postType, sortType, postFilter, readPostList); @@ -200,6 +202,8 @@ public class PostViewModel extends ViewModel { } public static class Factory extends ViewModelProvider.NewInstanceFactory { + private Executor executor; + private Handler handler; private Retrofit retrofit; private String accessToken; private String accountName; @@ -213,9 +217,11 @@ public class PostViewModel extends ViewModel { private String userWhere; private List<ReadPost> readPostList; - public Factory(Retrofit retrofit, String accessToken, String accountName, + public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -227,10 +233,11 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } - public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, String name, - int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String name, int postType, SortType sortType, PostFilter postFilter, + List<ReadPost> readPostList) {this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -244,9 +251,12 @@ public class PostViewModel extends ViewModel { } //User posts - public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String username, - int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { + public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String username, int postType, SortType sortType, PostFilter postFilter, String where, + List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -260,9 +270,12 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } - public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, - String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { + public Factory(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String accountName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, + String name, String query, int postType, SortType sortType, PostFilter postFilter, + List<ReadPost> readPostList) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -277,8 +290,10 @@ public class PostViewModel extends ViewModel { } //Anonymous Front Page - public Factory(Retrofit retrofit, SharedPreferences sharedPreferences, String concatenatedSubredditNames, - int postType, SortType sortType, PostFilter postFilter) { + public Factory(Executor executor, Handler handler, Retrofit retrofit, SharedPreferences sharedPreferences, + String concatenatedSubredditNames, int postType, SortType sortType, PostFilter postFilter) { + this.executor = executor; + this.handler = handler; this.retrofit = retrofit; this.sharedPreferences = sharedPreferences; this.name = concatenatedSubredditNames; @@ -291,22 +306,22 @@ public class PostViewModel extends ViewModel { @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { if (postType == PostDataSource.TYPE_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { - return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { - return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { - return (T) new PostViewModel(retrofit, null, null, sharedPreferences, + return (T) new PostViewModel(executor, handler, retrofit, null, null, sharedPreferences, null, name, postType, sortType, postFilter, null); } else { - return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, + return (T) new PostViewModel(executor, handler, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, userWhere, readPostList); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java index 504db565..e74d7e53 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/MaterialYouUtils.java @@ -30,7 +30,7 @@ public class MaterialYouUtils { if (wallpaperColors != null) { int colorPrimaryInt = shiftColorTo255(wallpaperColors.getPrimaryColor().toArgb(), 0.4); - int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.4); + int colorPrimaryDarkInt = shiftColorTo0(colorPrimaryInt, 0.3); int backgroundColor = shiftColorTo255(colorPrimaryInt, 0.6); int cardViewBackgroundColor = shiftColorTo255(colorPrimaryInt, 0.9); Color colorAccent = wallpaperColors.getSecondaryColor(); @@ -62,7 +62,7 @@ public class MaterialYouUtils { .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TAB_INDICATOR, colorPrimaryAppropriateTextColor) .putInt(CustomThemeSharedPreferencesUtils.TAB_LAYOUT_WITH_EXPANDED_COLLAPSING_TOOLBAR_TEXT_COLOR, colorPrimaryAppropriateTextColor) .putInt(CustomThemeSharedPreferencesUtils.CIRCULAR_PROGRESS_BAR_BACKGROUND, colorPrimaryInt) - .putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryDarkInt) == Color.toArgb(Color.BLACK)) + .putBoolean(CustomThemeSharedPreferencesUtils.LIGHT_STATUS_BAR, getAppropriateTextColor(colorPrimaryInt) == Color.toArgb(Color.BLACK)) .apply(); darkThemeSharedPreferences.edit() .putInt(CustomThemeSharedPreferencesUtils.COLOR_ACCENT, colorPrimaryInt) |