Skip to content

Commit

Permalink
Add option to create and save styles in app
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud0808 committed Jan 2, 2025
1 parent 6b68153 commit 7e1e825
Show file tree
Hide file tree
Showing 24 changed files with 823 additions and 327 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/drdisagree/colorblendr/common/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object Const {
const val FIRST_RUN: String = "firstRun"
const val THEMING_ENABLED: String = "themingEnabled"
const val MONET_STYLE: String = "customMonetStyle"
const val CUSTOM_MONET_STYLE: String = "userGeneratedMonetStyle"
const val MODE_SPECIFIC_THEMES: String = "modeSpecificThemes"
const val SCREEN_OFF_UPDATE_COLORS: String = "screenOffUpdateColors"
const val DARKER_LAUNCHER_ICONS: String = "darkerLauncherIcons"
Expand Down Expand Up @@ -75,6 +76,7 @@ object Const {
const val THEME_CUSTOMIZATION_OVERLAY_PACKAGES: String = "theme_customization_overlay_packages"
const val SHIZUKU_THEMING_ENABLED: String = "shizukuThemingEnabled"
const val APP_LIST_FILTER_METHOD: String = "appListFilterMethod"
const val SAVED_CUSTOM_MONET_STYLES: String = "savedCustomMonetStyles"
val screenOrientation: AtomicInteger = AtomicInteger(-1)

// Service preferences
Expand Down
162 changes: 61 additions & 101 deletions app/src/main/java/com/drdisagree/colorblendr/config/RPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.drdisagree.colorblendr.common.Const
import com.drdisagree.colorblendr.common.Const.EXCLUDED_PREFS_FROM_BACKUP
import com.drdisagree.colorblendr.common.Const.MONET_SEED_COLOR
import com.drdisagree.colorblendr.common.Const.MONET_SEED_COLOR_ENABLED
import com.drdisagree.colorblendr.common.Const.SAVED_CUSTOM_MONET_STYLES
import com.drdisagree.colorblendr.common.Const.THEMING_ENABLED
import com.drdisagree.colorblendr.common.Const.WALLPAPER_COLOR_LIST
import com.google.gson.reflect.TypeToken
Expand All @@ -23,103 +24,60 @@ object RPrefs {

private var prefs: SharedPreferences = appContext
.createDeviceProtectedStorageContext()
.getSharedPreferences(
Const.SHARED_PREFS, Context.MODE_PRIVATE
)
.getSharedPreferences(Const.SHARED_PREFS, Context.MODE_PRIVATE)
private var editor: SharedPreferences.Editor = prefs.edit()

fun putBoolean(key: String, `val`: Boolean) {
editor.putBoolean(key, `val`).apply()
}
fun putBoolean(key: String, `val`: Boolean) = editor.putBoolean(key, `val`).apply()

fun putInt(key: String, `val`: Int) {
editor.putInt(key, `val`).apply()
}
fun putInt(key: String, `val`: Int) = editor.putInt(key, `val`).apply()

fun putLong(key: String, `val`: Long) {
editor.putLong(key, `val`).apply()
}
fun putLong(key: String, `val`: Long) = editor.putLong(key, `val`).apply()

fun putFloat(key: String, `val`: Float) {
editor.putFloat(key, `val`).apply()
}
fun putFloat(key: String, `val`: Float) = editor.putFloat(key, `val`).apply()

fun putString(key: String, `val`: String) {
editor.putString(key, `val`).apply()
}
fun putString(key: String, `val`: String) = editor.putString(key, `val`).apply()

fun getBoolean(key: String): Boolean {
return prefs.getBoolean(key, false)
}
fun getBoolean(key: String): Boolean = prefs.getBoolean(key, false)

fun getBoolean(key: String, defValue: Boolean): Boolean {
return prefs.getBoolean(key, defValue)
}
fun getBoolean(key: String, defValue: Boolean): Boolean = prefs.getBoolean(key, defValue)

fun getInt(key: String): Int {
return prefs.getInt(key, 0)
}
fun getInt(key: String): Int = prefs.getInt(key, 0)

fun getInt(key: String, defValue: Int): Int {
return prefs.getInt(key, defValue)
}
fun getInt(key: String, defValue: Int): Int = prefs.getInt(key, defValue)

fun getLong(key: String): Long {
return prefs.getLong(key, 0)
}
fun getLong(key: String): Long = prefs.getLong(key, 0)

fun getLong(key: String, defValue: Long): Long {
return prefs.getLong(key, defValue)
}
fun getLong(key: String, defValue: Long): Long = prefs.getLong(key, defValue)

fun getFloat(key: String): Float {
return prefs.getFloat(key, 0f)
}
fun getFloat(key: String): Float = prefs.getFloat(key, 0f)

fun getFloat(key: String, defValue: Float): Float {
return prefs.getFloat(key, defValue)
}
fun getFloat(key: String, defValue: Float): Float = prefs.getFloat(key, defValue)

fun getString(key: String): String? {
return prefs.getString(key, null)
}
fun getString(key: String): String? = prefs.getString(key, null)

fun getString(key: String, defValue: String?): String? {
return prefs.getString(key, defValue)
}
fun getString(key: String, defValue: String?): String? = prefs.getString(key, defValue)

fun clearPref(key: String) {
editor.remove(key).apply()
}
fun getAllPrefs(): MutableMap<String, *> = prefs.all

fun clearPrefs(vararg keys: String) {
for (key in keys) {
editor.remove(key).apply()
}
}
fun getAllPrefsAsGson(): String = Const.GSON.toJson(getAllPrefs() as Map<String, *>)

fun clearAllPrefs() {
editor.clear().apply()
}
fun Map<String, Any>.toGsonString(): String = Const.GSON.toJson(this)

fun backupPrefs(outputStream: OutputStream) {
try {
outputStream.use {
ObjectOutputStream(outputStream).use { objectOutputStream ->
objectOutputStream.writeObject(
prefs.all
)
}
}
} catch (e: IOException) {
Log.e(TAG, "Error serializing preferences", e)
}
fun String.toPrefs(): Map<String, Any> =
Const.GSON.fromJson(this, object : TypeToken<Map<String, Any>>() {}.type)

fun clearPref(key: String) = editor.remove(key).apply()

fun clearPrefs(vararg keys: String) = keys.forEach { key -> editor.remove(key).apply() }

fun clearAllPrefs() = editor.clear().apply()

fun backupPrefs(outputStream: OutputStream) {
try {
outputStream.use {
ObjectOutputStream(outputStream).use { objectOutputStream ->
val allPrefs = prefs.all
for (excludedPref in EXCLUDED_PREFS_FROM_BACKUP) {
val allPrefs = getAllPrefs()
EXCLUDED_PREFS_FROM_BACKUP.forEach { excludedPref ->
allPrefs.remove(excludedPref)
}
objectOutputStream.writeObject(allPrefs)
Expand Down Expand Up @@ -147,22 +105,35 @@ object RPrefs {
return
}

restorePrefsMap(map)
}

fun restorePrefsMap(map: Map<String, Any>, isApplyingTheme: Boolean = false) {
val allPrefs = getAllPrefs()

// Retrieve excluded prefs from current prefs
val excludedPrefs: MutableMap<String, Any> = HashMap()

// Restoring config will enable theming service
excludedPrefs[THEMING_ENABLED] = true

for (excludedPref in EXCLUDED_PREFS_FROM_BACKUP) {
val prefValue = prefs.all[excludedPref]
EXCLUDED_PREFS_FROM_BACKUP.forEach { excludedPref ->
val prefValue = allPrefs[excludedPref]
if (prefValue != null) {
excludedPrefs[excludedPref] = prefValue
}
}

// Restore saved themes when enabling a theme otherwise it will be
// overwritten by the new preference of the theme
val savedThemes = allPrefs[SAVED_CUSTOM_MONET_STYLES]
if (savedThemes != null && isApplyingTheme) {
excludedPrefs[SAVED_CUSTOM_MONET_STYLES] = allPrefs[SAVED_CUSTOM_MONET_STYLES] as String
}

// Check if seed color is available in current wallpaper color list
val seedColor = map[MONET_SEED_COLOR] as? Int
val wallpaperColors = prefs.all[WALLPAPER_COLOR_LIST] as? String
val wallpaperColors = allPrefs[WALLPAPER_COLOR_LIST] as? String
val colorAvailable = if (seedColor != null && wallpaperColors != null) {
Const.GSON.fromJson<ArrayList<Int?>?>(
wallpaperColors,
Expand All @@ -179,7 +150,9 @@ object RPrefs {

// Restore non-excluded prefs
for ((key, value) in map) {
if (EXCLUDED_PREFS_FROM_BACKUP.contains(key)) {
if (EXCLUDED_PREFS_FROM_BACKUP.contains(key) ||
(isApplyingTheme && key == SAVED_CUSTOM_MONET_STYLES)
) {
continue
}

Expand All @@ -194,29 +167,16 @@ object RPrefs {

private fun putObject(key: String, value: Any) {
when (value) {
is Boolean -> {
editor.putBoolean(key, value)
}

is String -> {
editor.putString(key, value)
}

is Int -> {
editor.putInt(key, value)
}

is Float -> {
editor.putFloat(key, value)
}

is Long -> {
editor.putLong(key, value)
}

else -> {
throw IllegalArgumentException("Type " + value.javaClass.name + " is unknown")
}
is Boolean -> editor.putBoolean(key, value)
is String -> editor.putString(key, value)
is Int -> editor.putInt(key, value)
is Long -> editor.putLong(key, value)
// Float and Double are unused in this project
// is Float -> editor.putFloat(key, value)
// is Double -> editor.putFloat(key, value.toFloat())
is Float -> editor.putInt(key, value.toInt())
is Double -> editor.putInt(key, value.toInt())
else -> throw IllegalArgumentException("Type ${value.javaClass.simpleName} is unknown")
}
}
}
}
Loading

0 comments on commit 7e1e825

Please sign in to comment.