![JAR search and dependency download from the Maven repository](/logo.png)
com.sefford.kor.usecases.UseCase.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kor-common Show documentation
Show all versions of kor-common Show documentation
Kor common platform to implement a clean architecture over your Android
application
The newest version!
package com.sefford.kor.usecases
import arrow.core.Either
import arrow.core.identity
import arrow.data.Try
import com.sefford.kor.errors.Error
import com.sefford.kor.repositories.utils.emptyErrorHandler
import com.sefford.kor.responses.Response
class UseCase private constructor(internal val logic: () -> R,
internal val postProcessor: (response: R) -> R = ::identity,
internal val cachePersistance: (response: R) -> R = ::identity,
internal val errorHandler: (ex: Throwable) -> E) {
fun execute(): Either {
return Try { cachePersistance(postProcessor(logic())) }
.fold({ Either.left(errorHandler(it)) },
{ Either.right(it) })
}
class Execute(internal val logic: () -> R) {
internal var postProcessor: (R) -> R = ::identity
internal var cachePersistance: (R) -> R = ::identity
internal var errorHandler: (Throwable) -> E = emptyErrorHandler()
fun process(postProcessor: (R) -> R): Execute {
this.postProcessor = postProcessor
return this
}
fun persist(cachePersistance: (R) -> R): Execute {
this.cachePersistance = cachePersistance
return this
}
fun onError(errorHandler: (Throwable) -> E): Execute {
this.errorHandler = errorHandler
return this;
}
fun build(): UseCase {
return UseCase(logic, postProcessor, cachePersistance, errorHandler)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy