Skip to content

Instantly share code, notes, and snippets.

@diefferson
Created July 22, 2019 15:09
Show Gist options
  • Select an option

  • Save diefferson/c705c09986a6ef95c840a57d2884ff9d to your computer and use it in GitHub Desktop.

Select an option

Save diefferson/c705c09986a6ef95c840a57d2884ff9d to your computer and use it in GitHub Desktop.
Kotlin Use Case
class Authenticate(private val userRepository: UserRepository) :UseCase<Boolean, Authenticate.Params>(){
override suspend fun run(params: Params): Boolean {
return userRepository.authenticate(params.username, params.password)
}
class Params(val username:String, val password:String)
}
class Logout(private val userRepository: UserRepository) :UseCase<Unit, UseCase.None>(){
override suspend fun run(params: None) {
return userRepository.logout()
}
}
class MyViewModel(val authenticateCase:Authenticate,
val logoutCase:Logout): ViewModel(), CoroutineScope {
private val executionJob: Job by lazy { Job() }
override val coroutineContext: CoroutineContext by lazy {
Dispatchers.Default + executionJob
}
fun authenticate() {
authenticateCase(this, Authenticate.Params(email, password)).onSuccess {
//successs
}.onFailure {
//error
}
}
fun logout(){
logoutCase(this).onSuccess {
//sucess
}
}
override fun onCleared() {
super.onCleared()
executionJob.cancel()
}
}
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ResultAsync<T> private constructor( scope: CoroutineScope, action: suspend () -> T) {
internal var onSuccess : (T) -> Unit = {}
internal var onError : (e: Exception) -> Unit = {}
internal var onStatusChange : (status: AsyncStatus) -> Unit = {}
companion object {
fun <T> with( scope: CoroutineScope, action: suspend () -> T) : ResultAsync<T> {
return ResultAsync(scope, action)
}
}
init {
scope.launch {
withContext(Dispatchers.Main) { onStatusChange(AsyncStatus.RUNNING) }
try {
val result = action()
withContext(Dispatchers.Main){
onStatusChange(AsyncStatus.DONE)
onSuccess(result)
}
}catch (e:Exception){
withContext(Dispatchers.Main){
onStatusChange(AsyncStatus.ERROR)
onError(e)
}
}
}
}
}
fun <T> ResultAsync<T>.onFailure(action: (exception: Exception) -> Unit): ResultAsync<T> {
this.onError = action
return this
}
fun <T> ResultAsync<T>.onSuccess(action: (value: T) -> Unit): ResultAsync<T> {
this.onSuccess = action
return this
}
fun <T> ResultAsync<T>.onStatusChange(action: (AsyncStatus) -> Unit): ResultAsync<T> {
this.onStatusChange = action
return this
}
fun <T> CoroutineScope.asyncCatching( action: suspend () -> T): ResultAsync<T> {
return ResultAsync.with( this,action)
}
import kotlinx.coroutines.CoroutineScope
/**
* Abstract class for a UseCase
*/
abstract class UseCase<Type, in Params> where Type : Any? {
abstract suspend fun run(params: Params): Type
operator fun invoke(scope: CoroutineScope, params: Params) = scope.asyncCatching {
run(params)
}
operator fun invoke(scope: CoroutineScope) = scope.asyncCatching {
run(None() as Params)
}
class None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment