Add an option to delete all read posts.

This commit is contained in:
Alex Ning 2020-12-09 15:15:17 +08:00
parent fa5008ea5e
commit c3b2ffa653
6 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class DeleteAllReadPostsAsyncTask extends AsyncTask<Void, Void, Void> {
public interface DeleteAllReadPostsAsyncTaskListener {
void success();
}
private RedditDataRoomDatabase redditDataRoomDatabase;
private DeleteAllReadPostsAsyncTaskListener deleteAllReadPostsAsyncTaskListener;
public DeleteAllReadPostsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
DeleteAllReadPostsAsyncTaskListener deleteAllReadPostsAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.deleteAllReadPostsAsyncTaskListener = deleteAllReadPostsAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
redditDataRoomDatabase.readPostDao().deleteAllReadPosts();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
deleteAllReadPostsAsyncTaskListener.success();
}
}

View File

@ -20,4 +20,7 @@ public interface ReadPostDao {
@Query("DELETE FROM read_posts WHERE rowid IN (SELECT rowid FROM read_posts LIMIT 100) AND username = :username") @Query("DELETE FROM read_posts WHERE rowid IN (SELECT rowid FROM read_posts LIMIT 100) AND username = :username")
void deleteOldestReadPosts(String username); void deleteOldestReadPosts(String username);
@Query("DELETE FROM read_posts")
void deleteAllReadPosts();
} }

View File

@ -20,6 +20,7 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllPostLayoutsAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllPostLayoutsAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllReadPostsAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllSortTypesAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllSortTypesAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllSubredditsAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllSubredditsAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllThemesAsyncTask; import ml.docilealligator.infinityforreddit.asynctasks.DeleteAllThemesAsyncTask;
@ -78,6 +79,7 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat {
Preference deletePostLaoutPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE); Preference deletePostLaoutPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE);
Preference deleteAllThemesPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_THEMES_IN_DATABASE); Preference deleteAllThemesPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_THEMES_IN_DATABASE);
Preference deletePostFeedScrolledPositionsPreference = findPreference(SharedPreferencesUtils.DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE); Preference deletePostFeedScrolledPositionsPreference = findPreference(SharedPreferencesUtils.DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE);
Preference deleteReadPostsPreference = findPreference(SharedPreferencesUtils.DELETE_READ_POSTS_IN_DATABASE);
Preference deleteAllLegacySettingsPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_LEGACY_SETTINGS); Preference deleteAllLegacySettingsPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_LEGACY_SETTINGS);
Preference resetAllSettingsPreference = findPreference(SharedPreferencesUtils.RESET_ALL_SETTINGS); Preference resetAllSettingsPreference = findPreference(SharedPreferencesUtils.RESET_ALL_SETTINGS);
@ -168,6 +170,20 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat {
}); });
} }
if (deleteReadPostsPreference != null) {
deleteReadPostsPreference.setOnPreferenceClickListener(preference -> {
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.are_you_sure)
.setPositiveButton(R.string.yes, (dialogInterface, i)
-> new DeleteAllReadPostsAsyncTask(mRedditDataRoomDatabase, () -> {
Toast.makeText(activity, R.string.delete_all_read_posts_success, Toast.LENGTH_SHORT).show();
}).execute())
.setNegativeButton(R.string.no, null)
.show();
return true;
});
}
if (deleteAllLegacySettingsPreference != null) { if (deleteAllLegacySettingsPreference != null) {
deleteAllLegacySettingsPreference.setOnPreferenceClickListener(preference -> { deleteAllLegacySettingsPreference.setOnPreferenceClickListener(preference -> {
new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme)

View File

@ -110,6 +110,7 @@ public class SharedPreferencesUtils {
public static final String DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE = "delete_all_post_layout_data_in_database"; public static final String DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE = "delete_all_post_layout_data_in_database";
public static final String DELETE_ALL_THEMES_IN_DATABASE = "delete_all_themes_in_database"; public static final String DELETE_ALL_THEMES_IN_DATABASE = "delete_all_themes_in_database";
public static final String DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE = "delete_front_page_scrolled_positions_in_database"; public static final String DELETE_FRONT_PAGE_SCROLLED_POSITIONS_IN_DATABASE = "delete_front_page_scrolled_positions_in_database";
public static final String DELETE_READ_POSTS_IN_DATABASE = "delete_read_posts_in_database";
public static final String DELETE_ALL_LEGACY_SETTINGS = "delete_all_legacy_settings"; public static final String DELETE_ALL_LEGACY_SETTINGS = "delete_all_legacy_settings";
public static final String RESET_ALL_SETTINGS = "reset_all_settings"; public static final String RESET_ALL_SETTINGS = "reset_all_settings";
public static final String IMAGE_DOWNLOAD_LOCATION = "image_download_location"; public static final String IMAGE_DOWNLOAD_LOCATION = "image_download_location";

View File

@ -446,6 +446,7 @@
<string name="settings_delete_all_post_layout_data_in_database_title">Delete All Post Layouts in Database</string> <string name="settings_delete_all_post_layout_data_in_database_title">Delete All Post Layouts in Database</string>
<string name="settings_delete_all_themes_in_database_title">Delete All Themes in Database</string> <string name="settings_delete_all_themes_in_database_title">Delete All Themes in Database</string>
<string name="settings_delete_front_page_scrolled_positions_in_database_title">Delete All Front Page Scrolled Positions in Database</string> <string name="settings_delete_front_page_scrolled_positions_in_database_title">Delete All Front Page Scrolled Positions in Database</string>
<string name="settings_delete_read_posts_in_database_title">Delete All Read Posts in Database</string>
<string name="settings_delete_all_legacy_settings_title">Delete All Legacy Settings</string> <string name="settings_delete_all_legacy_settings_title">Delete All Legacy Settings</string>
<string name="settings_reset_all_settings_title">Reset All Settings</string> <string name="settings_reset_all_settings_title">Reset All Settings</string>
<string name="settings_advanced_settings_summary">Clean the database and shared preferences</string> <string name="settings_advanced_settings_summary">Clean the database and shared preferences</string>
@ -820,6 +821,7 @@
<string name="delete_all_post_layouts_success">Delete all post layouts successfully</string> <string name="delete_all_post_layouts_success">Delete all post layouts successfully</string>
<string name="delete_all_themes_success">Delete all themes successfully</string> <string name="delete_all_themes_success">Delete all themes successfully</string>
<string name="delete_all_front_page_scrolled_positions_success">Delete all scrolled positions in front page successfully</string> <string name="delete_all_front_page_scrolled_positions_success">Delete all scrolled positions in front page successfully</string>
<string name="delete_all_read_posts_success">Delete all read posts successfully</string>
<string name="delete_all_legacy_settings_success">Delete all legacy settings successfully</string> <string name="delete_all_legacy_settings_success">Delete all legacy settings successfully</string>
<string name="reset_all_settings_success">Reset all settings successfully</string> <string name="reset_all_settings_success">Reset all settings successfully</string>

View File

@ -26,6 +26,10 @@
app:key="delete_front_page_scrolled_positions_in_database" app:key="delete_front_page_scrolled_positions_in_database"
app:title="@string/settings_delete_front_page_scrolled_positions_in_database_title" /> app:title="@string/settings_delete_front_page_scrolled_positions_in_database_title" />
<Preference
app:key="delete_read_posts_in_database"
app:title="@string/settings_delete_read_posts_in_database_title" />
<Preference <Preference
app:key="delete_all_legacy_settings" app:key="delete_all_legacy_settings"
app:title="@string/settings_delete_all_legacy_settings_title" /> app:title="@string/settings_delete_all_legacy_settings_title" />