Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply suggestions from Compose rules #1199

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
Expand Down Expand Up @@ -62,7 +64,7 @@ internal object LauncherScreen : Parcelable, Screen {
internal fun LauncherScreen(
state: LauncherScreen.State,
modifier: Modifier = Modifier,
onFullyDrawn: () -> Unit,
reportFullyDrawn: () -> Unit,
) {
val eventSink = state.eventSink

Expand All @@ -74,7 +76,7 @@ internal fun LauncherScreen(
items(state.entries) { entry ->
LauncherItem(
item = entry,
modifier = modifier.padding(24.dp),
modifier = Modifier.padding(24.dp),
onClick = OnClick("launcher_goto", mapOf("screen" to entry.screen)) {
eventSink(NavEvent.GoTo(entry.screen))
},
Expand All @@ -83,8 +85,10 @@ internal fun LauncherScreen(
}
}

val latestReportFullyDrawn by rememberUpdatedState(reportFullyDrawn)

LaunchedEffect(Unit) {
onFullyDrawn()
latestReportFullyDrawn()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal class KeyNavigationDecoration(
args = args,
backStackDepth = backStackDepth,
modifier = modifier
.focusOnPlacement()
.focusOnPlacement(remember { FocusRequester() })
.onPreviewKeyUp(
predicate = predicate,
action = onBackInvoked,
Expand All @@ -43,13 +43,12 @@ internal class KeyNavigationDecoration(
}
}

@Composable
private fun Modifier.focusOnPlacement(): Modifier {
val focusRequester = remember { FocusRequester() }
return focusRequester(focusRequester).onPlaced { focusRequester.requestFocus() }
private fun Modifier.focusOnPlacement(
focusRequester: FocusRequester,
): Modifier = focusRequester(focusRequester).onPlaced {
focusRequester.requestFocus()
}

@Composable
private fun Modifier.onPreviewKeyUp(
predicate: (KeyEvent) -> Boolean = { true },
action: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import androidx.compose.ui.unit.dp
@ExperimentalMaterial3Api
public fun BottomSheetScaffold(
sheetContent: @Composable ColumnScope.() -> Unit,
modifier: Modifier = Modifier,
topBar: @Composable () -> Unit = { },
bottomBar: @Composable () -> Unit = { },
floatingActionButton: @Composable () -> Unit = { },
showDragHandle: Boolean = true,
isExpanded: Boolean = false,
modifier: Modifier = Modifier,
content: @Composable (PaddingValues) -> Unit,
) {
val bottomSheetState = rememberStandardBottomSheetState(SheetValue.Hidden, skipHiddenState = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import androidx.compose.runtime.Composable
import androidx.paging.PagingData
import io.ashdavies.paging.LazyPagingItems
import io.ashdavies.paging.collectAsLazyPagingItems
import io.ashdavies.party.tooling.DayNightPreview
import io.ashdavies.party.tooling.PreviewDayNight
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

private val DroidconEvents = listOf(AndroidMakers, DroidconBerlin, DroidconLondon)

@Composable
@DayNightPreview
@PreviewDayNight
internal fun EventsScreenPreview(data: List<Event> = DroidconEvents) {
EventsScreen(EventsScreen.State(lazyPagingItems(flowOf(PagingData.from(data)))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import io.ashdavies.party.tooling.DayNightPreview
import io.ashdavies.party.tooling.PreviewDayNight
import kotlinx.collections.immutable.persistentListOf

@Composable
@DayNightPreview
@PreviewDayNight
@OptIn(ExperimentalFoundationApi::class)
internal fun GalleryGridPreview() {
GalleryPreviewTheme {
Expand All @@ -23,16 +23,16 @@ internal fun GalleryGridPreview() {
galleryScreenStateItem(state = SyncState.SYNCING),
galleryScreenStateItem(state = SyncState.SYNCED),
),
isSelecting = true,
onSelect = { },
onExpand = { },
onSelect = { },
isSelecting = true,
)
}
}
}

@Composable
@DayNightPreview
@PreviewDayNight
internal fun GalleryBottomSheetPreview() {
GalleryPreviewTheme {
GallerySheetContent(eventSink = { })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import io.ashdavies.identity.IdentityState
import io.ashdavies.party.profile.ProfileActionButton
import io.ashdavies.party.tooling.DayNightPreview
import io.ashdavies.party.tooling.PreviewDayNight

@Composable
@DayNightPreview
@PreviewDayNight
@OptIn(ExperimentalMaterial3Api::class)
internal fun HomeTopAppBarPreview() {
MaterialPreviewTheme {
Expand All @@ -27,10 +27,10 @@ internal fun HomeTopAppBarPreview() {
}

@Composable
@DayNightPreview
@PreviewDayNight
internal fun HomeBottomSheetPreview() {
MaterialPreviewTheme {
HomeBottomBar(HomeScreen)
HomeBottomBar()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import androidx.compose.ui.tooling.preview.Preview

@Preview(name = "Day")
@Preview(name = "Night", uiMode = Configuration.UI_MODE_NIGHT_YES)
internal annotation class DayNightPreview
internal annotation class PreviewDayNight
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ internal fun EventsScreen(

items(itemCount) { index ->
EventSection(
event = state.pagingItems.getOrNull(index),
modifier = Modifier.animateItemPlacement(),
emphasis = when (index) {
0 -> TextEmphasis.Significant
1 -> TextEmphasis.Moderate
else -> TextEmphasis.Standard
},
modifier = Modifier.animateItemPlacement(),
event = state.pagingItems.getOrNull(index),
)
}
}
Expand All @@ -124,9 +124,9 @@ private enum class TextEmphasis {
@Composable
@ExperimentalMaterialApi
private fun EventSection(
event: Event?,
modifier: Modifier = Modifier,
emphasis: TextEmphasis,
modifier: Modifier = Modifier,
event: Event? = null,
) {
Card(
modifier = modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal fun GalleryScreen(
when {
state.itemList.isEmpty() -> {
Box(
modifier = modifier.fillMaxSize(),
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center,
content = { Text("Empty") },
)
Expand All @@ -148,10 +148,10 @@ internal fun GalleryScreen(
else -> {
GalleryGrid(
itemList = state.itemList.toImmutableList(),
isSelecting = isSelecting,
modifier = Modifier.padding(paddingValues),
onExpand = { eventSink(GalleryScreen.Event.Selection.Expand(it)) },
onSelect = { eventSink(GalleryScreen.Event.Selection.Toggle(it)) },
modifier = Modifier.padding(paddingValues),
isSelecting = isSelecting,
)

if (state.expandedItem != null) {
Expand Down Expand Up @@ -207,11 +207,11 @@ private fun GalleryExpandedItem(
@ExperimentalFoundationApi
internal fun GalleryGrid(
itemList: ImmutableList<GalleryScreen.State.StandardItem>,
columnCount: Int = DEFAULT_COLUMN_COUNT,
isSelecting: Boolean = false,
modifier: Modifier = Modifier,
onExpand: (Int) -> Unit,
onSelect: (Int) -> Unit,
modifier: Modifier = Modifier,
columnCount: Int = DEFAULT_COLUMN_COUNT,
isSelecting: Boolean = false,
) {
LazyVerticalGrid(
columns = GridCells.Fixed(columnCount),
Expand All @@ -223,10 +223,10 @@ internal fun GalleryGrid(
itemsIndexed(itemList) { index, item ->
GalleryItem(
item = item,
isSelecting = isSelecting,
modifier = Modifier.animateItemPlacement(),
onSelect = { onSelect(index) },
onExpand = { onExpand(index) },
modifier = Modifier.animateItemPlacement(),
isSelecting = isSelecting,
)
}
}
Expand All @@ -236,10 +236,10 @@ internal fun GalleryGrid(
@ExperimentalFoundationApi
internal fun GalleryItem(
item: GalleryScreen.State.StandardItem,
isSelecting: Boolean = false,
modifier: Modifier = Modifier,
onSelect: () -> Unit,
onExpand: () -> Unit,
modifier: Modifier = Modifier,
isSelecting: Boolean = false,
) {
val itemBorderRadius by animateDpAsState(if (item.isSelected) 12.dp else 8.dp)
val itemPadding by animateDpAsState(if (item.isSelected) 12.dp else 0.dp)
Expand Down Expand Up @@ -306,10 +306,10 @@ internal fun GalleryItem(

@Composable
private fun SelectedIndicator(
modifier: Modifier = Modifier,
surfaceColor: Color = MaterialTheme.colorScheme.surface,
onPrimaryContainerColor: Color = MaterialTheme.colorScheme.onPrimaryContainer,
iconPainter: Painter = rememberVectorPainter(Icons.Filled.CheckCircle),
modifier: Modifier = Modifier,
) {
Canvas(modifier) {
drawCircle(
Expand All @@ -327,9 +327,9 @@ private fun SelectedIndicator(

@Composable
private fun UnselectedIndicator(
modifier: Modifier = Modifier,
highlightColor: Color = Color.White.copy(alpha = 0.5F),
strokeWidth: Dp = 2.dp,
modifier: Modifier = Modifier,
) {
Canvas(modifier) {
drawCircle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
Expand All @@ -42,7 +42,7 @@ internal fun SyncIndicator(isSyncing: Boolean, modifier: Modifier = Modifier) {
val tint by animateColorAsState(if (isSyncing) Color.Orange else Color.LightGreen)
val scale by animateFloatAsState(if (isSyncing) SYNCING_SCALE else 1f)

var currentRotation by remember { mutableStateOf(0f) }
var currentRotation by remember { mutableFloatStateOf(0f) }
val rotation = remember { Animatable(currentRotation) }

LaunchedEffect(isSyncing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.vector.ImageVector
Expand Down Expand Up @@ -63,14 +64,15 @@ internal object HomeScreen : Parcelable, Screen {
internal fun HomeScreen(
state: HomeScreen.State,
modifier: Modifier = Modifier,
onFullyDrawn: () -> Unit,
reportFullyDrawn: () -> Unit,
) {
val isProfileEnabled by booleanConfigAsState { isProfileEnabled() }
val isHomeEnabled by booleanConfigAsState { isHomeEnabled() }
val eventSink = state.eventSink

BottomSheetScaffold(
sheetContent = { GallerySheetContent({ }) },
modifier = modifier,
showDragHandle = false,
topBar = {
HomeTopBar(
Expand All @@ -95,7 +97,6 @@ internal fun HomeScreen(
},
floatingActionButton = { },
isExpanded = false,
modifier = modifier,
) { contentPadding ->
CircuitContent(
screen = state.screen,
Expand All @@ -106,16 +107,18 @@ internal fun HomeScreen(
)
}

val latestReportFullyDrawn by rememberUpdatedState(reportFullyDrawn)

LaunchedEffect(Unit) {
onFullyDrawn()
latestReportFullyDrawn()
}
}

@Composable
@ExperimentalMaterial3Api
internal fun HomeTopBar(
title: String = "Home",
modifier: Modifier = Modifier,
title: String = "Home",
actions: @Composable RowScope.() -> Unit = { },
scrollBehavior: TopAppBarScrollBehavior = enterAlwaysScrollBehavior(rememberTopAppBarState()),
) {
Expand All @@ -138,8 +141,8 @@ internal fun HomeTopBar(

@Composable
internal fun HomeBottomBar(
selected: Screen = HomeScreen,
modifier: Modifier = Modifier,
selected: Screen = HomeScreen,
onClick: (Screen) -> Unit = { },
) {
BottomAppBar(modifier) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.ashdavies.dominion

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.slack.circuit.runtime.Navigator

@Composable
internal fun BoxSetListPresenter(
navigator: Navigator,
boxSetStore: BoxSetStore,
): DominionScreen.BoxSetList.State {
var isLoading by remember { mutableStateOf(true) }
val boxSetList by produceState(emptyList<BoxSet>()) {
value = boxSetStore()
isLoading = false
}

return DominionScreen.BoxSetList.State(
boxSetList = boxSetList,
isLoading = isLoading,
) { event ->
when (event) {
is DominionScreen.BoxSetList.Event.ShowBoxSet -> {
navigator.goTo(DominionScreen.BoxSetDetails(event.boxSet.title))
}
}
}
}
Loading
Loading