From 93fcd33c48b34a1d84c17122c756845873cea9ea Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Mon, 30 Oct 2017 10:41:59 +0100 Subject: [PATCH] add actionbar to PreferencesActivity --- .../tusky/PreferencesActivity.java | 73 +++++++++++++++-- .../tusky/fragment/PreferencesFragment.java | 79 +++++++++++++++---- .../main/res/layout/activity_preferences.xml | 26 ++++++ .../main/res/xml/notification_preferences.xml | 58 ++++++++++++++ app/src/main/res/xml/preferences.xml | 75 ++---------------- .../res/xml/timeline_filter_preferences.xml | 16 ++++ 6 files changed, 236 insertions(+), 91 deletions(-) create mode 100644 app/src/main/res/layout/activity_preferences.xml create mode 100644 app/src/main/res/xml/notification_preferences.xml create mode 100644 app/src/main/res/xml/timeline_filter_preferences.xml diff --git a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java index fe6de818c..80704422d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java @@ -20,12 +20,20 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.Nullable; +import android.support.annotation.StringRes; +import android.support.annotation.XmlRes; +import android.support.v7.app.ActionBar; +import android.support.v7.widget.Toolbar; +import android.view.MenuItem; import com.keylesspalace.tusky.fragment.PreferencesFragment; public class PreferencesActivity extends BaseActivity implements SharedPreferences.OnSharedPreferenceChangeListener { + private boolean themeSwitched; + private @XmlRes int currentPreferences; + private @StringRes int currentTitle; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -41,17 +49,51 @@ public class PreferencesActivity extends BaseActivity if (preferences.getBoolean("lightTheme", false)) { setTheme(R.style.AppTheme_Light); } + + setContentView(R.layout.activity_preferences); + + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowHomeEnabled(true); + } + + preferences.registerOnSharedPreferenceChangeListener(this); + if(savedInstanceState == null) { + currentPreferences = R.xml.preferences; + currentTitle = R.string.action_view_preferences; + } else { + currentPreferences = savedInstanceState.getInt("preferences"); + currentTitle = savedInstanceState.getInt("title"); + } + showFragment(currentPreferences, currentTitle); + + } + + public void showFragment(@XmlRes int preferenceId, @StringRes int title) { + + //TODO: cache the Fragments so they can be reused getFragmentManager().beginTransaction() - .replace(android.R.id.content, new PreferencesFragment()) + .replace(R.id.fragment_container, PreferencesFragment.newInstance(preferenceId)) .commit(); + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setTitle(title); + } + currentPreferences = preferenceId; + currentTitle = title; } private void saveInstanceState(Bundle outState) { outState.putBoolean("themeSwitched", themeSwitched); + outState.putInt("preferences", currentPreferences); + outState.putInt("title", currentTitle); } @Override @@ -95,16 +137,33 @@ public class PreferencesActivity extends BaseActivity @Override public void onBackPressed() { + //if we are not on the top level, show the top level. Else exit the activity + if(currentPreferences != R.xml.preferences) { + showFragment(R.xml.preferences, R.string.action_view_preferences); + + } else { /* Switching themes won't actually change the theme of activities on the back stack. * Either the back stack activities need to all be recreated, or do the easier thing, which * is hijack the back button press and use it to launch a new MainActivity and clear the * back stack. */ - if (themeSwitched) { - Intent intent = new Intent(this, MainActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - startActivity(intent); - } else { - super.onBackPressed(); + if (themeSwitched) { + Intent intent = new Intent(this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + startActivity(intent); + } else { + super.onBackPressed(); + } } } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + onBackPressed(); + return true; + } + } + return super.onOptionsItemSelected(item); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/PreferencesFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/PreferencesFragment.java index 5d8560792..fccdf7ca1 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/PreferencesFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/PreferencesFragment.java @@ -20,38 +20,87 @@ import android.os.Build; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceFragment; -import android.preference.PreferenceScreen; +import android.support.annotation.XmlRes; import com.keylesspalace.tusky.BuildConfig; +import com.keylesspalace.tusky.PreferencesActivity; import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.util.NotificationMaker; public class PreferencesFragment extends PreferenceFragment { + + public static PreferencesFragment newInstance(@XmlRes int preference) { + PreferencesFragment fragment = new PreferencesFragment(); + + Bundle args = new Bundle(); + args.putInt("preference", preference); + + fragment.setArguments(args); + + return fragment; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.preferences); + + int preference = getArguments().getInt("preference"); + + addPreferencesFromResource(preference); - //on Android O and newer, launch the system notification settings instead of the app settings - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationMaker.createNotificationChannels(getContext()); - PreferenceScreen notificationPreferences = (PreferenceScreen) findPreference("notificationSettings"); - notificationPreferences.removeAll(); - notificationPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + Preference notificationPreferences = findPreference("notificationPreferences"); + + if(notificationPreferences != null) { + + //on Android O and newer, launch the system notification settings instead of the app settings + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + + NotificationMaker.createNotificationChannels(getContext()); + + notificationPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Intent intent = new Intent(); + intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); + + intent.putExtra("android.provider.extra.APP_PACKAGE", BuildConfig.APPLICATION_ID); + + startActivity(intent); + return true; + } + }); + + } else { + notificationPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + PreferencesActivity activity = (PreferencesActivity) getActivity(); + if (activity != null) { + activity.showFragment(R.xml.notification_preferences, R.string.pref_title_edit_notification_settings); + } + + return true; + } + }); + } + } + + Preference timelineFilterPreferences = findPreference("timelineFilterPreferences"); + if(timelineFilterPreferences != null) { + timelineFilterPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { - Intent intent = new Intent(); - intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); + PreferencesActivity activity = (PreferencesActivity) getActivity(); + if (activity != null) { + activity.showFragment(R.xml.timeline_filter_preferences, R.string.pref_title_status_tabs); + } - intent.putExtra("android.provider.extra.APP_PACKAGE", BuildConfig.APPLICATION_ID); - - startActivity(intent); return true; } }); - - } + } + } diff --git a/app/src/main/res/layout/activity_preferences.xml b/app/src/main/res/layout/activity_preferences.xml new file mode 100644 index 000000000..b113ad218 --- /dev/null +++ b/app/src/main/res/layout/activity_preferences.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/notification_preferences.xml b/app/src/main/res/xml/notification_preferences.xml new file mode 100644 index 000000000..1520e632e --- /dev/null +++ b/app/src/main/res/xml/notification_preferences.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index cc6d163f1..3ceba8759 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -33,20 +33,10 @@ - + - - - - - - @@ -59,62 +49,9 @@ android:summary="%s" android:title="@string/pref_title_pull_notification_check_interval" /> - + - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/xml/timeline_filter_preferences.xml b/app/src/main/res/xml/timeline_filter_preferences.xml new file mode 100644 index 000000000..93a9db135 --- /dev/null +++ b/app/src/main/res/xml/timeline_filter_preferences.xml @@ -0,0 +1,16 @@ + + + + + + + + + \ No newline at end of file