-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature|optimize] Support select dark mode strategy; support buildin…
…g multiple APKs per ABI; optimize code
- Loading branch information
Showing
18 changed files
with
540 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app/src/main/java/com/skyd/anivu/model/preference/appearance/DarkModePreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.skyd.anivu.model.preference.appearance | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import androidx.appcompat.app.AppCompatDelegate | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.intPreferencesKey | ||
import com.skyd.anivu.R | ||
import com.skyd.anivu.base.BasePreference | ||
import com.skyd.anivu.ext.dataStore | ||
import com.skyd.anivu.ext.put | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
object DarkModePreference : BasePreference<Int> { | ||
private const val DARK_MODE = "darkMode" | ||
|
||
val values: List<Int> = mutableListOf( | ||
AppCompatDelegate.MODE_NIGHT_NO, | ||
AppCompatDelegate.MODE_NIGHT_YES, | ||
).apply { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
add(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) | ||
} | ||
} | ||
|
||
override val default = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { | ||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM | ||
} else { | ||
AppCompatDelegate.MODE_NIGHT_NO | ||
} | ||
|
||
val key = intPreferencesKey(DARK_MODE) | ||
|
||
fun toDisplayName(context: Context, value: Int): String = context.getString( | ||
when (value) { | ||
AppCompatDelegate.MODE_NIGHT_NO -> R.string.dark_mode_light | ||
AppCompatDelegate.MODE_NIGHT_YES -> R.string.dark_mode_dark | ||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM -> R.string.dark_mode_follow_system | ||
else -> R.string.unknown | ||
} | ||
) | ||
|
||
fun put(context: Context, scope: CoroutineScope, value: Int) { | ||
if (value != AppCompatDelegate.MODE_NIGHT_YES && | ||
value != AppCompatDelegate.MODE_NIGHT_NO && | ||
value != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM | ||
) { | ||
throw IllegalArgumentException("darkMode value invalid!!!") | ||
} | ||
scope.launch(Dispatchers.IO) { | ||
context.dataStore.put(key, value) | ||
withContext(Dispatchers.Main) { | ||
AppCompatDelegate.setDefaultNightMode(value) | ||
} | ||
} | ||
} | ||
|
||
override fun fromPreferences(preferences: Preferences): Int { | ||
val scope = CoroutineScope(context = Dispatchers.Main) | ||
val value = preferences[key] ?: default | ||
scope.launch(Dispatchers.Main) { | ||
AppCompatDelegate.setDefaultNightMode(value) | ||
} | ||
return value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.