-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsActivity.java
70 lines (56 loc) · 2.57 KB
/
SettingsActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.zybooks.inventoryapp;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreferenceCompat;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
String username = AuthenticatedUserManager.getInstance().getUser().getUserName();
NotificationsManager notifyManager = NotificationsManager.getInstance();
notifyManager.setUsername(username);
// Save notifications preference in NotificationsManager so it persists when app closes.
SwitchPreferenceCompat notifyPref = findPreference("notifications");
notifyPref.setChecked(notifyManager.getNotificationPreference());
notifyPref.setOnPreferenceChangeListener((preference, newValue) -> {
if ((Boolean) newValue) {
notifyManager.saveNotificationPreference(true);
} else {
notifyManager.saveNotificationPreference(false);
}
return true;
});
// Preference for dark mode.
SwitchPreferenceCompat darkPref = findPreference("dark_theme");
if (darkPref != null) {
darkPref.setOnPreferenceChangeListener((preference, newValue) -> {
if ((Boolean) newValue) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
return true;
});
}
}
}
}