From 06092c73d43bb9ddcf9b272776d9ef9e7caafbeb Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sat, 14 Oct 2023 22:51:46 -0400 Subject: Prepare to show post filter usages in PostFilterPreferenceActivity. --- .../activities/PostFilterPreferenceActivity.java | 19 +++++++++--- .../adapters/PostFilterRecyclerViewAdapter.java | 15 ++++----- .../postfilter/PostFilterDao.java | 5 +++ .../postfilter/PostFilterViewModel.java | 36 ---------------------- .../postfilter/PostFilterWithUsage.java | 16 ++++++++++ .../postfilter/PostFilterWithUsageViewModel.java | 36 ++++++++++++++++++++++ 6 files changed, 79 insertions(+), 48 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsage.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java index a4a63b1b..a8e49ae0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java @@ -3,11 +3,13 @@ package ml.docilealligator.infinityforreddit.activities; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; @@ -16,6 +18,7 @@ import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.List; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -32,7 +35,8 @@ import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.postfilter.DeletePostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; -import ml.docilealligator.infinityforreddit.postfilter.PostFilterViewModel; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterWithUsageViewModel; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterWithUsage; public class PostFilterPreferenceActivity extends BaseActivity { @@ -61,7 +65,7 @@ public class PostFilterPreferenceActivity extends BaseActivity { CustomThemeWrapper customThemeWrapper; @Inject Executor executor; - public PostFilterViewModel postFilterViewModel; + public PostFilterWithUsageViewModel postFilterWithUsageViewModel; private PostFilterRecyclerViewAdapter adapter; @Override @@ -116,10 +120,15 @@ public class PostFilterPreferenceActivity extends BaseActivity { recyclerView.setAdapter(adapter); - postFilterViewModel = new ViewModelProvider(this, - new PostFilterViewModel.Factory(redditDataRoomDatabase)).get(PostFilterViewModel.class); + postFilterWithUsageViewModel = new ViewModelProvider(this, + new PostFilterWithUsageViewModel.Factory(redditDataRoomDatabase)).get(PostFilterWithUsageViewModel.class); - postFilterViewModel.getPostFilterListLiveData().observe(this, postFilters -> adapter.setPostFilterList(postFilters)); + postFilterWithUsageViewModel.getPostFilterWithUsageListLiveData().observe(this, new Observer>() { + @Override + public void onChanged(List postFilterWithUsages) { + adapter.setPostFilterWithUsageList(postFilterWithUsages); + } + }); } public void showPostFilterOptions(Post post, PostFilter postFilter) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java index 56559ed1..f15c269c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java @@ -15,6 +15,7 @@ import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.databinding.ItemPostFilterBinding; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterWithUsage; import ml.docilealligator.infinityforreddit.utils.Utils; public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter { @@ -25,7 +26,7 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter postFilterList; + private List postFilterWithUsageList; public interface OnItemClickListener { void onItemClick(PostFilter postFilter); @@ -59,17 +60,17 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter postFilterList) { - this.postFilterList = postFilterList; + public void setPostFilterWithUsageList(List postFilterWithUsageList) { + this.postFilterWithUsageList = postFilterWithUsageList; notifyDataSetChanged(); } @@ -85,8 +86,8 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter { - onItemClickListener.onItemClick(postFilterList.get(getBindingAdapterPosition() - 1)); + binding.getRoot().setOnClickListener(view -> { + onItemClickListener.onItemClick(postFilterWithUsageList.get(getBindingAdapterPosition() - 1).postFilter); }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java index ec53d185..37a511b1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java @@ -6,6 +6,7 @@ import androidx.room.Delete; import androidx.room.Insert; import androidx.room.OnConflictStrategy; import androidx.room.Query; +import androidx.room.Transaction; import java.util.List; @@ -39,4 +40,8 @@ public interface PostFilterDao { "(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 getValidPostFilters(int usage, String nameOfUsage); + + @Transaction + @Query("SELECT * FROM post_filter ORDER BY name") + public LiveData> getAllPostFilterWithUsageLiveData(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java deleted file mode 100644 index 48acd7f7..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java +++ /dev/null @@ -1,36 +0,0 @@ -package ml.docilealligator.infinityforreddit.postfilter; - -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelProvider; - -import java.util.List; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class PostFilterViewModel extends ViewModel { - private LiveData> mPostFilterListLiveData; - - public PostFilterViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { - mPostFilterListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFiltersLiveData(); - } - - public LiveData> getPostFilterListLiveData() { - return mPostFilterListLiveData; - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - - private final RedditDataRoomDatabase mRedditDataRoomDatabase; - - public Factory(RedditDataRoomDatabase redditDataRoomDatabase) { - mRedditDataRoomDatabase = redditDataRoomDatabase; - } - - @Override - public T create(Class modelClass) { - //noinspection unchecked - return (T) new PostFilterViewModel(mRedditDataRoomDatabase); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsage.java new file mode 100644 index 00000000..c305a63f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsage.java @@ -0,0 +1,16 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import androidx.room.Embedded; +import androidx.room.Relation; + +import java.util.List; + +public class PostFilterWithUsage { + @Embedded + public PostFilter postFilter; + @Relation( + parentColumn = "name", + entityColumn = "name" + ) + public List postFilterUsages; +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java new file mode 100644 index 00000000..792eda40 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java @@ -0,0 +1,36 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class PostFilterWithUsageViewModel extends ViewModel { + private LiveData> mPostFilterWithUsageListLiveData; + + public PostFilterWithUsageViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { + mPostFilterWithUsageListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFilterWithUsageLiveData(); + } + + public LiveData> getPostFilterWithUsageListLiveData() { + return mPostFilterWithUsageListLiveData; + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + + public Factory(RedditDataRoomDatabase redditDataRoomDatabase) { + mRedditDataRoomDatabase = redditDataRoomDatabase; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new PostFilterWithUsageViewModel(mRedditDataRoomDatabase); + } + } +} -- cgit v1.2.3