Changed: Store app wide night mode in NightMode.APP_NIGHT_MODE so that libraries can use it directly without having to load or get it from termux properties

This commit is contained in:
agnostic-apollo
2021-11-12 08:33:11 +05:00
parent 1b794b3518
commit b79ed509f1
5 changed files with 97 additions and 14 deletions

View File

@@ -0,0 +1,25 @@
package com.termux.shared.termux.theme;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.termux.shared.termux.settings.properties.TermuxPropertyConstants;
import com.termux.shared.termux.settings.properties.TermuxSharedProperties;
import com.termux.shared.theme.NightMode;
public class TermuxThemeUtils {
/** Get the {@link TermuxPropertyConstants#KEY_NIGHT_MODE} value from the properties file on disk
* and set it to app wide night mode value. */
public static void setAppNightMode(@NonNull Context context) {
NightMode.setAppNightMode(TermuxSharedProperties.getNightMode(context));
}
/** Set name as app wide night mode value. */
public static void setAppNightMode(@Nullable String name) {
NightMode.setAppNightMode(name);
}
}

View File

@@ -1,7 +1,11 @@
package com.termux.shared.theme;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import com.termux.shared.logger.Logger;
/** The modes used by to decide night mode for themes. */
public enum NightMode {
@@ -17,6 +21,11 @@ public enum NightMode {
*/
SYSTEM("system", AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
/** The current app wide night mode used by various libraries. Defaults to {@link #SYSTEM}. */
private static NightMode APP_NIGHT_MODE;
private static final String LOG_TAG = "NightMode";
private final String name;
private final int mode;
@@ -33,16 +42,50 @@ public enum NightMode {
return mode;
}
public static Integer modeOf(String name) {
if (TRUE.name.equals(name))
return TRUE.mode;
else if (FALSE.name.equals(name))
return FALSE.mode;
else if (SYSTEM.name.equals(name)) {
return SYSTEM.mode;
} else {
return null;
/** Get {@link NightMode} for {@code name} if found, otherwise {@code null}. */
@Nullable
public static NightMode modeOf(String name) {
for (NightMode v : NightMode.values()) {
if (v.name.equals(name)) {
return v;
}
}
return null;
}
/** Get {@link NightMode} for {@code name} if found, otherwise {@code def}. */
@NonNull
public static NightMode modeOf(@Nullable String name, @NonNull NightMode def) {
NightMode nightMode = modeOf(name);
return nightMode != null ? nightMode : def;
}
/** Set {@link #APP_NIGHT_MODE}. */
public static void setAppNightMode(@Nullable String name) {
if (name == null || name.isEmpty()) {
APP_NIGHT_MODE = SYSTEM;
} else {
NightMode nightMode = NightMode.modeOf(name);
if (nightMode == null) {
Logger.logError(LOG_TAG, "Invalid APP_NIGHT_MODE \"" + name + "\"");
return;
}
APP_NIGHT_MODE = nightMode;
}
Logger.logVerbose(LOG_TAG, "Set APP_NIGHT_MODE to \"" + APP_NIGHT_MODE.getName() + "\"");
}
/** Get {@link #APP_NIGHT_MODE}. */
@NonNull
public static NightMode getAppNightMode() {
if (APP_NIGHT_MODE == null)
APP_NIGHT_MODE = SYSTEM;
return APP_NIGHT_MODE;
}
}