Skip to content

Commit

Permalink
Shared viewmodel added
Browse files Browse the repository at this point in the history
  • Loading branch information
v-shahzadahmad committed Sep 25, 2024
1 parent 932548a commit baad499
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 56 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ kotlin {
implementation(libs.coil.network.ktor)
implementation(libs.voyager.navigator)
implementation(libs.voyager.tab.navigator)
implementation(libs.voyager.screenmodel)
implementation(libs.lifecycle.viewmodel.compose)
implementation(libs.markdown.renderer)
api(libs.compose.window.size)
Expand Down
8 changes: 4 additions & 4 deletions composeApp/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import coil3.memory.MemoryCache
import coil3.request.CachePolicy
import coil3.request.crossfade
import coil3.util.DebugLogger
import di.HomeScreenModelProvider
import okio.FileSystem
import theme.TravelAppTheme
import ui.component.BottomMenuBar
Expand All @@ -26,19 +27,18 @@ import ui.screen.CartTab
import ui.screen.FavoriteTab
import ui.screen.HomeTab
import ui.screen.GeminiTab
import ui.viewmodel.HomeViewModel
import util.AnimateVisibility

@Composable
internal fun App(
viewModel: HomeViewModel = viewModel { HomeViewModel() }
) {
internal fun App() {
TravelAppTheme {

setSingletonImageLoaderFactory { context ->
getAsyncImageLoader(context)
}

val viewModel = HomeScreenModelProvider.homeScreenModel

val bottomNavBarVisibility by viewModel.bottomNavBarVisible.collectAsState()

TabNavigator(HomeTab) {
Expand Down
8 changes: 8 additions & 0 deletions composeApp/src/commonMain/kotlin/di/Koin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package di

import ui.viewmodel.HomeScreenModel

//import org.koin.core.context.startKoin
//import org.koin.dsl.module
//
Expand All @@ -14,3 +16,9 @@ package di
// )
// }
//}

object HomeScreenModelProvider {
val homeScreenModel: HomeScreenModel by lazy {
HomeScreenModel()
}
}
20 changes: 6 additions & 14 deletions composeApp/src/commonMain/kotlin/ui/screen/ArticleDetailScreen.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package ui.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
Expand All @@ -20,42 +15,39 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow
import di.HomeScreenModelProvider
import model.Article
import model.Destination
import theme.Black
import theme.BorderColor
import theme.White
import ui.component.ChildLayout
import ui.component.VerticalScrollLayout
import ui.component.article.ArticleBodyHeader
import ui.component.article.ArticleDescription
import ui.component.article.ArticleDestination
import ui.component.article.ArticleHeader
import ui.component.article.ArticleOther
import ui.component.article.ArticlePharagraphs
import ui.viewmodel.HomeViewModel
import ui.viewmodel.HomeScreenModel
import util.BOTTOM_NAV_SPACE

data class ArticleDetailScreen(val article: Article) : Screen {
@Composable
override fun Content() {
val screenModel = HomeScreenModelProvider.homeScreenModel
val navigator = LocalNavigator.currentOrThrow
ArticleDetailScreenView(navigator, article)
ArticleDetailScreenView(navigator = navigator, article = article, viewModel = screenModel)
}
}

@Composable
fun ArticleDetailScreenView(
navigator: Navigator,
article: Article,
viewModel: HomeViewModel = androidx.lifecycle.viewmodel.compose.viewModel { HomeViewModel() },
viewModel: HomeScreenModel,
) {
val rememberThumbnail = remember { mutableStateOf(article.thumbnail) }
Column(
Expand Down
9 changes: 6 additions & 3 deletions composeApp/src/commonMain/kotlin/ui/screen/CartTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.tab.TabOptions
import di.HomeScreenModelProvider
import model.Destination
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource
Expand All @@ -32,7 +34,7 @@ import travelbuddy.composeapp.generated.resources.menu_cart
import ui.component.CartCard
import ui.component.LoadItemAfterSafeCast
import ui.component.Tabx
import ui.viewmodel.HomeViewModel
import ui.viewmodel.HomeScreenModel
import util.BOTTOM_NAV_SPACE

data object CartTab : Tabx {
Expand All @@ -56,15 +58,16 @@ data object CartTab : Tabx {

@Composable
override fun Content() {
val screenModel = HomeScreenModelProvider.homeScreenModel
val navigator = LocalNavigator.currentOrThrow
CartScreenView(navigator)
CartScreenView(navigator = navigator, viewModel = screenModel)
}
}

@Composable
fun CartScreenView(
navigator: Navigator,
viewModel: HomeViewModel = androidx.lifecycle.viewmodel.compose.viewModel { HomeViewModel() },
viewModel: HomeScreenModel,
) {
val cartItems by viewModel.cartItems.collectAsState()

Expand Down
100 changes: 91 additions & 9 deletions composeApp/src/commonMain/kotlin/ui/screen/DestinationDetailScreen.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package ui.screen

import androidx.compose.foundation.Image
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandIn
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkOut
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
Expand All @@ -20,14 +27,16 @@ import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Card
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithCache
Expand All @@ -40,29 +49,31 @@ import androidx.compose.ui.text.toUpperCase
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.tab.LocalTabNavigator
import di.HomeScreenModelProvider
import model.Destination
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import theme.PrimaryColor
import theme.ReviewBodyBg
import theme.White
import theme.SecondTextColor
import theme.TextColor
import theme.ThirdTextColor
import theme.Yellow
import travelbuddy.composeapp.generated.resources.Res
import travelbuddy.composeapp.generated.resources.arrow_forward
import travelbuddy.composeapp.generated.resources.choose_date
import travelbuddy.composeapp.generated.resources.choose_meeting_point
import travelbuddy.composeapp.generated.resources.ci_location
import travelbuddy.composeapp.generated.resources.estimation
import travelbuddy.composeapp.generated.resources.facilities
import travelbuddy.composeapp.generated.resources.preview
import travelbuddy.composeapp.generated.resources.ratting
import travelbuddy.composeapp.generated.resources.star
import travelbuddy.composeapp.generated.resources.type
import travelbuddy.composeapp.generated.resources.via
import ui.component.DestinationDetailChipItem
Expand All @@ -73,23 +84,24 @@ import ui.component.DestinationDetailSubItemDivider
import ui.component.DestinationDetailSubItemRatting
import ui.component.PrimaryButton
import ui.component.destinationDetailHeader
import ui.viewmodel.HomeViewModel
import ui.viewmodel.HomeScreenModel
import util.BOTTOM_NAV_SPACE
import util.ImageItem

data class DestinationDetailScreen(val destination: Destination) : Screen {
@Composable
override fun Content() {
val screenModel = HomeScreenModelProvider.homeScreenModel
val navigator = LocalNavigator.currentOrThrow
DestinationDetailScreenView(navigator, destination)
DestinationDetailScreenView(navigator = navigator, destination = destination, viewModel = screenModel)
}
}

@Composable
fun DestinationDetailScreenView(
navigator: Navigator,
destination: Destination,
viewModel: HomeViewModel = androidx.lifecycle.viewmodel.compose.viewModel { HomeViewModel() },
viewModel: HomeScreenModel,
) {
val rememberThumbnail = remember { mutableStateOf(destination.thumbnail) }
Column(
Expand Down Expand Up @@ -123,6 +135,15 @@ fun DestinationDetailScreenView(
paddingValues = PaddingValues(start = 25.dp, top = 36.dp, end = 25.dp, bottom = 36.dp),
onClick = { viewModel.addToCart(destination) }
)

GeminiRoundButton(
viewModel = viewModel,
modifier = Modifier
.align(Alignment.CenterHorizontally),
logo = Res.drawable.arrow_forward,
destination = destination,
navigator = navigator
)
}
}

Expand Down Expand Up @@ -319,3 +340,64 @@ fun contentSection(destination: Destination, onImageClicked: (String) -> Unit) {
DestinationDetailFacilityItem(destination.facilities)
}
}

@Composable
fun GeminiRoundButton(
viewModel: HomeScreenModel,
modifier: Modifier = Modifier,
logo: DrawableResource,
destination: Destination,
navigator: Navigator
) {
var isExpanded by remember { mutableStateOf(true) }
val navigateToGemini by viewModel.navigateToGemini.collectAsState()
if (navigateToGemini.first) {
LocalNavigator.current?.pop()
LocalTabNavigator.current.current = GeminiTab
}

// Animate width change
val buttonWidth by animateDpAsState(
targetValue = if (isExpanded) 220.dp else 100.dp,
animationSpec = tween(durationMillis = 300)
)

BoxWithConstraints(
modifier = modifier
.padding(end = 16.dp, bottom = 36.dp)
.width(buttonWidth)
.background(color = PrimaryColor, shape = RoundedCornerShape(8.dp))
.clickable {
if (isExpanded) {
viewModel.navigateToGimini(Pair(true, destination))
}
isExpanded = !isExpanded
},
contentAlignment = Alignment.Center
) {
Row(
modifier = Modifier.padding(vertical = 8.dp, horizontal = 4.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
Icon(
painter = painterResource(logo),
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(24.dp)
)
AnimatedVisibility(
visible = isExpanded,
enter = fadeIn() + expandIn(),
exit = fadeOut() + shrinkOut()
) {
Text(
text = "Explore with Gemini",
color = Color.White,
style = MaterialTheme.typography.bodyLarge,
fontSize = 16.sp
)
}
}
}
}
8 changes: 5 additions & 3 deletions composeApp/src/commonMain/kotlin/ui/screen/FavoriteTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.Navigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.navigator.tab.TabOptions
import di.HomeScreenModelProvider
import model.Destination
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.StringResource
Expand All @@ -34,7 +35,7 @@ import travelbuddy.composeapp.generated.resources.fav_tab
import travelbuddy.composeapp.generated.resources.favorite_destination
import travelbuddy.composeapp.generated.resources.menu_fav
import ui.component.Tabx
import ui.viewmodel.HomeViewModel
import ui.viewmodel.HomeScreenModel
import util.BOTTOM_NAV_SPACE

data object FavoriteTab : Tabx {
Expand Down Expand Up @@ -65,16 +66,17 @@ data object FavoriteTab : Tabx {
object FavoriteScreen : Screen {
@Composable
override fun Content() {
val screenModel = HomeScreenModelProvider.homeScreenModel
val navigator = LocalNavigator.currentOrThrow
FavoriteScreenView(navigator)
FavoriteScreenView(navigator = navigator, viewModel = screenModel)
}
}


@Composable
fun FavoriteScreenView(
navigator: Navigator,
viewModel: HomeViewModel = androidx.lifecycle.viewmodel.compose.viewModel { HomeViewModel() },
viewModel: HomeScreenModel,
) {
val favorites by viewModel.favorites.collectAsState()

Expand Down
Loading

0 comments on commit baad499

Please sign in to comment.