Check for duplicate when saving a post filter. Fix an issue in PostFilter Parcelable implementation.

This commit is contained in:
Alex Ning 2021-01-06 23:55:42 +08:00
parent 07390678e0
commit 6ad34783c5
6 changed files with 116 additions and 73 deletions

View File

@ -22,6 +22,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.checkbox.MaterialCheckBox; 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.switchmaterial.SwitchMaterial;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout; 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 EXTRA_FROM_SETTINGS = "EFS";
public static final String RETURN_EXTRA_POST_FILTER = "REPF"; public static final String RETURN_EXTRA_POST_FILTER = "REPF";
private static final String POST_FILTER_STATE = "PFS"; 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_SUBREDDITS_REQUEST_CODE = 1;
private static final int ADD_USERS_REQUEST_CODE = 2; private static final int ADD_USERS_REQUEST_CODE = 2;
@ -176,6 +178,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
Executor mExecutor; Executor mExecutor;
private PostFilter postFilter; private PostFilter postFilter;
private boolean fromSettings; private boolean fromSettings;
private String originalName;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -249,10 +252,14 @@ public class CustomizePostFilterActivity extends BaseActivity {
if (savedInstanceState != null) { if (savedInstanceState != null) {
postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE); postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE);
originalName = savedInstanceState.getString(ORIGINAL_NAME_STATE);
} else { } else {
postFilter = getIntent().getParcelableExtra(EXTRA_POST_FILTER); postFilter = getIntent().getParcelableExtra(EXTRA_POST_FILTER);
if (postFilter == null) { if (postFilter == null) {
postFilter = new PostFilter(); postFilter = new PostFilter();
originalName = "";
} else {
originalName = postFilter.name;
} }
bindView(); bindView();
} }
@ -376,13 +383,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
constructPostFilter(); constructPostFilter();
if (!postFilter.name.equals("")) { if (!postFilter.name.equals("")) {
Handler handler = new Handler(); savePostFilter(originalName);
SavePostFilter.savePostFilter(mRedditDataRoomDatabase, mExecutor, postFilter, () -> handler.post(() -> {
Intent returnIntent = new Intent();
returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}));
} else { } else {
Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show(); 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; 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 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
@ -449,5 +473,6 @@ public class CustomizePostFilterActivity extends BaseActivity {
protected void onSaveInstanceState(@NonNull Bundle outState) { protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putParcelable(POST_FILTER_STATE, postFilter); outState.putParcelable(POST_FILTER_STATE, postFilter);
outState.putString(ORIGINAL_NAME_STATE, originalName);
} }
} }

View File

@ -188,34 +188,34 @@ public class CustomizeThemeActivity extends BaseActivity {
@Override @Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) { public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { int itemId = item.getItemId();
case android.R.id.home: if (itemId == android.R.id.home) {
finish(); finish();
return true; return true;
case R.id.action_preview_customize_theme_activity: } else if (itemId == R.id.action_preview_customize_theme_activity) {
Intent intent = new Intent(this, CustomThemePreviewActivity.class); Intent intent = new Intent(this, CustomThemePreviewActivity.class);
intent.putParcelableArrayListExtra(CustomThemePreviewActivity.EXTRA_CUSTOM_THEME_SETTINGS_ITEMS, customThemeSettingsItems); intent.putParcelableArrayListExtra(CustomThemePreviewActivity.EXTRA_CUSTOM_THEME_SETTINGS_ITEMS, customThemeSettingsItems);
startActivity(intent); startActivity(intent);
return true; return true;
case R.id.action_save_customize_theme_activity: } else if (itemId == R.id.action_save_customize_theme_activity) {
if (adapter != null) { if (adapter != null) {
themeName = adapter.getThemeName(); themeName = adapter.getThemeName();
if (themeName.equals("")) { if (themeName.equals("")) {
Snackbar.make(coordinatorLayout, R.string.no_theme_name, Snackbar.LENGTH_SHORT).show(); Snackbar.make(coordinatorLayout, R.string.no_theme_name, Snackbar.LENGTH_SHORT).show();
return true; 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();
} }
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; return false;

View File

@ -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) { public static boolean isPostAllowed(Post post, PostFilter postFilter) {
if (postFilter == null || post == null) { if (postFilter == null || post == null) {
return true; return true;
@ -222,42 +259,6 @@ public class PostFilter implements Parcelable {
return postFilter; 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 @Override
public int describeContents() { public int describeContents() {
return 0; return 0;
@ -284,6 +285,7 @@ public class PostFilter implements Parcelable {
parcel.writeByte((byte) (containTextType ? 1 : 0)); parcel.writeByte((byte) (containTextType ? 1 : 0));
parcel.writeByte((byte) (containLinkType ? 1 : 0)); parcel.writeByte((byte) (containLinkType ? 1 : 0));
parcel.writeByte((byte) (containImageType ? 1 : 0)); parcel.writeByte((byte) (containImageType ? 1 : 0));
parcel.writeByte((byte) (containGifType ? 1 : 0));
parcel.writeByte((byte) (containVideoType ? 1 : 0)); parcel.writeByte((byte) (containVideoType ? 1 : 0));
parcel.writeByte((byte) (containGalleryType ? 1 : 0)); parcel.writeByte((byte) (containGalleryType ? 1 : 0));
} }

View File

@ -20,10 +20,13 @@ public interface PostFilterDao {
@Delete @Delete
void deletePostFilter(PostFilter postFilter); 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") @Query("SELECT * FROM post_filter WHERE name = :name LIMIT 1")
PostFilter getPostFilter(String name); PostFilter getPostFilter(String name);
@Query("SELECT * FROM post_filter") @Query("SELECT * FROM post_filter ORDER BY name")
LiveData<List<PostFilter>> getAllPostFiltersLiveData(); LiveData<List<PostFilter>> getAllPostFiltersLiveData();
@Query("SELECT * FROM post_filter WHERE post_filter.name IN " + @Query("SELECT * FROM post_filter WHERE post_filter.name IN " +

View File

@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.postfilter; package ml.docilealligator.infinityforreddit.postfilter;
import android.os.Handler;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
@ -8,13 +10,22 @@ public class SavePostFilter {
public interface SavePostFilterListener { public interface SavePostFilterListener {
//Need to make sure it is running in the UI thread. //Need to make sure it is running in the UI thread.
void success(); void success();
void duplicate();
} }
public static void savePostFilter(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, public static void savePostFilter(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase,
PostFilter postFilter, SavePostFilterListener savePostFilterListener) { PostFilter postFilter, String originalName, SavePostFilterListener savePostFilterListener) {
executor.execute(() -> { executor.execute(() -> {
redditDataRoomDatabase.postFilterDao().insert(postFilter); if (!originalName.equals(postFilter.name) &&
savePostFilterListener.success(); 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);
}
}); });
} }
} }

View File

@ -986,6 +986,8 @@
<string name="max_awards_hint">Max awards (-1: no restriction)</string> <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_name_hint">Post Filter Name</string>
<string name="post_filter_requires_a_name">What is the name of this post filter?</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="apply_post_filter_to">Apply to</string>
<string name="post_filter_usage_home">Home</string> <string name="post_filter_usage_home">Home</string>
<string name="post_filter_usage_subreddit">Subreddit: %1$s</string> <string name="post_filter_usage_subreddit">Subreddit: %1$s</string>