[app] Save dark mode setting with string

This commit is contained in:
tehcneko 2021-02-15 21:06:53 +08:00
parent 3cbacc8644
commit 1a4458ef72
6 changed files with 36 additions and 97 deletions

View File

@ -37,6 +37,7 @@ import java.io.StringWriter;
import io.github.lsposed.manager.repo.RepoLoader;
import io.github.lsposed.manager.ui.activity.CrashReportActivity;
import io.github.lsposed.manager.util.DoHDNS;
import io.github.lsposed.manager.util.theme.ThemeUtil;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import rikka.material.app.DayNightDelegate;
@ -93,7 +94,7 @@ public class App extends Application {
pref = PreferenceManager.getDefaultSharedPreferences(this);
DayNightDelegate.setApplicationContext(this);
DayNightDelegate.setDefaultNightMode(pref.getInt("theme", -1));
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
RepoLoader.getInstance().loadRemoteData();
}

View File

@ -58,8 +58,8 @@ import io.github.lsposed.manager.R;
import io.github.lsposed.manager.databinding.ActivitySettingsBinding;
import io.github.lsposed.manager.ui.activity.base.BaseActivity;
import io.github.lsposed.manager.ui.fragment.StatusDialogBuilder;
import io.github.lsposed.manager.ui.widget.IntegerListPreference;
import io.github.lsposed.manager.util.BackupUtils;
import io.github.lsposed.manager.util.theme.ThemeUtil;
import rikka.core.util.ResourceUtils;
import rikka.material.app.DayNightDelegate;
import rikka.recyclerview.RecyclerViewKt;
@ -278,16 +278,16 @@ public class SettingsActivity extends BaseActivity {
if (restore != null) {
restore.setEnabled(installed);
restore.setOnPreferenceClickListener(preference -> {
restoreLauncher.launch(new String[]{"*/*" });
restoreLauncher.launch(new String[]{"*/*"});
return true;
});
}
IntegerListPreference theme = findPreference("theme");
Preference theme = findPreference("dark_theme");
if (theme != null) {
theme.setOnPreferenceChangeListener((preference, newValue) -> {
if (preferences.getInt("theme", -1) != Integer.parseInt((String) newValue)) {
DayNightDelegate.setDefaultNightMode(Integer.parseInt((String) newValue));
if (!preferences.getString("dark_theme", ThemeUtil.MODE_NIGHT_FOLLOW_SYSTEM).equals(newValue)) {
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme((String) newValue));
SettingsActivity activity = (SettingsActivity) getActivity();
if (activity != null) {
activity.restart();
@ -297,7 +297,7 @@ public class SettingsActivity extends BaseActivity {
});
}
SwitchPreference black_dark_theme = findPreference("black_dark_theme");
Preference black_dark_theme = findPreference("black_dark_theme");
if (black_dark_theme != null) {
black_dark_theme.setOnPreferenceChangeListener((preference, newValue) -> {
SettingsActivity activity = (SettingsActivity) getActivity();

View File

@ -1,83 +0,0 @@
/*
* This file is part of LSPosed.
*
* LSPosed is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LSPosed is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2020 EdXposed Contributors
* Copyright (C) 2021 LSPosed Contributors
*/
package io.github.lsposed.manager.ui.widget;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import com.takisoft.preferencex.SimpleMenuPreference;
@SuppressWarnings("unused")
public class IntegerListPreference extends SimpleMenuPreference {
public IntegerListPreference(Context context) {
super(context);
}
public IntegerListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
private static int getIntValue(String value) {
if (value == null)
return 0;
return (int) ((value.startsWith("0x"))
? Long.parseLong(value.substring(2), 16)
: Long.parseLong(value));
}
@Override
public void setValue(String value) {
super.setValue(value);
notifyChanged();
}
@Override
protected boolean persistString(String value) {
return value != null && persistInt(getIntValue(value));
}
@Override
protected String getPersistedString(String defaultReturnValue) {
SharedPreferences pref = getPreferenceManager().getSharedPreferences();
String key = getKey();
if (!shouldPersist() || !pref.contains(key))
return defaultReturnValue;
return String.valueOf(pref.getInt(key, 0));
}
@Override
public int findIndexOfValue(String value) {
CharSequence[] entryValues = getEntryValues();
int intValue = getIntValue(value);
if (value != null && entryValues != null) {
for (int i = entryValues.length - 1; i >= 0; i--) {
if (getIntValue(entryValues[i].toString()) == intValue) {
return i;
}
}
}
return -1;
}
}

View File

@ -31,11 +31,16 @@ import java.util.Map;
import io.github.lsposed.manager.App;
import io.github.lsposed.manager.R;
import rikka.core.util.ResourceUtils;
import rikka.material.app.DayNightDelegate;
public class ThemeUtil {
private static final Map<String, Integer> colorThemeMap = new HashMap<>();
private static final SharedPreferences preferences;
public static final String MODE_NIGHT_FOLLOW_SYSTEM = "MODE_NIGHT_FOLLOW_SYSTEM";
public static final String MODE_NIGHT_NO = "MODE_NIGHT_NO";
public static final String MODE_NIGHT_YES = "MODE_NIGHT_YES";
static {
preferences = App.getPreferences();
colorThemeMap.put("COLOR_PRIMARY", R.style.ThemeOverlay_color_primary);
@ -139,4 +144,20 @@ public class ThemeUtil {
return resourceId;
}
}
public static int getDarkTheme(String mode) {
switch (mode) {
case MODE_NIGHT_FOLLOW_SYSTEM:
default:
return DayNightDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
case MODE_NIGHT_YES:
return DayNightDelegate.MODE_NIGHT_YES;
case MODE_NIGHT_NO:
return DayNightDelegate.MODE_NIGHT_NO;
}
}
public static int getDarkTheme() {
return getDarkTheme(preferences.getString("dark_theme", MODE_NIGHT_FOLLOW_SYSTEM));
}
}

View File

@ -22,15 +22,15 @@
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
<string-array name="theme_texts" translatable="false">
<item>@string/dark_theme_follow_system</item>
<item>@string/dark_theme_off</item>
<item>@string/dark_theme_on</item>
<item>@string/dark_theme_follow_system</item>
</string-array>
<string-array name="theme_values" translatable="false">
<item>-1</item>
<item>1</item>
<item>2</item>
<item>MODE_NIGHT_NO</item>
<item>MODE_NIGHT_YES</item>
<item>MODE_NIGHT_FOLLOW_SYSTEM</item>
</string-array>
<string-array name="variant_texts" translatable="false">

View File

@ -42,11 +42,11 @@
android:key="theme_color"
android:title="@string/theme_color"
app:iconSpaceReserved="false" />
<io.github.lsposed.manager.ui.widget.IntegerListPreference
android:defaultValue="-1"
<com.takisoft.preferencex.SimpleMenuPreference
android:defaultValue="MODE_NIGHT_FOLLOW_SYSTEM"
android:entries="@array/theme_texts"
android:entryValues="@array/theme_values"
android:key="theme"
android:key="dark_theme"
android:summary="%s"
android:title="@string/dark_theme"
app:iconSpaceReserved="false" />