Created
March 16, 2019 18:46
-
-
Save NezSpencer/57573dbee631e58cf9a65c1ef820ad37 to your computer and use it in GitHub Desktop.
Activity viewModel
This file contains hidden or 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
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