Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/killswitch' into feature…
Browse files Browse the repository at this point in the history
…/mirego-base-setup-klint
  • Loading branch information
OlivierPineau committed Nov 7, 2023
2 parents dabdbf0 + 383a583 commit 803ff3b
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AndroidImageProvider : VMDImageProvider {
SharedImageResource.emptyPageIcon -> R.drawable.baseline_question_mark_24
SharedImageResource.errorPageIcon -> R.drawable.baseline_warning_24
SharedImageResource.imagePlaceholder -> R.drawable.baseline_image_24
SharedImageResource.closeIcon -> TODO()
SharedImageResource.closeIcon -> R.drawable.baseline_close_24
}

else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import com.google.accompanist.placeholder.placeholder
import com.google.accompanist.placeholder.shimmer
import com.mirego.kmp.boilerplate.app.ui.theme.ShimmerBackground
import com.mirego.kmp.boilerplate.app.ui.theme.ShimmerHighlight
import com.mirego.trikot.viewmodels.declarative.properties.VMDColor

fun Modifier.loading(isLoading: Boolean) = this.then(
placeholder(
visible = isLoading,
highlight = PlaceholderHighlight.shimmer(highlightColor = Color.ShimmerHighlight),
color = Color.ShimmerBackground
)
)
)

fun VMDColor.toColor() = Color(red, green, blue)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.mirego.kmp.boilerplate.app.ui.navigation

import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import com.mirego.kmp.boilerplate.app.ui.projectdetails.ProjectDetailsView
import com.mirego.kmp.boilerplate.viewmodel.navigation.NavigationRoute
import com.mirego.kmp.boilerplate.viewmodel.navigation.NavigationViewModel

@Composable
fun BoilerplateNavHost(
navController: NavHostController,
startDestination: String,
navigationViewModel: NavigationViewModel,
content: @Composable () -> Unit
) = NavHost(
navController = navController,
startDestination = startDestination,
enterTransition = { fadeIn(tween(500)) },
exitTransition = { fadeOut(tween(500)) }
) {
composable(startDestination) {
content()
}
composable(NavigationRoute.ProjectDetails.NAME) {
NavigableContent(navigationViewModel)
}
}

