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
class ConsumableLiveData<T>(var consume: Boolean = false) : MutableLiveData<T>() { | |
private val pending = AtomicBoolean(false) | |
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) { | |
super.observe(owner, Observer<T> { | |
if(consume){ | |
if(pending.compareAndSet(true,false)) observer.onChanged(it) | |
}else{ | |
observer.onChanged(it) |
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
fun <A, B> DoubleTriggerLiveData(a: LiveData<A>, b: LiveData<B>): LiveData<Pair<A, B>> { | |
return MediatorLiveData<Pair<A, B>>().apply { | |
var lastA: A? = null | |
var lastB: B? = null | |
fun update() { | |
val localLastA = lastA | |
val localLastB = lastB | |
if (localLastA != null && localLastB != null) | |
this.value = Pair(localLastA, localLastB) |
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
class FieldValidator<T>(private var fieldObject: T) { | |
// Store the validation rules at a ValidationRule List, by fieldId | |
private val rules = ArrayList<Rule<T>>() | |
// Store the field error string, or null if validation pass | |
private val validations = MutableLiveData<String>() | |
// Get the LiveData<String> for the property | |
fun getValidation(): LiveData<String>? { |