Skip to content

Instantly share code, notes, and snippets.

@DevPicon
Created June 8, 2020 04:04
Show Gist options
  • Save DevPicon/c01e1ce96f8d89c2274e248247dfb1bd to your computer and use it in GitHub Desktop.
Save DevPicon/c01e1ce96f8d89c2274e248247dfb1bd to your computer and use it in GitHub Desktop.
package com.cornershopapp.shopper.android.ui
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
class Demo : ViewModel() {
fun login(name: String, pass: String
) = viewModelScope.launch {
val result = requestLogin(name, pass)
show(result)
}
private suspend fun requestLogin(
name: String, pass: String
): Either<Throwable, Boolean> = withContext(Dispatchers.IO) {
if (name.isNotBlank() && pass.isNotBlank()) {
return@withContext Either.Right(true)
} else {
return@withContext Either.Left(IllegalStateException("Invalid"))
}
}
fun show(result: Either<Throwable, Boolean>) {
// ...
}
}
sealed class Either<out A, out B> {
class Left<A>(val value: A) : Either<A, Nothing>()
class Right<B>(val value: B) : Either<Nothing, B>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment