Finished adding advanced preferences.

This commit is contained in:
Alex Ning 2020-03-29 16:28:50 +08:00
parent ca599fede5
commit 84d4679aa9
12 changed files with 319 additions and 29 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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);
Preference deleteAllThemesPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_THEMES_IN_DATABASE);
Preference resetAllSettingsPreference = findPreference(SharedPreferencesUtils.RESET_ALL_SETTINGS);
deleteSubredditsPreference.setOnPreferenceClickListener(preference -> {
new DeleteAllSubredditsAsyncTask(mRedditDataRoomDatabase,
() -> Toast.makeText(activity, R.string.delete_all_subreddits_success, Toast.LENGTH_SHORT).show()).execute();
return true;
});
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;
});
}
deleteUsersPreference.setOnPreferenceClickListener(preference -> {
new DeleteAllUsersAsyncTask(mRedditDataRoomDatabase,
() -> Toast.makeText(activity, R.string.delete_all_users_success, Toast.LENGTH_SHORT).show()).execute();
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;
});
}
deleteSortTypePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
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;
});
}
return false;
}
});
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;
});
}
deletePostLaoutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
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;
});
}
return false;
}
});
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

View File

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

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M11,7h2v2h-2zM11,11h2v6h-2zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"
android:fillColor="#4DFFFFFF"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M11,7h2v2h-2zM11,11h2v6h-2zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"
android:fillColor="#42000000"/>
</vector>

View File

@ -365,12 +365,15 @@
<string name="settings_customize_dark_theme_title">Dark Theme</string>
<string name="settings_customize_amoled_theme_title">Amoled Theme</string>
<string name="settings_manage_themes_title">Manage Themes</string>
<string name="settings_custom_theme_cannot_apply_to_settings_page_title">Custom themes cannot be applied to settings page (except toolbar, status bar and navigation bar).</string>
<string name="settings_custom_theme_cannot_apply_to_settings_page_summary">Custom themes cannot be applied to settings page (except toolbar, status bar and navigation bar).</string>
<string name="settings_advanced_master_title">Advanced</string>
<string name="settings_delete_all_subreddits_data_in_database_title">Delete All Subreddits in Database</string>
<string name="settings_delete_all_users_data_in_database_title">Delete All Users in Database</string>
<string name="settings_delete_all_sort_type_data_in_database_title">Delete All Sort Type in Database</string>
<string name="settings_delete_all_post_layout_data_in_database_title">Delete All Post Layout Data in Database</string>
<string name="settings_delete_all_sort_type_data_in_database_title">Delete All Sort Types 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_reset_all_settings_title">Reset All Settings</string>
<string name="settings_advanced_settings_summary">Clean the database and shared preferences</string>
<string name="no_link_available">Cannot get the link</string>
@ -645,5 +648,10 @@
<string name="delete_all_subreddits_success">Delete all subreddits successful</string>
<string name="delete_all_users_success">Delete all users successful</string>
<string name="delete_all_sort_types_success">Delete all sort types successful</string>
<string name="delete_all_post_layouts_success">Delete all post layouts successful</string>
<string name="delete_all_themes_success">Delete all themes successful</string>
<string name="reset_all_settings_success">Reset all settings successful</string>
</resources>

View File

@ -18,4 +18,17 @@
app:key="delete_all_post_layout_data_in_database"
app:title="@string/settings_delete_all_post_layout_data_in_database_title" />
<Preference
app:key="delete_all_themes_in_database"
app:title="@string/settings_delete_all_themes_in_database_title" />
<Preference
app:key="reset_all_settings"
app:title="@string/settings_reset_all_settings_title" />
<Preference
android:icon="@drawable/ic_info_preference_24dp"
app:summary="@string/settings_custom_theme_cannot_apply_to_settings_page_summary"
app:enabled="false" />
</PreferenceScreen>

View File

@ -42,7 +42,8 @@
app:title="@string/settings_manage_themes_title" />
<Preference
app:summary="@string/settings_custom_theme_cannot_apply_to_settings_page_title"
android:icon="@drawable/ic_info_preference_24dp"
app:summary="@string/settings_custom_theme_cannot_apply_to_settings_page_summary"
app:enabled="false" />
</PreferenceScreen>