Last active
February 24, 2019 21:59
-
-
Save aykuttasil/063671e6353b61954daeeef52c12eed5 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
inline fun <T> dependantLiveData( | |
vararg dependencies: LiveData<*>, | |
crossinline mapper: () -> T? | |
) = MediatorLiveData<T>().also { mediatorLiveData -> | |
val observer = Observer<Any> { mediatorLiveData.value = mapper() } | |
dependencies.forEach { dependencyLiveData -> | |
mediatorLiveData.addSource(dependencyLiveData, observer) | |
} | |
} | |
sealed class LoginUiStates | |
data class LoginUiStateSuccessfulLogin(val user: FirebaseUser) : LoginUiStates() | |
data class LoginUiStateSuccessfulRegister(val dataOkDialog: DataOkDialog) : LoginUiStates() | |
data class LoginUiStateError(val dataOkDialog: DataOkDialog) : LoginUiStates() | |
open class LoginViewModel @Inject constructor(private val dataManager: DataManager) : | |
RxAwareViewModel() { | |
val liveUiStates = MutableLiveData<LoginUiStates>() | |
val liveSnackbar = SingleLiveEvent<String>() | |
val liveOkDialog = SingleLiveEvent<DataOkDialog>() | |
val liveEmail = MutableLiveData<String>() | |
val livePass = MutableLiveData<String>() | |
val displayName = dependantLiveData(liveEmail) { | |
liveEmail.value ?: "null" | |
} | |
open fun login( | |
email: String, | |
password: String | |
) { | |
disposables.add(dataManager.loginUser(email, password) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({ | |
Logger.i("Kullanıcı girişi başarılı.") | |
liveSnackbar.value = "${it?.email} ile oturum açıldı." | |
liveUiStates.value = LoginUiStateSuccessfulLogin(it) | |
}, { | |
it.printStackTrace() | |
val dialog = DataOkDialog("SweetLoc", it?.message ?: "") {} | |
liveUiStates.value = LoginUiStateError(dialog) | |
}) | |
) | |
} | |
fun register( | |
email: String, | |
password: String | |
) { | |
disposables.add(dataManager.registerUser(email, password) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({ | |
Logger.i("Kullanıcı kaydı başarılı.") | |
liveSnackbar.value = "${it?.email} ile kayıt olundu." | |
liveUiStates.value = LoginUiStateSuccessfulLogin(it) | |
}, { | |
it.printStackTrace() | |
val dialog = DataOkDialog("SweetLoc", it?.message ?: "") {} | |
liveUiStates.value = LoginUiStateError(dialog) | |
}) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment