From a174ae998d028ff765e9ce9dc8f4bdb0366b88ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sun, 20 Jan 2019 14:16:24 +0100 Subject: [PATCH] Add api initialization to the message repository (#220) --- .../data/repositories/MessagesRepository.kt | 82 ++++++++++--------- .../wulkanowy/services/job/SyncWorker.kt | 22 ++--- .../preview/MessagePreviewPresenter.kt | 2 +- .../message/tab/MessageTabPresenter.kt | 2 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 3 +- 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/MessagesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/MessagesRepository.kt index 06a0f6f9..85a1e076 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/MessagesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/MessagesRepository.kt @@ -2,6 +2,7 @@ package io.github.wulkanowy.data.repositories import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings +import io.github.wulkanowy.data.ApiHelper import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.local.MessagesLocal @@ -16,7 +17,8 @@ import javax.inject.Singleton class MessagesRepository @Inject constructor( private val settings: InternetObservingSettings, private val local: MessagesLocal, - private val remote: MessagesRemote + private val remote: MessagesRemote, + private val apiHelper: ApiHelper ) { enum class MessageFolder(val id: Int = 1) { @@ -25,44 +27,50 @@ class MessagesRepository @Inject constructor( TRASHED(3) } - fun getMessages(studentId: Int, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single> { - return local.getMessages(studentId, folder).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getMessages(studentId, folder) - else Single.error(UnknownHostException()) - }.flatMap { new -> - local.getMessages(studentId, folder).toSingle(emptyList()) - .doOnSuccess { old -> - local.deleteMessages(old - new) - local.saveMessages((new - old) - .onEach { - it.isNotified = !notify - }) - } - }.flatMap { local.getMessages(studentId, folder).toSingle(emptyList()) } - ) + fun getMessages(student: Student, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single> { + return Single.just(apiHelper.initApi(student)) + .flatMap { _ -> + local.getMessages(student.studentId, folder).filter { !forceRefresh } + .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) + .flatMap { + if (it) remote.getMessages(student.studentId, folder) + else Single.error(UnknownHostException()) + }.flatMap { new -> + local.getMessages(student.studentId, folder).toSingle(emptyList()) + .doOnSuccess { old -> + local.deleteMessages(old - new) + local.saveMessages((new - old) + .onEach { + it.isNotified = !notify + }) + } + }.flatMap { local.getMessages(student.studentId, folder).toSingle(emptyList()) } + ) + } } - fun getMessage(studentId: Int, messageId: Int, markAsRead: Boolean = false): Single { - return local.getMessage(studentId, messageId) - .filter { !it.content.isNullOrEmpty() } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) local.getMessage(studentId, messageId).toSingle() - else Single.error(UnknownHostException()) - } - .flatMap { dbMessage -> - remote.getMessagesContent(dbMessage, markAsRead).doOnSuccess { - local.updateMessage(dbMessage.copy(unread = false).apply { - id = dbMessage.id - content = it - }) - } - }.flatMap { - local.getMessage(studentId, messageId).toSingle() - } - ) + fun getMessage(student: Student, messageId: Int, markAsRead: Boolean = false): Single { + return Single.just(apiHelper.initApi(student)) + .flatMap { _ -> + local.getMessage(student.studentId, messageId) + .filter { !it.content.isNullOrEmpty() } + .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) + .flatMap { + if (it) local.getMessage(student.studentId, messageId).toSingle() + else Single.error(UnknownHostException()) + } + .flatMap { dbMessage -> + remote.getMessagesContent(dbMessage, markAsRead).doOnSuccess { + local.updateMessage(dbMessage.copy(unread = false).apply { + id = dbMessage.id + content = it + }) + } + }.flatMap { + local.getMessage(student.studentId, messageId).toSingle() + } + ) + } } fun getNewMessages(student: Student): Single> { diff --git a/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt index 65fb3abc..c958c22d 100644 --- a/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/job/SyncWorker.kt @@ -84,19 +84,19 @@ class SyncWorker : SimpleJobService() { var error: Throwable? = null disposable.add(student.getCurrentStudent() - .flatMap { semester.getCurrentSemester(it, true) } + .flatMap { semester.getCurrentSemester(it, true).map { semester -> semester to it } } .flatMapPublisher { Single.merge( listOf( - gradesDetails.getGrades(it, true, true), - gradesSummary.getGradesSummary(it, true), - attendance.getAttendance(it, start, end, true), - exam.getExams(it, start, end, true), - timetable.getTimetable(it, start, end, true), - message.getMessages(it.studentId, RECEIVED, true, true), - note.getNotes(it, true, true), - homework.getHomework(it, LocalDate.now(), true), - homework.getHomework(it, LocalDate.now().plusDays(1), true) + gradesDetails.getGrades(it.first, true, true), + gradesSummary.getGradesSummary(it.first, true), + attendance.getAttendance(it.first, start, end, true), + exam.getExams(it.first, start, end, true), + timetable.getTimetable(it.first, start, end, true), + message.getMessages(it.second, RECEIVED, true, true), + note.getNotes(it.first, true, true), + homework.getHomework(it.first, LocalDate.now(), true), + homework.getHomework(it.first, LocalDate.now().plusDays(1), true) ) ) } @@ -138,7 +138,7 @@ class SyncWorker : SimpleJobService() { disposable.add(student.getCurrentStudent() .flatMap { message.getNewMessages(it) } .map { it.filter { message -> !message.isNotified } } - .doOnSuccess{ + .doOnSuccess { if (it.isNotEmpty()) { Timber.d("Found ${it.size} unread messages") MessageNotification(applicationContext).sendNotification(it) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index aae11b77..d0ce9250 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -32,7 +32,7 @@ class MessagePreviewPresenter @Inject constructor( disposable.apply { clear() add(studentRepository.getCurrentStudent() - .flatMap { messagesRepository.getMessage(it.studentId, messageId, true) } + .flatMap { messagesRepository.getMessage(it, messageId, true) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { view?.showProgress(false) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index 0d413b99..9636e4df 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -38,7 +38,7 @@ class MessageTabPresenter @Inject constructor( disposable.apply { clear() add(studentRepository.getCurrentStudent() - .flatMap { messagesRepository.getMessages(it.studentId, folder, forceRefresh) } + .flatMap { messagesRepository.getMessages(it, folder, forceRefresh) } .map { items -> items.map { MessageItem(it, view?.noSubjectString.orEmpty()) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) diff --git a/build.gradle b/build.gradle index 629b4a48..fdd750f7 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:3.3.0' classpath 'com.google.gms:google-services:4.2.0' classpath "io.fabric.tools:gradle:1.26.1" classpath "com.github.triplet.gradle:play-publisher:1.2.2" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 3f0f9be6..acee7e1b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Sun Jan 20 00:47:56 CET 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip