-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomePresenter.kt
119 lines (106 loc) · 4.71 KB
/
HomePresenter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package beepbeep.pixels.home
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import android.util.Log
import beepbeep.pixels.cache.submission.SubmissionCache
import beepbeep.pixels.shared.PixelsApplication
import beepbeep.pixels.shared.extension.addTo
import beepbeep.pixels.shared.extension.downScheduler
import beepbeep.pixels.shared.extension.upScheduler
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.PublishSubject
import net.dean.jraw.models.Listing
import net.dean.jraw.models.Submission
class HomePresenter(val input: HomeContract.Input, val repo: HomeRepoInterface = HomeRepo()) : LifecycleObserver {
val disposables = CompositeDisposable()
val onDataLoaded = PublishSubject.create<Unit>()
val showNoInternetSnackbarSubject = PublishSubject.create<Unit>()
val output = object : HomeContract.Output() {
override val showNoInternetSnackbar: Observable<Unit>
get() = showNoInternetSnackbarSubject.hide()
override val onDataLoaded: Observable<Unit>
get() = onDataLoaded.hide()
}
private fun onDataLoaded(): (Pair<Boolean, Listing<Submission>>) -> Unit {
return { (started, submissionListing) ->
submissionListing.forEachIndexed { index, submission ->
PixelsApplication.pixelsCache?.submissionDao()?.insert(SubmissionCache(submission))
}
}
}
private fun deleteOldSubmission(pair: Pair<Boolean, Listing<Submission>>) {
val (started, submissionListing) = pair
if (!started) {
repo.deleteAllFromSub()
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
val initialTrigger = PublishSubject.create<Unit>()
// refresh flow
input.refresh
.upScheduler(repo.getMainUiThread())
.map { input.isConnectedToInternet() }
.filter { it }
.map { repo.initGuestRedditClient() }
.flatMap { repo.data(it) }
.doOnNext { deleteOldSubmission(it) }
.upScheduler(repo.getBackgroundThread())
.downScheduler(repo.getBackgroundThread())
.subscribe(onDataLoaded())
.addTo(disposables)
Observable
.merge(initialTrigger, input.retry, input.loadMore)
.publish()
.apply {
// Initial flow
map { input.isConnectedToInternet() }
.filter { it }
.filter { !repo.isRedditClientAuthed() }
.map { repo.initGuestRedditClient() }
.flatMap { repo.data(it) }
.doOnNext { deleteOldSubmission(it) }
.upScheduler(repo.getBackgroundThread())
.downScheduler(repo.getBackgroundThread())
.subscribe(onDataLoaded())
// load more flow
map { input.isConnectedToInternet() }
.filter { it }
.filter { repo.isRedditClientAuthed() }
.map { repo.getPaginator() }
.flatMap { repo.data(it) }
.doOnNext { (started, submissionListing) ->
if (!started) {
repo.deleteAllFromSub()
}
}
.subscribe(onDataLoaded())
// when offline
map { !input.isConnectedToInternet() }
.filter { it }
.upScheduler(repo.getBackgroundThread())
.downScheduler(repo.getMainUiThread())
.subscribe { showNoInternetSnackbarSubject.onNext(Unit) }
}
.connect()
.addTo(disposables)
repo.bindToDb()?.apply {
upScheduler(repo.getBackgroundThread())
.downScheduler(repo.getMainUiThread())
.subscribe { listing ->
listing.forEachIndexed { index, submissionCache ->
Log.d("ddw", "[${index}] ${submissionCache.author}: ${submissionCache.title}")
}
}
.addTo(disposables)
}
initialTrigger.onNext(Unit)
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
disposables.dispose()
repo.destroy()
}
}