aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java19
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java15
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsage.java16
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java (renamed from app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java)14
5 files changed, 50 insertions, 19 deletions
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<List<PostFilterWithUsage>>() {
+ @Override
+ public void onChanged(List<PostFilterWithUsage> 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<RecyclerView.ViewHolder> {
@@ -25,7 +26,7 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
private BaseActivity activity;
private CustomThemeWrapper customThemeWrapper;
private final OnItemClickListener onItemClickListener;
- private List<PostFilter> postFilterList;
+ private List<PostFilterWithUsage> postFilterWithUsageList;
public interface OnItemClickListener {
void onItemClick(PostFilter postFilter);
@@ -59,17 +60,17 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof PostFilterViewHolder) {
- ((PostFilterViewHolder) holder).binding.postFilterNameTextViewItemPostFilter.setText(postFilterList.get(position - 1).name);
+ ((PostFilterViewHolder) holder).binding.postFilterNameTextViewItemPostFilter.setText(postFilterWithUsageList.get(position - 1).postFilter.name);
}
}
@Override
public int getItemCount() {
- return postFilterList == null ? 1 : 1 + postFilterList.size();
+ return postFilterWithUsageList == null ? 1 : 1 + postFilterWithUsageList.size();
}
- public void setPostFilterList(List<PostFilter> postFilterList) {
- this.postFilterList = postFilterList;
+ public void setPostFilterWithUsageList(List<PostFilterWithUsage> postFilterWithUsageList) {
+ this.postFilterWithUsageList = postFilterWithUsageList;
notifyDataSetChanged();
}
@@ -85,8 +86,8 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
binding.postFilterNameTextViewItemPostFilter.setTypeface(activity.typeface);
}
- itemView.setOnClickListener(view -> {
- 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<PostFilter> getValidPostFilters(int usage, String nameOfUsage);
+
+ @Transaction
+ @Query("SELECT * FROM post_filter ORDER BY name")
+ public LiveData<List<PostFilterWithUsage>> getAllPostFilterWithUsageLiveData();
}
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<PostFilterUsage> postFilterUsages;
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java
index 48acd7f7..792eda40 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterWithUsageViewModel.java
@@ -8,15 +8,15 @@ import java.util.List;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
-public class PostFilterViewModel extends ViewModel {
- private LiveData<List<PostFilter>> mPostFilterListLiveData;
+public class PostFilterWithUsageViewModel extends ViewModel {
+ private LiveData<List<PostFilterWithUsage>> mPostFilterWithUsageListLiveData;
- public PostFilterViewModel(RedditDataRoomDatabase redditDataRoomDatabase) {
- mPostFilterListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFiltersLiveData();
+ public PostFilterWithUsageViewModel(RedditDataRoomDatabase redditDataRoomDatabase) {
+ mPostFilterWithUsageListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFilterWithUsageLiveData();
}
- public LiveData<List<PostFilter>> getPostFilterListLiveData() {
- return mPostFilterListLiveData;
+ public LiveData<List<PostFilterWithUsage>> getPostFilterWithUsageListLiveData() {
+ return mPostFilterWithUsageListLiveData;
}
public static class Factory extends ViewModelProvider.NewInstanceFactory {
@@ -30,7 +30,7 @@ public class PostFilterViewModel extends ViewModel {
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
//noinspection unchecked
- return (T) new PostFilterViewModel(mRedditDataRoomDatabase);
+ return (T) new PostFilterWithUsageViewModel(mRedditDataRoomDatabase);
}
}
}