Skip to content

Instantly share code, notes, and snippets.

@cloudbank
Last active May 27, 2020 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudbank/9da806a0475e6fdcf1f55e18ad864c53 to your computer and use it in GitHub Desktop.
Save cloudbank/9da806a0475e6fdcf1f55e18ad864c53 to your computer and use it in GitHub Desktop.
//1) inject a datasource and repository, which is passed to the constructor of the ViewModel
@Singleton
class LoginDataSource @Inject constructor(){
private var mAuth: FirebaseAuth. ...//
class LoginRepository @Inject constructor(val dataSource: LoginDataSource) { ...
//2) use the Executor overload from the DataSource:
var executor: ThreadPoolExecutor = ThreadPoolExecutor(
numCores * 2, numCores * 2,
60L, TimeUnit.SECONDS, LinkedBlockingQueue<Runnable>()
)
fun login(email: String, password: String, liveData: MutableLiveData<Result>) {
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(
executor,
OnCompleteListener<AuthResult?> { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success")
val user = mAuth.currentUser
val result = Result()
result.data = user as FirebaseUser
result.status = Status.SUCCESS
liveData.postValue(result)
//3) The LiveData is passed in from the VM:
fun login(email: String, password: String, liveData: MutableLiveData<Result>) {
// and observed in the fragment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment