Add missing sdk initialization in lucky number repository (#752)

This commit is contained in:
Mikołaj Pich 2020-04-04 20:59:44 +02:00 committed by GitHub
parent bf61dd1bad
commit 3612326628
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ package io.github.wulkanowy.data.repositories.luckynumber
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.SdkHelper
import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.LuckyNumber
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Completable import io.reactivex.Completable
@ -15,33 +16,36 @@ import javax.inject.Singleton
class LuckyNumberRepository @Inject constructor( class LuckyNumberRepository @Inject constructor(
private val settings: InternetObservingSettings, private val settings: InternetObservingSettings,
private val local: LuckyNumberLocal, private val local: LuckyNumberLocal,
private val remote: LuckyNumberRemote private val remote: LuckyNumberRemote,
private val sdkHelper: SdkHelper
) { ) {
fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): Maybe<LuckyNumber> { fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): Maybe<LuckyNumber> {
return local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh } return Maybe.just(sdkHelper.init(student)).flatMap {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh }
.flatMapMaybe { .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
if (it) remote.getLuckyNumber(student) .flatMapMaybe {
else Maybe.error(UnknownHostException()) if (it) remote.getLuckyNumber(student)
}.flatMap { new -> else Maybe.error(UnknownHostException())
local.getLuckyNumber(student, LocalDate.now()) }.flatMap { new ->
.doOnSuccess { old -> local.getLuckyNumber(student, LocalDate.now())
if (new != old) { .doOnSuccess { old ->
local.deleteLuckyNumber(old) if (new != old) {
local.deleteLuckyNumber(old)
local.saveLuckyNumber(new.apply {
if (notify) isNotified = false
})
}
}
.doOnComplete {
local.saveLuckyNumber(new.apply { local.saveLuckyNumber(new.apply {
if (notify) isNotified = false if (notify) isNotified = false
}) })
} }
} }.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) },
.doOnComplete { { local.getLuckyNumber(student, LocalDate.now()) })
local.saveLuckyNumber(new.apply { )
if (notify) isNotified = false }
})
}
}.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) },
{ local.getLuckyNumber(student, LocalDate.now()) })
)
} }
fun getNotNotifiedLuckyNumber(student: Student): Maybe<LuckyNumber> { fun getNotNotifiedLuckyNumber(student: Student): Maybe<LuckyNumber> {