-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sunildhiman90/using_decompose_navigation
Using decompose for navigation
- Loading branch information
Showing
12 changed files
with
381 additions
and
26 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
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
43 changes: 43 additions & 0 deletions
43
composeApp/src/commonMain/kotlin/detail/DetailComponent.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package detail | ||
|
||
import HomeViewModel | ||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.value.MutableValue | ||
import com.arkivanov.decompose.value.Value | ||
import data.Product | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
import list.ListComponent | ||
|
||
interface DetailComponent { | ||
val model: Value<Model> | ||
|
||
fun onBackPressed() | ||
|
||
data class Model( | ||
val item: Product | ||
) | ||
} | ||
|
||
class DefaultDetailComponent( | ||
private val componentContext: ComponentContext, | ||
private val item: Product, | ||
private val onBack: () -> Unit, | ||
) : DetailComponent, ComponentContext by componentContext { | ||
|
||
private val _model = MutableValue(DetailComponent.Model(item = item)) | ||
override val model: Value<DetailComponent.Model> = _model | ||
override fun onBackPressed() { | ||
onBack() | ||
} | ||
|
||
init { | ||
CoroutineScope(Dispatchers.Default).launch { | ||
//IF we need to call api in Detail Component, call here | ||
} | ||
} | ||
|
||
|
||
} |
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,65 @@ | ||
package detail | ||
|
||
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.WindowInsets | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.windowInsetsPadding | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material.icons.filled.Build | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.key.Key.Companion.P | ||
import androidx.compose.ui.unit.dp | ||
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState | ||
|
||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun DetailContent( | ||
component: DetailComponent | ||
) { | ||
|
||
val product = component.model.subscribeAsState() | ||
|
||
Scaffold( | ||
modifier = Modifier, | ||
containerColor = MaterialTheme.colorScheme.surface, | ||
topBar = { | ||
TopAppBar( | ||
modifier = Modifier.windowInsetsPadding(WindowInsets(0.dp)), | ||
title = { Text("Product Detail") }, | ||
navigationIcon = { | ||
IconButton(onClick = { | ||
component.onBackPressed() | ||
}) { | ||
Icon(Icons.Default.ArrowBack, contentDescription = "Go Back") | ||
} | ||
} | ||
) | ||
} | ||
) { | ||
|
||
Column( | ||
verticalArrangement = Arrangement.Center, | ||
modifier = Modifier.fillMaxHeight().fillMaxWidth().padding(16.dp) | ||
) { | ||
Text("Product title: ${product.value.item.title}") | ||
Text("Product Desc: ${product.value.item.description}") | ||
} | ||
} | ||
} |
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,44 @@ | ||
package list | ||
|
||
import HomeViewModel | ||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.value.MutableValue | ||
import com.arkivanov.decompose.value.Value | ||
import data.Product | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
|
||
interface ListComponent { | ||
val model: Value<Model> | ||
|
||
fun onItemClicked(item: Product) | ||
|
||
data class Model( | ||
val items: List<Product> | ||
) | ||
} | ||
|
||
class DefaultListComponent( | ||
private val componentContext: ComponentContext, | ||
private val homeViewModel: HomeViewModel, | ||
private val onItemSelected: (item: Product) -> Unit | ||
) : ListComponent, ComponentContext by componentContext { | ||
|
||
private val _model = MutableValue<ListComponent.Model>(ListComponent.Model(items = emptyList())) | ||
override val model: Value<ListComponent.Model> = _model | ||
override fun onItemClicked(item: Product) { | ||
onItemSelected(item) | ||
} | ||
|
||
init { | ||
CoroutineScope(Dispatchers.Default).launch { | ||
homeViewModel.products.collect { | ||
_model.value = ListComponent.Model(items = it) | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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,18 @@ | ||
package list | ||
|
||
import AppContent | ||
import androidx.compose.runtime.Composable | ||
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState | ||
|
||
|
||
@Composable | ||
fun ListContent( | ||
component: ListComponent | ||
) { | ||
val products = component.model.subscribeAsState() | ||
|
||
AppContent(products) { | ||
component.onItemClicked(it) | ||
} | ||
|
||
} |
Oops, something went wrong.