Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Apr 29, 2022
1 parent 929b7b0 commit 38a1719
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 13 deletions.
2 changes: 1 addition & 1 deletion reactiveviewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ afterEvaluate {
release(MavenPublication) {
from components.release
groupId = 'com.alexdeww.reactiveviewmodel'
version = '2.4.3'
version = '2.4.4'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.alexdeww.reactiveviewmodel.core.property.Action
import com.alexdeww.reactiveviewmodel.core.property.ConfirmationEvent
import com.alexdeww.reactiveviewmodel.core.property.Event
import com.alexdeww.reactiveviewmodel.core.property.State
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.core.Maybe
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable

Expand All @@ -14,8 +17,16 @@ import io.reactivex.rxjava3.disposables.Disposable
* https://github.com/dmdevgo/RxPM
*/

const val DEF_PROGRESS_DEBOUNCE_INTERVAL = 500L //ms
const val DEF_ACTION_DEBOUNCE_INTERVAL = 300L //ms

abstract class ReactiveViewModel : ViewModel(), RvmComponent {

protected interface Invocable<T> {
val isExecute: Boolean
operator fun invoke(params: T)
}

private val disposableList = CompositeDisposable()

override fun onCleared() {
Expand All @@ -28,25 +39,108 @@ abstract class ReactiveViewModel : ViewModel(), RvmComponent {
return this
}

protected fun <T : Any> state(initValue: T? = null, debounceInterval: Long? = null): State<T> =
State(initValue, debounceInterval)
protected fun <T : Any> state(
initValue: T? = null,
debounceInterval: Long? = null
): State<T> = State(initValue, debounceInterval)

protected fun progressState(
initValue: Boolean? = null,
debounceInterval: Long = DEF_PROGRESS_DEBOUNCE_INTERVAL
): State<Boolean> = state(initValue, debounceInterval)

protected fun <T : Any> event(debounceInterval: Long? = null): Event<T> =
Event(debounceInterval)

protected fun eventNone(debounceInterval: Long? = null): Event<Unit> =
Event(debounceInterval)
event(debounceInterval)

protected fun <T : Any> confirmationEvent(debounceInterval: Long? = null): ConfirmationEvent<T> =
ConfirmationEvent(debounceInterval)
protected fun <T : Any> confirmationEvent(
debounceInterval: Long? = null
): ConfirmationEvent<T> = ConfirmationEvent(debounceInterval)

protected fun confirmationEventNone(debounceInterval: Long? = null): ConfirmationEvent<Unit> =
ConfirmationEvent(debounceInterval)
protected fun confirmationEventNone(
debounceInterval: Long? = null
): ConfirmationEvent<Unit> = confirmationEvent(debounceInterval)

protected fun <T : Any> action(debounceInterval: Long? = null): Action<T> =
Action(debounceInterval)

protected fun actionNone(debounceInterval: Long? = null): Action<Unit> =
Action(debounceInterval)
action(debounceInterval)

protected fun <T : Any> debouncedAction(
debounceInterval: Long = DEF_ACTION_DEBOUNCE_INTERVAL
): Action<T> = action(debounceInterval)

protected fun debouncedActionNone(
debounceInterval: Long = DEF_ACTION_DEBOUNCE_INTERVAL
): Action<Unit> = action(debounceInterval)

protected fun <T : Any, R : Any> Action<T>.bind(
transformChainBlock: Observable<T>.() -> Observable<R>
) {
observable
.transformChainBlock()
.applyDefaultErrorHandler()
.retry()
.subscribe()
.disposeOnCleared()
}

protected fun <T : Any> State<T>.bind(
transformChainBlock: Observable<T>.() -> Observable<out Any>
) {
observable
.transformChainBlock()
.applyDefaultErrorHandler()
.retry()
.subscribe()
.disposeOnCleared()
}

protected fun <T : Any> invocable(
block: (params: T) -> Completable
): Lazy<Invocable<T>> = lazy {
val action = action<T>()
var isExecute = false
action.bind {
this.switchMapCompletable { params -> block(params).bindProgress { isExecute = it } }
.toObservable<Unit>()
}
object : Invocable<T> {
override val isExecute: Boolean get() = isExecute
override fun invoke(params: T) = action.consumer.accept(params)
}
}

protected fun <T : Any> Observable<T>.untilOn(vararg action: Action<out Any>): Observable<T> =
takeUntil(Observable.merge(action.map { it.observable }))

protected fun <T : Any> Observable<T>.untilOn(vararg event: Event<out Any>): Observable<T> =
takeUntil(Observable.merge(event.map { it.observable }))

protected fun <T : Any> Observable<T>.untilOn(vararg observable: Observable<*>): Observable<T> =
takeUntil(Observable.merge(observable.toList()))

protected fun <T : Any> Maybe<T>.untilOn(vararg action: Action<*>): Maybe<T> =
takeUntil(Maybe.merge(action.map { it.observable.firstElement() }))

protected fun <T : Any> Maybe<T>.untilOn(vararg event: Event<*>): Maybe<T> =
takeUntil(Maybe.merge(event.map { it.observable.firstElement() }))

protected fun <T : Any> Maybe<T>.untilOn(vararg maybe: Maybe<*>): Maybe<T> =
takeUntil(Maybe.merge(maybe.toList()))

protected fun Completable.untilOn(vararg action: Action<*>): Completable =
takeUntil(Completable.merge(action.map { it.observable.firstElement().ignoreElement() }))

protected fun Completable.untilOn(vararg event: Event<*>): Completable =
takeUntil(Completable.merge(event.map { it.observable.firstElement().ignoreElement() }))

protected fun Completable.untilOn(vararg completable: Completable): Completable =
takeUntil(Completable.merge(completable.toList()))

protected open fun <T : Any> Observable<T>.applyDefaultErrorHandler(): Observable<T> = this

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@ package com.alexdeww.reactiveviewmodel.widget

import com.alexdeww.reactiveviewmodel.core.common.RvmComponent
import com.alexdeww.reactiveviewmodel.core.property.Action
import com.alexdeww.reactiveviewmodel.core.property.ConfirmationEvent
import com.alexdeww.reactiveviewmodel.core.property.Event
import com.alexdeww.reactiveviewmodel.core.property.State

abstract class BaseControl : RvmComponent {

protected fun <T : Any> state(initValue: T? = null, debounceInterval: Long? = null): State<T> =
State(initValue, debounceInterval)
protected fun <T : Any> state(
initValue: T? = null,
debounceInterval: Long? = null
): State<T> = State(initValue, debounceInterval)

protected fun <T : Any> event(debounceInterval: Long? = null): Event<T> =
Event(debounceInterval)

protected fun eventNone(debounceInterval: Long? = null): Event<Unit> =
Event(debounceInterval)
event(debounceInterval)

protected fun <T : Any> action(debounceInterval: Long? = null): Action<T> =
Action(debounceInterval)

protected fun actionNone(debounceInterval: Long? = null): Action<Unit> =
Action(debounceInterval)
action(debounceInterval)

protected fun <T : Any> confirmationEvent(
debounceInterval: Long? = null
): ConfirmationEvent<T> = ConfirmationEvent(debounceInterval)

protected fun confirmationEventNone(
debounceInterval: Long? = null
): ConfirmationEvent<Unit> = confirmationEvent(debounceInterval)

}

0 comments on commit 38a1719

Please sign in to comment.