Clean Android Architecture by Alexandru Dumbravan

Clean Android Architecture by Alexandru Dumbravan

Author:Alexandru Dumbravan
Language: eng
Format: epub
Publisher: Packt Publishing Pvt Ltd
Published: 2022-05-20T00:00:00+00:00


class LocationException(cause: Throwable) :

UseCaseException(cause)

class UnknownException(cause: Throwable) :

UseCaseException(cause)

companion object {

fun extractException(throwable: Throwable):

UseCaseException {

return if (throwable is UseCaseException)

throwable else UnknownException(throwable)

}

}

}

Here, we have created a sealed class that will have as subclasses a dedicated error for each entity, plus an unknown error that will deal with errors we haven't accounted for, and a companion method that will check a Throwable object and return UnknownException for any Throwable that isn't UseCaseException. We will need to make sure that the error is propagated through the flow stream, but first, we can combine the entity for success with the entity for error to ensure that the consumer of the use case will not need to check the type of Throwable again and make a cast. We can do this with the following approach:

sealed class Result<out T : Any> {

data class Success<out T : Any>(val data: T) :

Result<T>()

class Error(val exception: UseCaseException) :

Result<Nothing>()

}

Here, we defined a Result sealed class, which will have two subclasses for success and error. The Success class will hold the relevant data for the use case, and the Error class will contain the exceptions defined before. The Error class can be further expanded if needed to hold data as well as the error if we want to display the cached or persisted data as a placeholder. We can now modify the use case to incorporate the Result class and the error state:

class GetUserWithLocationUseCase(

private val userRepository: UserRepository,

private val locationRepository: LocationRepository

) {

fun getUser(id: String) = combine(

userRepository.getUser(id),

locationRepository.getLocation(id)

) { user, location ->

Result.Success(UserWithLocation(user, location)) as

Result<UserWithLocation>

}.flowOn(Dispatchers.IO)

.catch {

emit(Result.Error(UseCaseException.

extractException(it)))

}

}

Here, we return Result.Success, which will hold the UserWithLocation object if no errors occur, and add use the catch operator to emit Result.Error with UseCaseException that occurred while fetching the data. Because these operations will repeat for multiple use cases, we can use abstraction to create a template for how each use case behaves and let the implementations deal with only processing the necessary data. An example might look like the following:

abstract class UseCase<T : Any, R : Any>(private val dispatcher: CoroutineDispatcher) {

fun execute(input: T): Flow<Result<R>> =

executeData(input)

.map {

Result.Success(it) as Result<R>

}

.flowOn(dispatcher)

.catch {

emit(Result.Error(UseCaseException.

extractException(it)))

}

internal abstract fun executeData(input: T): Flow<R>

}

In the preceding example, we have defined an abstract class that will contain the execute method, which will invoke the abstract executeData method and then map the result of that method into a Result object, followed by setting the flow on a CoroutineDispatcher, and finally, handling the errors in the catch operator. The implementation of this will look like the following. Note that the internal keyword for the executeData method will only make the method accessible in the current module. This is because we only want the execute method to be called by the users of this use case:

class GetUserWithLocationUseCase(

dispatcher: CoroutineDispatcher,

private val userRepository: UserRepository,

private val locationRepository: LocationRepository

) : UseCase<String, UserWithLocation>(dispatcher) {

override fun executeData(input: String):

Flow<UserWithLocation> {

return combine(

userRepository.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.