Use Executor to save read posts. Hide the fab and the bottom app bar when scrolling comments in ViewUserDetailActivity.

This commit is contained in:
Alex Ning 2020-12-26 19:18:37 +08:00
parent 60908860b7
commit 44c758204c
15 changed files with 76 additions and 92 deletions

View File

@ -30,6 +30,8 @@ import com.r0adkll.slidr.model.SlidrInterface;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -87,6 +89,8 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
SharedPreferences mPostLayoutSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter;
private SlidrInterface mSlidrInterface;
@ -340,7 +344,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
private class SectionsPagerAdapter extends FragmentStateAdapter {

View File

@ -245,6 +245,8 @@ public class CustomizePostFilterActivity extends BaseActivity {
postTypeGifCheckBox.setChecked(postFilter.containGifType);
postTypeVideoCheckBox.setChecked(postFilter.containVideoType);
postTypeGalleryCheckBox.setChecked(postFilter.containGalleryType);
onlyNSFWSwitch.setChecked(postFilter.onlyNSFW);
onlySpoilerSwitch.setChecked(postFilter.onlySpoiler);
titleExcludesStringsTextInputEditText.setText(postFilter.postTitleExcludesStrings);
titleExcludesRegexTextInputEditText.setText(postFilter.postTitleExcludesRegex);
excludesSubredditsTextInputEditText.setText(postFilter.excludeSubreddits);

View File

@ -24,6 +24,8 @@ import com.r0adkll.slidr.Slidr;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -33,7 +35,6 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface;
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
import ml.docilealligator.infinityforreddit.PostFragmentContentScrollingInterface;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@ -51,6 +52,7 @@ import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.fragments.PostFragment;
import ml.docilealligator.infinityforreddit.post.Post;
import ml.docilealligator.infinityforreddit.post.PostDataSource;
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
import ml.docilealligator.infinityforreddit.readpost.InsertReadPost;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
@ -92,6 +94,8 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec
SharedPreferences mPostLayoutSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private boolean isInLazyMode = false;
private boolean mNullAccessToken = false;
private String mAccessToken;
@ -537,7 +541,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
@Override

View File

@ -58,6 +58,7 @@ import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@ -198,6 +199,8 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
SharedPreferences bottomAppBarSharedPreference;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter;
private AppBarLayout.LayoutParams params;
@ -1318,7 +1321,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
private class SectionsPagerAdapter extends FragmentStateAdapter {

View File

@ -24,7 +24,7 @@ import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.adapters.FilterFragmentPostFilterRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.adapters.PostFilterRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostFilterOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.postfilter.DeletePostFilter;
@ -53,7 +53,7 @@ public class PostFilterPreferenceActivity extends BaseActivity {
@Inject
Executor executor;
public PostFilterViewModel postFilterViewModel;
private FilterFragmentPostFilterRecyclerViewAdapter adapter;
private PostFilterRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -75,7 +75,7 @@ public class PostFilterPreferenceActivity extends BaseActivity {
startActivity(intent);
});
adapter = new FilterFragmentPostFilterRecyclerViewAdapter(postFilter -> {
adapter = new PostFilterRecyclerViewAdapter(postFilter -> {
PostFilterOptionsBottomSheetFragment postFilterOptionsBottomSheetFragment = new PostFilterOptionsBottomSheetFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(PostFilterOptionsBottomSheetFragment.EXTRA_POST_FILTER, postFilter);

View File

@ -4,9 +4,11 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.lifecycle.ViewModelProvider;
@ -180,6 +182,16 @@ public class PostFilterUsageListingActivity extends BaseActivity {
DeletePostFilterUsage.deletePostFilterUsage(redditDataRoomDatabase, executor, postFilterUsage);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return false;
}
@Override
protected SharedPreferences getDefaultSharedPreferences() {
return sharedPreferences;

View File

@ -23,6 +23,8 @@ import com.r0adkll.slidr.Slidr;
import org.greenrobot.eventbus.EventBus;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -91,6 +93,8 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
SharedPreferences mPostLayoutSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
private boolean mNullAccessToken = false;
private String mAccessToken;
private String mAccountName;
@ -366,6 +370,6 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
}

View File

@ -54,6 +54,7 @@ import org.greenrobot.eventbus.Subscribe;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -190,6 +191,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
SharedPreferences bottomAppBarSharedPreference;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
public SubredditViewModel mSubredditViewModel;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter;
@ -1246,7 +1249,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
private class SectionsPagerAdapter extends FragmentStateAdapter {

View File

@ -57,6 +57,7 @@ import org.greenrobot.eventbus.Subscribe;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@ -192,6 +193,8 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
SharedPreferences bottomAppBarSharedPreference;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
public UserViewModel userViewModel;
private FragmentManager fragmentManager;
private SectionsPagerAdapter sectionsPagerAdapter;
@ -1272,7 +1275,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
@Override
public void markPostAsRead(Post post) {
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mAccountName, post.getId());
InsertReadPost.insertReadPost(mRedditDataRoomDatabase, mExecutor, mAccountName, post.getId());
}
@Override

View File

@ -13,7 +13,7 @@ import java.util.List;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
public class FilterFragmentPostFilterRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_HEADER = 1;
private static final int VIEW_TYPE_POST_FILTER = 2;
@ -25,7 +25,7 @@ public class FilterFragmentPostFilterRecyclerViewAdapter extends RecyclerView.Ad
void onItemClick(PostFilter postFilter);
}
public FilterFragmentPostFilterRecyclerViewAdapter(OnItemClickListener onItemClickListener) {
public PostFilterRecyclerViewAdapter(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}

View File

@ -36,17 +36,18 @@ import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.PostFragmentContentScrollingInterface;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.adapters.CommentsListingRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask;
import ml.docilealligator.infinityforreddit.comment.CommentViewModel;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.FragmentCommunicator;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import ml.docilealligator.infinityforreddit.utils.Utils;
import retrofit2.Retrofit;
@ -289,6 +290,19 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni
mCommentRecyclerView.setAdapter(mAdapter);
if (mActivity instanceof PostFragmentContentScrollingInterface) {
mCommentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
((PostFragmentContentScrollingInterface) mActivity).contentScrollDown();
} else if (dy < 0) {
((PostFragmentContentScrollingInterface) mActivity).contentScrollUp();
}
}
});
}
CommentViewModel.Factory factory;
if (mAccessToken == null) {

View File

@ -6,11 +6,6 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class DeletePostFilter {
public static void deletePostFilter(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, PostFilter postFilter) {
executor.execute(new Runnable() {
@Override
public void run() {
redditDataRoomDatabase.postFilterDao().deletePostFilter(postFilter);
}
});
executor.execute(() -> redditDataRoomDatabase.postFilterDao().deletePostFilter(postFilter));
}
}

View File

@ -26,6 +26,8 @@ public interface PostFilterDao {
@Query("SELECT * FROM post_filter")
LiveData<List<PostFilter>> getAllPostFiltersLiveData();
@Query("SELECT * FROM post_filter WHERE post_filter.name IN (SELECT post_filter_usage.name FROM post_filter_usage WHERE usage = :usage AND name_of_usage = :nameOfUsage)")
@Query("SELECT * FROM post_filter WHERE post_filter.name IN " +
"(SELECT post_filter_usage.name FROM post_filter_usage WHERE (usage = :usage AND name_of_usage = :nameOfUsage) " +
"OR (usage =:usage AND name_of_usage = '--'))")
List<PostFilter> getValidPostFilters(int usage, String nameOfUsage);
}

View File

@ -1,46 +0,0 @@
package ml.docilealligator.infinityforreddit.readpost;
import android.os.AsyncTask;
import java.util.ArrayList;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class FetchReadPosts {
public interface FetchReadPostsListener {
void success(ArrayList<ReadPost> readPosts);
}
public static void fetchReadPosts(RedditDataRoomDatabase redditDataRoomDatabase, String username,
FetchReadPostsListener fetchReadPostsListener) {
new FetchAllReadPostsAsyncTask(redditDataRoomDatabase, username, fetchReadPostsListener).execute();
}
private static class FetchAllReadPostsAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private String username;
private FetchReadPostsListener fetchReadPostsListener;
private ArrayList<ReadPost> readPosts;
private FetchAllReadPostsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String username,
FetchReadPostsListener fetchReadPostsListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.username = username;
this.fetchReadPostsListener = fetchReadPostsListener;
}
@Override
protected Void doInBackground(Void... voids) {
readPosts = (ArrayList<ReadPost>) redditDataRoomDatabase.readPostDao().getAllReadPosts(username);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
fetchReadPostsListener.success(readPosts);
}
}
}

View File

@ -1,28 +1,13 @@
package ml.docilealligator.infinityforreddit.readpost;
import android.os.AsyncTask;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class InsertReadPost {
public static void insertReadPost(RedditDataRoomDatabase redditDataRoomDatabase, String username, String postId) {
new InsertReadPostAsyncTask(redditDataRoomDatabase, username, postId).execute();
}
private static class InsertReadPostAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private String username;
private String postId;
public InsertReadPostAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String username, String postId) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.username = username;
this.postId = postId;
}
@Override
protected Void doInBackground(Void... voids) {
public static void insertReadPost(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor,
String username, String postId) {
executor.execute(() -> {
ReadPostDao readPostDao = redditDataRoomDatabase.readPostDao();
if (readPostDao.getReadPostsCount() > 500) {
readPostDao.deleteOldestReadPosts(username);
@ -30,7 +15,6 @@ public class InsertReadPost {
if (username != null && !username.equals("")) {
readPostDao.insert(new ReadPost(username, postId));
}
return null;
}
});
}
}