forked from github/wulkanowy-mirror
Add api initialization to the message repository (#220)
This commit is contained in:
parent
c5bab52fa2
commit
a174ae998d
@ -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<List<Message>> {
|
||||
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<List<Message>> {
|
||||
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<Message> {
|
||||
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<Message> {
|
||||
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<List<Message>> {
|
||||
|
@ -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)
|
||||
|
@ -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) }
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
|
3
gradle/wrapper/gradle-wrapper.properties
vendored
3
gradle/wrapper/gradle-wrapper.properties
vendored
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user