Skip to content

Instantly share code, notes, and snippets.

@stepango
Last active December 28, 2016 11:22
Show Gist options
  • Save stepango/992df45505d4ebb5cab55ed1c74bee25 to your computer and use it in GitHub Desktop.
Save stepango/992df45505d4ebb5cab55ed1c74bee25 to your computer and use it in GitHub Desktop.
class RegisterViewModel(
naviComponent: NaviComponent
) : ViewModel by ViewModelImpl(naviComponent) {
val name = ObservableString("")
val email = ObservableString("")
val password = ObservableString("")
var location: LocationModel? = null
val locationName = ObservableString("")
val isNameValid = ObservableBoolean(false)
val isEmailValid = ObservableBoolean(false)
val isPasswordValid = ObservableBoolean(false)
val flags = listOf(
isNameValid.observe(),
isEmailValid.observe(),
isPasswordValid.observe()
)
val isFormValid = ObservableBoolean(false)
init {
addListener(Event.ACTIVITY_RESULT) {
location = it.data()?.getParcelableExtra<LocationModel>(LOCATION_RESULT)
locationName.set(location?.let { it.city_name ?: it.country_name } ?: "")
}
composite += name.observe()
.map(::validateName)
.setTo(isNameValid)
.subscribe()
composite += email.observe()
.map(::validateEmail)
.setTo(isEmailValid)
.subscribe()
composite += password.observe()
.map(::validatePassword)
.setTo(isPasswordValid)
.subscribe()
composite += combineLatest(flags) { it }
.map { it.all { it == true } }
.setTo(isFormValid)
.subscribe()
}
fun userRegistrationModel() = CreateUserModel(
email = email.get(),
password = password.get(),
name = name.get(),
location_id = location?.id ?: 0
)
override fun args(): Map<String, Any> = mapOf(CREATE_USER_MODEL to userRegistrationModel())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment