DON'T Do This Jetpack Compose Mistake 😅

preview_player
Показать описание
Follow for more Android & Kotlin tips 🙌
Рекомендации по теме
Комментарии
Автор

I taught I was a master class native Android Developer until Jetpack Compose came along

echoingtrends
Автор

Hm. Normally we would do it by having a "Content" composable, which takes the state and it allows us preview the "Content" only. That way the viewmodels aren't injected in the navigation, but instead in the screen composable, and the content gets the state.

MrSN
Автор

bro you literally learned me to do it like this in mvvm video!

glywyui
Автор

This is not a hard fast rule for the root compostable function view model can be passed in parameter it is just a single source of truth and pass down data to the lower composable function.
This rule is correct for the lower composable function.

Jeewanct
Автор

Great tip and greatly explained, looking forward to get some more !!

jeanbourgarel
Автор

I prefer to pass the view model to the view and have a flag in its constructor for instantiating it as a mock for testing.

jeyhey
Автор

Well, idk, looks wierd. In docs you read about hilt and how to provide it inside the screen, now you're not even allowed to use view model normally but to split it in state and functions. Kinda wierd what if you have so many of functions?
I dont usually preview the entire screen but rather some functions.

johnshepard
Автор

I heard the same problem for quite some time, thank mahn

francismwangi
Автор

can you show us the "HomeState" and "HomeEvent" class?

CorneliusJubril
Автор

Hi Philipp, can you advice how to handle events when you want to send them to the UI when you need a context, such as e.g on press of a button you want to open an Intent for mail app. I would handle a button click like you do here with viewModel:onEvent function but how I would send a action to the UI (if you do it via state object, do you have examples of how you model that). Much appreciated!

adrian
Автор

Hi, interesting! However, it looks like we could end up with a blotted navigation component?

thebatu
Автор

Thank you!! So where can we collect a flow data?

ywiglmu
Автор

You can create a route composable also

SahilTheExplorer
Автор

and if a child component needs that same state, you should do it in the same way, that looks really ugly, android needs to change this

haroldpepete
Автор

You dont need to preview screen level composables. You should use preview on miny pieces of the puzzle. Anyway you are hoisting the state down to these pieces so they dont have the viewmodel as parameter.

radu
Автор

Excellent, and is that the best way to add navigation composable on each composable screen on multi-module architecture?

imrkrly
Автор

nice, but if you start prototyping an actual home-screen you would need a view model 😊 this video holds true for unit testing navigation only not testing an api response that loads data to a view

pattmehta
Автор

guess i gotta edit some projects. thanks!

theevan
Автор

Can we make internal fun version of composable there we create val ViewModel and then call private fun of this composable to which we pass states and receive callbacks? Pretty much the same, but make nav more clean

kovshichek
Автор

Cool ❤ what about the hiltViewModel does it solve the problem?

MobileAcademys