Skip to content

Instantly share code, notes, and snippets.

@NezSpencer
Created March 16, 2019 18:46
Show Gist options
  • Save NezSpencer/57573dbee631e58cf9a65c1ef820ad37 to your computer and use it in GitHub Desktop.
Save NezSpencer/57573dbee631e58cf9a65c1ef820ad37 to your computer and use it in GitHub Desktop.
Activity viewModel
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.text.TextUtils
import com.google.firebase.auth.FirebaseAuth
class SubViewModel : ViewModel(), FirebaseAuth.AuthStateListener{
private val mAuth : FirebaseAuth = FirebaseAuth.getInstance()
private val _authLivedata : MutableLiveData<AuthState> = MutableLiveData()
val authLivedata : LiveData<AuthState>
get() = _authLivedata
init {
mAuth.addAuthStateListener(this)
}
override fun onAuthStateChanged(p0: FirebaseAuth) {
_authLivedata.postValue(AuthState(Progress.DONE, p0))
}
override fun onCleared() {
super.onCleared()
mAuth.removeAuthStateListener(this)
}
fun signInUser(email : String, password : String){
if (!TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)) {
_authLivedata.postValue(AuthState(Progress.LOADING, null))
mAuth.signInWithEmailAndPassword(email, password)
}
}
fun createAccount(email : String, password: String){
if (!TextUtils.isEmpty(email) || !TextUtils.isEmpty(password)) {
_authLivedata.postValue(AuthState(Progress.LOADING, null))
mAuth.createUserWithEmailAndPassword(email, password)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment