mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-01-18 17:46:44 -06:00
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.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.ApiHelper
|
||||||
import io.github.wulkanowy.data.db.entities.Message
|
import io.github.wulkanowy.data.db.entities.Message
|
||||||
import io.github.wulkanowy.data.db.entities.Student
|
import io.github.wulkanowy.data.db.entities.Student
|
||||||
import io.github.wulkanowy.data.repositories.local.MessagesLocal
|
import io.github.wulkanowy.data.repositories.local.MessagesLocal
|
||||||
@ -16,7 +17,8 @@ import javax.inject.Singleton
|
|||||||
class MessagesRepository @Inject constructor(
|
class MessagesRepository @Inject constructor(
|
||||||
private val settings: InternetObservingSettings,
|
private val settings: InternetObservingSettings,
|
||||||
private val local: MessagesLocal,
|
private val local: MessagesLocal,
|
||||||
private val remote: MessagesRemote
|
private val remote: MessagesRemote,
|
||||||
|
private val apiHelper: ApiHelper
|
||||||
) {
|
) {
|
||||||
|
|
||||||
enum class MessageFolder(val id: Int = 1) {
|
enum class MessageFolder(val id: Int = 1) {
|
||||||
@ -25,44 +27,50 @@ class MessagesRepository @Inject constructor(
|
|||||||
TRASHED(3)
|
TRASHED(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessages(studentId: Int, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single<List<Message>> {
|
fun getMessages(student: Student, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single<List<Message>> {
|
||||||
return local.getMessages(studentId, folder).filter { !forceRefresh }
|
return Single.just(apiHelper.initApi(student))
|
||||||
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
|
.flatMap { _ ->
|
||||||
.flatMap {
|
local.getMessages(student.studentId, folder).filter { !forceRefresh }
|
||||||
if (it) remote.getMessages(studentId, folder)
|
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
|
||||||
else Single.error(UnknownHostException())
|
.flatMap {
|
||||||
}.flatMap { new ->
|
if (it) remote.getMessages(student.studentId, folder)
|
||||||
local.getMessages(studentId, folder).toSingle(emptyList())
|
else Single.error(UnknownHostException())
|
||||||
.doOnSuccess { old ->
|
}.flatMap { new ->
|
||||||
local.deleteMessages(old - new)
|
local.getMessages(student.studentId, folder).toSingle(emptyList())
|
||||||
local.saveMessages((new - old)
|
.doOnSuccess { old ->
|
||||||
.onEach {
|
local.deleteMessages(old - new)
|
||||||
it.isNotified = !notify
|
local.saveMessages((new - old)
|
||||||
})
|
.onEach {
|
||||||
}
|
it.isNotified = !notify
|
||||||
}.flatMap { local.getMessages(studentId, folder).toSingle(emptyList()) }
|
})
|
||||||
)
|
}
|
||||||
|
}.flatMap { local.getMessages(student.studentId, folder).toSingle(emptyList()) }
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessage(studentId: Int, messageId: Int, markAsRead: Boolean = false): Single<Message> {
|
fun getMessage(student: Student, messageId: Int, markAsRead: Boolean = false): Single<Message> {
|
||||||
return local.getMessage(studentId, messageId)
|
return Single.just(apiHelper.initApi(student))
|
||||||
.filter { !it.content.isNullOrEmpty() }
|
.flatMap { _ ->
|
||||||
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
|
local.getMessage(student.studentId, messageId)
|
||||||
.flatMap {
|
.filter { !it.content.isNullOrEmpty() }
|
||||||
if (it) local.getMessage(studentId, messageId).toSingle()
|
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
|
||||||
else Single.error(UnknownHostException())
|
.flatMap {
|
||||||
}
|
if (it) local.getMessage(student.studentId, messageId).toSingle()
|
||||||
.flatMap { dbMessage ->
|
else Single.error(UnknownHostException())
|
||||||
remote.getMessagesContent(dbMessage, markAsRead).doOnSuccess {
|
}
|
||||||
local.updateMessage(dbMessage.copy(unread = false).apply {
|
.flatMap { dbMessage ->
|
||||||
id = dbMessage.id
|
remote.getMessagesContent(dbMessage, markAsRead).doOnSuccess {
|
||||||
content = it
|
local.updateMessage(dbMessage.copy(unread = false).apply {
|
||||||
})
|
id = dbMessage.id
|
||||||
}
|
content = it
|
||||||
}.flatMap {
|
})
|
||||||
local.getMessage(studentId, messageId).toSingle()
|
}
|
||||||
}
|
}.flatMap {
|
||||||
)
|
local.getMessage(student.studentId, messageId).toSingle()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getNewMessages(student: Student): Single<List<Message>> {
|
fun getNewMessages(student: Student): Single<List<Message>> {
|
||||||
|
@ -84,19 +84,19 @@ class SyncWorker : SimpleJobService() {
|
|||||||
var error: Throwable? = null
|
var error: Throwable? = null
|
||||||
|
|
||||||
disposable.add(student.getCurrentStudent()
|
disposable.add(student.getCurrentStudent()
|
||||||
.flatMap { semester.getCurrentSemester(it, true) }
|
.flatMap { semester.getCurrentSemester(it, true).map { semester -> semester to it } }
|
||||||
.flatMapPublisher {
|
.flatMapPublisher {
|
||||||
Single.merge(
|
Single.merge(
|
||||||
listOf(
|
listOf(
|
||||||
gradesDetails.getGrades(it, true, true),
|
gradesDetails.getGrades(it.first, true, true),
|
||||||
gradesSummary.getGradesSummary(it, true),
|
gradesSummary.getGradesSummary(it.first, true),
|
||||||
attendance.getAttendance(it, start, end, true),
|
attendance.getAttendance(it.first, start, end, true),
|
||||||
exam.getExams(it, start, end, true),
|
exam.getExams(it.first, start, end, true),
|
||||||
timetable.getTimetable(it, start, end, true),
|
timetable.getTimetable(it.first, start, end, true),
|
||||||
message.getMessages(it.studentId, RECEIVED, true, true),
|
message.getMessages(it.second, RECEIVED, true, true),
|
||||||
note.getNotes(it, true, true),
|
note.getNotes(it.first, true, true),
|
||||||
homework.getHomework(it, LocalDate.now(), true),
|
homework.getHomework(it.first, LocalDate.now(), true),
|
||||||
homework.getHomework(it, LocalDate.now().plusDays(1), true)
|
homework.getHomework(it.first, LocalDate.now().plusDays(1), true)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ class SyncWorker : SimpleJobService() {
|
|||||||
disposable.add(student.getCurrentStudent()
|
disposable.add(student.getCurrentStudent()
|
||||||
.flatMap { message.getNewMessages(it) }
|
.flatMap { message.getNewMessages(it) }
|
||||||
.map { it.filter { message -> !message.isNotified } }
|
.map { it.filter { message -> !message.isNotified } }
|
||||||
.doOnSuccess{
|
.doOnSuccess {
|
||||||
if (it.isNotEmpty()) {
|
if (it.isNotEmpty()) {
|
||||||
Timber.d("Found ${it.size} unread messages")
|
Timber.d("Found ${it.size} unread messages")
|
||||||
MessageNotification(applicationContext).sendNotification(it)
|
MessageNotification(applicationContext).sendNotification(it)
|
||||||
|
@ -32,7 +32,7 @@ class MessagePreviewPresenter @Inject constructor(
|
|||||||
disposable.apply {
|
disposable.apply {
|
||||||
clear()
|
clear()
|
||||||
add(studentRepository.getCurrentStudent()
|
add(studentRepository.getCurrentStudent()
|
||||||
.flatMap { messagesRepository.getMessage(it.studentId, messageId, true) }
|
.flatMap { messagesRepository.getMessage(it, messageId, true) }
|
||||||
.subscribeOn(schedulers.backgroundThread)
|
.subscribeOn(schedulers.backgroundThread)
|
||||||
.observeOn(schedulers.mainThread)
|
.observeOn(schedulers.mainThread)
|
||||||
.doFinally { view?.showProgress(false) }
|
.doFinally { view?.showProgress(false) }
|
||||||
|
@ -38,7 +38,7 @@ class MessageTabPresenter @Inject constructor(
|
|||||||
disposable.apply {
|
disposable.apply {
|
||||||
clear()
|
clear()
|
||||||
add(studentRepository.getCurrentStudent()
|
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()) } }
|
.map { items -> items.map { MessageItem(it, view?.noSubjectString.orEmpty()) } }
|
||||||
.subscribeOn(schedulers.backgroundThread)
|
.subscribeOn(schedulers.backgroundThread)
|
||||||
.observeOn(schedulers.mainThread)
|
.observeOn(schedulers.mainThread)
|
||||||
|
@ -9,7 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
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 'com.google.gms:google-services:4.2.0'
|
||||||
classpath "io.fabric.tools:gradle:1.26.1"
|
classpath "io.fabric.tools:gradle:1.26.1"
|
||||||
classpath "com.github.triplet.gradle:play-publisher:1.2.2"
|
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
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
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…
x
Reference in New Issue
Block a user