Skip to content

Commit

Permalink
feat(ui): top bar elevation preference
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Nov 20, 2024
1 parent 5cba1e4 commit 133c09d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ val LocalFlowTopBarTonalElevation =
compositionLocalOf<FlowTopBarTonalElevationPreference> { FlowTopBarTonalElevationPreference.default }

sealed class FlowTopBarTonalElevationPreference(val value: Int) : Preference() {
object Level0 : FlowTopBarTonalElevationPreference(ElevationTokens.Level0)
object Level1 : FlowTopBarTonalElevationPreference(ElevationTokens.Level1)
object Level2 : FlowTopBarTonalElevationPreference(ElevationTokens.Level2)
object Level3 : FlowTopBarTonalElevationPreference(ElevationTokens.Level3)
object Level4 : FlowTopBarTonalElevationPreference(ElevationTokens.Level4)
object Level5 : FlowTopBarTonalElevationPreference(ElevationTokens.Level5)
object None : FlowTopBarTonalElevationPreference(ElevationTokens.Level0)
object Elevated : FlowTopBarTonalElevationPreference(ElevationTokens.Level2)

override fun put(context: Context, scope: CoroutineScope) {
scope.launch {
Expand All @@ -30,27 +26,19 @@ sealed class FlowTopBarTonalElevationPreference(val value: Int) : Preference() {

fun toDesc(context: Context): String =
when (this) {
Level0 -> "Level 0 (${ElevationTokens.Level0}dp)"
Level1 -> "Level 1 (${ElevationTokens.Level1}dp)"
Level2 -> "Level 2 (${ElevationTokens.Level2}dp)"
Level3 -> "Level 3 (${ElevationTokens.Level3}dp)"
Level4 -> "Level 4 (${ElevationTokens.Level4}dp)"
Level5 -> "Level 5 (${ElevationTokens.Level5}dp)"
None -> "Level 0 (${ElevationTokens.Level0}dp)"
Elevated -> "Level 2 (${ElevationTokens.Level2}dp)"
}

companion object {

val default = Level0
val values = listOf(Level0, Level1, Level2, Level3, Level4, Level5)
val default = Elevated
val values = listOf(None, Elevated)

fun fromPreferences(preferences: Preferences) =
when (preferences[DataStoreKey.keys[flowTopBarTonalElevation]?.key as Preferences.Key<Int>]) {
ElevationTokens.Level0 -> Level0
ElevationTokens.Level1 -> Level1
ElevationTokens.Level2 -> Level2
ElevationTokens.Level3 -> Level3
ElevationTokens.Level4 -> Level4
ElevationTokens.Level5 -> Level5
ElevationTokens.Level0 -> None
ElevationTokens.Level2 -> Elevated
else -> default
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fun FlowPage(
val articleListTonalElevation = LocalFlowArticleListTonalElevation.current
val articleListFeedIcon = LocalFlowArticleListFeedIcon.current
val articleListDateStickyHeader = LocalFlowArticleListDateStickyHeader.current
val topBarTonalElevation = LocalFlowTopBarTonalElevation.current
val filterBarStyle = LocalFlowFilterBarStyle.current
val filterBarFilled = LocalFlowFilterBarFilled.current
val filterBarPadding = LocalFlowFilterBarPadding.current
Expand All @@ -107,9 +108,10 @@ fun FlowPage(
val listState =
if (pagingItems.itemCount > 0) flowUiState.listState else rememberLazyListState()

val isTopBarElevated = topBarTonalElevation.value > 0
val isScrolled by remember(listState) { derivedStateOf { listState.firstVisibleItemIndex != 0 } }
val topBarContainerColor by animateColorAsState(with(MaterialTheme.colorScheme) {
if (isScrolled) surfaceContainer else surface
if (isScrolled && isTopBarElevated) surfaceContainer else surface
}, label = "")

val titleText = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import androidx.compose.material.icons.rounded.DoneAll
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import me.ash.reader.R
import me.ash.reader.domain.model.article.Article
import me.ash.reader.domain.model.article.ArticleWithFeed
Expand Down Expand Up @@ -55,8 +58,19 @@ fun FlowPagePreview(
shape = RoundedCornerShape(24.dp)
)
) {
val preview = generateArticleWithFeedPreview()
val feed = preview.feed
val article = preview.article

TopAppBar(
title = {},
title = {
Text(
text = feed.name,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
style = MaterialTheme.typography.titleMedium.copy(fontSize = 18.sp),
)
},
navigationIcon = {
FeedbackIconButton(
imageVector = Icons.AutoMirrored.Rounded.ArrowBack,
Expand All @@ -83,10 +97,6 @@ fun FlowPagePreview(
)
Spacer(modifier = Modifier.height(12.dp))

val preview = generateArticleWithFeedPreview()
val feed = preview.feed
val article = preview.article

ArticleItem(
modifier = Modifier,
feedName = feed.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun FlowPageStylePage(
var filterBarStyleDialogVisible by remember { mutableStateOf(false) }
var filterBarPaddingDialogVisible by remember { mutableStateOf(false) }
var filterBarTonalElevationDialogVisible by remember { mutableStateOf(false) }
// var topBarTonalElevationDialogVisible by remember { mutableStateOf(false) }
var topBarTonalElevationDialogVisible by remember { mutableStateOf(false) }
var articleListTonalElevationDialogVisible by remember { mutableStateOf(false) }
var articleListReadIndicatorDialogVisible by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -108,14 +108,14 @@ fun FlowPageStylePage(
enabled = false,
onClick = {},
) {}
/* SettingItem(
SettingItem(
title = stringResource(R.string.tonal_elevation),
desc = "${topBarTonalElevation.value}dp",
onClick = {
topBarTonalElevationDialogVisible = true
},
) {}*/
// Tips(text = stringResource(R.string.tips_top_bar_tonal_elevation))
) {}
Tips(text = stringResource(R.string.tips_top_bar_tonal_elevation))
Spacer(modifier = Modifier.height(24.dp))
}

Expand Down Expand Up @@ -297,7 +297,7 @@ fun FlowPageStylePage(
filterBarTonalElevationDialogVisible = false
}

/* RadioDialog(
RadioDialog(
visible = topBarTonalElevationDialogVisible,
title = stringResource(R.string.tonal_elevation),
options = FlowTopBarTonalElevationPreference.values.map {
Expand All @@ -310,7 +310,7 @@ fun FlowPageStylePage(
}
) {
topBarTonalElevationDialogVisible = false
}*/
}

RadioDialog(
visible = articleListTonalElevationDialogVisible,
Expand Down

0 comments on commit 133c09d

Please sign in to comment.