Open luongvo opened 1 month ago
- All API executions are in the default empty
CoroutineContext
withviewModelScope.launch
. Should we execute them with theio
context?
The presentation should be main safe. It should be the method that actually makes the call to the endpoint or cache that should use a Dispatcher. In room, retrofit, or ktor that would be handled internally by their libraries. So in most cases you might not need to switch dispatchers.
- Why do we fetch the "survey list" in the
MainViewModel
of theMainActivity
? It's being fetched and handled in the HomeViewModel of HomeScreen already
I thought that was feature to fetch the survey when the app was starting i.e. splash screen so it uses the same usecase to fetch the survey list. However, only for logged in users. The reason I have added it to the HomeViewModel as that uses the same fetching usecase to fetch the survey, when landing on the homepage after logging in. However, if the user has logged in then it just passes this survey list to the homepage without making a second request. This could also be used for pull to refresh.
- Kotlin Flow is now a popular asynchronous data stream in Kotlin. Can I know why you haven't used Flow yet? All the data flow in the project is being executed and streamed by
suspended fun
with a lot of boilerplate code. Kotlin Flow and its operation likemap
,flatMap
,merge
,catch
, etc, is so powerful to optimize the code.
The calls to fetch use suspend for the use case. But handling the UI updates from the viewmodel uses compose state. I have in the passed used StateFlow and I find it easier to use the savedStateHandle with them. However, you can create a Flow from compose state using the snapshotFlow if you want to use those operators. For this project I decided to go with compose state.
- Why do we mix Compose components (UI) in the ViewModel layer (
mutableStateOf
-> Compose'sState
)? Do you think it's good for unit testing for ViewModel classes?
If I understand you correctly. You want to avoid having any compose related state free from the viewmodels? However, the unit testing can be done with this implementation as I have written unit test for this viewmodel. However, I used to use StateFlow for my projects. But for simple projects I might prefer to use compose state. However, this could be debatable based on the business requires and developer preference. Some developers might prefer compose state for developing and updating composables. I am not sure if this is the recommended best practice is from google
Kotlin Flow is now a popular asynchronous data stream in Kotlin. Can I know why you haven't used Flow yet? All the data flow in the project is being executed and streamed by
suspended fun
with a lot of boilerplate code. Kotlin Flow and its operation likemap
,flatMap
,merge
,catch
, etc, is so powerful to optimize the code. https://github.com/steve1rm/BusbyNimbleSurvey/blob/5b80ada6d22093fc66d36debe028eaa15cc4886e/presentation/src/main/java/me/androidbox/presentation/authentication/login/LoginViewModel.kt#L45-L65All API executions are in the default empty
CoroutineContext
withviewModelScope.launch
. Should we execute them with theio
context? https://github.com/steve1rm/BusbyNimbleSurvey/blob/5b80ada6d22093fc66d36debe028eaa15cc4886e/presentation/src/main/java/me/androidbox/presentation/authentication/login/LoginViewModel.kt#L39-L45Why do we mix Compose components (UI) in the ViewModel layer (
mutableStateOf
-> Compose'sState
)? Do you think it's good for unit testing for ViewModel classes? https://github.com/steve1rm/BusbyNimbleSurvey/blob/5b80ada6d22093fc66d36debe028eaa15cc4886e/presentation/src/main/java/me/androidbox/presentation/authentication/login/LoginViewModel.kt#L5-L21Why do we fetch the "survey list" in the
MainViewModel
of theMainActivity
? It's being fetched and handled in the HomeViewModel of HomeScreen already 🤔 https://github.com/steve1rm/BusbyNimbleSurvey/blob/5b80ada6d22093fc66d36debe028eaa15cc4886e/app/src/main/java/me/androidbox/busbynimblesurvey/MainViewModel.kt#L33-L44 https://github.com/steve1rm/BusbyNimbleSurvey/blob/5b80ada6d22093fc66d36debe028eaa15cc4886e/presentation/src/main/java/me/androidbox/presentation/home/HomeViewModel.kt#L35-L66