Dynamic color setting

pull/3450/head
Alex Baker 8 months ago
parent e9eac51726
commit d5dbddaab8

@ -1,6 +1,8 @@
package org.tasks.injection
import android.app.Activity
import android.util.TypedValue
import com.google.android.material.color.DynamicColors
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@ -23,6 +25,19 @@ class ActivityModule {
@Provides
@ActivityScoped
fun getThemeColor(colorProvider: ColorProvider, preferences: Preferences): ThemeColor
= colorProvider.getThemeColor(preferences.defaultThemeColor, true)
}
fun getThemeColor(
colorProvider: ColorProvider,
preferences: Preferences,
activity: Activity
): ThemeColor {
return if (preferences.dynamicColor && DynamicColors.isDynamicColorAvailable()) {
val context = DynamicColors.wrapContextIfAvailable(activity)
val typedValue = TypedValue()
context.theme.resolveAttribute(com.google.android.material.R.attr.colorPrimary, typedValue, true)
val dynamicColor = typedValue.data
ThemeColor(context, dynamicColor)
} else {
colorProvider.getThemeColor(preferences.defaultThemeColor, true)
}
}
}

@ -549,6 +549,9 @@ class Preferences @JvmOverloads constructor(
val defaultThemeColor: Int
get() = getInt(R.string.p_theme_color, BLUE_500)
val dynamicColor: Boolean
get() = getBoolean(R.string.p_dynamic_color, false)
val markdown: Boolean
get() = getBoolean(R.string.p_markdown, false)

@ -11,6 +11,8 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.tasks.LocalBroadcastManager
@ -89,6 +91,14 @@ class LookAndFeel : InjectingPreferenceFragment() {
}
openUrl(R.string.translations, R.string.url_translations)
val themeColor = findPreference(R.string.p_theme_color)
val dynamicColor = findPreference(R.string.p_dynamic_color) as SwitchPreferenceCompat
themeColor.isVisible = !dynamicColor.isChecked
dynamicColor.setOnPreferenceChangeListener { _, newValue ->
themeColor.isVisible = !(newValue as Boolean)
true
}
requires(DynamicColors.isDynamicColorAvailable(), R.string.p_dynamic_color)
}
override fun onResume() {
@ -101,6 +111,19 @@ class LookAndFeel : InjectingPreferenceFragment() {
REQUEST_COLOR_PICKER
)
updateLauncherPreference()
if (DynamicColors.isDynamicColorAvailable()) {
(findPreference(R.string.p_dynamic_color) as SwitchPreferenceCompat).apply {
if (inventory.hasPro) {
summary = null
isEnabled = true
} else {
summary = getString(R.string.requires_pro_subscription)
isEnabled = false
isChecked = false
}
}
}
}
private fun updateLauncherPreference() {

@ -436,4 +436,5 @@
<string name="p_last_sync">last_sync_time</string>
<string name="p_microsoft_sync">microsoft_sync</string>
<string name="p_multiline_title">multiline_title</string>
<string name="p_dynamic_color">dynamic_color</string>
</resources>

@ -7,6 +7,11 @@
android:key="@string/p_theme"
android:title="@string/theme" />
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/p_dynamic_color"
android:title="@string/theme_dynamic" />
<Preference
android:key="@string/p_theme_color"
android:title="@string/color" />

Loading…
Cancel
Save