diff options
Diffstat (limited to 'app/src/main/java/ml')
7 files changed, 281 insertions, 31 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllPostLayoutsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllPostLayoutsAsyncTask.java new file mode 100644 index 00000000..fb81d1e6 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllPostLayoutsAsyncTask.java @@ -0,0 +1,57 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.content.SharedPreferences; +import android.os.AsyncTask; + +import java.util.Map; + +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class DeleteAllPostLayoutsAsyncTask extends AsyncTask<Void, Void, Void> { + + private SharedPreferences defaultSharedPreferences; + private SharedPreferences postLayoutSharedPreferences; + private DeleteAllPostLayoutsAsyncTaskListener deleteAllPostLayoutsAsyncTaskListener; + + public interface DeleteAllPostLayoutsAsyncTaskListener { + void success(); + } + + public DeleteAllPostLayoutsAsyncTask(SharedPreferences defaultSharedPreferences, + SharedPreferences postLayoutSharedPreferences, + DeleteAllPostLayoutsAsyncTaskListener deleteAllPostLayoutsAsyncTaskListener) { + this.defaultSharedPreferences = defaultSharedPreferences; + this.postLayoutSharedPreferences = postLayoutSharedPreferences; + this.deleteAllPostLayoutsAsyncTaskListener = deleteAllPostLayoutsAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + Map<String,?> keys = defaultSharedPreferences.getAll(); + SharedPreferences.Editor editor = defaultSharedPreferences.edit(); + + for(Map.Entry<String,?> entry : keys.entrySet()){ + String key = entry.getKey(); + if (key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SHARED_PREFERENCES_FILE) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_ALL_POST) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SUBREDDIT_POST_BASE) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_MULTI_REDDIT_POST_BASE) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE) + || key.startsWith(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST)) { + editor.remove(key); + } + } + editor.apply(); + + postLayoutSharedPreferences.edit().clear().apply(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllPostLayoutsAsyncTaskListener.success(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllSortTypesAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllSortTypesAsyncTask.java new file mode 100644 index 00000000..153e0899 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllSortTypesAsyncTask.java @@ -0,0 +1,58 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.content.SharedPreferences; +import android.os.AsyncTask; + +import java.util.Map; + +import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; + +public class DeleteAllSortTypesAsyncTask extends AsyncTask<Void, Void, Void> { + private SharedPreferences defaultSharedPreferences; + private SharedPreferences sortTypeSharedPreferences; + private DeleteAllSortTypesAsyncTaskListener deleteAllSortTypesAsyncTaskListener; + + public interface DeleteAllSortTypesAsyncTaskListener { + void success(); + } + + public DeleteAllSortTypesAsyncTask(SharedPreferences defaultSharedPreferences, + SharedPreferences sortTypeSharedPreferences, + DeleteAllSortTypesAsyncTaskListener deleteAllSortTypesAsyncTaskListener) { + this.defaultSharedPreferences = defaultSharedPreferences; + this.sortTypeSharedPreferences = sortTypeSharedPreferences; + this.deleteAllSortTypesAsyncTaskListener = deleteAllSortTypesAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + Map<String,?> keys = defaultSharedPreferences.getAll(); + SharedPreferences.Editor editor = defaultSharedPreferences.edit(); + + for(Map.Entry<String,?> entry : keys.entrySet()){ + String key = entry.getKey(); + if (key.startsWith(SharedPreferencesUtils.SORT_TYPE_BEST_POST) || key.startsWith(SharedPreferencesUtils.SORT_TIME_BEST_POST) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_ALL_POST) || key.startsWith(SharedPreferencesUtils.SORT_TIME_ALL_POST) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_POPULAR_POST) || key.startsWith(SharedPreferencesUtils.SORT_TIME_POPULAR_POST) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST) || key.startsWith(SharedPreferencesUtils.SORT_TIME_SEARCH_POST) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_SUBREDDIT_POST_BASE) || key.startsWith(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_MULTI_REDDIT_POST_BASE) || key.startsWith(SharedPreferencesUtils.SORT_TIME_MULTI_REDDIT_POST_BASE) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_USER_POST_BASE) || key.startsWith(SharedPreferencesUtils.SORT_TIME_USER_POST_BASE) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_USER_COMMENT) || key.startsWith(SharedPreferencesUtils.SORT_TIME_USER_COMMENT) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_SEARCH_SUBREDDIT) || key.startsWith(SharedPreferencesUtils.SORT_TYPE_SEARCH_USER) + || key.startsWith(SharedPreferencesUtils.SORT_TYPE_POST_COMMENT)) { + editor.remove(key); + } + } + editor.apply(); + + sortTypeSharedPreferences.edit().clear().apply(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllSortTypesAsyncTaskListener.success(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllThemesAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllThemesAsyncTask.java new file mode 100644 index 00000000..0859fefd --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllThemesAsyncTask.java @@ -0,0 +1,45 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.content.SharedPreferences; +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteAllThemesAsyncTask extends AsyncTask<Void, Void, Void> { + private RedditDataRoomDatabase redditDataRoomDatabase; + private SharedPreferences lightThemeSharedPreferences; + private SharedPreferences darkThemeSharedPreferences; + private SharedPreferences amoledThemeSharedPreferences; + private DeleteAllThemesAsyncTaskListener deleteAllThemesAsyncTaskListener; + + public interface DeleteAllThemesAsyncTaskListener { + void success(); + } + + public DeleteAllThemesAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + SharedPreferences lightThemeSharedPreferences, + SharedPreferences darkThemeSharedPreferences, + SharedPreferences amoledThemeSharedPreferences, + DeleteAllThemesAsyncTaskListener deleteAllThemesAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.lightThemeSharedPreferences = lightThemeSharedPreferences; + this.darkThemeSharedPreferences = darkThemeSharedPreferences; + this.amoledThemeSharedPreferences = amoledThemeSharedPreferences; + this.deleteAllThemesAsyncTaskListener = deleteAllThemesAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.customThemeDao().deleteAllCustomThemes(); + lightThemeSharedPreferences.edit().clear().apply(); + darkThemeSharedPreferences.edit().clear().apply(); + amoledThemeSharedPreferences.edit().clear().apply(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllThemesAsyncTaskListener.success(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java index f6443841..86a0f43b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertCustomThemeAsyncTask.java @@ -49,19 +49,19 @@ public class InsertCustomThemeAsyncTask extends AsyncTask<Void, Void, Void> { if (customTheme.isLightTheme) { redditDataRoomDatabase.customThemeDao().unsetLightTheme(); CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, lightThemeSharedPreferences); - } else if (previousTheme.isLightTheme) { + } else if (previousTheme != null && previousTheme.isLightTheme) { lightThemeSharedPreferences.edit().clear().apply(); } if (customTheme.isDarkTheme) { redditDataRoomDatabase.customThemeDao().unsetDarkTheme(); CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, darkThemeSharedPreferences); - } else if (previousTheme.isDarkTheme) { + } else if (previousTheme != null && previousTheme.isDarkTheme) { darkThemeSharedPreferences.edit().clear().apply(); } if (customTheme.isAmoledTheme) { redditDataRoomDatabase.customThemeDao().unsetAmoledTheme(); CustomThemeSharedPreferencesUtils.insertThemeToSharedPreferences(customTheme, amoledThemeSharedPreferences); - } else if (previousTheme.isAmoledTheme) { + } else if (previousTheme != null && previousTheme.isAmoledTheme) { amoledThemeSharedPreferences.edit().clear().apply(); } redditDataRoomDatabase.customThemeDao().insert(customTheme); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java index 8741f8e5..68b4de13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CustomTheme/CustomThemeDao.java @@ -51,4 +51,7 @@ public interface CustomThemeDao { @Query("UPDATE custom_themes SET name = :newName WHERE name = :oldName") void updateName(String oldName, String newName); + + @Query("DELETE FROM custom_themes") + void deleteAllCustomThemes(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java index 5101e60c..de92c7fe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java @@ -12,11 +12,19 @@ import androidx.fragment.app.Fragment; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import org.greenrobot.eventbus.EventBus; + import javax.inject.Inject; import javax.inject.Named; +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllPostLayoutsAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllSortTypesAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllSubredditsAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllThemesAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllUsersAsyncTask; +import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; @@ -32,6 +40,21 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { @Inject @Named("default") SharedPreferences mSharedPreferences; + @Inject + @Named("sort_type") + SharedPreferences mSortTypeSharedPreferences; + @Inject + @Named("post_layout") + SharedPreferences mPostLayoutSharedPreferences; + @Inject + @Named("light_theme") + SharedPreferences lightThemeSharedPreferences; + @Inject + @Named("dark_theme") + SharedPreferences darkThemeSharedPreferences; + @Inject + @Named("amoled_theme") + SharedPreferences amoledThemeSharedPreferences; private Activity activity; @Override @@ -44,34 +67,96 @@ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { Preference deleteUsersPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_USERS_DATA_IN_DATABASE); Preference deleteSortTypePreference = findPreference(SharedPreferencesUtils.DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE); Preference deletePostLaoutPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_POST_LAYOUT_DATA_IN_DATABASE); - - deleteSubredditsPreference.setOnPreferenceClickListener(preference -> { - new DeleteAllSubredditsAsyncTask(mRedditDataRoomDatabase, - () -> Toast.makeText(activity, R.string.delete_all_subreddits_success, Toast.LENGTH_SHORT).show()).execute(); - return true; - }); - - deleteUsersPreference.setOnPreferenceClickListener(preference -> { - new DeleteAllUsersAsyncTask(mRedditDataRoomDatabase, - () -> Toast.makeText(activity, R.string.delete_all_users_success, Toast.LENGTH_SHORT).show()).execute(); - return true; - }); - - deleteSortTypePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - - return false; - } - }); - - deletePostLaoutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - - return false; - } - }); + Preference deleteAllThemesPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_THEMES_IN_DATABASE); + Preference resetAllSettingsPreference = findPreference(SharedPreferencesUtils.RESET_ALL_SETTINGS); + + if (deleteSubredditsPreference != null) { + deleteSubredditsPreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> new DeleteAllSubredditsAsyncTask(mRedditDataRoomDatabase, + () -> Toast.makeText(activity, R.string.delete_all_subreddits_success, Toast.LENGTH_SHORT).show()).execute()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } + + if (deleteUsersPreference != null) { + deleteUsersPreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> new DeleteAllUsersAsyncTask(mRedditDataRoomDatabase, + () -> Toast.makeText(activity, R.string.delete_all_users_success, Toast.LENGTH_SHORT).show()).execute()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } + + if (deleteSortTypePreference != null) { + deleteSortTypePreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> new DeleteAllSortTypesAsyncTask(mSharedPreferences, mSortTypeSharedPreferences, () -> { + Toast.makeText(activity, R.string.delete_all_sort_types_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + }).execute()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } + + if (deletePostLaoutPreference != null) { + deletePostLaoutPreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> new DeleteAllPostLayoutsAsyncTask(mSharedPreferences, mPostLayoutSharedPreferences, () -> { + Toast.makeText(activity, R.string.delete_all_post_layouts_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + }).execute()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } + + if (deleteAllThemesPreference != null) { + deleteAllThemesPreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> new DeleteAllThemesAsyncTask(mRedditDataRoomDatabase, lightThemeSharedPreferences, + darkThemeSharedPreferences, amoledThemeSharedPreferences, () -> { + Toast.makeText(activity, R.string.delete_all_themes_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + }).execute()) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } + + if (resetAllSettingsPreference != null) { + resetAllSettingsPreference.setOnPreferenceClickListener(preference -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.are_you_sure) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> { + mSharedPreferences.edit().clear().apply(); + Toast.makeText(activity, R.string.reset_all_settings_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new RecreateActivityEvent()); + }) + .setNegativeButton(R.string.no, null) + .show(); + return true; + }); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index e3d3d699..906bcdd5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -90,4 +90,6 @@ public class SharedPreferencesUtils { public static final String DELETE_ALL_USERS_DATA_IN_DATABASE= "delete_all_users_data_in_database"; public static final String DELETE_ALL_SORT_TYPE_DATA_IN_DATABASE= "delete_all_sort_type_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 RESET_ALL_SETTINGS = "reset_all_settings"; } |