Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,35 @@ class GlobalPropsUtils {
private val removableGlobalKeys = SafeConcurrentHashMap<String, MutableList<String>>()

companion object {
private const val PREFS_NAME = "sparkling_global"
private const val KEY_PREFERRED_THEME = "preferredTheme"

val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
GlobalPropsUtils()
}

/** Persist the user's theme preference so all new containers inherit it. */
@JvmStatic
fun setPreferredTheme(context: Context, theme: String?) {
val prefs = context.applicationContext
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
if (theme.isNullOrBlank()) {
prefs.edit().remove(KEY_PREFERRED_THEME).apply()
} else {
prefs.edit().putString(KEY_PREFERRED_THEME, theme.trim()).apply()
}
}

/** Read the persisted theme preference, if any. */
@JvmStatic
fun getPreferredTheme(context: Context): String? {
return context.applicationContext
.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
.getString(KEY_PREFERRED_THEME, null)
?.trim()
?.ifEmpty { null }
}

val builtInStableFields = mapOf<String, () -> Any>(
RuntimeInfo.LYNX_SDK_VERSION to { LynxEnv.inst().lynxVersion },
RuntimeInfo.SPARKLING_VERSION to { RuntimeInfo.SPARKLING_VERSION_VALUE },
Expand Down Expand Up @@ -221,6 +246,9 @@ class GlobalPropsUtils {
put("screenOrientation", if (isPortrait) "Portrait" else "Landscape")
put("orientation", if (isPortrait) 0 else 1)
put("theme", if (hybridContext.getTheme(context) == Theme.DARK) "dark" else "light")
context?.let { ctx ->
getPreferredTheme(ctx)?.let { put("preferredTheme", it) }
}
}
putAll(unstableMap)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ import Foundation
/// and providing fallback values when necessary.
@objcMembers
open class SPKGlobalPropsUtils: NSObject {
private static let preferredThemeKey = "sparkling.preferredTheme"

/// Persist the user's theme preference so all new containers inherit it.
public static func setPreferredTheme(_ theme: String?) {
if let theme = theme?.trimmingCharacters(in: .whitespacesAndNewlines), !theme.isEmpty {
UserDefaults.standard.set(theme, forKey: preferredThemeKey)
} else {
UserDefaults.standard.removeObject(forKey: preferredThemeKey)
}
}

/// Read the persisted theme preference, if any.
public static func preferredTheme() -> String? {
UserDefaults.standard.string(forKey: preferredThemeKey)
}
/// Generates a dictionary of default global properties for the current device and environment.
///
/// This method collects comprehensive device information including screen dimensions,
Expand Down Expand Up @@ -82,7 +97,8 @@ open class SPKGlobalPropsUtils: NSObject {
"isLowPowerMode": ProcessInfo.processInfo.isLowPowerModeEnabled ? 1 : 0,
"isAppBackground": UIApplication.shared.applicationState == .background,
"screenOrientation": self.screenOrientationString(),
"deviceModel": UIDevice.spk.hwModel?.lowercased() ?? ""
"deviceModel": UIDevice.spk.hwModel?.lowercased() ?? "",
"preferredTheme": preferredTheme() ?? ""
]
}

Expand Down
Loading