Skip to content

Commit

Permalink
Migrate from context receivers to extension receiver and explicit par…
Browse files Browse the repository at this point in the history
…am. (#264)
  • Loading branch information
ychescale9 authored Jan 4, 2025
1 parent 0f0fd59 commit c33f6b7
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class KSActivity : ComponentActivity() {
when (it) {
is NavDestination.Main -> {
MainScreen(
animatedVisibilityScope = this@AnimatedContent,
selectedNavItem = selectedNavItem,
onSelectedNavItemChanged = { item -> selectedNavItem = item },
homeListState = homeListState,
Expand Down Expand Up @@ -117,6 +118,7 @@ class KSActivity : ComponentActivity() {

is NavDestination.ContentViewer -> {
ContentViewerScreen(
animatedVisibilityScope = this@AnimatedContent,
boundsKey = it.boundsKey,
topBarBoundsKey = it.topBarBoundsKey,
saveButtonElementKey = it.saveButtonElementKey,
Expand All @@ -129,6 +131,7 @@ class KSActivity : ComponentActivity() {

is NavDestination.KotlinWeeklyIssue -> {
KotlinWeeklyIssueScreen(
animatedVisibilityScope = this@AnimatedContent,
boundsKey = it.boundKey,
topBarBoundsKey = it.topBarBoundsKey,
titleElementKey = it.titleElementKey,
Expand All @@ -142,6 +145,7 @@ class KSActivity : ComponentActivity() {

is NavDestination.TalkingKotlinEpisode -> {
TalkingKotlinEpisodeScreen(
animatedVisibilityScope = this@AnimatedContent,
boundsKey = it.boundsKey,
topBarBoundsKey = it.topBarBoundsKey,
playerElementKey = it.playerElementKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ import io.github.reactivecircus.kstreamlined.android.foundation.designsystem.fou
import io.github.reactivecircus.kstreamlined.kmp.model.feed.FeedItem
import kotlin.math.absoluteValue

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
fun MainScreen(
fun SharedTransitionScope.MainScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
selectedNavItem: NavItemKey,
onSelectedNavItemChanged: (NavItemKey) -> Unit,
homeListState: LazyListState,
Expand All @@ -61,6 +61,7 @@ fun MainScreen(
when (it) {
NavItemKey.Home.ordinal -> {
HomeScreen(
animatedVisibilityScope = animatedVisibilityScope,
listState = homeListState,
onViewItem = { item -> onViewItem(item, NavItemKey.Home) },
modifier = Modifier.pagerScaleTransition(it, pagerState)
Expand All @@ -69,6 +70,7 @@ fun MainScreen(

NavItemKey.Saved.ordinal -> {
SavedForLaterScreen(
animatedVisibilityScope = animatedVisibilityScope,
listState = savedListState,
onViewItem = { item -> onViewItem(item, NavItemKey.Saved) },
modifier = Modifier.pagerScaleTransition(it, pagerState)
Expand All @@ -87,38 +89,40 @@ fun MainScreen(
)
}

NavigationIsland(
modifier = Modifier
.navigationBarsPadding()
.padding(8.dp)
.align(Alignment.BottomCenter)
.renderInSharedTransitionScopeOverlay(
zIndexInOverlay = 1f
with(animatedVisibilityScope) {
NavigationIsland(
modifier = Modifier
.navigationBarsPadding()
.padding(8.dp)
.align(Alignment.BottomCenter)
.renderInSharedTransitionScopeOverlay(
zIndexInOverlay = 1f
)
.animateEnterExit(
enter = fadeIn() + slideInVertically(
tween(delayMillis = 200, easing = LinearOutSlowInEasing)
) { it * 2 },
exit = fadeOut(),
),
) {
NavigationIslandItem(
selected = selectedNavItem == NavItemKey.Home,
icon = KSIcons.Kotlin,
contentDescription = "Home",
onClick = {
onSelectedNavItemChanged(NavItemKey.Home)
},
)
.animateEnterExit(
enter = fadeIn() + slideInVertically(
tween(delayMillis = 200, easing = LinearOutSlowInEasing)
) { it * 2 },
exit = fadeOut(),
),
) {
NavigationIslandItem(
selected = selectedNavItem == NavItemKey.Home,
icon = KSIcons.Kotlin,
contentDescription = "Home",
onClick = {
onSelectedNavItemChanged(NavItemKey.Home)
},
)
NavigationIslandDivider()
NavigationIslandItem(
selected = selectedNavItem == NavItemKey.Saved,
icon = KSIcons.Bookmarks,
contentDescription = "Saved",
onClick = {
onSelectedNavItemChanged(NavItemKey.Saved)
},
)
NavigationIslandDivider()
NavigationIslandItem(
selected = selectedNavItem == NavItemKey.Saved,
icon = KSIcons.Bookmarks,
contentDescription = "Saved",
onClick = {
onSelectedNavItemChanged(NavItemKey.Saved)
},
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ import io.github.reactivecircus.kstreamlined.kmp.presentation.contentviewer.Cont
import io.github.reactivecircus.kstreamlined.kmp.presentation.contentviewer.ContentViewerUiState
import io.github.reactivecircus.kstreamlined.android.feature.common.R as commonR

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
public fun ContentViewerScreen(
public fun SharedTransitionScope.ContentViewerScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
boundsKey: String,
topBarBoundsKey: String,
saveButtonElementKey: String,
Expand All @@ -85,12 +85,12 @@ public fun ContentViewerScreen(
.background(KSTheme.colorScheme.background)
.sharedBounds(
rememberSharedContentState(key = boundsKey),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
) {
val context = LocalContext.current
TopNavBar(
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
boundsKey = topBarBoundsKey,
title = "",
contentPadding = WindowInsets.statusBars.asPaddingValues(),
Expand Down Expand Up @@ -122,7 +122,7 @@ public fun ContentViewerScreen(
onClick = { eventSink(ContentViewerUiEvent.ToggleSavedForLater) },
modifier = Modifier.sharedElement(
rememberSharedContentState(key = saveButtonElementKey),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
zIndexInOverlay = 1f,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ import io.github.reactivecircus.kstreamlined.kmp.presentation.home.HomeUiState
import kotlinx.coroutines.delay
import io.github.reactivecircus.kstreamlined.android.feature.common.R as commonR

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
public fun HomeScreen(
public fun SharedTransitionScope.HomeScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
listState: LazyListState,
onViewItem: (FeedItem) -> Unit,
modifier: Modifier = Modifier,
Expand All @@ -77,6 +77,7 @@ public fun HomeScreen(
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val eventSink = viewModel.eventSink
HomeScreen(
animatedVisibilityScope = animatedVisibilityScope,
listState = listState,
onViewItem = onViewItem,
uiState = uiState,
Expand All @@ -86,9 +87,9 @@ public fun HomeScreen(
ReportDrawnWhen { uiState !is HomeUiState.Loading }
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
internal fun HomeScreen(
internal fun SharedTransitionScope.HomeScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
listState: LazyListState,
onViewItem: (FeedItem) -> Unit,
uiState: HomeUiState,
Expand All @@ -102,7 +103,7 @@ internal fun HomeScreen(
.background(KSTheme.colorScheme.background),
) {
TopNavBar(
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
boundsKey = "Bounds/Home/TopBar",
titleElementKey = "Element/Home/TopBar/Title",
title = stringResource(id = R.string.title_home),
Expand Down Expand Up @@ -147,25 +148,24 @@ internal fun HomeScreen(
}

is HomeUiState.Content -> {
with(this@AnimatedVisibilityScope) {
ContentUi(
listState = listState,
items = state.feedItems,
showTransientError = state.hasTransientError,
onItemClick = onViewItem,
eventSink = eventSink,
)
}
ContentUi(
animatedVisibilityScope = animatedVisibilityScope,
listState = listState,
items = state.feedItems,
showTransientError = state.hasTransientError,
onItemClick = onViewItem,
eventSink = eventSink,
)
}
}
}
}
}
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
private fun ContentUi(
private fun SharedTransitionScope.ContentUi(
animatedVisibilityScope: AnimatedVisibilityScope,
listState: LazyListState,
items: List<HomeFeedItem>,
showTransientError: Boolean,
Expand Down Expand Up @@ -224,9 +224,9 @@ private fun ContentUi(
.animateItem()
.sharedBounds(
rememberSharedContentState(key = "Bounds/Home/${item.id}"),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
saveButtonElementKey = "Element/Home/${item.id}/saveButton",
)
}
Expand All @@ -242,7 +242,7 @@ private fun ContentUi(
.animateItem()
.sharedBounds(
rememberSharedContentState(key = "Bounds/Home/${item.id}"),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
)
}
Expand All @@ -258,9 +258,9 @@ private fun ContentUi(
.animateItem()
.sharedBounds(
rememberSharedContentState(key = "Bounds/Home/${item.id}"),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
saveButtonElementKey = "Element/Home/${item.id}/saveButton",
)
}
Expand All @@ -276,9 +276,9 @@ private fun ContentUi(
.animateItem()
.sharedBounds(
rememberSharedContentState(key = "Bounds/Home/${item.id}"),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
cardElementKey = "Element/Home/${item.id}/player",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ import io.github.reactivecircus.kstreamlined.kmp.presentation.kotlinweeklyissue.
import io.github.reactivecircus.kstreamlined.kmp.presentation.kotlinweeklyissue.KotlinWeeklyIssueUiState
import io.github.reactivecircus.kstreamlined.android.feature.common.R as commonR

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
public fun KotlinWeeklyIssueScreen(
public fun SharedTransitionScope.KotlinWeeklyIssueScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
boundsKey: String,
topBarBoundsKey: String,
titleElementKey: String,
Expand All @@ -85,6 +85,7 @@ public fun KotlinWeeklyIssueScreen(
val context = LocalContext.current
val title = stringResource(id = R.string.title_kotlin_weekly_issue, issueNumber)
KotlinWeeklyIssueScreen(
animatedVisibilityScope = animatedVisibilityScope,
topBarBoundsKey = topBarBoundsKey,
titleElementKey = titleElementKey,
id = id,
Expand All @@ -100,14 +101,14 @@ public fun KotlinWeeklyIssueScreen(
eventSink = eventSink,
modifier = modifier.sharedBounds(
rememberSharedContentState(key = boundsKey),
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
),
)
}

context(SharedTransitionScope, AnimatedVisibilityScope)
@Composable
internal fun KotlinWeeklyIssueScreen(
internal fun SharedTransitionScope.KotlinWeeklyIssueScreen(
animatedVisibilityScope: AnimatedVisibilityScope,
topBarBoundsKey: String,
titleElementKey: String,
id: String,
Expand All @@ -126,7 +127,7 @@ internal fun KotlinWeeklyIssueScreen(
.background(KSTheme.colorScheme.background),
) {
TopNavBar(
animatedVisibilityScope = this@AnimatedVisibilityScope,
animatedVisibilityScope = animatedVisibilityScope,
boundsKey = topBarBoundsKey,
titleElementKey = titleElementKey,
title = title,
Expand Down
Loading

0 comments on commit c33f6b7

Please sign in to comment.