MoreDialogFragment: Better theme and color handling

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-04-19 12:11:42 +05:30
parent 96cd614ab6
commit b48cc00cbe
2 changed files with 93 additions and 42 deletions

View File

@@ -0,0 +1,33 @@
package com.aurora.store.view.theme
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import com.aurora.store.R
@Composable
fun AuroraTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
val colorScheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (darkTheme) {
dynamicDarkColorScheme(LocalContext.current)
} else {
dynamicLightColorScheme(LocalContext.current)
}
} else {
if (darkTheme) {
darkColorScheme(primary = colorResource(id = R.color.colorAccent))
} else {
lightColorScheme(primary = colorResource(id = R.color.colorAccent))
}
}
MaterialTheme(colorScheme = colorScheme, typography = Typography(), content = content)
}

View File

@@ -9,6 +9,7 @@ import androidx.annotation.StringRes
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@@ -43,17 +44,16 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.compose.ui.util.fastForEach import androidx.compose.ui.util.fastForEach
import androidx.core.graphics.ColorUtils
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import coil.compose.SubcomposeAsyncImage import coil.compose.SubcomposeAsyncImage
import coil.request.ImageRequest import coil.request.ImageRequest
import com.aurora.Constants import com.aurora.Constants
import com.aurora.Constants.URL_TOS import com.aurora.Constants.URL_TOS
import com.aurora.extensions.accentColor
import com.aurora.extensions.browse import com.aurora.extensions.browse
import com.aurora.store.R import com.aurora.store.R
import com.aurora.store.data.providers.AuthProvider import com.aurora.store.data.providers.AuthProvider
import com.aurora.store.view.theme.AuroraTheme
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class MoreDialogFragment : DialogFragment() { class MoreDialogFragment : DialogFragment() {
@@ -74,59 +74,80 @@ class MoreDialogFragment : DialogFragment() {
return ComposeView(context).apply { return ComposeView(context).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent { setContent {
val bColor = ColorUtils.setAlphaComponent(LocalContext.current.accentColor(), 20) AuroraTheme {
Column( val backgroundColor = if (isSystemInDarkTheme()) {
modifier = Modifier MaterialTheme.colorScheme.onSecondary
.fillMaxWidth() } else {
.background(color = Color(bColor)) MaterialTheme.colorScheme.surfaceVariant
.verticalScroll(rememberScrollState()) }
.padding(13.dp),
verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.CenterVertically) val onBackgroundColor = if (isSystemInDarkTheme()) {
) { MaterialTheme.colorScheme.onSurface
AppBar() } else {
AccountHeader() MaterialTheme.colorScheme.onSecondary
}
val tintColor = if (isSystemInDarkTheme()) Color.White else Color.Black
Column( Column(
modifier = Modifier modifier = Modifier
.clip( .fillMaxWidth()
RoundedCornerShape( .background(color = backgroundColor)
topStart = 5.dp, .verticalScroll(rememberScrollState())
topEnd = 5.dp, .padding(10.dp),
bottomStart = 20.dp, verticalArrangement = Arrangement.spacedBy(5.dp, Alignment.CenterVertically)
bottomEnd = 20.dp
)
)
.background(color = Color.White)
) { ) {
getOptions().fastForEach { option -> OptionItem(option = option) } AppBar(tintColor = tintColor)
AccountHeader(backgroundColor = onBackgroundColor)
Column(
modifier = Modifier
.clip(
RoundedCornerShape(
topStart = 5.dp,
topEnd = 5.dp,
bottomStart = 20.dp,
bottomEnd = 20.dp
)
)
.background(color = onBackgroundColor)
) {
getOptions().fastForEach { option -> OptionItem(option = option) }
}
getExtraOptions().fastForEach { option ->
OptionItem(
option = option,
tintColor = tintColor
)
}
Footer(tintColor)
} }
getExtraOptions().fastForEach { option -> OptionItem(option = option) }
Footer()
} }
} }
} }
} }
@Composable @Composable
fun AppBar() { fun AppBar(tintColor: Color) {
Box(contentAlignment = Alignment.CenterStart) { Box(contentAlignment = Alignment.CenterStart) {
IconButton(onClick = { findNavController().navigateUp() }) { IconButton(onClick = { findNavController().navigateUp() }) {
Icon( Icon(
painter = painterResource(id = R.drawable.ic_cancel), painter = painterResource(id = R.drawable.ic_cancel),
contentDescription = stringResource(id = R.string.action_cancel), contentDescription = stringResource(id = R.string.action_cancel),
tint = Color.Black tint = tintColor
) )
} }
Text( Text(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
text = stringResource(id = R.string.app_name), text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.titleMedium, style = MaterialTheme.typography.titleMedium,
color = tintColor,
textAlign = TextAlign.Center textAlign = TextAlign.Center
) )
} }
} }
@Composable @Composable
fun Footer() { fun Footer(tintColor: Color) {
Row( Row(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
@@ -135,34 +156,31 @@ class MoreDialogFragment : DialogFragment() {
TextButton(onClick = { requireContext().browse(Constants.URL_POLICY) }) { TextButton(onClick = { requireContext().browse(Constants.URL_POLICY) }) {
Text( Text(
text = stringResource(id = R.string.privacy_policy_title), text = stringResource(id = R.string.privacy_policy_title),
color = Color.Black color = tintColor
) )
} }
Text(text = "") Text(text = "", color = tintColor)
TextButton(onClick = { requireContext().browse(URL_TOS) }) { TextButton(onClick = { requireContext().browse(URL_TOS) }) {
Text( Text(
text = stringResource(id = R.string.menu_terms), text = stringResource(id = R.string.menu_terms),
color = Color.Black color = tintColor
) )
} }
} }
} }
@Composable @Composable
private fun AccountHeader() { private fun AccountHeader(backgroundColor: Color) {
val authData = AuthProvider.with(LocalContext.current).getAuthData() val authData = AuthProvider.with(LocalContext.current).getAuthData()
Column( Column(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.clip( .clip(
RoundedCornerShape( RoundedCornerShape(
topStart = 20.dp, topStart = 20.dp, topEnd = 20.dp, bottomStart = 5.dp, bottomEnd = 5.dp
topEnd = 20.dp,
bottomStart = 5.dp,
bottomEnd = 5.dp
) )
) )
.background(color = Color.White) .background(color = backgroundColor)
.padding(20.dp), .padding(20.dp),
verticalArrangement = Arrangement.spacedBy(15.dp), verticalArrangement = Arrangement.spacedBy(15.dp),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
@@ -214,7 +232,7 @@ class MoreDialogFragment : DialogFragment() {
} }
@Composable @Composable
private fun OptionItem(option: Option) { private fun OptionItem(option: Option, tintColor: Color = Color.Black) {
Row( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@@ -226,16 +244,16 @@ class MoreDialogFragment : DialogFragment() {
Image( Image(
painter = painterResource(id = option.icon), painter = painterResource(id = option.icon),
contentDescription = stringResource(id = option.title), contentDescription = stringResource(id = option.title),
colorFilter = ColorFilter.tint(color = Color.Black), colorFilter = ColorFilter.tint(color = tintColor),
modifier = Modifier modifier = Modifier
.padding(horizontal = 10.dp) .padding(horizontal = 10.dp)
.requiredSize(25.dp) .requiredSize(23.dp)
) )
Text( Text(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
text = stringResource(id = option.title), text = stringResource(id = option.title),
color = Color.Black, color = tintColor,
fontSize = 16.sp, fontSize = 15.sp,
) )
} }
} }