aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-01-06 15:55:42 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-01-06 15:55:42 +0000
commit6ad34783c51f47c26d6f1e559d86c28edf65c3a8 (patch)
tree66e0170f86b49d2b6265cccbe356415fa0e848ca
parent07390678e0830aafa3a64bfaa15a64d101f197a8 (diff)
downloadinfinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar.gz
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar.bz2
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar.lz
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar.xz
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.tar.zst
infinity-for-reddit-6ad34783c51f47c26d6f1e559d86c28edf65c3a8.zip
Check for duplicate when saving a post filter. Fix an issue in PostFilter Parcelable implementation.
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java39
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java52
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java74
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java19
-rw-r--r--app/src/main/res/values/strings.xml2
6 files changed, 117 insertions, 74 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
index b5bbb03a..fabc37d2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
@@ -22,6 +22,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.checkbox.MaterialCheckBox;
+import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
@@ -50,6 +51,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
public static final String EXTRA_FROM_SETTINGS = "EFS";
public static final String RETURN_EXTRA_POST_FILTER = "REPF";
private static final String POST_FILTER_STATE = "PFS";
+ private static final String ORIGINAL_NAME_STATE = "ONS";
private static final int ADD_SUBREDDITS_REQUEST_CODE = 1;
private static final int ADD_USERS_REQUEST_CODE = 2;
@@ -176,6 +178,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
Executor mExecutor;
private PostFilter postFilter;
private boolean fromSettings;
+ private String originalName;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -249,10 +252,14 @@ public class CustomizePostFilterActivity extends BaseActivity {
if (savedInstanceState != null) {
postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE);
+ originalName = savedInstanceState.getString(ORIGINAL_NAME_STATE);
} else {
postFilter = getIntent().getParcelableExtra(EXTRA_POST_FILTER);
if (postFilter == null) {
postFilter = new PostFilter();
+ originalName = "";
+ } else {
+ originalName = postFilter.name;
}
bindView();
}
@@ -376,13 +383,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
constructPostFilter();
if (!postFilter.name.equals("")) {
- Handler handler = new Handler();
- SavePostFilter.savePostFilter(mRedditDataRoomDatabase, mExecutor, postFilter, () -> handler.post(() -> {
- Intent returnIntent = new Intent();
- returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter);
- setResult(Activity.RESULT_OK, returnIntent);
- finish();
- }));
+ savePostFilter(originalName);
} else {
Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show();
}
@@ -390,6 +391,29 @@ public class CustomizePostFilterActivity extends BaseActivity {
return false;
}
+ private void savePostFilter(String originalName) {
+ SavePostFilter.savePostFilter(mExecutor, new Handler(), mRedditDataRoomDatabase, postFilter, originalName,
+ new SavePostFilter.SavePostFilterListener() {
+ @Override
+ public void success() {
+ Intent returnIntent = new Intent();
+ returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter);
+ setResult(Activity.RESULT_OK, returnIntent);
+ finish();
+ }
+
+ @Override
+ public void duplicate() {
+ new MaterialAlertDialogBuilder(CustomizePostFilterActivity.this, R.style.MaterialAlertDialogTheme)
+ .setTitle(getString(R.string.duplicate_post_filter_dialog_title, postFilter.name))
+ .setMessage(R.string.duplicate_post_filter_dialog_message)
+ .setPositiveButton(R.string.override, (dialogInterface, i) -> savePostFilter(postFilter.name))
+ .setNegativeButton(R.string.cancel, null)
+ .show();
+ }
+ });
+ }
+
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
@@ -449,5 +473,6 @@ public class CustomizePostFilterActivity extends BaseActivity {
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(POST_FILTER_STATE, postFilter);
+ outState.putString(ORIGINAL_NAME_STATE, originalName);
}
} \ No newline at end of file
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java
index a0b41cc2..6041d52f 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java
@@ -188,34 +188,34 @@ public class CustomizeThemeActivity extends BaseActivity {
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- finish();
- return true;
- case R.id.action_preview_customize_theme_activity:
- Intent intent = new Intent(this, CustomThemePreviewActivity.class);
- intent.putParcelableArrayListExtra(CustomThemePreviewActivity.EXTRA_CUSTOM_THEME_SETTINGS_ITEMS, customThemeSettingsItems);
- startActivity(intent);
-
- return true;
- case R.id.action_save_customize_theme_activity:
- if (adapter != null) {
- themeName = adapter.getThemeName();
- if (themeName.equals("")) {
- Snackbar.make(coordinatorLayout, R.string.no_theme_name, Snackbar.LENGTH_SHORT).show();
- return true;
- }
- CustomTheme customTheme = CustomTheme.convertSettingsItemsToCustomTheme(customThemeSettingsItems, themeName);
- new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences,
- darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme,
- false, () -> {
- Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
- EventBus.getDefault().post(new RecreateActivityEvent());
- finish();
- }).execute();
+ int itemId = item.getItemId();
+ if (itemId == android.R.id.home) {
+ finish();
+ return true;
+ } else if (itemId == R.id.action_preview_customize_theme_activity) {
+ Intent intent = new Intent(this, CustomThemePreviewActivity.class);
+ intent.putParcelableArrayListExtra(CustomThemePreviewActivity.EXTRA_CUSTOM_THEME_SETTINGS_ITEMS, customThemeSettingsItems);
+ startActivity(intent);
+
+ return true;
+ } else if (itemId == R.id.action_save_customize_theme_activity) {
+ if (adapter != null) {
+ themeName = adapter.getThemeName();
+ if (themeName.equals("")) {
+ Snackbar.make(coordinatorLayout, R.string.no_theme_name, Snackbar.LENGTH_SHORT).show();
+ return true;
}
+ CustomTheme customTheme = CustomTheme.convertSettingsItemsToCustomTheme(customThemeSettingsItems, themeName);
+ new InsertCustomThemeAsyncTask(redditDataRoomDatabase, lightThemeSharedPreferences,
+ darkThemeSharedPreferences, amoledThemeSharedPreferences, customTheme,
+ false, () -> {
+ Toast.makeText(CustomizeThemeActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
+ EventBus.getDefault().post(new RecreateActivityEvent());
+ finish();
+ }).execute();
+ }
- return true;
+ return true;
}
return false;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java
index 5eaf70bc..3bc28b0e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java
@@ -68,6 +68,43 @@ public class PostFilter implements Parcelable {
}
+ protected PostFilter(Parcel in) {
+ name = in.readString();
+ maxVote = in.readInt();
+ minVote = in.readInt();
+ maxComments = in.readInt();
+ minComments = in.readInt();
+ maxAwards = in.readInt();
+ minAwards = in.readInt();
+ allowNSFW = in.readByte() != 0;
+ onlyNSFW = in.readByte() != 0;
+ onlySpoiler = in.readByte() != 0;
+ postTitleExcludesRegex = in.readString();
+ postTitleExcludesStrings = in.readString();
+ excludeSubreddits = in.readString();
+ excludeUsers = in.readString();
+ containFlairs = in.readString();
+ excludeFlairs = in.readString();
+ containTextType = in.readByte() != 0;
+ containLinkType = in.readByte() != 0;
+ containImageType = in.readByte() != 0;
+ containGifType = in.readByte() != 0;
+ containVideoType = in.readByte() != 0;
+ containGalleryType = in.readByte() != 0;
+ }
+
+ public static final Creator<PostFilter> CREATOR = new Creator<PostFilter>() {
+ @Override
+ public PostFilter createFromParcel(Parcel in) {
+ return new PostFilter(in);
+ }
+
+ @Override
+ public PostFilter[] newArray(int size) {
+ return new PostFilter[size];
+ }
+ };
+
public static boolean isPostAllowed(Post post, PostFilter postFilter) {
if (postFilter == null || post == null) {
return true;
@@ -222,42 +259,6 @@ public class PostFilter implements Parcelable {
return postFilter;
}
- protected PostFilter(Parcel in) {
- name = in.readString();
- maxVote = in.readInt();
- minVote = in.readInt();
- maxComments = in.readInt();
- minComments = in.readInt();
- maxAwards = in.readInt();
- minAwards = in.readInt();
- allowNSFW = in.readByte() != 0;
- onlyNSFW = in.readByte() != 0;
- onlySpoiler = in.readByte() != 0;
- postTitleExcludesRegex = in.readString();
- postTitleExcludesStrings = in.readString();
- excludeSubreddits = in.readString();
- excludeUsers = in.readString();
- containFlairs = in.readString();
- excludeFlairs = in.readString();
- containTextType = in.readByte() != 0;
- containLinkType = in.readByte() != 0;
- containImageType = in.readByte() != 0;
- containVideoType = in.readByte() != 0;
- containGalleryType = in.readByte() != 0;
- }
-
- public static final Creator<PostFilter> CREATOR = new Creator<PostFilter>() {
- @Override
- public PostFilter createFromParcel(Parcel in) {
- return new PostFilter(in);
- }
-
- @Override
- public PostFilter[] newArray(int size) {
- return new PostFilter[size];
- }
- };
-
@Override
public int describeContents() {
return 0;
@@ -284,6 +285,7 @@ public class PostFilter implements Parcelable {
parcel.writeByte((byte) (containTextType ? 1 : 0));
parcel.writeByte((byte) (containLinkType ? 1 : 0));
parcel.writeByte((byte) (containImageType ? 1 : 0));
+ parcel.writeByte((byte) (containGifType ? 1 : 0));
parcel.writeByte((byte) (containVideoType ? 1 : 0));
parcel.writeByte((byte) (containGalleryType ? 1 : 0));
}
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 05a27120..55b65bab 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java
@@ -20,10 +20,13 @@ public interface PostFilterDao {
@Delete
void deletePostFilter(PostFilter postFilter);
+ @Query("DELETE FROM post_filter WHERE name = :name")
+ void deletePostFilter(String name);
+
@Query("SELECT * FROM post_filter WHERE name = :name LIMIT 1")
PostFilter getPostFilter(String name);
- @Query("SELECT * FROM post_filter")
+ @Query("SELECT * FROM post_filter ORDER BY name")
LiveData<List<PostFilter>> getAllPostFiltersLiveData();
@Query("SELECT * FROM post_filter WHERE post_filter.name IN " +
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java
index f7362925..aa5a47c3 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java
@@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.postfilter;
+import android.os.Handler;
+
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@@ -8,13 +10,22 @@ public class SavePostFilter {
public interface SavePostFilterListener {
//Need to make sure it is running in the UI thread.
void success();
+ void duplicate();
}
- public static void savePostFilter(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor,
- PostFilter postFilter, SavePostFilterListener savePostFilterListener) {
+ public static void savePostFilter(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase,
+ PostFilter postFilter, String originalName, SavePostFilterListener savePostFilterListener) {
executor.execute(() -> {
- redditDataRoomDatabase.postFilterDao().insert(postFilter);
- savePostFilterListener.success();
+ if (!originalName.equals(postFilter.name) &&
+ redditDataRoomDatabase.postFilterDao().getPostFilter(postFilter.name) != null) {
+ handler.post(savePostFilterListener::duplicate);
+ } else {
+ if (!originalName.equals(postFilter.name)) {
+ redditDataRoomDatabase.postFilterDao().deletePostFilter(originalName);
+ }
+ redditDataRoomDatabase.postFilterDao().insert(postFilter);
+ handler.post(savePostFilterListener::success);
+ }
});
}
}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 957b5a48..aebcb892 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -986,6 +986,8 @@
<string name="max_awards_hint">Max awards (-1: no restriction)</string>
<string name="post_filter_name_hint">Post Filter Name</string>
<string name="post_filter_requires_a_name">What is the name of this post filter?</string>
+ <string name="duplicate_post_filter_dialog_title">\'%1$s\' Already Exists</string>
+ <string name="duplicate_post_filter_dialog_message">Override it?</string>
<string name="apply_post_filter_to">Apply to</string>
<string name="post_filter_usage_home">Home</string>
<string name="post_filter_usage_subreddit">Subreddit: %1$s</string>