Last active
April 1, 2019 16:08
-
-
Save ShinichiroFunatsu/a24a0941ac5540838802eaf0dc6407b0 to your computer and use it in GitHub Desktop.
ViewModel + LiveData + Kotlin Coroutines
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
class MainActivity : AppCompatActivity() { | |
val mainViewModel: MainViewModel by lazy { | |
ViewModelProviders.of(this).get(MainViewModel::class.java) | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mainViewModel.getUsr() | |
mainViewModel.addUser.observe(this, Observer { | |
when (it) { | |
is PushResult.Success -> text.text = "OK" | |
is PushResult.Error -> Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show() | |
} | |
}) | |
mainViewModel.updateUser.observe(this, Observer { | |
when (it) { | |
is PullResult.Success -> text.text = "OK2" | |
is PullResult.Error -> Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show() | |
} | |
}) | |
} | |
fun onClick() { | |
mainViewModel.addUser(mapOf("name" to "Nobita")) | |
} | |
} | |
class MainViewModel(val userRep: UserRepository) : ViewModel() { | |
val addUser = MutableLiveData<PushResult>() | |
val updateUser = MutableLiveData<PullResult>() | |
fun addUser(user: Map<String, Any>) { | |
viewModelScope.launch(IO) { | |
addUser.postValue(userRep.push(user)) | |
} | |
} | |
fun getUsr() { | |
viewModelScope.launch(IO) { | |
updateUser.postValue(userRep.pull()) | |
} | |
} | |
} | |
class UserRepository(val db: FirebaseFirestore) { | |
// FIXME Map<String, Any> to User class | |
suspend fun push(user: Map<String, Any>): PushResult { | |
return suspendCoroutine { continuation -> | |
db.collection("users") | |
.add(user) | |
.addOnCompleteListener { | |
when (it.isSuccessful) { | |
true -> continuation.resume( | |
PushResult.Success( | |
checkNotNull(it.result) | |
) | |
) | |
false -> continuation.resume( | |
PushResult.Error( | |
checkNotNull(it.exception) | |
) | |
) | |
} | |
} | |
} | |
} | |
suspend fun pull(): PullResult { | |
return suspendCoroutine { continuation -> | |
db.collection("users") | |
.get() | |
.addOnCompleteListener { | |
when (it.isSuccessful) { | |
true -> continuation.resume( | |
PullResult.Success( | |
checkNotNull(it.result) | |
) | |
) | |
false -> continuation.resume( | |
PullResult.Error( | |
checkNotNull(it.exception) | |
) | |
) | |
} | |
} | |
} | |
} | |
} | |
sealed class PushResult { | |
data class Success(val id: DocumentReference) : PushResult() | |
data class Error(val e: Throwable) : PushResult() | |
} | |
sealed class PullResult { | |
data class Success(val id: QuerySnapshot) : PullResult() | |
data class Error(val e: Throwable) : PullResult() | |
} |
Author
ShinichiroFunatsu
commented
Apr 1, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment