Skip to content

Instantly share code, notes, and snippets.

View mahdit83's full-sized avatar
🔵
Focusing

Mahdi Tajik mahdit83

🔵
Focusing
View GitHub Profile
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)
@mahdit83
mahdit83 / Double trigger Live data
Created January 15, 2021 07:42
Double trigger Live data
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)
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>? {