Request manager is used to process any request from Viewmodel to respective Repositories.
-
Wrap the request inside the
RequestManager
object like below, hereT
can be class of interest for example User in this case,object : RequestManager<T>(params:Params) { override suspend fun createCall(): Either<BaseError, User> { return userRepository.login(params) } }.asFlow().collect { _tokenFlow.value = it }
-
params : It will check all the presentation validation like checking if email is present and is it valid or not or any parameters which needs to be verified before executing the use case.
-
createCall :- Any request/use-case which is suspend function can be executed here.
-
The result from request can be captured or collected as a flow using
asFlow
-
The result returns an instance of a Resource which contains data that can be used by the UI - this includes the ResourceState (LOADING,SUCCESS,ERROR), data to be used by the UI and a message if required (for error states).
-
To handle the exception for the each request we need to wrap the above RequestManager call in ExceptionHandler to manage the exception automatically. Request is handled via exception handler and the exception is received in the catch block.
exceptionHandler.handle { object : RequestManager<User>(params = params) { override suspend fun createCall(): Either<BaseError, User> { return userRepository.login(params) } }.asFlow().collect { _tokenFlow.value = it } }.catch<Exception> { false }.execute()
-
If you wish to handle the exception to make some custom behaviour to your UI you can return
true
else returnfalse
when u want the automatic exception handler behaviour defined in the viewmodel.To learn more about architecture see
ExceptionHandler