From b09701918f625462c1d90b97710ab6ae2352eb0f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 2 Feb 2020 10:37:35 +0800 Subject: Confirm to exit. --- .../infinityforreddit/Activity/MainActivity.java | 20 +++++++++++++++++++- .../Event/ChangeConfirmToExitEvent.java | 9 +++++++++ .../Settings/MainPreferenceFragment.java | 12 ++++++++++++ .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/drawable-night/ic_exit_24dp.xml | 9 +++++++++ app/src/main/res/drawable/ic_exit_24dp.xml | 9 +++++++++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/interface_preference.xml | 5 +++++ app/src/main/res/xml/main_preferences.xml | 5 +++-- 9 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeConfirmToExitEvent.java create mode 100644 app/src/main/res/drawable-night/ic_exit_24dp.xml create mode 100644 app/src/main/res/drawable/ic_exit_24dp.xml (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index 70b38606..531098c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -47,6 +47,7 @@ import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.bottomappbar.BottomAppBar; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; @@ -70,6 +71,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.SwitchToAnonymousAccountAsyncTask; +import ml.docilealligator.infinityforreddit.Event.ChangeConfirmToExitEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Event.SwitchAccountEvent; @@ -209,6 +211,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private Menu mMenu; private boolean isInLazyMode = false; private boolean showBottomAppBar; + private boolean mConfirmToExit; @Override protected void onCreate(Bundle savedInstanceState) { @@ -309,6 +312,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb params = (AppBarLayout.LayoutParams) collapsingToolbarLayout.getLayoutParams(); showBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY, false); + mConfirmToExit = mSharedPreferences.getBoolean(SharedPreferencesUtils.CONFIRM_TO_EXIT, false); if (savedInstanceState != null) { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); @@ -870,7 +874,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { - super.onBackPressed(); + if (mConfirmToExit) { + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.exit_app) + .setPositiveButton(R.string.yes, (dialogInterface, i) + -> finish()) + .setNegativeButton(R.string.no, null) + .show(); + } else { + super.onBackPressed(); + } } } @@ -973,6 +986,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb recreate(); } + @Subscribe + public void onChangeConfirmToExitEvent(ChangeConfirmToExitEvent changeConfirmToExitEvent) { + mConfirmToExit = changeConfirmToExitEvent.confirmToExit; + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { private PostFragment frontPagePostFragment; private PostFragment popularPostFragment; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeConfirmToExitEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeConfirmToExitEvent.java new file mode 100644 index 00000000..9371c9ac --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeConfirmToExitEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeConfirmToExitEvent { + public boolean confirmToExit; + + public ChangeConfirmToExitEvent(boolean confirmToExit) { + this.confirmToExit = confirmToExit; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java index 1a94cc37..58bd8cf1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/MainPreferenceFragment.java @@ -11,6 +11,7 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.ListPreference; +import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; @@ -46,6 +47,7 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { ((Infinity) activity.getApplication()).getAppComponent().inject(this); SwitchPreference amoledDarkSwitch = findPreference(SharedPreferencesUtils.AMOLED_DARK_KEY); + SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY); SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY); SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY); @@ -61,6 +63,16 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { }); } + if (confirmToExitSwitch != null) { + confirmToExitSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + EventBus.getDefault().post(new RecreateActivityEvent()); + return true; + } + }); + } + if (nsfwSwitch != null) { nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> { EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue)); 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 5ae81003..0f7f4eca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -72,4 +72,5 @@ public class SharedPreferencesUtils { public static final String LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "lock_jump_to_next_top_level_comment_button"; public static final String SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "swipe_up_to_hide_jump_to_next_top_level_comments_button"; public static final String SHOW_TOP_LEVEL_COMMENTS_FIRST = "show_top_level_comments_first"; + public static final String CONFIRM_TO_EXIT = "confirm_to_exit"; } diff --git a/app/src/main/res/drawable-night/ic_exit_24dp.xml b/app/src/main/res/drawable-night/ic_exit_24dp.xml new file mode 100644 index 00000000..ce5bd595 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_exit_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_exit_24dp.xml b/app/src/main/res/drawable/ic_exit_24dp.xml new file mode 100644 index 00000000..5f4abdc7 --- /dev/null +++ b/app/src/main/res/drawable/ic_exit_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b1b6b636..6a287783 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -302,6 +302,7 @@ Use Volume Keys to Navigate Comments in Posts Use Volume Keys to Navigate Posts Mute Video + Confirm to Exit Show Top-level Comments First Show Elapsed Time in Posts and Comments Default Post Layout @@ -420,4 +421,6 @@ Save comment Unsave comment + + Exit? diff --git a/app/src/main/res/xml/interface_preference.xml b/app/src/main/res/xml/interface_preference.xml index 434a0173..626f1333 100644 --- a/app/src/main/res/xml/interface_preference.xml +++ b/app/src/main/res/xml/interface_preference.xml @@ -41,4 +41,9 @@ app:key="show_divider_in_compact_layout" app:title="@string/settings_show_divider_in_compact_layout" /> + + \ No newline at end of file diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 4f238327..89d0a032 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -44,8 +44,9 @@ + app:key="confirm_to_exit" + app:icon="@drawable/ic_exit_24dp" + app:title="@string/settings_confirm_to_exit" />