Skip to content

Instantly share code, notes, and snippets.

@uteke
Last active September 1, 2023 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uteke/b18b35cceea90a3f536ec22c5956d7ea to your computer and use it in GitHub Desktop.
Save uteke/b18b35cceea90a3f536ec22c5956d7ea to your computer and use it in GitHub Desktop.
UserListViewModel load users
class UserListViewModel(
private val getUserListRepository: GetUserListRepository,
private val getUserContactRepository: GetUserContactRepository,
private val connectivityMonitor: ConnectivityMonitor,
private val resources: Resources,
private val tracker: Tracker,
private val logger: Logger,
): ViewModel() {
...
fun process(action: Action) {
when (action) {
is Action.Load -> load()
...
}
}
private fun load() {
viewModelScope.launch {
_viewState.update { currentState ->
currentState.copy(
isLoaderVisible = true,
isContentVisible = false,
isEmptyVisible = false,
isErrorVisible = false,
)
}
when (val result = GetUserListRepository()) {
is Success -> {
tracker.trackEvent(CatalogFetched)
_viewState.update { currentState ->
currentState.copy(
isLoaderVisible = false,
isEmptyVisible = false,
isErrorVisible = false,
isUserListVisible = true,
users = result.users.map { it.toUserState() }
)
}
}
is Empty -> {
tracker.trackEvent(EmptyCatalogFetched)
logger.error(catalogResources.emptyCatalogMessage)
_viewState.update { currentState ->
currentState.copy(
isLoaderVisible = false,
isUserListVisible = false,
isEmptyVisible = true,
isErrorVisible = false,
)
}
}
is Failure -> {
tracker.trackEvent(CatalogFetchFailed)
logger.error(catalogResources.failedCatalogMessage(result.errorType))
_viewState.update { currentState ->
currentState.copy(
isLoaderVisible = false,
isUserListVisible = false,
isEmptyVisible = false,
isErrorVisible = true,
errorMessage = when(result.errorType) {
ErrorType.NoUser -> featureResources.noUserErrorMessage
ErrorType.ServerError -> featureResources.serverErrorMessage
else -> featureResources.genericErrorMessage
},
)
}
}
}
}
}
...
private fun UserDataModel.toUserState() = ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment