Manage pref

This commit is contained in:
tom79 2019-11-12 19:36:18 +01:00
parent 01753c904b
commit c964b268e1
5 changed files with 97 additions and 62 deletions

View File

@ -12,6 +12,7 @@ import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.jaredrummler.cyanea.Cyanea;
@ -35,13 +36,90 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat implements
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.fragment_settings_color);
Preference button = findPreference("reset_pref");
createPref();
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SettingsActivity.needRestart = true;
if (key.equals("use_custom_theme")) {
createPref();
}
if( key.compareTo("pref_theme_picker") == 0){
String theme = sharedPreferences.getString("pref_theme_picker", null);
List<CyaneaTheme> list = CyaneaTheme.Companion.from(Objects.requireNonNull(getActivity()).getAssets(), "themes/cyanea_themes.json");
if( getActivity() != null && theme != null) {
SharedPreferences sharedpreferences = getActivity().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int i = 0;
if( theme.compareTo("2") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_LIGHT);
}else if( theme.compareTo("1") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_DARK);
i = 1;
}else if( theme.compareTo("3") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_BLACK);
i = 2;
}
editor.commit();
list.get(i).apply(Cyanea.getInstance()).recreate(getActivity());
}
}
}
private void createPref(){
getPreferenceScreen().removeAll();
addPreferencesFromResource(R.xml.fragment_settings_color);
PreferenceScreen preferenceScreen = getPreferenceScreen();
FragmentActivity context = getActivity();
assert context != null;
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
ListPreference pref_theme_picker = (ListPreference) findPreference("pref_theme_picker");
Preference theme_link_color = findPreference("theme_link_color");
Preference theme_boost_header_color = findPreference("theme_boost_header_color");
Preference theme_statuses_color = findPreference("theme_statuses_color");
Preference theme_icons_color = findPreference("theme_icons_color");
Preference theme_text_color = findPreference("theme_text_color");
Preference theme_primary = findPreference("theme_primary");
Preference theme_accent = findPreference("theme_accent");
Preference pref_color_navigation_bar = findPreference("pref_color_navigation_bar");
Preference pref_color_background = findPreference("pref_color_background");
Preference reset_pref = findPreference("reset_pref");
if( !sharedpreferences.getBoolean("use_custom_theme", false)){
preferenceScreen.removePreference(theme_link_color);
preferenceScreen.removePreference(theme_boost_header_color);
preferenceScreen.removePreference(theme_statuses_color);
preferenceScreen.removePreference(theme_icons_color);
preferenceScreen.removePreference(theme_text_color);
preferenceScreen.removePreference(theme_primary);
preferenceScreen.removePreference(theme_accent);
preferenceScreen.removePreference(pref_color_navigation_bar);
preferenceScreen.removePreference(pref_color_background);
preferenceScreen.removePreference(reset_pref);
final ListPreference listPreference = (ListPreference) findPreference("pref_theme_picker");
}
List<String> array = Arrays.asList(getResources().getStringArray(R.array.settings_theme));
CharSequence[] entries = array.toArray(new CharSequence[array.size()]);
CharSequence[] entryValues = new CharSequence[3];
@ -50,11 +128,12 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat implements
entryValues[0] = String.valueOf(Helper.THEME_LIGHT);
entryValues[1] = String.valueOf(Helper.THEME_DARK);
entryValues[2] = String.valueOf(Helper.THEME_BLACK);
listPreference.setEntries(entries);
listPreference.setEntryValues(entryValues);
listPreference.setDefaultValue(String.valueOf(theme));
pref_theme_picker.setEntries(entries);
pref_theme_picker.setEntryValues(entryValues);
pref_theme_picker.setDefaultValue(String.valueOf(theme));
button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
reset_pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
@ -67,7 +146,6 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat implements
editor.remove("theme_statuses_color");
editor.remove("theme_link_color");
editor.remove("theme_icons_color");
editor.remove("theme_background_color");
editor.remove("pref_color_background");
editor.remove("pref_color_navigation_bar");
editor.remove("theme_accent");
@ -93,46 +171,4 @@ public class ColorSettingsFragment extends PreferenceFragmentCompat implements
}
});
}
@Override
public void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
SettingsActivity.needRestart = true;
if( key.compareTo("pref_theme_picker") == 0){
String theme = sharedPreferences.getString("pref_theme_picker", null);
List<CyaneaTheme> list = CyaneaTheme.Companion.from(Objects.requireNonNull(getActivity()).getAssets(), "themes/cyanea_themes.json");
if( getActivity() != null && theme != null) {
SharedPreferences sharedpreferences = getActivity().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
int i = 0;
if( theme.compareTo("2") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_LIGHT);
}else if( theme.compareTo("1") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_DARK);
i = 1;
}else if( theme.compareTo("3") == 0 ) {
editor.putInt(Helper.SET_THEME, Helper.THEME_BLACK);
i = 2;
}
editor.commit();
list.get(i).apply(Cyanea.getInstance()).recreate(getActivity());
}
}
}
}

View File

@ -160,12 +160,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}
}
});
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
main_timeline_container = rootView.findViewById(R.id.main_timeline_container);
int mainBackgroundColor = prefs.getInt("theme_background_color", -1);
if( main_timeline_container != null && mainBackgroundColor != -1 ){
main_timeline_container.setBackgroundColor(mainBackgroundColor);
}
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {

View File

@ -162,13 +162,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
}
if (ischannel)
type = RetrieveFeedsAsyncTask.Type.CHANNEL;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
RelativeLayout main_timeline_container = rootView.findViewById(R.id.main_timeline_container);
int mainBackgroundColor = prefs.getInt("theme_background_color", -1);
if( main_timeline_container != null && mainBackgroundColor != -1 ){
main_timeline_container.setBackgroundColor(mainBackgroundColor);
}
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
//instanceType should not be null only for Peertube accounts

View File

@ -1273,5 +1273,7 @@
<string name="apply_changes">Apply changes</string>
<string name="restart_message">You need to restart the application to apply changes</string>
<string name="restart">Restart</string>
<string name="pref_custom_theme">Use a custom theme</string>
<string name="pref_custom_theme_summary">Allow to override colors of the selected theme above</string>
</resources>

View File

@ -11,6 +11,15 @@
android:summary="@string/pref_theme_summary"
android:title="@string/pref_theme_title"/>
<androidx.preference.SwitchPreferenceCompat
app:iconSpaceReserved="false"
android:defaultValue="false"
android:key="use_custom_theme"
android:summary="@string/pref_custom_theme_summary"
android:title="@string/pref_custom_theme"
/>
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:iconSpaceReserved="false"
android:defaultValue="-1"