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.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);
}
}

View File

@ -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);
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;
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();
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;

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) {
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));
}

View File

@ -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 " +

View File

@ -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);
}
});
}
}

View File

@ -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>