Dynamic colors [2/2]

This commit is contained in:
Rahul Patel
2024-09-02 03:23:24 +05:30
parent 3546ed8a54
commit 20b507b029
12 changed files with 120 additions and 80 deletions

View File

@@ -19,27 +19,12 @@
package com.aurora.extensions
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.ColorUtils
import androidx.core.view.WindowInsetsControllerCompat
import androidx.appcompat.app.AppCompatDelegate
@Suppress("DEPRECATION")
private fun AppCompatActivity.setSystemBarConfiguration(light: Boolean) {
WindowInsetsControllerCompat(this.window, this.window.decorView.rootView).apply {
// Status bar color
if (isMAndAbove()) {
isAppearanceLightStatusBars = light
} else {
// Add a semi-transparent black color to the status bar & navigation bar
window.statusBarColor = ColorUtils.setAlphaComponent(Color.BLACK, 120)
window.navigationBarColor = ColorUtils.setAlphaComponent(Color.BLACK, 120)
}
// Navigation bar color
if (isOMR1AndAbove()) {
isAppearanceLightNavigationBars = light
window.navigationBarColor = getStyledAttributeColor(android.R.attr.colorBackground)
}
fun setAppTheme(themeStyle: Int) {
when (themeStyle) {
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}

View File

@@ -25,12 +25,14 @@ import androidx.core.content.ContextCompat
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import com.aurora.extensions.isPAndAbove
import com.aurora.extensions.setAppTheme
import com.aurora.store.data.event.EventFlow
import com.aurora.store.data.receiver.PackageManagerReceiver
import com.aurora.store.util.CommonUtil
import com.aurora.store.util.DownloadWorkerUtil
import com.aurora.store.util.NotificationUtil
import com.aurora.store.util.PackageUtil
import com.aurora.store.util.Preferences
import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.MainScope
@@ -64,9 +66,13 @@ class AuroraApp : Application(), Configuration.Provider {
override fun onCreate() {
super.onCreate()
// Set the app theme
val themeStyle = Preferences.getInteger(this, Preferences.PREFERENCE_THEME_STYLE)
setAppTheme(themeStyle)
// Apply dynamic colors to activities0
DynamicColors.applyToActivitiesIfAvailable(this);
DynamicColors.applyToActivitiesIfAvailable(this)
DynamicColors.wrapContextIfAvailable(this)
// Required for Shizuku installer
if (isPAndAbove()) HiddenApiBypass.addHiddenApiExemptions("I", "L")

View File

@@ -25,7 +25,6 @@ import android.view.View
import androidx.activity.addCallback
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
@@ -72,8 +71,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
B = ActivityMainBinding.inflate(layoutInflater)
ViewCompat.setOnApplyWindowInsetsListener(B.root) { view, windowInsets ->

View File

@@ -5,20 +5,18 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import com.aurora.Constants
import com.aurora.extensions.isSAndAbove
import com.aurora.store.data.work.CacheWorker
import com.aurora.store.util.CertUtil
import com.aurora.store.util.Preferences
import com.aurora.store.util.Preferences.PREFERENCE_DISPENSER_URLS
import com.aurora.store.util.Preferences.PREFERENCE_INTRO
import com.aurora.store.util.Preferences.PREFERENCE_MIGRATION_VERSION
import com.aurora.store.util.Preferences.PREFERENCE_THEME_ACCENT
import com.aurora.store.util.Preferences.PREFERENCE_VENDING_VERSION
import com.aurora.store.util.save
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class MigrationReceiver: BroadcastReceiver() {
class MigrationReceiver : BroadcastReceiver() {
companion object {
private const val TAG = "MigrationReceiver"
@@ -47,7 +45,6 @@ class MigrationReceiver: BroadcastReceiver() {
// 58 -> 59
if (currentVersion == 0) {
CacheWorker.scheduleAutomatedCacheCleanup(context) // !1089
if (isSAndAbove()) context.save(PREFERENCE_THEME_ACCENT, 0)
context.save(PREFERENCE_DISPENSER_URLS, setOf(Constants.URL_DISPENSER)) // !1117
if (Preferences.getInteger(context, PREFERENCE_VENDING_VERSION) == -1) {
context.save(PREFERENCE_VENDING_VERSION, 0) // !1049

View File

@@ -31,8 +31,7 @@ object Preferences {
const val PREFERENCE_AUTH_DATA = "PREFERENCE_AUTH_DATA"
const val PREFERENCE_INSTALLER_ID = "PREFERENCE_INSTALLER_ID"
const val PREFERENCE_THEME_TYPE = "PREFERENCE_THEME_TYPE"
const val PREFERENCE_THEME_ACCENT = "PREFERENCE_THEME_ACCENT"
const val PREFERENCE_THEME_STYLE = "PREFERENCE_THEME_STYLE"
const val PREFERENCE_FOR_YOU = "PREFERENCE_FOR_YOU"
const val PREFERENCE_DEFAULT_SELECTED_TAB = "PREFERENCE_DEFAULT_SELECTED_TAB"
const val PREFERENCE_SIMILAR = "PREFERENCE_SIMILAR"

View File

@@ -11,23 +11,25 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -52,9 +54,11 @@ import com.aurora.Constants
import com.aurora.Constants.URL_TOS
import com.aurora.extensions.browse
import com.aurora.extensions.getStyledAttributeColor
import com.aurora.extensions.setAppTheme
import com.aurora.store.MR
import com.aurora.store.R
import com.aurora.store.data.providers.AuthProvider
import com.aurora.store.util.Preferences
import com.aurora.store.view.theme.AuroraTheme
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
@@ -152,21 +156,30 @@ class MoreDialogFragment : DialogFragment() {
@Composable
fun AppBar(backgroundColor: Color = Color.Transparent, onBackgroundColor: Color) {
Box(contentAlignment = Alignment.CenterEnd) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.padding(4.dp, 10.dp, 4.dp, 10.dp)
) {
ThreeStateIconButton(tint = onBackgroundColor)
Text(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier.wrapContentWidth(),
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.titleMedium,
color = onBackgroundColor,
textAlign = TextAlign.Center
)
IconButton(onClick = { findNavController().navigateUp() }) {
Icon(
painter = painterResource(id = R.drawable.ic_cancel),
contentDescription = stringResource(id = R.string.action_cancel),
tint = onBackgroundColor
)
}
Image(
painter = painterResource(id = R.drawable.ic_cancel),
contentDescription = stringResource(id = R.string.action_cancel),
modifier = Modifier.clickable {
findNavController().navigateUp()
},
colorFilter = ColorFilter.tint(onBackgroundColor)
)
}
}
@@ -298,6 +311,56 @@ class MoreDialogFragment : DialogFragment() {
}
}
@Composable
fun ThreeStateIconButton(
tint: Color = Color.White
) {
var currentState by remember {
mutableStateOf(
Preferences.getInteger(
requireContext(),
Preferences.PREFERENCE_THEME_STYLE
).let {
State.entries.getOrNull(it)
} ?: State.Auto
)
}
val iconRes = when (currentState) {
State.Light -> R.drawable.ic_light
State.Dark -> R.drawable.ic_dark
else -> R.drawable.ic_auto
}
Image(
painter = painterResource(id = iconRes),
contentDescription = null,
modifier = Modifier.clickable {
currentState = when (currentState) {
State.Light -> State.Dark
State.Dark -> State.Auto
State.Auto -> State.Light
}
Preferences.putInteger(
requireContext(),
Preferences.PREFERENCE_THEME_STYLE,
currentState.value
)
setAppTheme(currentState.value)
findNavController().navigateUp()
},
colorFilter = ColorFilter.tint(tint)
)
}
enum class State(val value: Int) {
Auto(0),
Light(1),
Dark(2),
}
private fun getOptions(): List<Option> {
return listOf(
Option(

View File

@@ -29,7 +29,6 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.aurora.Constants
import com.aurora.extensions.isIgnoringBatteryOptimizations
import com.aurora.extensions.isSAndAbove
import com.aurora.store.R
import com.aurora.store.data.work.CacheWorker
import com.aurora.store.data.work.UpdateWorker
@@ -48,8 +47,7 @@ import com.aurora.store.util.Preferences.PREFERENCE_FOR_YOU
import com.aurora.store.util.Preferences.PREFERENCE_INSTALLER_ID
import com.aurora.store.util.Preferences.PREFERENCE_INTRO
import com.aurora.store.util.Preferences.PREFERENCE_SIMILAR
import com.aurora.store.util.Preferences.PREFERENCE_THEME_ACCENT
import com.aurora.store.util.Preferences.PREFERENCE_THEME_TYPE
import com.aurora.store.util.Preferences.PREFERENCE_THEME_STYLE
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_AUTO
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_CHECK_INTERVAL
import com.aurora.store.util.Preferences.PREFERENCE_UPDATES_EXTENDED
@@ -161,8 +159,7 @@ class OnboardingFragment : BaseFragment<FragmentOnboardingBinding>() {
save(PREFERENCE_VENDING_VERSION, 0)
/*Customization*/
save(PREFERENCE_THEME_TYPE, 0)
save(PREFERENCE_THEME_ACCENT, if (isSAndAbove()) 0 else 1)
save(PREFERENCE_THEME_STYLE, 0)
save(PREFERENCE_DEFAULT_SELECTED_TAB, 0)
save(PREFERENCE_FOR_YOU, true)
save(PREFERENCE_SIMILAR, true)

View File

@@ -21,18 +21,14 @@ package com.aurora.store.view.ui.preferences
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.findNavController
import androidx.preference.ListPreference
import androidx.preference.Preference
import com.aurora.extensions.isTAndAbove
import com.aurora.store.R
import com.aurora.store.util.Preferences
import com.aurora.store.util.save
import dagger.hilt.android.AndroidEntryPoint
import java.util.Locale
@@ -64,28 +60,5 @@ class UIPreference : BasePreferenceFragment() {
title = getString(R.string.pref_ui_title)
setNavigationOnClickListener { findNavController().navigateUp() }
}
val themePreference: ListPreference? = findPreference(Preferences.PREFERENCE_THEME_TYPE)
themePreference?.let {
it.setOnPreferenceChangeListener { _, newValue ->
val themeId = Integer.parseInt(newValue.toString())
save(Preferences.PREFERENCE_THEME_TYPE, themeId)
requireActivity().recreate()
true
}
}
val accentPreference: ListPreference? = findPreference(Preferences.PREFERENCE_THEME_ACCENT)
accentPreference?.let {
it.isVisible = Build.VERSION.SDK_INT < Build.VERSION_CODES.S
it.setOnPreferenceChangeListener { _, newValue ->
val accentId = Integer.parseInt(newValue.toString())
save(Preferences.PREFERENCE_THEME_ACCENT, accentId)
requireActivity().recreate()
true
}
}
}
}

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M396,564q-32,-32 -58.5,-67T289,423q-5,14 -6.5,28.5T281,480q0,83 58,141t141,58q14,0 28.5,-2t28.5,-6q-39,-22 -74,-48.5T396,564ZM453,508q51,51 114,87.5T702,652q-40,51 -98,79.5T481,760q-117,0 -198.5,-81.5T201,480q0,-65 28.5,-123t79.5,-98q20,72 56.5,135T453,508ZM743,580q-20,-5 -39.5,-11T665,555q8,-18 11.5,-36.5T680,480q0,-83 -58.5,-141.5T480,280q-20,0 -38.5,3.5T405,295q-8,-19 -13.5,-38T381,218q24,-9 49,-13.5t51,-4.5q117,0 198.5,81.5T761,480q0,26 -4.5,51T743,580ZM440,120v-120h80v120h-80ZM440,960v-120h80L520,960h-80ZM763,254 L706,197 791,113 848,169 763,254ZM169,847l-57,-56 85,-85 57,57 -85,84ZM840,520v-80h120v80L840,520ZM0,520v-80h120v80L0,520ZM791,848 L706,763 763,706 847,791 791,848ZM197,254l-84,-85 56,-57 85,85 -57,57ZM396,564Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M480,840q-150,0 -255,-105T120,480q0,-150 105,-255t255,-105q14,0 27.5,1t26.5,3q-41,29 -65.5,75.5T444,300q0,90 63,153t153,63q55,0 101,-24.5t75,-65.5q2,13 3,26.5t1,27.5q0,150 -105,255T480,840ZM480,760q88,0 158,-48.5T740,585q-20,5 -40,8t-40,3q-123,0 -209.5,-86.5T364,300q0,-20 3,-40t8,-40q-78,32 -126.5,102T200,480q0,116 82,198t198,82ZM470,490Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="M480,600q50,0 85,-35t35,-85q0,-50 -35,-85t-85,-35q-50,0 -85,35t-35,85q0,50 35,85t85,35ZM480,680q-83,0 -141.5,-58.5T280,480q0,-83 58.5,-141.5T480,280q83,0 141.5,58.5T680,480q0,83 -58.5,141.5T480,680ZM200,520L40,520v-80h160v80ZM920,520L760,520v-80h160v80ZM440,200v-160h80v160h-80ZM440,920v-160h80v160h-80ZM256,310l-101,-97 57,-59 96,100 -52,56ZM748,806 L651,705 704,650 805,747 748,806ZM650,256 L747,155 806,212 706,308 650,256ZM154,748l101,-97 55,53 -97,101 -59,-57ZM480,480Z"
android:fillColor="#e8eaed"/>
</vector>

View File

@@ -46,9 +46,5 @@
<item name="widgetLayout">@layout/preference_material_switch</item>
</style>
<style name="AppTheme.FilterChip" parent="@style/Widget.Material3.Chip.Filter">
<item name="chipBackgroundColor">@drawable/filter_chip_background</item>
<item name="chipStrokeColor">?colorControlHighlight</item>
<item name="chipCornerRadius">32dp</item>
</style>
<style name="AppTheme.FilterChip" parent="@style/Widget.Material3.Chip.Filter" />
</resources>