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
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.
Deep Learning with Python by François Chollet(12525)
Hello! Python by Anthony Briggs(9870)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9760)
The Mikado Method by Ola Ellnestam Daniel Brolund(9751)
Dependency Injection in .NET by Mark Seemann(9296)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8261)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7744)
Grails in Action by Glen Smith Peter Ledbrook(7670)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7520)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6754)
Microservices with Go by Alexander Shuiskov(6520)
Practical Design Patterns for Java Developers by Miroslav Wengner(6419)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6395)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6381)
Angular Projects - Third Edition by Aristeidis Bampakos(5777)
The Art of Crafting User Stories by The Art of Crafting User Stories(5308)
NetSuite for Consultants - Second Edition by Peter Ries(5251)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5070)
Kotlin in Action by Dmitry Jemerov(5022)
