From ea27af4080ff6056dd260501cc91b8b2ac4f716f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 29 Mar 2020 10:46:55 +0800 Subject: Continue playing videos after orientation. Deleting all subreddits and users is now available in advanced settings. --- app/src/main/AndroidManifest.xml | 3 +- .../infinityforreddit/AppComponent.java | 3 ++ .../AsyncTask/DeleteAllSubredditsAsyncTask.java | 33 +++++++++++++ .../AsyncTask/DeleteAllUsersAsyncTask.java | 33 +++++++++++++ .../Settings/AdvancedPreferenceFragment.java | 56 ++++++++++++++++++++++ app/src/main/res/values/strings.xml | 4 +- 6 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllSubredditsAsyncTask.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllUsersAsyncTask.java (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1091ef63..c18183cc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -267,7 +267,8 @@ + android:theme="@style/AppTheme.ActionBar.Transparent" + android:configChanges="orientation|screenSize|layoutDirection" /> { + + private RedditDataRoomDatabase redditDataRoomDatabase; + private DeleteAllSubredditsAsyncTaskListener deleteAllSubredditsAsyncTaskListener; + + public interface DeleteAllSubredditsAsyncTaskListener { + void success(); + } + + public DeleteAllSubredditsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + DeleteAllSubredditsAsyncTaskListener deleteAllSubredditsAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.deleteAllSubredditsAsyncTaskListener = deleteAllSubredditsAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.subredditDao().deleteAllSubreddits(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllSubredditsAsyncTaskListener.success(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllUsersAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllUsersAsyncTask.java new file mode 100644 index 00000000..d73f749c --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/DeleteAllUsersAsyncTask.java @@ -0,0 +1,33 @@ +package ml.docilealligator.infinityforreddit.AsyncTask; + +import android.os.AsyncTask; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeleteAllUsersAsyncTask extends AsyncTask { + + private RedditDataRoomDatabase redditDataRoomDatabase; + private DeleteAllUsersAsyncTaskListener deleteAllUsersAsyncTaskListener; + + public interface DeleteAllUsersAsyncTaskListener { + void success(); + } + + public DeleteAllUsersAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + DeleteAllUsersAsyncTaskListener deleteAllUsersAsyncTaskListener) { + this.redditDataRoomDatabase = redditDataRoomDatabase; + this.deleteAllUsersAsyncTaskListener = deleteAllUsersAsyncTaskListener; + } + + @Override + protected Void doInBackground(Void... voids) { + redditDataRoomDatabase.userDao().deleteAllUsers(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + deleteAllUsersAsyncTaskListener.success(); + } +} 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 7af72c72..5101e60c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/AdvancedPreferenceFragment.java @@ -1,12 +1,25 @@ package ml.docilealligator.infinityforreddit.Settings; +import android.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; import android.os.Bundle; +import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; +import javax.inject.Inject; +import javax.inject.Named; + +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllSubredditsAsyncTask; +import ml.docilealligator.infinityforreddit.AsyncTask.DeleteAllUsersAsyncTask; +import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; /** @@ -14,13 +27,56 @@ import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; */ public class AdvancedPreferenceFragment extends PreferenceFragmentCompat { + @Inject + RedditDataRoomDatabase mRedditDataRoomDatabase; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + private Activity activity; + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.xml.advanced_preferences, rootKey); + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + Preference deleteSubredditsPreference = findPreference(SharedPreferencesUtils.DELETE_ALL_SUBREDDITS_DATA_IN_DATABASE); 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; + } + }); + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (AppCompatActivity) context; } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 45de4c40..a05fb242 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -643,7 +643,7 @@ Color Picker Invalid Color - - Hello blank fragment + Delete all subreddits successful + Delete all users successful -- cgit v1.2.3