-
-
Save uteke/b18b35cceea90a3f536ec22c5956d7ea to your computer and use it in GitHub Desktop.
UserListViewModel load users
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
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