Ink
05/17/2021, 2:26 PM@Parcelize
data class Post(
val author: String,
val title: String,
val description: String,
val reactionNumber: Int
) : Parcelable
In my `navigationController`;
composable(
"PostDetails/{post}",
arguments = listOf(
navArgument("post") { type = NavType.ParcelableType(Post::class.java)}
)
) {
PostDetailsScreen(it.arguments?.get("post") as Post)
}
Now I get error: Parcelables don't support default values.
Should I pass object using GSON ot this way is corretly?
I heard that NavType.ParcelableType didn't work but right now it's implemented and developers can use it (?)Ian Lake
05/17/2021, 2:29 PMInk
05/17/2021, 2:35 PMJavier
05/17/2021, 2:40 PMInk
05/17/2021, 2:55 PMLazyColumn {
items(postList) { post ->
Post(
onPostClick = { mainViewModel.actions.offer(MainAction.SelectPost(post)) },
post
)
}
}
My viewModel has action channel (I want to put here destination screen with possibly data)
class MainViewModel : ViewModel() {
val state = MainViewState()
val actions = Channel<MainAction>()
init {
viewModelScope.launch {
try {
actions.receiveAsFlow().collect { action ->
when (action) {
is MainAction.SelectPost -> {
state.destinationScreen.value = DestinationScreen.POST_DETAILS(action.data)
}
}
}
} catch (e: Exception) {
}
}
}
}
enum class DestinationScreen(val data: Any? = null){
DEFAULT,
POST_DETAILS
}
class MainViewState {
val destinationScreen = mutableStateOf(DestinationScreen.DEFAULT)
}
sealed class MainAction {
data class SelectPost(val data: Post) : MainAction()
}
I have MainViewState where I put destinationScreen from action with data.
In my @Compose screen I observe state and react for specific screenDestination:
@Composable
fun AppNavigationHost() {
val mainViewModel: MainViewModel = viewModel()
val appNavController = rememberNavController()
NavHost(
navController = appNavController,
startDestination = Screens.HOME.title
) {
when (mainViewModel.state.destinationScreen.value) {
DestinationScreen.POST_DETAILS -> {
composable(DestinationScreen.POST_DETAILS.name) {
PostDetailsScreen(//Post object here)
}
}
else -> {}
}
}
}
The problem is I don't know hot to set DestinationScreen with data to my state.destinationScreen.value
Should I use Pair()?Ian Lake
05/17/2021, 11:41 PMIan Lake
05/17/2021, 11:42 PMPost
instance given that unique ID, preferably in some observable format (i.e., a getPost(postId: String): Flow<Post>
) that you can then call collectAsState()
onIan Lake
05/17/2021, 11:43 PMgetPosts(): Flow<List<Post>>
type of method)Adam Powell
05/18/2021, 1:31 PMAdam Powell
05/18/2021, 1:37 PM