-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseDialogFragment.kt
48 lines (36 loc) · 1.3 KB
/
BaseDialogFragment.kt
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
42
43
44
45
46
47
48
package com.rethinkbooks.main
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.DialogFragment
import com.rethinkbooks.AsyncTaskReplacment.RxAsyncTask
import io.reactivex.disposables.CompositeDisposable
/**
* Created by bradthome on 12/4/17.
*/
abstract class BaseDialogFragment : DialogFragment() {
protected val compositeDisposable = CompositeDisposable()
init {
lifecycle.addObserver(ObserveLifecycle())
}
inner class ObserveLifecycle : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
compositeDisposable.clear()
}
}
fun <Params> RxAsyncTask<Params, *, *>.execute(vararg params: Params) {
this.disposable(compositeDisposable).run(*params)
}
}
abstract class BaseDialogViewModelFragment : BaseDialogFragment() {
protected val viewModel by lazy { ViewModelProviders.of(this).get(viewModelClass) }
abstract protected val viewModelClass: Class<BaseViewModel>
}