@Composable
private fun NavigableContent(navigationViewModel: NavigationViewModel) {
Box(modifier = Modifier.fillMaxSize()) {
VMDNavigableContent(navigationViewModel = navigationViewModel) { navigationRoute ->
when (navigationRoute) {
is NavigationRoute.ProjectDetails -> ProjectDetailsView(projectDetailsViewModel = navigationRoute.viewModel)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mirego.kmp.boilerplate.app.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.navigation.compose.rememberNavController
import com.mirego.kmp.boilerplate.viewmodel.navigation.NavigationViewModel

@Composable
fun NavigationView(navigationViewModel: NavigationViewModel, content: @Composable () -> Unit) {
if (LocalInspectionMode.current) {
content()
return
}

val navController = rememberNavController()
VMDNavigationView(
navigationViewModel = navigationViewModel,
navController = navController
) { startDestination ->
BoilerplateNavHost(
navController = navController,
startDestination = startDestination,
navigationViewModel = navigationViewModel,
content = content
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mirego.kmp.boilerplate.app.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.mirego.kmp.mirego.trikot.viewmodels.declarative.navigation.VMDNavigationRoute
import com.mirego.kmp.mirego.trikot.viewmodels.declarative.navigation.VMDNavigationViewModel

/**
* When using VMDNavigationView, wrap the different navigation destinations in a VMDNavigableContent
*
* @param navigationViewModel The VMDNavigationViewModel
* @param content The wrapped navigation content. The block will receive the navigationRoute, as well as a flag to indicate if the composition is done in the context of an exit transition.
*/
@Composable
fun <T : VMDNavigationRoute> VMDNavigableContent(
navigationViewModel: VMDNavigationViewModel<T>,
content: @Composable (navigationRoute: T) -> Unit
) {
val route: T? by remember(key1 = navigationViewModel) {
mutableStateOf(navigationViewModel.navigationRoute)
}
val navigationRoute = route ?: return
content(navigationRoute)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.mirego.kmp.boilerplate.app.ui.navigation

import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.navigation.NavDestination
import androidx.navigation.NavHostController
import androidx.navigation.NavOptionsBuilder
import com.mirego.kmp.boilerplate.viewmodel.navigation.NavigationViewModel
import com.mirego.kmp.mirego.trikot.viewmodels.declarative.navigation.VMDNavigationRoute
import com.mirego.kmp.mirego.trikot.viewmodels.declarative.navigation.VMDNavigationViewModel
import com.mirego.trikot.viewmodels.declarative.compose.extensions.observeAsState
import kotlinx.coroutines.flow.MutableStateFlow

private const val ROOT_ROUTE = "ROOT"

/**
* For views backed by a VMDNavigationViewModel, wrap your content with VMDNavigationView to handle navigation.
*
* @param navigationViewModel The VMDNavigationViewModel
* @param navController The NavHostController that controls the navigation. Note that it is possible to use custom implementations (for instance if a navigation library is used).
* @param navOptionsBuilder The options to use for navigation
* @param navHost The navHost containing the different navigation destinations. Note that it is possible to use custom implementations (for instance if a navigation library is used).
*/
@Composable
fun <T : VMDNavigationRoute> VMDNavigationView(
navigationViewModel: VMDNavigationViewModel<T>,
navController: NavHostController,
navOptionsBuilder: NavOptionsBuilder.() -> Unit = {
launchSingleTop = true
},
navHost: @Composable (startDestination: String) -> Unit
) {
navHost(startDestination = ROOT_ROUTE)

NavigationHandler(
navigationViewModel = navigationViewModel,
navController = navController,
navOptionsBuilder = navOptionsBuilder
)
}

@Composable
private fun <T : VMDNavigationRoute> NavigationHandler(
navigationViewModel: VMDNavigationViewModel<T>,
navController: NavHostController,
navOptionsBuilder: NavOptionsBuilder.() -> Unit
) {
val navigationRoute by navigationViewModel.observeAsState(navigationViewModel::navigationRoute, navigationViewModel.navigationRoute)
val currentRoute = navController.currentDestination

when (val navigation = getNavigation(navigationRoute, currentRoute)) {
is VMDNavigation.POP -> navController.popBackStack()
is VMDNavigation.LAUNCH -> navController.navigate(
route = navigation.destinationRoute,
builder = navOptionsBuilder
)
is VMDNavigation.NONE -> Unit
}
val lifecycleEventState = MutableStateFlow(Lifecycle.Event.ON_START)
val observer = LifecycleEventObserver { _, event ->
lifecycleEventState.value = event
}
val lifecycleOwner = LocalLifecycleOwner.current

lifecycleOwner.lifecycle.addObserver(observer)

DisposableEffect(lifecycleOwner) {
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
val state = lifecycleEventState.collectAsState()

key(state.value) {
BackHandler(
enabled = navigationRoute != null && (navigationRoute?.viewModel as? NavigationViewModel)?.navigationRoute == null
) {
navigationRoute?.resetBlock?.invoke()
}
}
}

private fun getNavigation(
navigationRoute: VMDNavigationRoute?,
currentNavDestination: NavDestination?
): VMDNavigation = when {
navigationRoute == null && !currentNavDestination.isRoot -> VMDNavigation.POP
navigationRoute != null && currentNavDestination.isRoot -> VMDNavigation.LAUNCH(navigationRoute.name)
else -> VMDNavigation.NONE
}

private val NavDestination?.isRoot: Boolean
get() = this == null || this.route == null || this.route == ROOT_ROUTE

private sealed class VMDNavigation {
object NONE : VMDNavigation()
object POP : VMDNavigation()
data class LAUNCH(
val destinationRoute: String
) : VMDNavigation()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package com.mirego.kmp.boilerplate.app.ui.projectdetails

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import com.mirego.kmp.boilerplate.app.ui.common.Const.padding
import com.mirego.kmp.boilerplate.app.ui.common.loading
import com.mirego.kmp.boilerplate.app.ui.common.toColor
import com.mirego.kmp.boilerplate.app.ui.theme.TextSize
import com.mirego.kmp.boilerplate.app.ui.theme.TextWeight
import com.mirego.kmp.boilerplate.app.ui.theme.style
import com.mirego.kmp.boilerplate.viewmodel.projectdetails.ProjectDetailsRoot
import com.mirego.trikot.viewmodels.declarative.compose.viewmodel.LocalImage
import com.mirego.trikot.viewmodels.declarative.compose.viewmodel.PlaceholderState
import com.mirego.trikot.viewmodels.declarative.compose.viewmodel.VMDImage
import com.mirego.trikot.viewmodels.declarative.properties.VMDImageResource

@Composable
fun ProjectDetailsContentView(content: ProjectDetailsRoot.Content) {
val configuration = LocalConfiguration.current
val screenWidth = configuration.screenWidthDp.dp
val textColor = content.textColor.toColor()
Box(
modifier = Modifier
.fillMaxSize()
.navigationBarsPadding()
) {
VMDImage(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = screenWidth * 1.25f)
.align(Alignment.TopCenter),
viewModel = content.image,
contentScale = ContentScale.FillWidth,
placeholder = { placeholderImageResource: VMDImageResource, _: PlaceholderState ->
ImagePlaceholder(
placeholderImageResource = placeholderImageResource,
color = textColor
)
}
)

Column(
modifier = Modifier
.align(Alignment.BottomStart)
.padding(horizontal = padding)
.padding(bottom = padding),
) {
Text(
modifier = Modifier.loading(content.isLoading),
text = content.title,
style = style(TextSize.LARGE_TITLE, TextWeight.BOLD),
color = textColor
)

Text(
modifier = Modifier
.padding(top = 24.dp)
.loading(content.isLoading),
text = content.subtitle,
style = style(TextSize.TITLE1, TextWeight.SEMI_BOLD),
color = textColor
)

Box(
modifier = Modifier
.padding(top = 24.dp)
.height(1.dp)
.fillMaxWidth()
.background(textColor)
)

Column(
modifier = Modifier.padding(top = 32.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
modifier = Modifier.loading(content.isLoading),
text = content.projectType.first,
style = style(TextSize.SUB_HEADLINE, TextWeight.SEMI_BOLD),
color = textColor
)

Text(
modifier = Modifier.loading(content.isLoading),
text = content.projectType.second,
style = style(TextSize.SUB_HEADLINE, TextWeight.REGULAR),
color = textColor,
maxLines = 2
)
}

Column(
modifier = Modifier.padding(top = 16.dp),
verticalArrangement = Arrangement.spacedBy(4.dp)
) {
Text(
modifier = Modifier.loading(content.isLoading),
text = content.releaseYear.first,
style = style(TextSize.SUB_HEADLINE, TextWeight.SEMI_BOLD),
color = textColor
)

Text(
modifier = Modifier.loading(content.isLoading),
text = content.releaseYear.second,
style = style(TextSize.SUB_HEADLINE, TextWeight.REGULAR),
color = textColor,
maxLines = 2
)
}
}
}
}

@Composable
private fun ImagePlaceholder(placeholderImageResource: VMDImageResource, color: Color) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(300.dp),
contentAlignment = Alignment.Center
) {
LocalImage(
modifier = Modifier.size(96.dp),
imageResource = placeholderImageResource,
contentScale = ContentScale.FillWidth,
colorFilter = ColorFilter.tint(color)
)
}
}
Loading

0 comments on commit 803ff3b

Please sign in to comment.