add actionbar to PreferencesActivity

This commit is contained in:
Conny Duck 2017-10-30 10:41:59 +01:00
parent 15e37576e5
commit 93fcd33c48
6 changed files with 236 additions and 91 deletions

View file

@ -20,12 +20,20 @@ import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.Nullable; 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; import com.keylesspalace.tusky.fragment.PreferencesFragment;
public class PreferencesActivity extends BaseActivity public class PreferencesActivity extends BaseActivity
implements SharedPreferences.OnSharedPreferenceChangeListener { implements SharedPreferences.OnSharedPreferenceChangeListener {
private boolean themeSwitched; private boolean themeSwitched;
private @XmlRes int currentPreferences;
private @StringRes int currentTitle;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -41,17 +49,51 @@ public class PreferencesActivity extends BaseActivity
if (preferences.getBoolean("lightTheme", false)) { if (preferences.getBoolean("lightTheme", false)) {
setTheme(R.style.AppTheme_Light); 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); 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() getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PreferencesFragment()) .replace(R.id.fragment_container, PreferencesFragment.newInstance(preferenceId))
.commit(); .commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(title);
}
currentPreferences = preferenceId;
currentTitle = title;
} }
private void saveInstanceState(Bundle outState) { private void saveInstanceState(Bundle outState) {
outState.putBoolean("themeSwitched", themeSwitched); outState.putBoolean("themeSwitched", themeSwitched);
outState.putInt("preferences", currentPreferences);
outState.putInt("title", currentTitle);
} }
@Override @Override
@ -95,6 +137,11 @@ public class PreferencesActivity extends BaseActivity
@Override @Override
public void onBackPressed() { 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. /* 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 * 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 * is hijack the back button press and use it to launch a new MainActivity and clear the
@ -107,4 +154,16 @@ public class PreferencesActivity extends BaseActivity
super.onBackPressed(); super.onBackPressed();
} }
} }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}
} }

View file

@ -20,24 +20,44 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen; import android.support.annotation.XmlRes;
import com.keylesspalace.tusky.BuildConfig; import com.keylesspalace.tusky.BuildConfig;
import com.keylesspalace.tusky.PreferencesActivity;
import com.keylesspalace.tusky.R; import com.keylesspalace.tusky.R;
import com.keylesspalace.tusky.util.NotificationMaker; import com.keylesspalace.tusky.util.NotificationMaker;
public class PreferencesFragment extends PreferenceFragment { 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 @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
int preference = getArguments().getInt("preference");
addPreferencesFromResource(preference);
Preference notificationPreferences = findPreference("notificationPreferences");
if(notificationPreferences != null) {
//on Android O and newer, launch the system notification settings instead of the app settings //on Android O and newer, launch the system notification settings instead of the app settings
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationMaker.createNotificationChannels(getContext()); NotificationMaker.createNotificationChannels(getContext());
PreferenceScreen notificationPreferences = (PreferenceScreen) findPreference("notificationSettings");
notificationPreferences.removeAll();
notificationPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { notificationPreferences.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
@ -51,7 +71,36 @@ public class PreferencesFragment extends PreferenceFragment {
} }
}); });
} 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) {
PreferencesActivity activity = (PreferencesActivity) getActivity();
if (activity != null) {
activity.showFragment(R.xml.timeline_filter_preferences, R.string.pref_title_status_tabs);
}
return true;
}
});
}
} }
}
} }

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_view_thread"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.keylesspalace.tusky.PreferencesActivity">
<include layout="@layout/toolbar_basic" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<include layout="@layout/toolbar_shadow_shim" />
<FrameLayout
android:id="@+id/overlay_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>

View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="notificationSettings"
android:title="@string/pref_title_edit_notification_settings"
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationsEnabled"
android:title="@string/pref_title_notifications_enabled" />
<PreferenceCategory
android:dependency="notificationsEnabled"
android:title="@string/pref_title_notification_filters">
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterMentions"
android:title="@string/pref_title_notification_filter_mentions" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterFollows"
android:title="@string/pref_title_notification_filter_follows" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterReblogs"
android:title="@string/pref_title_notification_filter_reblogs" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterFavourites"
android:title="@string/pref_title_notification_filter_favourites" />
</PreferenceCategory>
<PreferenceCategory
android:dependency="notificationsEnabled"
android:title="@string/pref_title_notification_alerts">
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertSound"
android:title="@string/pref_title_notification_alert_sound" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertVibrate"
android:title="@string/pref_title_notification_alert_vibrate" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertLight"
android:title="@string/pref_title_notification_alert_light" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -33,20 +33,10 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_title_status_filter"> <PreferenceCategory android:title="@string/pref_title_status_filter">
<PreferenceScreen android:title="@string/pref_title_status_tabs"> <Preference
android:key="timelineFilterPreferences"
android:title="@string/pref_title_status_tabs" />
<PreferenceCategory android:title="@string/title_home">
<CheckBoxPreference
android:defaultValue="true"
android:key="tabFilterHomeBoosts"
android:title="@string/pref_title_show_boosts" />
<CheckBoxPreference
android:defaultValue="true"
android:key="tabFilterHomeReplies"
android:title="@string/pref_title_show_replies" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_title_notification_settings"> <PreferenceCategory android:title="@string/pref_title_notification_settings">
@ -59,62 +49,9 @@
android:summary="%s" android:summary="%s"
android:title="@string/pref_title_pull_notification_check_interval" /> android:title="@string/pref_title_pull_notification_check_interval" />
<PreferenceScreen <Preference
android:key="notificationSettings" android:key="notificationPreferences"
android:title="@string/pref_title_edit_notification_settings"> android:title="@string/pref_title_edit_notification_settings" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationsEnabled"
android:title="@string/pref_title_notifications_enabled" />
<PreferenceCategory
android:dependency="notificationsEnabled"
android:title="@string/pref_title_notification_filters">
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterMentions"
android:title="@string/pref_title_notification_filter_mentions" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterFollows"
android:title="@string/pref_title_notification_filter_follows" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterReblogs"
android:title="@string/pref_title_notification_filter_reblogs" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationFilterFavourites"
android:title="@string/pref_title_notification_filter_favourites" />
</PreferenceCategory>
<PreferenceCategory
android:dependency="notificationsEnabled"
android:title="@string/pref_title_notification_alerts">
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertSound"
android:title="@string/pref_title_notification_alert_sound" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertVibrate"
android:title="@string/pref_title_notification_alert_vibrate" />
<CheckBoxPreference
android:defaultValue="true"
android:key="notificationAlertLight"
android:title="@string/pref_title_notification_alert_light" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:title="@string/pref_title_status_tabs"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/title_home">
<CheckBoxPreference
android:defaultValue="true"
android:key="tabFilterHomeBoosts"
android:title="@string/pref_title_show_boosts" />
<CheckBoxPreference
android:defaultValue="true"
android:key="tabFilterHomeReplies"
android:title="@string/pref_title_show_replies" />
</PreferenceCategory>
</PreferenceScreen>