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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
package ml.docilealligator.infinityforreddit.activities;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import javax.inject.Inject;
import javax.inject.Named;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.databinding.ActivitySettingsBinding;
import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent;
import ml.docilealligator.infinityforreddit.settings.AboutPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.FontPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.GesturesAndButtonsPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.InterfacePreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.MainPreferenceFragment;
import ml.docilealligator.infinityforreddit.settings.PostPreferenceFragment;
public class SettingsActivity extends BaseActivity implements
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
private static final String TITLE_STATE = "TS";
private ActivitySettingsBinding binding;
@Inject
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Override
protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this);
setImmersiveModeNotApplicable();
super.onCreate(savedInstanceState);
binding = ActivitySettingsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
EventBus.getDefault().register(this);
applyCustomTheme();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) {
addOnOffsetChangedListener(binding.appbarLayoutSettingsActivity);
}
setSupportActionBar(binding.toolbarSettingsActivity);
if (savedInstanceState == null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_layout_settings_activity, new MainPreferenceFragment())
.commit();
} else {
setTitle(savedInstanceState.getCharSequence(TITLE_STATE));
}
getSupportFragmentManager().addOnBackStackChangedListener(() -> {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
setTitle(R.string.settings_activity_label);
return;
}
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity);
if (fragment instanceof AboutPreferenceFragment) {
setTitle(R.string.settings_about_master_title);
} else if (fragment instanceof InterfacePreferenceFragment) {
setTitle(R.string.settings_interface_title);
} else if (fragment instanceof FontPreferenceFragment) {
setTitle(R.string.settings_font_title);
} else if (fragment instanceof GesturesAndButtonsPreferenceFragment) {
setTitle(R.string.settings_gestures_and_buttons_title);
} else if (fragment instanceof PostPreferenceFragment) {
setTitle(R.string.settings_category_post_title);
} else if (fragment instanceof AdvancedPreferenceFragment) {
setTitle(R.string.settings_advanced_master_title);
}
});
}
@Override
public SharedPreferences getDefaultSharedPreferences() {
return mSharedPreferences;
}
@Override
public SharedPreferences getCurrentAccountSharedPreferences() {
return mCurrentAccountSharedPreferences;
}
@Override
public CustomThemeWrapper getCustomThemeWrapper() {
return mCustomThemeWrapper;
}
@Override
protected void applyCustomTheme() {
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(binding.appbarLayoutSettingsActivity,
binding.collapsingToolbarLayoutSettingsActivity, binding.toolbarSettingsActivity);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence(TITLE_STATE, getTitle());
}
@Override
public boolean onSupportNavigateUp() {
if (getSupportFragmentManager().popBackStackImmediate()) {
return true;
}
return super.onSupportNavigateUp();
}
@Override
public boolean onPreferenceStartFragment(@NonNull PreferenceFragmentCompat caller, Preference pref) {
// Instantiate the new Fragment
final Bundle args = pref.getExtras();
final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
getClassLoader(),
pref.getFragment());
fragment.setArguments(args);
fragment.setTargetFragment(caller, 0);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right)
.replace(R.id.frame_layout_settings_activity, fragment)
.addToBackStack(null)
.commit();
setTitle(pref.getTitle());
return true;
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void showSnackbar(int stringId, int actionStringId, View.OnClickListener onClickListener) {
Snackbar.make(binding.getRoot(), stringId, BaseTransientBottomBar.LENGTH_SHORT).setAction(actionStringId, onClickListener).show();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) {
ActivityCompat.recreate(this);
}
}
|