Created
June 8, 2020 04:04
-
-
Save DevPicon/c01e1ce96f8d89c2274e248247dfb1bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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