-
-
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.
- Loading branch information
Showing
50 changed files
with
1,348 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.skyd.anivu.ext | ||
|
||
import android.view.animation.Animation | ||
|
||
val Animation.isRunning: Boolean | ||
get() = hasStarted() && !hasEnded() |
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
80 changes: 80 additions & 0 deletions
80
app/src/main/java/com/skyd/anivu/model/preference/appearance/ThemePreference.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,80 @@ | ||
package com.skyd.anivu.model.preference.appearance | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import com.skyd.anivu.R | ||
import com.skyd.anivu.base.BasePreference | ||
import com.skyd.anivu.ext.dataStore | ||
import com.skyd.anivu.ext.getOrDefault | ||
import com.skyd.anivu.ext.put | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
object ThemePreference : BasePreference<String> { | ||
private const val THEME = "theme" | ||
|
||
const val DYNAMIC = "Dynamic" | ||
const val PINK = "Pink" | ||
const val GREEN = "Green" | ||
const val BLUE = "Blue" | ||
const val YELLOW = "Yellow" | ||
const val PURPLE = "Purple" | ||
|
||
val values: Array<String> | ||
get() { | ||
val v = arrayOf(PINK, GREEN, BLUE, YELLOW, PURPLE) | ||
return if (supportDynamicTheme()) arrayOf(DYNAMIC, *v) else v | ||
} | ||
|
||
override val default = if (supportDynamicTheme()) DYNAMIC else PINK | ||
|
||
val key = stringPreferencesKey(THEME) | ||
|
||
fun put( | ||
context: Context, | ||
scope: CoroutineScope, | ||
value: String, | ||
onSuccess: (() -> Unit)? = null, | ||
) { | ||
scope.launch(Dispatchers.IO) { | ||
context.dataStore.put(key, value) | ||
withContext(Dispatchers.Main) { | ||
onSuccess?.invoke() | ||
} | ||
} | ||
} | ||
|
||
override fun fromPreferences(preferences: Preferences): String = preferences[key] ?: default | ||
|
||
private fun supportDynamicTheme(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S | ||
|
||
fun toDisplayName( | ||
context: Context, | ||
value: String = context.dataStore.getOrDefault(this), | ||
): String = when (value) { | ||
DYNAMIC -> context.getString(R.string.theme_dynamic) | ||
PINK -> context.getString(R.string.theme_pink) | ||
GREEN -> context.getString(R.string.theme_green) | ||
BLUE -> context.getString(R.string.theme_blue) | ||
YELLOW -> context.getString(R.string.theme_yellow) | ||
PURPLE -> context.getString(R.string.theme_purple) | ||
else -> context.getString(R.string.unknown) | ||
} | ||
|
||
fun toResId( | ||
context: Context, | ||
value: String = context.dataStore.getOrDefault(this), | ||
): Int = when (value) { | ||
DYNAMIC -> R.style.Theme_AniVu_Dynamic | ||
PINK -> R.style.Theme_AniVu_Pink | ||
GREEN -> R.style.Theme_AniVu_Green | ||
BLUE -> R.style.Theme_AniVu_Blue | ||
YELLOW -> R.style.Theme_AniVu_Yellow | ||
PURPLE -> R.style.Theme_AniVu_Purple | ||
else -> R.style.Theme_AniVu_Pink | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/skyd/anivu/ui/fragment/settings/appearance/AppearanceFragment.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,52 @@ | ||
package com.skyd.anivu.ui.fragment.settings.appearance | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.preference.DropDownPreference | ||
import androidx.preference.ListPreference | ||
import androidx.preference.PreferenceCategory | ||
import androidx.preference.PreferenceScreen | ||
import com.skyd.anivu.R | ||
import com.skyd.anivu.base.BasePreferenceFragmentCompat | ||
import com.skyd.anivu.ext.dataStore | ||
import com.skyd.anivu.ext.getOrDefault | ||
import com.skyd.anivu.model.preference.appearance.ThemePreference | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
|
||
@AndroidEntryPoint | ||
class AppearanceFragment : BasePreferenceFragmentCompat() { | ||
override val title by lazy { resources.getString(R.string.appearance_fragment_name) } | ||
override fun Context.onAddPreferences( | ||
savedInstanceState: Bundle?, | ||
rootKey: String?, | ||
screen: PreferenceScreen | ||
) { | ||
val themeCategory = PreferenceCategory(this).apply { | ||
key = "themeCategory" | ||
title = getString(R.string.appearance_fragment_theme_category) | ||
screen.addPreference(this) | ||
} | ||
|
||
DropDownPreference(this).apply { | ||
key = "theme" | ||
title = getString(R.string.appearance_fragment_theme) | ||
summaryProvider = ListPreference.SimpleSummaryProvider.getInstance() | ||
value = context.dataStore.getOrDefault(ThemePreference) | ||
entries = ThemePreference.values.map { | ||
ThemePreference.toDisplayName(context, it) | ||
}.toTypedArray() | ||
entryValues = ThemePreference.values | ||
setOnPreferenceChangeListener { _, newValue -> | ||
ThemePreference.put( | ||
requireContext(), lifecycleScope, newValue as String, | ||
) { | ||
requireActivity().recreate() | ||
} | ||
true | ||
} | ||
themeCategory.addPreference(this) | ||
} | ||
} | ||
} |
Oops, something went wrong.