-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: promote scaffold as parent container and uses dynamic route path (
#5) * implement dynamic param by arguments * created ScreenScaffold for unification
- Loading branch information
1 parent
3a94a0b
commit 7141d2a
Showing
4 changed files
with
133 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,44 @@ | ||
@file:Suppress("MemberVisibilityCanBePrivate") | ||
|
||
package id.gdg.app.ui | ||
|
||
import id.gdg.app.ui.state.EventDetailUiModel | ||
import kotlin.jvm.JvmInline | ||
|
||
sealed class AppRouter(val route: String) { | ||
sealed class AppRouter(val route: Path) { | ||
|
||
data object Splash : AppRouter(SplashRoute) | ||
data object Onboarding : AppRouter(OnboardingRoute) | ||
data object Home : AppRouter(HomeRoute) | ||
data object Splash : AppRouter(Path("splash")) | ||
data object Onboarding : AppRouter(Path("onboarding")) | ||
data object Home : AppRouter(Path("home")) | ||
data object EventDetail : AppRouter(Path("detail/{${ARG_EVENT_ID}}")) | ||
|
||
data class EventDetail(val eventId: Int) : AppRouter(EventDetailRoute) | ||
override fun toString(): String { | ||
return "gdg://$route" | ||
} | ||
|
||
companion object { | ||
val SplashRoute get() = "splash" | ||
val OnboardingRoute get() = "onboarding" | ||
const val ARG_EVENT_ID = "eventId" | ||
} | ||
} | ||
|
||
val HomeRoute get() = "home" | ||
@JvmInline | ||
value class Path(private val value: String) { | ||
|
||
// SAMPAH | ||
val EventDetailRoute get() = "detail/{$ArgumentEventId}" | ||
fun constructEventDetailRoute(eventId: String) = EventDetailRoute | ||
.replace("{$ArgumentEventId}", eventId) | ||
fun param(param: String): String { | ||
return param(arrayOf(param)) | ||
} | ||
|
||
val ArgumentEventId get() = "eventId" | ||
fun param(args: Array<String>): String { | ||
var result = value | ||
val regex = Regex("\\{([^}]+)\\}") | ||
val matches = regex.findAll(value) | ||
if (matches.any()) { | ||
matches.forEachIndexed { index, match -> | ||
result = result.replace(match.value, args[index]) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
override fun toString() = value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package id.gdg.app.ui | ||
|
||
import androidx.compose.material3.Surface | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.unit.dp | ||
import id.gdg.app.AppViewModel | ||
import id.gdg.ui.TwoPanelScaffold | ||
import id.gdg.ui.TwoPanelScaffoldAnimationSpec | ||
import id.gdg.ui.androidx.compose.material3.windowsizeclass.CommonWindowSizeClass | ||
import id.gdg.ui.androidx.compose.material3.windowsizeclass.CommonWindowWidthSizeClass | ||
import id.gdg.ui.androidx.compose.material3.windowsizeclass.LocalWindowSizeClass | ||
|
||
@Composable | ||
fun ScreenScaffold( | ||
viewModel: AppViewModel, | ||
mainScreen: @Composable (AppViewModel, onEventDetailClicked: (String) -> Unit) -> Unit, | ||
detailScreen: @Composable (AppViewModel, String) -> Unit, | ||
navigateToDetailScreen: (String) -> Unit | ||
) { | ||
var selectedEventId by rememberSaveable { mutableStateOf("") } | ||
|
||
val windowSizeClazz: CommonWindowSizeClass = LocalWindowSizeClass.current | ||
var shouldPanelOpened: Boolean? by rememberSaveable { mutableStateOf(null) } | ||
var panelVisibility by rememberSaveable { mutableStateOf(shouldPanelOpened != null) } | ||
|
||
LaunchedEffect(windowSizeClazz) { | ||
shouldPanelOpened = shouldPanelOpened.takeIf { | ||
windowSizeClazz.widthSizeClass != CommonWindowWidthSizeClass.Compact | ||
} | ||
|
||
panelVisibility = shouldPanelOpened != null | ||
} | ||
|
||
TwoPanelScaffold( | ||
panelVisibility = panelVisibility, | ||
animationSpec = TwoPanelScaffoldAnimationSpec( | ||
finishedListener = { fraction -> if (fraction == 1f) shouldPanelOpened = null } | ||
), | ||
body = { | ||
mainScreen(viewModel) { | ||
// If the screen size is compact (or mobile device screen size), then | ||
// navigate to detail page with router. Otherwise, render the [panel]. | ||
if (windowSizeClazz.widthSizeClass == CommonWindowWidthSizeClass.Compact) { | ||
navigateToDetailScreen(it) | ||
return@mainScreen | ||
} | ||
|
||
selectedEventId = it | ||
shouldPanelOpened = true | ||
panelVisibility = true | ||
} | ||
}, | ||
panel = { | ||
Surface(tonalElevation = 1.dp) { | ||
if (shouldPanelOpened != null) { | ||
detailScreen(viewModel, selectedEventId) | ||
} | ||
} | ||
} | ||
) | ||
} |
70 changes: 2 additions & 68 deletions
70
app/src/commonMain/kotlin/id/gdg/app/ui/screen/MainScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters