From 36123266286dbf64dce803204f156b3bed5bc81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 4 Apr 2020 20:59:44 +0200 Subject: [PATCH] Add missing sdk initialization in lucky number repository (#752) --- .../luckynumber/LuckyNumberRepository.kt | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt index 374b9a29..0ea06350 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt @@ -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.internet.observing.InternetObservingSettings +import io.github.wulkanowy.data.SdkHelper import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import io.reactivex.Completable @@ -15,33 +16,36 @@ import javax.inject.Singleton class LuckyNumberRepository @Inject constructor( private val settings: InternetObservingSettings, 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 { - return local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMapMaybe { - if (it) remote.getLuckyNumber(student) - else Maybe.error(UnknownHostException()) - }.flatMap { new -> - local.getLuckyNumber(student, LocalDate.now()) - .doOnSuccess { old -> - if (new != old) { - local.deleteLuckyNumber(old) + return Maybe.just(sdkHelper.init(student)).flatMap { + local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh } + .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) + .flatMapMaybe { + if (it) remote.getLuckyNumber(student) + else Maybe.error(UnknownHostException()) + }.flatMap { new -> + local.getLuckyNumber(student, LocalDate.now()) + .doOnSuccess { old -> + if (new != old) { + local.deleteLuckyNumber(old) + local.saveLuckyNumber(new.apply { + if (notify) isNotified = false + }) + } + } + .doOnComplete { local.saveLuckyNumber(new.apply { if (notify) isNotified = false }) } - } - .doOnComplete { - local.saveLuckyNumber(new.apply { - if (notify) isNotified = false - }) - } - }.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) }, - { local.getLuckyNumber(student, LocalDate.now()) }) - ) + }.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) }, + { local.getLuckyNumber(student, LocalDate.now()) }) + ) + } } fun getNotNotifiedLuckyNumber(student: Student): Maybe {