-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex
committed
Dec 8, 2021
1 parent
e52981b
commit 2653d21
Showing
6 changed files
with
122 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
...viewmodel/src/main/java/com/alexdeww/reactiveviewmodel/core/property/ConfirmationEvent.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.alexdeww.reactiveviewmodel.core.property | ||
|
||
import androidx.annotation.MainThread | ||
import androidx.collection.ArraySet | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import androidx.lifecycle.Observer | ||
import com.alexdeww.reactiveviewmodel.core.property.ConfirmationEvent.ObserverWrapper | ||
import io.reactivex.rxjava3.core.Flowable | ||
import io.reactivex.rxjava3.core.Observable | ||
import io.reactivex.rxjava3.functions.Consumer | ||
|
||
class ConfirmationEvent<T : Any> internal constructor(debounceInterval: Long? = null) { | ||
|
||
private sealed class EventType { | ||
data class Pending(val data: Any) : EventType() | ||
object Confirmed : EventType() | ||
} | ||
|
||
private val eventState = State<EventType>(EventType.Confirmed, debounceInterval) | ||
|
||
internal val consumer: Consumer<T> = Consumer { | ||
eventState.consumer.accept(EventType.Pending(it)) | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
internal val observable: Observable<T> = eventState.observable | ||
.ofType(EventType.Pending::class.java) | ||
.map { it.data as T } | ||
|
||
val liveData: LiveData<T> by lazy { ConfirmationEventLiveData() } | ||
val viewFlowable: Flowable<T> by lazy { observable.toViewFlowable() } | ||
val isConfirmed: Boolean get() = eventState.value === EventType.Confirmed | ||
|
||
fun confirm() { | ||
eventState.consumer.accept(EventType.Confirmed) | ||
} | ||
|
||
private inner class ConfirmationEventLiveData : MediatorLiveData<T>() { | ||
|
||
private val observers = ArraySet<ConfirmationEvent.ObserverWrapper<T>>() | ||
|
||
@MainThread | ||
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) { | ||
val wrapper = ObserverWrapper(observer) | ||
observers.add(wrapper) | ||
super.observe(owner, wrapper) | ||
} | ||
|
||
@MainThread | ||
override fun removeObserver(observer: Observer<in T>) { | ||
if (observers.remove(observer as Observer<*>)) { | ||
super.removeObserver(observer) | ||
return | ||
} | ||
|
||
val iterator = observers.iterator() | ||
while (iterator.hasNext()) { | ||
val wrapper = iterator.next() | ||
if (wrapper.observer == observer) { | ||
iterator.remove() | ||
super.removeObserver(observer) | ||
break | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun onActive() { | ||
super.onActive() | ||
addSource(eventState.liveData) { value = (it as? EventType.Pending)?.data as T } | ||
} | ||
|
||
override fun onInactive() { | ||
removeSource(eventState.liveData) | ||
super.onInactive() | ||
} | ||
} | ||
|
||
private class ObserverWrapper<T>(val observer: Observer<in T>) : Observer<T> { | ||
override fun onChanged(t: T?) { | ||
if (t != null) observer.onChanged(t) | ||
} | ||
} | ||
|
||
} |