-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePresenter.kt
More file actions
41 lines (36 loc) · 1.14 KB
/
BasePresenter.kt
File metadata and controls
41 lines (36 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
abstract class BasePresenter<V : MvpView> : Presenter<V> {
protected var mvpView: V? = null
private var mData = mutableListOf<PresenterData<*>>()
override fun onDestroyedByLoader() {
mData.forEach {
it.subscription?.unsubscribe()
}
}
@CallSuper
override fun onViewAttached(view: V) {
this.mvpView = view
mData.forEach {
val data = it as PresenterData<Any>
if (data.cachedValue != null) {
data.displayToView(data.cachedValue!!)
}
}
}
fun getView(): V {
return mvpView!!
}
fun <T> load(observable: Observable<T?>, displayToView: (T) -> Unit) {
val data = PresenterData(observable, displayToView)
data.subscription = data.observable
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
data.cachedValue = it
if (isViewAttached && it != null) {
data.displayToView(it)
}
}, {
logError(it)
})
mData.add(data)
}
}