Migrate repositories from rxjava to coroutines (#885)

This commit is contained in:
Mikołaj Pich 2020-06-20 15:07:57 +02:00 committed by GitHub
parent 8cee882c08
commit 4434d6f024
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
177 changed files with 1752 additions and 2004 deletions

View File

@ -124,10 +124,13 @@ configurations.all {
} }
dependencies { dependencies {
implementation "io.github.wulkanowy:sdk:0.19.0" implementation "io.github.wulkanowy:sdk:61250d3"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "androidx.core:core-ktx:1.2.0" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.7'
implementation "androidx.core:core-ktx:1.3.0"
implementation "androidx.activity:activity-ktx:1.1.0" implementation "androidx.activity:activity-ktx:1.1.0"
implementation "androidx.appcompat:appcompat:1.2.0-rc01" implementation "androidx.appcompat:appcompat:1.2.0-rc01"
implementation "androidx.appcompat:appcompat-resources:1.1.0" implementation "androidx.appcompat:appcompat-resources:1.1.0"
@ -167,7 +170,6 @@ dependencies {
implementation "com.ncapdevi:frag-nav:3.3.0" implementation "com.ncapdevi:frag-nav:3.3.0"
implementation "com.github.YarikSOffice:lingver:1.2.2" implementation "com.github.YarikSOffice:lingver:1.2.2"
implementation "com.github.pwittchen:reactivenetwork-rx2:3.0.8"
implementation "io.reactivex.rxjava2:rxandroid:2.1.1" implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.19" implementation "io.reactivex.rxjava2:rxjava:2.2.19"
@ -197,7 +199,6 @@ dependencies {
testImplementation "junit:junit:4.13" testImplementation "junit:junit:4.13"
testImplementation "io.mockk:mockk:$mockk" testImplementation "io.mockk:mockk:$mockk"
testImplementation "org.threeten:threetenbp:1.4.4" testImplementation "org.threeten:threetenbp:1.4.4"
testImplementation "org.mockito:mockito-inline:3.3.3"
androidTestImplementation "androidx.test:core:1.2.0" androidTestImplementation "androidx.test:core:1.2.0"
androidTestImplementation "androidx.test:runner:1.2.0" androidTestImplementation "androidx.test:runner:1.2.0"
@ -205,7 +206,6 @@ dependencies {
androidTestImplementation "io.mockk:mockk-android:$mockk" androidTestImplementation "io.mockk:mockk-android:$mockk"
androidTestImplementation "androidx.room:room-testing:$room" androidTestImplementation "androidx.room:room-testing:$room"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
androidTestImplementation "org.mockito:mockito-android:3.3.3"
} }
apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.gms.google-services'

View File

@ -4,6 +4,7 @@ import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase.CONFLICT_FAIL import android.database.sqlite.SQLiteDatabase.CONFLICT_FAIL
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.runBlocking
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -29,7 +30,7 @@ class Migration12Test : AbstractMigrationTest() {
helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = db.studentDao.loadAll().blockingGet() val students = runBlocking { db.studentDao.loadAll() }
assertEquals(2, students.size) assertEquals(2, students.size)
@ -58,7 +59,7 @@ class Migration12Test : AbstractMigrationTest() {
helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = db.studentDao.loadAll().blockingGet() val students = runBlocking { db.studentDao.loadAll() }
assertEquals(1, students.size) assertEquals(1, students.size)
@ -84,7 +85,7 @@ class Migration12Test : AbstractMigrationTest() {
helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) helper.runMigrationsAndValidate(dbName, 12, true, Migration12())
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = db.studentDao.loadAll().blockingGet() val students = runBlocking { db.studentDao.loadAll() }
assertEquals(3, students.size) assertEquals(3, students.size)

View File

@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import io.github.wulkanowy.data.db.Converters import io.github.wulkanowy.data.db.Converters
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
@ -26,7 +27,7 @@ class Migration13Test : AbstractMigrationTest() {
helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) helper.runMigrationsAndValidate(dbName, 13, true, Migration13())
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = db.studentDao.loadAll().blockingGet() val students = runBlocking { db.studentDao.loadAll() }
assertEquals(3, students.size) assertEquals(3, students.size)
@ -60,7 +61,7 @@ class Migration13Test : AbstractMigrationTest() {
helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) helper.runMigrationsAndValidate(dbName, 13, true, Migration13())
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = db.studentDao.loadAll().blockingGet() val students = runBlocking { db.studentDao.loadAll() }
assertEquals(2, students.size) assertEquals(2, students.size)

View File

@ -1,19 +0,0 @@
package io.github.wulkanowy.data.repositories
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingStrategy
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.error.ErrorHandler
import io.reactivex.Observable
import io.reactivex.Single
class TestInternetObservingStrategy : InternetObservingStrategy {
override fun checkInternetConnectivity(host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Single<Boolean> {
return Single.just(true)
}
override fun observeInternetConnectivity(initialIntervalInMs: Int, intervalInMs: Int, host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Observable<Boolean> {
return Observable.just(true)
}
override fun getDefaultPingHost() = "localhost"
}

View File

@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Attendance
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -35,7 +36,7 @@ class AttendanceLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
attendanceLocal.saveAttendance(listOf( val list = listOf(
getAttendanceEntity( getAttendanceEntity(
of(2018, 9, 10), of(2018, 9, 10),
SentExcuseStatus.ACCEPTED SentExcuseStatus.ACCEPTED
@ -48,14 +49,11 @@ class AttendanceLocalTest {
of(2018, 9, 17), of(2018, 9, 17),
SentExcuseStatus.ACCEPTED SentExcuseStatus.ACCEPTED
) )
))
val attendance = attendanceLocal
.getAttendance(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1),
of(2018, 9, 10),
of(2018, 9, 14)
) )
.blockingGet() runBlocking { attendanceLocal.saveAttendance(list) }
val semester = Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1)
val attendance = runBlocking { attendanceLocal.getAttendance(semester, of(2018, 9, 10), of(2018, 9, 14)) }
assertEquals(2, attendance.size) assertEquals(2, attendance.size)
assertEquals(attendance[0].date, of(2018, 9, 10)) assertEquals(attendance[0].date, of(2018, 9, 10))
assertEquals(attendance[1].date, of(2018, 9, 14)) assertEquals(attendance[1].date, of(2018, 9, 14))

View File

@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.CompletedLesson
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -24,7 +25,8 @@ class CompletedLessonsLocalTest {
@Before @Before
fun createDb() { fun createDb() {
testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) testDb = Room
.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java)
.build() .build()
completedLessonsLocal = CompletedLessonsLocal(testDb.completedLessonsDao) completedLessonsLocal = CompletedLessonsLocal(testDb.completedLessonsDao)
} }
@ -36,18 +38,15 @@ class CompletedLessonsLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
completedLessonsLocal.saveCompletedLessons(listOf( val list = listOf(
getCompletedLesson(of(2018, 9, 10), 1), getCompletedLesson(of(2018, 9, 10), 1),
getCompletedLesson(of(2018, 9, 14), 2), getCompletedLesson(of(2018, 9, 14), 2),
getCompletedLesson(of(2018, 9, 17), 3) getCompletedLesson(of(2018, 9, 17), 3)
))
val completed = completedLessonsLocal
.getCompletedLessons(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1),
of(2018, 9, 10),
of(2018, 9, 14)
) )
.blockingGet() runBlocking { completedLessonsLocal.saveCompletedLessons(list) }
val semester = Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1)
val completed = runBlocking { completedLessonsLocal.getCompletedLessons(semester, of(2018, 9, 10), of(2018, 9, 14)) }
assertEquals(2, completed.size) assertEquals(2, completed.size)
assertEquals(completed[0].date, of(2018, 9, 10)) assertEquals(completed[0].date, of(2018, 9, 10))
assertEquals(completed[1].date, of(2018, 9, 14)) assertEquals(completed[1].date, of(2018, 9, 14))

View File

@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Exam
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -34,18 +35,15 @@ class ExamLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
examLocal.saveExams(listOf( val list = listOf(
Exam(1, 2, of(2018, 9, 10), now(), "", "", "", "", "", ""), Exam(1, 2, of(2018, 9, 10), now(), "", "", "", "", "", ""),
Exam(1, 2, of(2018, 9, 14), now(), "", "", "", "", "", ""), Exam(1, 2, of(2018, 9, 14), now(), "", "", "", "", "", ""),
Exam(1, 2, of(2018, 9, 17), now(), "", "", "", "", "", "") Exam(1, 2, of(2018, 9, 17), now(), "", "", "", "", "", "")
))
val exams = examLocal
.getExams(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1),
of(2018, 9, 10),
of(2018, 9, 14)
) )
.blockingGet() runBlocking { examLocal.saveExams(list) }
val semester = Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1)
val exams = runBlocking { examLocal.getExams(semester, of(2018, 9, 10), of(2018, 9, 14)) }
assertEquals(2, exams.size) assertEquals(2, exams.size)
assertEquals(exams[0].date, of(2018, 9, 10)) assertEquals(exams[0].date, of(2018, 9, 10))
assertEquals(exams[1].date, of(2018, 9, 14)) assertEquals(exams[1].date, of(2018, 9, 14))

View File

@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -22,7 +23,8 @@ class GradeLocalTest {
@Before @Before
fun createDb() { fun createDb() {
testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) testDb = Room
.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java)
.build() .build()
gradeLocal = GradeLocal(testDb.gradeDao, testDb.gradeSummaryDao) gradeLocal = GradeLocal(testDb.gradeDao, testDb.gradeSummaryDao)
} }
@ -34,17 +36,16 @@ class GradeLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
gradeLocal.saveGrades(listOf( val list = listOf(
createGradeLocal(5, 3.0, LocalDate.of(2018, 9, 10), "", 1), createGradeLocal(5, 3.0, LocalDate.of(2018, 9, 10), "", 1),
createGradeLocal(4, 4.0, LocalDate.of(2019, 2, 27), "", 2), createGradeLocal(4, 4.0, LocalDate.of(2019, 2, 27), "", 2),
createGradeLocal(3, 5.0, LocalDate.of(2019, 2, 28), "", 2) createGradeLocal(3, 5.0, LocalDate.of(2019, 2, 28), "", 2)
)) )
runBlocking { gradeLocal.saveGrades(list) }
val semester = Semester(1, 2, "", 2019, 2, 1, now(), now(), 1, 1) val semester = Semester(1, 2, "", 2019, 2, 1, now(), now(), 1, 1)
val grades = gradeLocal val grades = runBlocking { gradeLocal.getGradesDetails(semester) }
.getGradesDetails(semester)
.blockingGet()
assertEquals(2, grades.size) assertEquals(2, grades.size)
assertEquals(grades[0].date, LocalDate.of(2019, 2, 27)) assertEquals(grades[0].date, LocalDate.of(2019, 2, 27))

View File

@ -5,17 +5,16 @@ import androidx.room.Room
import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress import androidx.test.filters.SdkSuppress
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.TestInternetObservingStrategy
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.sdk.pojo.Grade import io.github.wulkanowy.sdk.pojo.Grade
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.every import io.mockk.every
import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.MockK
import io.reactivex.Single import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -33,10 +32,6 @@ class GradeRepositoryTest {
@MockK @MockK
private lateinit var mockSdk: Sdk private lateinit var mockSdk: Sdk
private val settings = InternetObservingSettings.builder()
.strategy(TestInternetObservingStrategy())
.build()
@MockK @MockK
private lateinit var semesterMock: Semester private lateinit var semesterMock: Semester
@ -71,15 +66,17 @@ class GradeRepositoryTest {
@Test @Test
fun markOlderThanRegisterDateAsRead() { fun markOlderThanRegisterDateAsRead() {
every { mockSdk.getGrades(1) } returns Single.just(listOf( coEvery { mockSdk.getGrades(1) } returns (listOf(
createGradeApi(5, 4.0, of(2019, 2, 25), "Ocena pojawiła się"), createGradeApi(5, 4.0, of(2019, 2, 25), "Ocena pojawiła się"),
createGradeApi(5, 4.0, of(2019, 2, 26), "przed zalogowanie w aplikacji"), createGradeApi(5, 4.0, of(2019, 2, 26), "przed zalogowanie w aplikacji"),
createGradeApi(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"), createGradeApi(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"),
createGradeApi(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza") createGradeApi(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza")
) to emptyList()) ) to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet().first.sortedByDescending { it.date } GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
.first.sortedByDescending { it.date }
}
assertFalse { grades[0].isRead } assertFalse { grades[0].isRead }
assertFalse { grades[1].isRead } assertFalse { grades[1].isRead }
@ -89,21 +86,24 @@ class GradeRepositoryTest {
@Test @Test
fun mitigateOldGradesNotifications() { fun mitigateOldGradesNotifications() {
gradeLocal.saveGrades(listOf( val list = listOf(
createGradeLocal(5, 3.0, of(2019, 2, 25), "Jedna ocena"), createGradeLocal(5, 3.0, of(2019, 2, 25), "Jedna ocena"),
createGradeLocal(4, 4.0, of(2019, 2, 26), "Druga"), createGradeLocal(4, 4.0, of(2019, 2, 26), "Druga"),
createGradeLocal(3, 5.0, of(2019, 2, 27), "Trzecia") createGradeLocal(3, 5.0, of(2019, 2, 27), "Trzecia")
)) )
runBlocking { gradeLocal.saveGrades(list) }
every { mockSdk.getGrades(1) } returns Single.just(listOf( coEvery { mockSdk.getGrades(1) } returns (listOf(
createGradeApi(5, 2.0, of(2019, 2, 25), "Ocena ma datę, jest inna, ale nie zostanie powiadomiona"), createGradeApi(5, 2.0, of(2019, 2, 25), "Ocena ma datę, jest inna, ale nie zostanie powiadomiona"),
createGradeApi(4, 3.0, of(2019, 2, 26), "starszą niż ostatnia lokalnie"), createGradeApi(4, 3.0, of(2019, 2, 26), "starszą niż ostatnia lokalnie"),
createGradeApi(3, 4.0, of(2019, 2, 27), "Ta jest z tego samego dnia co ostatnia lokalnie"), createGradeApi(3, 4.0, of(2019, 2, 27), "Ta jest z tego samego dnia co ostatnia lokalnie"),
createGradeApi(2, 5.0, of(2019, 2, 28), "Ta jest już w ogóle nowa") createGradeApi(2, 5.0, of(2019, 2, 28), "Ta jest już w ogóle nowa")
) to emptyList()) ) to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet().first.sortedByDescending { it.date } GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
.first.sortedByDescending { it.date }
}
assertFalse { grades[0].isRead } assertFalse { grades[0].isRead }
assertFalse { grades[1].isRead } assertFalse { grades[1].isRead }
@ -113,69 +113,76 @@ class GradeRepositoryTest {
@Test @Test
fun subtractLocaleDuplicateGrades() { fun subtractLocaleDuplicateGrades() {
gradeLocal.saveGrades(listOf( val list = listOf(
createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
)) )
runBlocking { gradeLocal.saveGrades(list) }
every { mockSdk.getGrades(1) } returns Single.just(listOf( coEvery { mockSdk.getGrades(1) } returns (listOf(
createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
) to emptyList()) ) to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet() GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
}
assertEquals(2, grades.first.size) assertEquals(2, grades.first.size)
} }
@Test @Test
fun subtractRemoteDuplicateGrades() { fun subtractRemoteDuplicateGrades() {
gradeLocal.saveGrades(listOf( val list = listOf(
createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
)) )
runBlocking { gradeLocal.saveGrades(list) }
every { mockSdk.getGrades(1) } returns Single.just(listOf( coEvery { mockSdk.getGrades(1) } returns (listOf(
createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
) to emptyList()) ) to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet() GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
}
assertEquals(3, grades.first.size) assertEquals(3, grades.first.size)
} }
@Test @Test
fun emptyLocal() { fun emptyLocal() {
gradeLocal.saveGrades(listOf()) runBlocking { gradeLocal.saveGrades(listOf()) }
every { mockSdk.getGrades(1) } returns Single.just(listOf( coEvery { mockSdk.getGrades(1) } returns (listOf(
createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
) to emptyList()) ) to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet() GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
}
assertEquals(3, grades.first.size) assertEquals(3, grades.first.size)
} }
@Test @Test
fun emptyRemote() { fun emptyRemote() {
gradeLocal.saveGrades(listOf( val list = listOf(
createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"),
createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena")
)) )
runBlocking { gradeLocal.saveGrades(list) }
every { mockSdk.getGrades(1) } returns Single.just(emptyList<Grade>() to emptyList()) coEvery { mockSdk.getGrades(1) } returns (emptyList<Grade>() to emptyList())
val grades = GradeRepository(settings, gradeLocal, gradeRemote) val grades = runBlocking {
.getGrades(studentMock, semesterMock, true).blockingGet() GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true)
}
assertEquals(0, grades.first.size) assertEquals(0, grades.first.size)
} }

View File

@ -7,6 +7,7 @@ import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradePointsStatistics
import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.GradeStatistics
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -35,25 +36,27 @@ class GradeStatisticsLocalTest {
@Test @Test
fun saveAndRead_subject() { fun saveAndRead_subject() {
gradeStatisticsLocal.saveGradesStatistics(listOf( val list = listOf(
getGradeStatistics("Matematyka", 2, 1), getGradeStatistics("Matematyka", 2, 1),
getGradeStatistics("Fizyka", 1, 2) getGradeStatistics("Fizyka", 1, 2)
)) )
runBlocking { gradeStatisticsLocal.saveGradesStatistics(list) }
val stats = gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Matematyka").blockingGet() val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Matematyka") }
assertEquals(1, stats.size) assertEquals(1, stats.size)
assertEquals(stats[0].subject, "Matematyka") assertEquals(stats[0].subject, "Matematyka")
} }
@Test @Test
fun saveAndRead_all() { fun saveAndRead_all() {
gradeStatisticsLocal.saveGradesStatistics(listOf( val list = listOf(
getGradeStatistics("Matematyka", 2, 1), getGradeStatistics("Matematyka", 2, 1),
getGradeStatistics("Chemia", 2, 1), getGradeStatistics("Chemia", 2, 1),
getGradeStatistics("Fizyka", 1, 2) getGradeStatistics("Fizyka", 1, 2)
)) )
runBlocking { gradeStatisticsLocal.saveGradesStatistics(list) }
val stats = gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Wszystkie").blockingGet() val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Wszystkie") }
assertEquals(3, stats.size) assertEquals(3, stats.size)
assertEquals(stats[0].subject, "Wszystkie") assertEquals(stats[0].subject, "Wszystkie")
assertEquals(stats[1].subject, "Matematyka") assertEquals(stats[1].subject, "Matematyka")
@ -62,13 +65,14 @@ class GradeStatisticsLocalTest {
@Test @Test
fun saveAndRead_points() { fun saveAndRead_points() {
gradeStatisticsLocal.saveGradesPointsStatistics(listOf( val list = listOf(
getGradePointsStatistics("Matematyka", 2, 1), getGradePointsStatistics("Matematyka", 2, 1),
getGradePointsStatistics("Chemia", 2, 1), getGradePointsStatistics("Chemia", 2, 1),
getGradePointsStatistics("Fizyka", 1, 2) getGradePointsStatistics("Fizyka", 1, 2)
)) )
runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(list) }
val stats = gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka").blockingGet() val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka") }
with(stats[0]) { with(stats[0]) {
assertEquals(subject, "Matematyka") assertEquals(subject, "Matematyka")
assertEquals(others, 5.0) assertEquals(others, 5.0)
@ -78,18 +82,18 @@ class GradeStatisticsLocalTest {
@Test @Test
fun saveAndRead_subjectEmpty() { fun saveAndRead_subjectEmpty() {
gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) }
val stats = gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka").blockingGet() val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka") }
assertEquals(null, stats) assertEquals(emptyList(), stats)
} }
@Test @Test
fun saveAndRead_allEmpty() { fun saveAndRead_allEmpty() {
gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) }
val stats = gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Wszystkie").blockingGet() val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Wszystkie") }
assertEquals(null, stats) assertEquals(emptyList(), stats)
} }
private fun getSemester(): Semester { private fun getSemester(): Semester {

View File

@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
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 kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -23,7 +24,8 @@ class LuckyNumberLocalTest {
@Before @Before
fun createDb() { fun createDb() {
testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) testDb = Room
.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java)
.build() .build()
luckyNumberLocal = LuckyNumberLocal(testDb.luckyNumberDao) luckyNumberLocal = LuckyNumberLocal(testDb.luckyNumberDao)
} }
@ -35,14 +37,14 @@ class LuckyNumberLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
luckyNumberLocal.saveLuckyNumber(LuckyNumber(1, LocalDate.of(2019, 1, 20), 14)) val number = LuckyNumber(1, LocalDate.of(2019, 1, 20), 14)
runBlocking { luckyNumberLocal.saveLuckyNumber(number) }
val luckyNumber = luckyNumberLocal.getLuckyNumber(Student("", "", "", "", "", "", false, "", "", "", 1, 1, "", "", "", "", "", 1, false, now()), val student = Student("", "", "", "", "", "", false, "", "", "", 1, 1, "", "", "", "", "", 1, false, now())
LocalDate.of(2019, 1, 20) val luckyNumber = runBlocking { luckyNumberLocal.getLuckyNumber(student, LocalDate.of(2019, 1, 20)) }
).blockingGet()
assertEquals(1, luckyNumber.studentId) assertEquals(1, luckyNumber?.studentId)
assertEquals(LocalDate.of(2019, 1, 20), luckyNumber.date) assertEquals(LocalDate.of(2019, 1, 20), luckyNumber?.date)
assertEquals(14, luckyNumber.luckyNumber) assertEquals(14, luckyNumber?.luckyNumber)
} }
} }

View File

@ -7,6 +7,7 @@ import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Recipient
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -35,17 +36,21 @@ class RecipientLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
recipientLocal.saveRecipients(listOf( val list = listOf(
Recipient(1, "2rPracownik", "Kowalski Jan", "Kowalski Jan [KJ] - Pracownik (Fake123456)", 3, 4, 2, "hash"), Recipient(1, "2rPracownik", "Kowalski Jan", "Kowalski Jan [KJ] - Pracownik (Fake123456)", 3, 4, 2, "hash"),
Recipient(1, "3rPracownik", "Kowalska Karolina", "Kowalska Karolina [KK] - Pracownik (Fake123456)", 4, 4, 2, "hash"), Recipient(1, "3rPracownik", "Kowalska Karolina", "Kowalska Karolina [KK] - Pracownik (Fake123456)", 4, 4, 2, "hash"),
Recipient(1, "4rPracownik", "Krupa Stanisław", "Krupa Stanisław [KS] - Uczeń (Fake123456)", 5, 4, 1, "hash") Recipient(1, "4rPracownik", "Krupa Stanisław", "Krupa Stanisław [KS] - Uczeń (Fake123456)", 5, 4, 1, "hash")
)) )
runBlocking { recipientLocal.saveRecipients(list) }
val recipients = recipientLocal.getRecipients( val student = Student("fakelog.cf", "AUTO", "", "", "", "", false, "", "", "", 1, 0, "", "", "", "", "", 1, true, LocalDateTime.now())
Student("fakelog.cf", "AUTO", "", "", "", "", false, "", "", "", 1, 0, "", "", "", "", "", 1, true, LocalDateTime.now()), val recipients = runBlocking {
2, recipientLocal.getRecipients(
ReportingUnit(1, 4, "", 0, "", emptyList()) student = student,
).blockingGet() role = 2,
unit = ReportingUnit(1, 4, "", 0, "", emptyList())
)
}
assertEquals(2, recipients.size) assertEquals(2, recipients.size)
assertEquals(1, recipients[0].studentId) assertEquals(1, recipients[0].studentId)

View File

@ -6,6 +6,7 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.repositories.getStudent import io.github.wulkanowy.data.repositories.getStudent
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -36,9 +37,9 @@ class StudentLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
studentLocal.saveStudents(listOf(student)).blockingGet() runBlocking { studentLocal.saveStudents(listOf(student)) }
val student = studentLocal.getCurrentStudent(true).blockingGet() val student = runBlocking { studentLocal.getCurrentStudent(true) }
assertEquals("23", student.schoolSymbol) assertEquals("23", student?.schoolSymbol)
} }
} }

View File

@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -34,17 +35,21 @@ class TimetableLocalTest {
@Test @Test
fun saveAndReadTest() { fun saveAndReadTest() {
timetableDb.saveTimetable(listOf( val list = listOf(
createTimetableLocal(of(2018, 9, 10, 0, 0, 0), 1), createTimetableLocal(of(2018, 9, 10, 0, 0, 0), 1),
createTimetableLocal(of(2018, 9, 14, 0, 0, 0), 1), createTimetableLocal(of(2018, 9, 14, 0, 0, 0), 1),
createTimetableLocal(of(2018, 9, 17, 0, 0, 0), 1) createTimetableLocal(of(2018, 9, 17, 0, 0, 0), 1)
)) )
runBlocking { timetableDb.saveTimetable(list) }
val exams = timetableDb.getTimetable( val semester = Semester(1, 2, "", 1, 1, 2019, LocalDate.now(), LocalDate.now(), 1, 1)
Semester(1, 2, "", 1, 1, 2019, LocalDate.now(), LocalDate.now(), 1, 1), val exams = runBlocking {
LocalDate.of(2018, 9, 10), timetableDb.getTimetable(
LocalDate.of(2018, 9, 14) semester = semester,
).blockingGet() startDate = LocalDate.of(2018, 9, 10),
endDate = LocalDate.of(2018, 9, 14)
)
}
assertEquals(2, exams.size) assertEquals(2, exams.size)
assertEquals(exams[0].date, LocalDate.of(2018, 9, 10)) assertEquals(exams[0].date, LocalDate.of(2018, 9, 10))

View File

@ -5,19 +5,18 @@ import androidx.room.Room
import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress import androidx.test.filters.SdkSuppress
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.TestInternetObservingStrategy
import io.github.wulkanowy.data.repositories.getStudent import io.github.wulkanowy.data.repositories.getStudent
import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper
import io.mockk.MockKAnnotations import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.every import io.mockk.every
import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.MockK
import io.mockk.mockk import io.mockk.mockk
import io.reactivex.Single import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -33,10 +32,6 @@ class TimetableRepositoryTest {
@MockK @MockK
private lateinit var mockSdk: Sdk private lateinit var mockSdk: Sdk
private val settings = InternetObservingSettings.builder()
.strategy(TestInternetObservingStrategy())
.build()
@MockK @MockK
private lateinit var studentMock: Student private lateinit var studentMock: Student
@ -82,23 +77,31 @@ class TimetableRepositoryTest {
@Test @Test
fun copyRoomToCompletedFromPrevious() { fun copyRoomToCompletedFromPrevious() {
runBlocking {
timetableLocal.saveTimetable(listOf( timetableLocal.saveTimetable(listOf(
createTimetableLocal(of(2019, 3, 5, 8, 0), 1, "123", "Przyroda"), createTimetableLocal(of(2019, 3, 5, 8, 0), 1, "123", "Przyroda"),
createTimetableLocal(of(2019, 3, 5, 8, 50), 2, "321", "Religia"), createTimetableLocal(of(2019, 3, 5, 8, 50), 2, "321", "Religia"),
createTimetableLocal(of(2019, 3, 5, 9, 40), 3, "213", "W-F"), createTimetableLocal(of(2019, 3, 5, 9, 40), 3, "213", "W-F"),
createTimetableLocal(of(2019, 3, 5, 10, 30),3, "213", "W-F", "Jan Kowalski") createTimetableLocal(of(2019, 3, 5, 10, 30), 3, "213", "W-F", "Jan Kowalski")
)) ))
}
every { mockSdk.getTimetable(any(), any()) } returns Single.just(listOf( coEvery { mockSdk.getTimetable(any(), any()) } returns listOf(
createTimetableRemote(of(2019, 3, 5, 8, 0), 1, "", "Przyroda"), createTimetableRemote(of(2019, 3, 5, 8, 0), 1, "", "Przyroda"),
createTimetableRemote(of(2019, 3, 5, 8, 50), 2, "", "Religia"), createTimetableRemote(of(2019, 3, 5, 8, 50), 2, "", "Religia"),
createTimetableRemote(of(2019, 3, 5, 9, 40), 3, "", "W-F"), createTimetableRemote(of(2019, 3, 5, 9, 40), 3, "", "W-F"),
createTimetableRemote(of(2019, 3, 5, 10, 30), 4, "", "W-F") createTimetableRemote(of(2019, 3, 5, 10, 30), 4, "", "W-F")
)) )
val lessons = TimetableRepository(settings, timetableLocal, timetableRemote, timetableNotificationSchedulerHelper) val lessons = runBlocking {
.getTimetable(student, semesterMock, LocalDate.of(2019, 3, 5), LocalDate.of(2019, 3, 5), true) TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable(
.blockingGet() student = student,
semester = semesterMock,
start = LocalDate.of(2019, 3, 5),
end = LocalDate.of(2019, 3, 5),
forceRefresh = true
)
}
assertEquals(4, lessons.size) assertEquals(4, lessons.size)
assertEquals("123", lessons[0].room) assertEquals("123", lessons[0].room)
@ -108,7 +111,7 @@ class TimetableRepositoryTest {
@Test @Test
fun copyTeacherToCompletedFromPrevious() { fun copyTeacherToCompletedFromPrevious() {
timetableLocal.saveTimetable(listOf( val list = listOf(
createTimetableLocal(of(2019, 12, 23, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false), createTimetableLocal(of(2019, 12, 23, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false),
createTimetableLocal(of(2019, 12, 23, 8, 50), 2, "124", "Matematyka", "Paweł Poniedziałkowski", false), createTimetableLocal(of(2019, 12, 23, 8, 50), 2, "124", "Matematyka", "Paweł Poniedziałkowski", false),
createTimetableLocal(of(2019, 12, 23, 9, 40), 3, "125", "Język polski", "Joanna Wtorkowska", true), createTimetableLocal(of(2019, 12, 23, 9, 40), 3, "125", "Język polski", "Joanna Wtorkowska", true),
@ -123,9 +126,10 @@ class TimetableRepositoryTest {
createTimetableLocal(of(2019, 12, 25, 8, 50), 2, "124", "Matematyka", "", false), createTimetableLocal(of(2019, 12, 25, 8, 50), 2, "124", "Matematyka", "", false),
createTimetableLocal(of(2019, 12, 25, 9, 40), 3, "125", "Matematyka", "", true), createTimetableLocal(of(2019, 12, 25, 9, 40), 3, "125", "Matematyka", "", true),
createTimetableLocal(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "", true) createTimetableLocal(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "", true)
)) )
runBlocking { timetableLocal.saveTimetable(list) }
every { mockSdk.getTimetable(any(), any()) } returns Single.just(listOf( coEvery { mockSdk.getTimetable(any(), any()) } returns listOf(
createTimetableRemote(of(2019, 12, 23, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false), createTimetableRemote(of(2019, 12, 23, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false),
createTimetableRemote(of(2019, 12, 23, 8, 50), 2, "124", "Matematyka", "Jakub Wtorkowski", true), createTimetableRemote(of(2019, 12, 23, 8, 50), 2, "124", "Matematyka", "Jakub Wtorkowski", true),
createTimetableRemote(of(2019, 12, 23, 9, 40), 3, "125", "Język polski", "Joanna Poniedziałkowska", false), createTimetableRemote(of(2019, 12, 23, 9, 40), 3, "125", "Język polski", "Joanna Poniedziałkowska", false),
@ -140,11 +144,17 @@ class TimetableRepositoryTest {
createTimetableRemote(of(2019, 12, 25, 8, 50), 2, "124", "Matematyka", "Paweł Czwartkowski", true), createTimetableRemote(of(2019, 12, 25, 8, 50), 2, "124", "Matematyka", "Paweł Czwartkowski", true),
createTimetableRemote(of(2019, 12, 25, 9, 40), 3, "125", "Matematyka", "Paweł Środowski", false), createTimetableRemote(of(2019, 12, 25, 9, 40), 3, "125", "Matematyka", "Paweł Środowski", false),
createTimetableRemote(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true) createTimetableRemote(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true)
)) )
val lessons = TimetableRepository(settings, timetableLocal, timetableRemote, timetableNotificationSchedulerHelper) val lessons = runBlocking {
.getTimetable(student, semesterMock, LocalDate.of(2019, 12, 23), LocalDate.of(2019, 12, 25), true) TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable(
.blockingGet() student = student,
semester = semesterMock,
start = LocalDate.of(2019, 12, 23),
end = LocalDate.of(2019, 12, 25),
forceRefresh = true
)
}
assertEquals(12, lessons.size) assertEquals(12, lessons.size)

View File

@ -8,8 +8,6 @@ import androidx.preference.PreferenceManager
import com.chuckerteam.chucker.api.ChuckerCollector import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor import com.chuckerteam.chucker.api.ChuckerInterceptor
import com.chuckerteam.chucker.api.RetentionManager import com.chuckerteam.chucker.api.RetentionManager
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.strategy.WalledGardenInternetObservingStrategy
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.AppDatabase
@ -22,14 +20,6 @@ import javax.inject.Singleton
@Module @Module
internal class RepositoryModule { internal class RepositoryModule {
@Singleton
@Provides
fun provideInternetObservingSettings(): InternetObservingSettings {
return InternetObservingSettings.builder()
.strategy(WalledGardenInternetObservingStrategy())
.build()
}
@Singleton @Singleton
@Provides @Provides
fun provideSdk(chuckerCollector: ChuckerCollector, context: Context): Sdk { fun provideSdk(chuckerCollector: ChuckerCollector, context: Context): Sdk {

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Attendance
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface AttendanceDao : BaseDao<Attendance> { interface AttendanceDao : BaseDao<Attendance> {
@Query("SELECT * FROM Attendance WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") @Query("SELECT * FROM Attendance WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Attendance>> suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Attendance>
} }

View File

@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.AttendanceSummary
import io.reactivex.Maybe
@Dao @Dao
interface AttendanceSummaryDao : BaseDao<AttendanceSummary> { interface AttendanceSummaryDao : BaseDao<AttendanceSummary> {
@Query("SELECT * FROM AttendanceSummary WHERE diary_id = :diaryId AND student_id = :studentId AND subject_id = :subjectId") @Query("SELECT * FROM AttendanceSummary WHERE diary_id = :diaryId AND student_id = :studentId AND subject_id = :subjectId")
fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): Maybe<List<AttendanceSummary>> suspend fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): List<AttendanceSummary>
} }

View File

@ -7,11 +7,11 @@ import androidx.room.Update
interface BaseDao<T> { interface BaseDao<T> {
@Insert @Insert
fun insertAll(items: List<T>): List<Long> suspend fun insertAll(items: List<T>): List<Long>
@Update @Update
fun updateAll(items: List<T>) suspend fun updateAll(items: List<T>)
@Delete @Delete
fun deleteAll(items: List<T>) suspend fun deleteAll(items: List<T>)
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.CompletedLesson
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface CompletedLessonsDao : BaseDao<CompletedLesson> { interface CompletedLessonsDao : BaseDao<CompletedLesson> {
@Query("SELECT * FROM CompletedLesson WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") @Query("SELECT * FROM CompletedLesson WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<CompletedLesson>> suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<CompletedLesson>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Exam
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface ExamDao : BaseDao<Exam> { interface ExamDao : BaseDao<Exam> {
@Query("SELECT * FROM Exams WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") @Query("SELECT * FROM Exams WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Exam>> suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Exam>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Grade
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,6 +10,5 @@ import javax.inject.Singleton
interface GradeDao : BaseDao<Grade> { interface GradeDao : BaseDao<Grade> {
@Query("SELECT * FROM Grades WHERE semester_id = :semesterId AND student_id = :studentId") @Query("SELECT * FROM Grades WHERE semester_id = :semesterId AND student_id = :studentId")
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<Grade>> suspend fun loadAll(semesterId: Int, studentId: Int): List<Grade>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradePointsStatistics
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,8 +10,8 @@ import javax.inject.Singleton
interface GradePointsStatisticsDao : BaseDao<GradePointsStatistics> { interface GradePointsStatisticsDao : BaseDao<GradePointsStatistics> {
@Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName") @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName")
fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): Maybe<List<GradePointsStatistics>> suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): List<GradePointsStatistics>
@Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId") @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId")
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<GradePointsStatistics>> suspend fun loadAll(semesterId: Int, studentId: Int): List<GradePointsStatistics>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.GradeStatistics
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,8 +10,8 @@ import javax.inject.Singleton
interface GradeStatisticsDao : BaseDao<GradeStatistics> { interface GradeStatisticsDao : BaseDao<GradeStatistics> {
@Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName AND is_semester = :isSemester") @Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName AND is_semester = :isSemester")
fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): Maybe<List<GradeStatistics>> suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): List<GradeStatistics>
@Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND is_semester = :isSemester") @Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND is_semester = :isSemester")
fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): Maybe<List<GradeStatistics>> suspend fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): List<GradeStatistics>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.GradeSummary
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface GradeSummaryDao : BaseDao<GradeSummary> { interface GradeSummaryDao : BaseDao<GradeSummary> {
@Query("SELECT * FROM GradesSummary WHERE student_id = :studentId AND semester_id = :semesterId") @Query("SELECT * FROM GradesSummary WHERE student_id = :studentId AND semester_id = :semesterId")
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<GradeSummary>> suspend fun loadAll(semesterId: Int, studentId: Int): List<GradeSummary>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Homework
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface HomeworkDao : BaseDao<Homework> { interface HomeworkDao : BaseDao<Homework> {
@Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end") @Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Homework>> suspend fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Homework>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.LuckyNumber
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface LuckyNumberDao : BaseDao<LuckyNumber> { interface LuckyNumberDao : BaseDao<LuckyNumber> {
@Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date") @Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date")
fun load(studentId: Int, date: LocalDate): Maybe<LuckyNumber> suspend fun load(studentId: Int, date: LocalDate): LuckyNumber
} }

View File

@ -9,5 +9,5 @@ import io.github.wulkanowy.data.db.entities.MessageAttachment
interface MessageAttachmentDao : BaseDao<MessageAttachment> { interface MessageAttachmentDao : BaseDao<MessageAttachment> {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAttachments(items: List<MessageAttachment>): List<Long> suspend fun insertAttachments(items: List<MessageAttachment>): List<Long>
} }

View File

@ -5,19 +5,17 @@ import androidx.room.Query
import androidx.room.Transaction import androidx.room.Transaction
import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.MessageWithAttachment
import io.reactivex.Maybe
import io.reactivex.Single
@Dao @Dao
interface MessagesDao : BaseDao<Message> { interface MessagesDao : BaseDao<Message> {
@Transaction @Transaction
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId") @Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId")
fun loadMessageWithAttachment(studentId: Int, messageId: Int): Single<MessageWithAttachment> suspend fun loadMessageWithAttachment(studentId: Int, messageId: Int): MessageWithAttachment
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder AND removed = 0 ORDER BY date DESC") @Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder AND removed = 0 ORDER BY date DESC")
fun loadAll(studentId: Int, folder: Int): Maybe<List<Message>> suspend fun loadAll(studentId: Int, folder: Int): List<Message>
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC") @Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC")
fun loadDeleted(studentId: Int): Maybe<List<Message>> suspend fun loadDeleted(studentId: Int): List<Message>
} }

View File

@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.db.entities.MobileDevice
import io.reactivex.Maybe
@Dao @Dao
interface MobileDeviceDao : BaseDao<MobileDevice> { interface MobileDeviceDao : BaseDao<MobileDevice> {
@Query("SELECT * FROM MobileDevices WHERE student_id = :studentId ORDER BY date DESC") @Query("SELECT * FROM MobileDevices WHERE student_id = :studentId ORDER BY date DESC")
fun loadAll(studentId: Int): Maybe<List<MobileDevice>> suspend fun loadAll(studentId: Int): List<MobileDevice>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Note
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface NoteDao : BaseDao<Note> { interface NoteDao : BaseDao<Note> {
@Query("SELECT * FROM Notes WHERE student_id = :studentId") @Query("SELECT * FROM Notes WHERE student_id = :studentId")
fun loadAll(studentId: Int): Maybe<List<Note>> suspend fun loadAll(studentId: Int): List<Note>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Recipient
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface RecipientDao : BaseDao<Recipient> { interface RecipientDao : BaseDao<Recipient> {
@Query("SELECT * FROM Recipients WHERE student_id = :studentId AND role = :role AND unit_id = :unitId") @Query("SELECT * FROM Recipients WHERE student_id = :studentId AND role = :role AND unit_id = :unitId")
fun load(studentId: Int, role: Int, unitId: Int): Maybe<List<Recipient>> suspend fun load(studentId: Int, role: Int, unitId: Int): List<Recipient>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,8 +10,8 @@ import javax.inject.Singleton
interface ReportingUnitDao : BaseDao<ReportingUnit> { interface ReportingUnitDao : BaseDao<ReportingUnit> {
@Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId") @Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId")
fun load(studentId: Int): Maybe<List<ReportingUnit>> suspend fun load(studentId: Int): List<ReportingUnit>
@Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId AND real_id = :unitId") @Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId AND real_id = :unitId")
fun loadOne(studentId: Int, unitId: Int): Maybe<ReportingUnit> suspend fun loadOne(studentId: Int, unitId: Int): ReportingUnit?
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.data.db.entities.School
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface SchoolDao : BaseDao<School> { interface SchoolDao : BaseDao<School> {
@Query("SELECT * FROM School WHERE student_id = :studentId AND class_id = :classId") @Query("SELECT * FROM School WHERE student_id = :studentId AND class_id = :classId")
fun load(studentId: Int, classId: Int): Maybe<School> suspend fun load(studentId: Int, classId: Int): School?
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface SemesterDao : BaseDao<Semester> { interface SemesterDao : BaseDao<Semester> {
@Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId") @Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId")
fun loadAll(studentId: Int, classId: Int): Maybe<List<Semester>> suspend fun loadAll(studentId: Int, classId: Int): List<Semester>
} }

View File

@ -6,7 +6,6 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy.ABORT import androidx.room.OnConflictStrategy.ABORT
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -14,23 +13,23 @@ import javax.inject.Singleton
interface StudentDao { interface StudentDao {
@Insert(onConflict = ABORT) @Insert(onConflict = ABORT)
fun insertAll(student: List<Student>): List<Long> suspend fun insertAll(student: List<Student>): List<Long>
@Delete @Delete
fun delete(student: Student) suspend fun delete(student: Student)
@Query("SELECT * FROM Students WHERE is_current = 1") @Query("SELECT * FROM Students WHERE is_current = 1")
fun loadCurrent(): Maybe<Student> suspend fun loadCurrent(): Student?
@Query("SELECT * FROM Students WHERE id = :id") @Query("SELECT * FROM Students WHERE id = :id")
fun loadById(id: Int): Maybe<Student> suspend fun loadById(id: Int): Student?
@Query("SELECT * FROM Students") @Query("SELECT * FROM Students")
fun loadAll(): Maybe<List<Student>> suspend fun loadAll(): List<Student>
@Query("UPDATE Students SET is_current = 1 WHERE id = :id") @Query("UPDATE Students SET is_current = 1 WHERE id = :id")
fun updateCurrent(id: Long) suspend fun updateCurrent(id: Long)
@Query("UPDATE Students SET is_current = 0") @Query("UPDATE Students SET is_current = 0")
fun resetCurrent() suspend fun resetCurrent()
} }

View File

@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.db.entities.Subject
import io.reactivex.Maybe
@Dao @Dao
interface SubjectDao : BaseDao<Subject> { interface SubjectDao : BaseDao<Subject> {
@Query("SELECT * FROM Subjects WHERE diary_id = :diaryId AND student_id = :studentId") @Query("SELECT * FROM Subjects WHERE diary_id = :diaryId AND student_id = :studentId")
fun loadAll(diaryId: Int, studentId: Int): Maybe<List<Subject>> suspend fun loadAll(diaryId: Int, studentId: Int): List<Subject>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.data.db.entities.Teacher
import io.reactivex.Maybe
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@ -11,5 +10,5 @@ import javax.inject.Singleton
interface TeacherDao : BaseDao<Teacher> { interface TeacherDao : BaseDao<Teacher> {
@Query("SELECT * FROM Teachers WHERE student_id = :studentId AND class_id = :classId") @Query("SELECT * FROM Teachers WHERE student_id = :studentId AND class_id = :classId")
fun loadAll(studentId: Int, classId: Int): Maybe<List<Teacher>> suspend fun loadAll(studentId: Int, classId: Int): List<Teacher>
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Query import androidx.room.Query
import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.db.entities.Timetable
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Singleton import javax.inject.Singleton
@ -12,5 +11,5 @@ import javax.inject.Singleton
interface TimetableDao : BaseDao<Timetable> { interface TimetableDao : BaseDao<Timetable> {
@Query("SELECT * FROM Timetable WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") @Query("SELECT * FROM Timetable WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Timetable>> suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Timetable>
} }

View File

@ -3,14 +3,19 @@ package io.github.wulkanowy.data.repositories.appcreator
import android.content.res.AssetManager import android.content.res.AssetManager
import com.google.gson.Gson import com.google.gson.Gson
import io.github.wulkanowy.data.pojos.Contributor import io.github.wulkanowy.data.pojos.Contributor
import io.reactivex.Single import io.github.wulkanowy.utils.DispatchersProvider
import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AppCreatorRepository @Inject constructor(private val assets: AssetManager) { class AppCreatorRepository @Inject constructor(
fun getAppCreators(): Single<List<Contributor>> { private val assets: AssetManager,
return Single.fromCallable { private val dispatchers: DispatchersProvider
) {
suspend fun getAppCreators(): List<Contributor> {
return withContext(dispatchers.backgroundThread) {
Gson().fromJson( Gson().fromJson(
assets.open("contributors.json").bufferedReader().use { it.readText() }, assets.open("contributors.json").bufferedReader().use { it.readText() },
Array<Contributor>::class.java Array<Contributor>::class.java

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.attendance
import io.github.wulkanowy.data.db.dao.AttendanceDao import io.github.wulkanowy.data.db.dao.AttendanceDao
import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Attendance
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,15 +10,15 @@ import javax.inject.Singleton
@Singleton @Singleton
class AttendanceLocal @Inject constructor(private val attendanceDb: AttendanceDao) { class AttendanceLocal @Inject constructor(private val attendanceDb: AttendanceDao) {
fun saveAttendance(attendance: List<Attendance>) { suspend fun saveAttendance(attendance: List<Attendance>) {
attendanceDb.insertAll(attendance) attendanceDb.insertAll(attendance)
} }
fun deleteAttendance(attendance: List<Attendance>) { suspend fun deleteAttendance(attendance: List<Attendance>) {
attendanceDb.deleteAll(attendance) attendanceDb.deleteAll(attendance)
} }
fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Attendance>> { suspend fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Attendance> {
return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() } return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
} }
} }

View File

@ -6,7 +6,6 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.sdk.pojo.Absent import io.github.wulkanowy.sdk.pojo.Absent
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import org.threeten.bp.LocalDateTime import org.threeten.bp.LocalDateTime
import org.threeten.bp.LocalTime import org.threeten.bp.LocalTime
@ -16,11 +15,10 @@ import javax.inject.Singleton
@Singleton @Singleton
class AttendanceRemote @Inject constructor(private val sdk: Sdk) { class AttendanceRemote @Inject constructor(private val sdk: Sdk) {
fun getAttendance(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<Attendance>> { suspend fun getAttendance(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Attendance> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getAttendance(startDate, endDate, semester.semesterId) .getAttendance(startDate, endDate, semester.semesterId)
.map { attendance -> .map {
attendance.map {
Attendance( Attendance(
studentId = semester.studentId, studentId = semester.studentId,
diaryId = semester.diaryId, diaryId = semester.diaryId,
@ -40,9 +38,8 @@ class AttendanceRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
fun excuseAbsence(student: Student, semester: Semester, absenceList: List<Attendance>, reason: String?): Single<Boolean> { suspend fun excuseAbsence(student: Student, semester: Semester, absenceList: List<Attendance>, reason: String?): Boolean {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).excuseForAbsence(absenceList.map { attendance -> return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).excuseForAbsence(absenceList.map { attendance ->
Absent( Absent(
date = LocalDateTime.of(attendance.date, LocalTime.of(0, 0)), date = LocalDateTime.of(attendance.date, LocalTime.of(0, 0)),

View File

@ -1,45 +1,34 @@
package io.github.wulkanowy.data.repositories.attendance package io.github.wulkanowy.data.repositories.attendance
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Attendance
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AttendanceRepository @Inject constructor( class AttendanceRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: AttendanceLocal, private val local: AttendanceLocal,
private val remote: AttendanceRemote private val remote: AttendanceRemote
) { ) {
fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean): Single<List<Attendance>> { suspend fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean): List<Attendance> {
return local.getAttendance(semester, start.monday, end.sunday).filter { !forceRefresh } return local.getAttendance(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { val new = remote.getAttendance(student, semester, start.monday, end.sunday)
if (it) remote.getAttendance(student, semester, start.monday, end.sunday) val old = local.getAttendance(semester, start.monday, end.sunday)
else Single.error(UnknownHostException())
}.flatMap { newAttendance -> local.deleteAttendance(old.uniqueSubtract(new))
local.saveAttendance(new.uniqueSubtract(old))
local.getAttendance(semester, start.monday, end.sunday) local.getAttendance(semester, start.monday, end.sunday)
.toSingle(emptyList()) }.filter { it.date in start..end }
.doOnSuccess { oldAttendance ->
local.deleteAttendance(oldAttendance.uniqueSubtract(newAttendance))
local.saveAttendance(newAttendance.uniqueSubtract(oldAttendance))
}
}.flatMap {
local.getAttendance(semester, start.monday, end.sunday)
.toSingle(emptyList())
}).map { list -> list.filter { it.date in start..end } }
} }
fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List<Attendance>, reason: String? = null): Single<Boolean> { suspend fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List<Attendance>, reason: String? = null): Boolean {
return remote.excuseAbsence(student, semester, attendanceList, reason) return remote.excuseAbsence(student, semester, attendanceList, reason)
} }
} }

View File

@ -3,22 +3,21 @@ package io.github.wulkanowy.data.repositories.attendancesummary
import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao
import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.AttendanceSummary
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AttendanceSummaryLocal @Inject constructor(private val attendanceDb: AttendanceSummaryDao) { class AttendanceSummaryLocal @Inject constructor(private val attendanceDb: AttendanceSummaryDao) {
fun saveAttendanceSummary(attendance: List<AttendanceSummary>) { suspend fun saveAttendanceSummary(attendance: List<AttendanceSummary>) {
attendanceDb.insertAll(attendance) attendanceDb.insertAll(attendance)
} }
fun deleteAttendanceSummary(attendance: List<AttendanceSummary>) { suspend fun deleteAttendanceSummary(attendance: List<AttendanceSummary>) {
attendanceDb.deleteAll(attendance) attendanceDb.deleteAll(attendance)
} }
fun getAttendanceSummary(semester: Semester, subjectId: Int): Maybe<List<AttendanceSummary>> { suspend fun getAttendanceSummary(semester: Semester, subjectId: Int): List<AttendanceSummary> {
return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId).filter { it.isNotEmpty() } return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId)
} }
} }

View File

@ -5,18 +5,16 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AttendanceSummaryRemote @Inject constructor(private val sdk: Sdk) { class AttendanceSummaryRemote @Inject constructor(private val sdk: Sdk) {
fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int): Single<List<AttendanceSummary>> { suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int): List<AttendanceSummary> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getAttendanceSummary(subjectId) .getAttendanceSummary(subjectId)
.map { attendance -> .map {
attendance.map {
AttendanceSummary( AttendanceSummary(
studentId = semester.studentId, studentId = semester.studentId,
diaryId = semester.diaryId, diaryId = semester.diaryId,
@ -32,5 +30,4 @@ class AttendanceSummaryRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,35 +1,27 @@
package io.github.wulkanowy.data.repositories.attendancesummary package io.github.wulkanowy.data.repositories.attendancesummary
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.AttendanceSummary
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class AttendanceSummaryRepository @Inject constructor( class AttendanceSummaryRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: AttendanceSummaryLocal, private val local: AttendanceSummaryLocal,
private val remote: AttendanceSummaryRemote private val remote: AttendanceSummaryRemote
) { ) {
fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean = false): Single<List<AttendanceSummary>> { suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean = false): List<AttendanceSummary> {
return local.getAttendanceSummary(semester, subjectId).filter { !forceRefresh } return local.getAttendanceSummary(semester, subjectId).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getAttendanceSummary(student, semester, subjectId)
.flatMap {
if (it) remote.getAttendanceSummary(student, semester, subjectId) val old = local.getAttendanceSummary(semester, subjectId)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getAttendanceSummary(semester, subjectId).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteAttendanceSummary(old.uniqueSubtract(new)) local.deleteAttendanceSummary(old.uniqueSubtract(new))
local.saveAttendanceSummary(new.uniqueSubtract(old)) local.saveAttendanceSummary(new.uniqueSubtract(old))
return local.getAttendanceSummary(semester, subjectId)
} }
}.flatMap { local.getAttendanceSummary(semester, subjectId).toSingle(emptyList()) })
} }
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.completedlessons
import io.github.wulkanowy.data.db.dao.CompletedLessonsDao import io.github.wulkanowy.data.db.dao.CompletedLessonsDao
import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.CompletedLesson
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,15 +10,15 @@ import javax.inject.Singleton
@Singleton @Singleton
class CompletedLessonsLocal @Inject constructor(private val completedLessonsDb: CompletedLessonsDao) { class CompletedLessonsLocal @Inject constructor(private val completedLessonsDb: CompletedLessonsDao) {
fun saveCompletedLessons(completedLessons: List<CompletedLesson>) { suspend fun saveCompletedLessons(completedLessons: List<CompletedLesson>) {
completedLessonsDb.insertAll(completedLessons) completedLessonsDb.insertAll(completedLessons)
} }
fun deleteCompleteLessons(completedLessons: List<CompletedLesson>) { suspend fun deleteCompleteLessons(completedLessons: List<CompletedLesson>) {
completedLessonsDb.deleteAll(completedLessons) completedLessonsDb.deleteAll(completedLessons)
} }
fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): Maybe<List<CompletedLesson>> { suspend fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): List<CompletedLesson> {
return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end).filter { it.isNotEmpty() } return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end)
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -13,11 +12,10 @@ import javax.inject.Singleton
@Singleton @Singleton
class CompletedLessonsRemote @Inject constructor(private val sdk: Sdk) { class CompletedLessonsRemote @Inject constructor(private val sdk: Sdk) {
fun getCompletedLessons(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<CompletedLesson>> { suspend fun getCompletedLessons(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<CompletedLesson> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getCompletedLessons(startDate, endDate) .getCompletedLessons(startDate, endDate)
.map { lessons -> .map {
lessons.map {
it.absence it.absence
CompletedLesson( CompletedLesson(
studentId = semester.studentId, studentId = semester.studentId,
@ -34,5 +32,4 @@ class CompletedLessonsRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,42 +1,30 @@
package io.github.wulkanowy.data.repositories.completedlessons package io.github.wulkanowy.data.repositories.completedlessons
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.CompletedLesson
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class CompletedLessonsRepository @Inject constructor( class CompletedLessonsRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: CompletedLessonsLocal, private val local: CompletedLessonsLocal,
private val remote: CompletedLessonsRemote private val remote: CompletedLessonsRemote
) { ) {
fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single<List<CompletedLesson>> { suspend fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List<CompletedLesson> {
return local.getCompletedLessons(semester, start.monday, end.sunday).filter { !forceRefresh } return local.getCompletedLessons(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getCompletedLessons(student, semester, start.monday, end.sunday)
.flatMap { val old = local.getCompletedLessons(semester, start.monday, end.sunday)
if (it) remote.getCompletedLessons(student, semester, start.monday, end.sunday)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getCompletedLessons(semester, start.monday, end.sunday)
.toSingle(emptyList())
.doOnSuccess { old ->
local.deleteCompleteLessons(old.uniqueSubtract(new)) local.deleteCompleteLessons(old.uniqueSubtract(new))
local.saveCompletedLessons(new.uniqueSubtract(old)) local.saveCompletedLessons(new.uniqueSubtract(old))
}
}.flatMap {
local.getCompletedLessons(semester, start.monday, end.sunday) local.getCompletedLessons(semester, start.monday, end.sunday)
.toSingle(emptyList()) }.filter { it.date in start..end }
}).map { list -> list.filter { it.date in start..end } }
} }
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.exam
import io.github.wulkanowy.data.db.dao.ExamDao import io.github.wulkanowy.data.db.dao.ExamDao
import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Exam
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,16 +10,15 @@ import javax.inject.Singleton
@Singleton @Singleton
class ExamLocal @Inject constructor(private val examDb: ExamDao) { class ExamLocal @Inject constructor(private val examDb: ExamDao) {
fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Exam>> { suspend fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Exam> {
return examDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) return examDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
.filter { it.isNotEmpty() }
} }
fun saveExams(exams: List<Exam>) { suspend fun saveExams(exams: List<Exam>) {
examDb.insertAll(exams) examDb.insertAll(exams)
} }
fun deleteExams(exams: List<Exam>) { suspend fun deleteExams(exams: List<Exam>) {
examDb.deleteAll(exams) examDb.deleteAll(exams)
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -13,11 +12,10 @@ import javax.inject.Singleton
@Singleton @Singleton
class ExamRemote @Inject constructor(private val sdk: Sdk) { class ExamRemote @Inject constructor(private val sdk: Sdk) {
fun getExams(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<Exam>> { suspend fun getExams(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Exam> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getExams(startDate, endDate, semester.semesterId) .getExams(startDate, endDate, semester.semesterId)
.map { exams -> .map {
exams.map {
Exam( Exam(
studentId = semester.studentId, studentId = semester.studentId,
diaryId = semester.diaryId, diaryId = semester.diaryId,
@ -32,5 +30,4 @@ class ExamRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,42 +1,30 @@
package io.github.wulkanowy.data.repositories.exam package io.github.wulkanowy.data.repositories.exam
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Exam
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ExamRepository @Inject constructor( class ExamRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: ExamLocal, private val local: ExamLocal,
private val remote: ExamRemote private val remote: ExamRemote
) { ) {
fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single<List<Exam>> { suspend fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List<Exam> {
return local.getExams(semester, start.monday, end.sunday).filter { !forceRefresh } return local.getExams(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getExams(student, semester, start.monday, end.sunday)
.flatMap { val old = local.getExams(semester, start.monday, end.sunday)
if (it) remote.getExams(student, semester, start.monday, end.sunday)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getExams(semester, start.monday, end.sunday)
.toSingle(emptyList())
.doOnSuccess { old ->
local.deleteExams(old.uniqueSubtract(new)) local.deleteExams(old.uniqueSubtract(new))
local.saveExams(new.uniqueSubtract(old)) local.saveExams(new.uniqueSubtract(old))
}
}.flatMap {
local.getExams(semester, start.monday, end.sunday) local.getExams(semester, start.monday, end.sunday)
.toSingle(emptyList()) }.filter { it.date in start..end }
}).map { list -> list.filter { it.date in start..end } }
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.dao.GradeSummaryDao
import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -15,35 +14,35 @@ class GradeLocal @Inject constructor(
private val gradeSummaryDb: GradeSummaryDao private val gradeSummaryDb: GradeSummaryDao
) { ) {
fun saveGrades(grades: List<Grade>) { suspend fun saveGrades(grades: List<Grade>) {
gradeDb.insertAll(grades) gradeDb.insertAll(grades)
} }
fun deleteGrades(grades: List<Grade>) { suspend fun deleteGrades(grades: List<Grade>) {
gradeDb.deleteAll(grades) gradeDb.deleteAll(grades)
} }
fun updateGrades(grades: List<Grade>) { suspend fun updateGrades(grades: List<Grade>) {
gradeDb.updateAll(grades) gradeDb.updateAll(grades)
} }
fun updateGradesSummary(gradesSummary: List<GradeSummary>) { suspend fun updateGradesSummary(gradesSummary: List<GradeSummary>) {
gradeSummaryDb.updateAll(gradesSummary) gradeSummaryDb.updateAll(gradesSummary)
} }
fun getGradesDetails(semester: Semester): Maybe<List<Grade>> { suspend fun getGradesDetails(semester: Semester): List<Grade> {
return gradeDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } return gradeDb.loadAll(semester.semesterId, semester.studentId)
} }
fun saveGradesSummary(gradesSummary: List<GradeSummary>) { suspend fun saveGradesSummary(gradesSummary: List<GradeSummary>) {
gradeSummaryDb.insertAll(gradesSummary) gradeSummaryDb.insertAll(gradesSummary)
} }
fun deleteGradesSummary(gradesSummary: List<GradeSummary>) { suspend fun deleteGradesSummary(gradesSummary: List<GradeSummary>) {
gradeSummaryDb.deleteAll(gradesSummary) gradeSummaryDb.deleteAll(gradesSummary)
} }
fun getGradesSummary(semester: Semester): Maybe<List<GradeSummary>> { suspend fun getGradesSummary(semester: Semester): List<GradeSummary> {
return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId)
} }
} }

View File

@ -6,18 +6,19 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class GradeRemote @Inject constructor(private val sdk: Sdk) { class GradeRemote @Inject constructor(private val sdk: Sdk) {
fun getGrades(student: Student, semester: Semester): Single<Pair<List<Grade>, List<GradeSummary>>> { suspend fun getGrades(student: Student, semester: Semester): Pair<List<Grade>, List<GradeSummary>> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) val (details, summary) = sdk
.init(student)
.switchDiary(semester.diaryId, semester.schoolYear)
.getGrades(semester.semesterId) .getGrades(semester.semesterId)
.map { (details, summary) ->
details.map { return details.map {
Grade( Grade(
studentId = semester.studentId, studentId = semester.studentId,
semesterId = semester.semesterId, semesterId = semester.semesterId,
@ -49,5 +50,4 @@ class GradeRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,52 +1,45 @@
package io.github.wulkanowy.data.repositories.grade package io.github.wulkanowy.data.repositories.grade
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Completable
import io.reactivex.Single
import org.threeten.bp.LocalDateTime import org.threeten.bp.LocalDateTime
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class GradeRepository @Inject constructor( class GradeRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: GradeLocal, private val local: GradeLocal,
private val remote: GradeRemote private val remote: GradeRemote
) { ) {
fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Single<Pair<List<Grade>, List<GradeSummary>>> { suspend fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Pair<List<Grade>, List<GradeSummary>> {
return local.getGradesDetails(semester).flatMap { details -> val details = local.getGradesDetails(semester)
local.getGradesSummary(semester).map { summary -> details to summary } val summaries = local.getGradesSummary(semester)
}.filter { !forceRefresh }
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { if ((details.isNotEmpty() || summaries.isNotEmpty()) && !forceRefresh) {
if (it) remote.getGrades(student, semester) return details to summaries
else Single.error(UnknownHostException()) }
}.flatMap { (newDetails, newSummary) ->
local.getGradesDetails(semester).toSingle(emptyList()) val (newDetails, newSummary) = remote.getGrades(student, semester)
.doOnSuccess { old -> val oldGrades = local.getGradesDetails(semester)
val notifyBreakDate = old.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate()
local.deleteGrades(old.uniqueSubtract(newDetails)) val notifyBreakDate = oldGrades.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate()
local.saveGrades(newDetails.uniqueSubtract(old) local.deleteGrades(oldGrades.uniqueSubtract(newDetails))
.onEach { local.saveGrades(newDetails.uniqueSubtract(oldGrades).onEach {
if (it.date >= notifyBreakDate) it.apply { if (it.date >= notifyBreakDate) it.apply {
isRead = false isRead = false
if (notify) isNotified = false if (notify) isNotified = false
} }
}) })
}.flatMap {
local.getGradesSummary(semester).toSingle(emptyList()) val oldSummaries = local.getGradesSummary(semester)
.doOnSuccess { old ->
local.deleteGradesSummary(old.uniqueSubtract(newSummary)) local.deleteGradesSummary(oldSummaries.uniqueSubtract(newSummary))
local.saveGradesSummary(newSummary.uniqueSubtract(old) local.saveGradesSummary(newSummary.uniqueSubtract(oldSummaries).onEach { summary ->
.onEach { summary -> val oldSummary = oldSummaries.find { oldSummary -> oldSummary.subject == summary.subject }
val oldSummary = old.find { oldSummary -> oldSummary.subject == summary.subject }
summary.isPredictedGradeNotified = when { summary.isPredictedGradeNotified = when {
summary.predictedGrade.isEmpty() -> true summary.predictedGrade.isEmpty() -> true
notify && oldSummary?.predictedGrade != summary.predictedGrade -> false notify && oldSummary?.predictedGrade != summary.predictedGrade -> false
@ -69,42 +62,35 @@ class GradeRepository @Inject constructor(
else -> oldSummary.finalGradeLastChange else -> oldSummary.finalGradeLastChange
} }
}) })
}
} return local.getGradesDetails(semester) to local.getGradesSummary(semester)
}.flatMap {
local.getGradesDetails(semester).toSingle(emptyList()).flatMap { details ->
local.getGradesSummary(semester).toSingle(emptyList()).map { summary ->
details to summary
}
}
})
} }
fun getUnreadGrades(semester: Semester): Single<List<Grade>> { suspend fun getUnreadGrades(semester: Semester): List<Grade> {
return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isRead } }.toSingle(emptyList()) return local.getGradesDetails(semester).filter { grade -> !grade.isRead }
} }
fun getNotNotifiedGrades(semester: Semester): Single<List<Grade>> { suspend fun getNotNotifiedGrades(semester: Semester): List<Grade> {
return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isNotified } }.toSingle(emptyList()) return local.getGradesDetails(semester).filter { grade -> !grade.isNotified }
} }
fun getNotNotifiedPredictedGrades(semester: Semester): Single<List<GradeSummary>> { suspend fun getNotNotifiedPredictedGrades(semester: Semester): List<GradeSummary> {
return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } }.toSingle(emptyList()) return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified }
} }
fun getNotNotifiedFinalGrades(semester: Semester): Single<List<GradeSummary>> { suspend fun getNotNotifiedFinalGrades(semester: Semester): List<GradeSummary> {
return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } }.toSingle(emptyList()) return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isFinalGradeNotified }
} }
fun updateGrade(grade: Grade): Completable { suspend fun updateGrade(grade: Grade) {
return Completable.fromCallable { local.updateGrades(listOf(grade)) } return local.updateGrades(listOf(grade))
} }
fun updateGrades(grades: List<Grade>): Completable { suspend fun updateGrades(grades: List<Grade>) {
return Completable.fromCallable { local.updateGrades(grades) } return local.updateGrades(grades)
} }
fun updateGradesSummary(gradesSummary: List<GradeSummary>): Completable { suspend fun updateGradesSummary(gradesSummary: List<GradeSummary>) {
return Completable.fromCallable { local.updateGradesSummary(gradesSummary) } return local.updateGradesSummary(gradesSummary)
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.dao.GradeStatisticsDao
import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradePointsStatistics
import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.GradeStatistics
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -15,46 +14,47 @@ class GradeStatisticsLocal @Inject constructor(
private val gradePointsStatisticsDb: GradePointsStatisticsDao private val gradePointsStatisticsDb: GradePointsStatisticsDao
) { ) {
fun getGradesStatistics(semester: Semester, isSemester: Boolean): Maybe<List<GradeStatistics>> { suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean): List<GradeStatistics> {
return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester).filter { it.isNotEmpty() } return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester)
} }
fun getGradesPointsStatistics(semester: Semester): Maybe<List<GradePointsStatistics>> { suspend fun getGradesPointsStatistics(semester: Semester): List<GradePointsStatistics> {
return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId)
} }
fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): Maybe<List<GradeStatistics>> { suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): List<GradeStatistics> {
return when (subjectName) { return when (subjectName) {
"Wszystkie" -> gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester).map { list -> "Wszystkie" -> {
list.groupBy { it.grade }.map { val statistics = gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester)
statistics.groupBy { it.grade }.map {
GradeStatistics(semester.studentId, semester.semesterId, subjectName, it.key, GradeStatistics(semester.studentId, semester.semesterId, subjectName, it.key,
it.value.fold(0) { acc, e -> acc + e.amount }, false) it.value.fold(0) { acc, e -> acc + e.amount }, false)
} + list } + statistics
} }
else -> gradeStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName, isSemester) else -> gradeStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName, isSemester)
}.filter { it.isNotEmpty() } }
} }
fun getGradesPointsStatistics(semester: Semester, subjectName: String): Maybe<List<GradePointsStatistics>> { suspend fun getGradesPointsStatistics(semester: Semester, subjectName: String): List<GradePointsStatistics> {
return when (subjectName) { return when (subjectName) {
"Wszystkie" -> gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId) "Wszystkie" -> gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId)
else -> gradePointsStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName) else -> gradePointsStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName)
}.filter { it.isNotEmpty() } }
} }
fun saveGradesStatistics(gradesStatistics: List<GradeStatistics>) { suspend fun saveGradesStatistics(gradesStatistics: List<GradeStatistics>) {
gradeStatisticsDb.insertAll(gradesStatistics) gradeStatisticsDb.insertAll(gradesStatistics)
} }
fun saveGradesPointsStatistics(gradePointsStatistics: List<GradePointsStatistics>) { suspend fun saveGradesPointsStatistics(gradePointsStatistics: List<GradePointsStatistics>) {
gradePointsStatisticsDb.insertAll(gradePointsStatistics) gradePointsStatisticsDb.insertAll(gradePointsStatistics)
} }
fun deleteGradesStatistics(gradesStatistics: List<GradeStatistics>) { suspend fun deleteGradesStatistics(gradesStatistics: List<GradeStatistics>) {
gradeStatisticsDb.deleteAll(gradesStatistics) gradeStatisticsDb.deleteAll(gradesStatistics)
} }
fun deleteGradesPointsStatistics(gradesPointsStatistics: List<GradePointsStatistics>) { suspend fun deleteGradesPointsStatistics(gradesPointsStatistics: List<GradePointsStatistics>) {
gradePointsStatisticsDb.deleteAll(gradesPointsStatistics) gradePointsStatisticsDb.deleteAll(gradesPointsStatistics)
} }
} }

View File

@ -6,19 +6,17 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class GradeStatisticsRemote @Inject constructor(private val sdk: Sdk) { class GradeStatisticsRemote @Inject constructor(private val sdk: Sdk) {
fun getGradeStatistics(student: Student, semester: Semester, isSemester: Boolean): Single<List<GradeStatistics>> { suspend fun getGradeStatistics(student: Student, semester: Semester, isSemester: Boolean): List<GradeStatistics> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).let { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).let {
if (isSemester) it.getGradesAnnualStatistics(semester.semesterId) if (isSemester) it.getGradesAnnualStatistics(semester.semesterId)
else it.getGradesPartialStatistics(semester.semesterId) else it.getGradesPartialStatistics(semester.semesterId)
}.map { gradeStatistics -> }.map {
gradeStatistics.map {
GradeStatistics( GradeStatistics(
semesterId = semester.semesterId, semesterId = semester.semesterId,
studentId = semester.studentId, studentId = semester.studentId,
@ -29,13 +27,11 @@ class GradeStatisticsRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
fun getGradePointsStatistics(student: Student, semester: Semester): Single<List<GradePointsStatistics>> { suspend fun getGradePointsStatistics(student: Student, semester: Semester): List<GradePointsStatistics> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getGradesPointsStatistics(semester.semesterId) .getGradesPointsStatistics(semester.semesterId)
.map { gradePointsStatistics -> .map {
gradePointsStatistics.map {
GradePointsStatistics( GradePointsStatistics(
semesterId = semester.semesterId, semesterId = semester.semesterId,
studentId = semester.studentId, studentId = semester.studentId,
@ -45,5 +41,4 @@ class GradeStatisticsRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,7 +1,5 @@
package io.github.wulkanowy.data.repositories.gradestatistics package io.github.wulkanowy.data.repositories.gradestatistics
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradePointsStatistics
import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.GradeStatistics
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
@ -9,50 +7,40 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.data.pojos.GradeStatisticsItem
import io.github.wulkanowy.ui.modules.grade.statistics.ViewType import io.github.wulkanowy.ui.modules.grade.statistics.ViewType
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class GradeStatisticsRepository @Inject constructor( class GradeStatisticsRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: GradeStatisticsLocal, private val local: GradeStatisticsLocal,
private val remote: GradeStatisticsRemote private val remote: GradeStatisticsRemote
) { ) {
fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean = false): Single<List<GradeStatisticsItem>> { suspend fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean = false): List<GradeStatisticsItem> {
return local.getGradesStatistics(semester, isSemester, subjectName).map { it.mapToStatisticItems() }.filter { !forceRefresh } return local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems().filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getGradeStatistics(student, semester, isSemester)
.flatMap { val old = local.getGradesStatistics(semester, isSemester)
if (it) remote.getGradeStatistics(student, semester, isSemester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getGradesStatistics(semester, isSemester).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteGradesStatistics(old.uniqueSubtract(new)) local.deleteGradesStatistics(old.uniqueSubtract(new))
local.saveGradesStatistics(new.uniqueSubtract(old)) local.saveGradesStatistics(new.uniqueSubtract(old))
local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems()
} }
}.flatMap { local.getGradesStatistics(semester, isSemester, subjectName).map { it.mapToStatisticItems() }.toSingle(emptyList()) })
} }
fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): Single<List<GradeStatisticsItem>> { suspend fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): List<GradeStatisticsItem> {
return local.getGradesPointsStatistics(semester, subjectName).map { it.mapToStatisticsItem() }.filter { !forceRefresh } return local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem().filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getGradePointsStatistics(student, semester)
.flatMap { val old = local.getGradesPointsStatistics(semester)
if (it) remote.getGradePointsStatistics(student, semester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getGradesPointsStatistics(semester).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteGradesPointsStatistics(old.uniqueSubtract(new)) local.deleteGradesPointsStatistics(old.uniqueSubtract(new))
local.saveGradesPointsStatistics(new.uniqueSubtract(old)) local.saveGradesPointsStatistics(new.uniqueSubtract(old))
local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem()
} }
}.flatMap { local.getGradesPointsStatistics(semester, subjectName).map { it.mapToStatisticsItem() }.toSingle(emptyList()) })
} }
private fun List<GradeStatistics>.mapToStatisticItems(): List<GradeStatisticsItem> { private fun List<GradeStatistics>.mapToStatisticItems() = groupBy { it.subject }.map {
return groupBy { it.subject }.map {
GradeStatisticsItem( GradeStatisticsItem(
type = ViewType.PARTIAL, type = ViewType.PARTIAL,
partial = it.value partial = it.value
@ -61,15 +49,12 @@ class GradeStatisticsRepository @Inject constructor(
points = null points = null
) )
} }
}
private fun List<GradePointsStatistics>.mapToStatisticsItem(): List<GradeStatisticsItem> { private fun List<GradePointsStatistics>.mapToStatisticsItem() = map {
return map {
GradeStatisticsItem( GradeStatisticsItem(
type = ViewType.POINTS, type = ViewType.POINTS,
partial = emptyList(), partial = emptyList(),
points = it points = it
) )
} }
}
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.homework
import io.github.wulkanowy.data.db.dao.HomeworkDao import io.github.wulkanowy.data.db.dao.HomeworkDao
import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Homework
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,20 +10,19 @@ import javax.inject.Singleton
@Singleton @Singleton
class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) { class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) {
fun saveHomework(homework: List<Homework>) { suspend fun saveHomework(homework: List<Homework>) {
homeworkDb.insertAll(homework) homeworkDb.insertAll(homework)
} }
fun deleteHomework(homework: List<Homework>) { suspend fun deleteHomework(homework: List<Homework>) {
homeworkDb.deleteAll(homework) homeworkDb.deleteAll(homework)
} }
fun updateHomework(homework: List<Homework>) { suspend fun updateHomework(homework: List<Homework>) {
homeworkDb.updateAll(homework) homeworkDb.updateAll(homework)
} }
fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Homework>> { suspend fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Homework> {
return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate) return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate)
.filter { it.isNotEmpty() }
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -13,11 +12,10 @@ import javax.inject.Singleton
@Singleton @Singleton
class HomeworkRemote @Inject constructor(private val sdk: Sdk) { class HomeworkRemote @Inject constructor(private val sdk: Sdk) {
fun getHomework(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<Homework>> { suspend fun getHomework(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Homework> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getHomework(startDate, endDate) .getHomework(startDate, endDate)
.map { homework -> .map {
homework.map {
Homework( Homework(
semesterId = semester.semesterId, semesterId = semester.semesterId,
studentId = semester.studentId, studentId = semester.studentId,
@ -31,5 +29,4 @@ class HomeworkRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,49 +1,37 @@
package io.github.wulkanowy.data.repositories.homework package io.github.wulkanowy.data.repositories.homework
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Homework
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Completable
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class HomeworkRepository @Inject constructor( class HomeworkRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: HomeworkLocal, private val local: HomeworkLocal,
private val remote: HomeworkRemote private val remote: HomeworkRemote
) { ) {
fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single<List<Homework>> { suspend fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List<Homework> {
return Single.fromCallable { start.monday to end.sunday }.flatMap { (monday, friday) -> return local.getHomework(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty {
local.getHomework(semester, monday, friday).filter { !forceRefresh } val new = remote.getHomework(student, semester, start.monday, end.sunday)
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
.flatMap { val old = local.getHomework(semester, start.monday, end.sunday)
if (it) remote.getHomework(student, semester, monday, friday)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getHomework(semester, monday, friday).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteHomework(old.uniqueSubtract(new)) local.deleteHomework(old.uniqueSubtract(new))
local.saveHomework(new.uniqueSubtract(old)) local.saveHomework(new.uniqueSubtract(old))
}
}.flatMap { local.getHomework(semester, monday, friday).toSingle(emptyList()) }) local.getHomework(semester, start.monday, end.sunday)
} }
} }
fun toggleDone(homework: Homework): Completable { suspend fun toggleDone(homework: Homework) {
return Completable.fromCallable {
local.updateHomework(listOf(homework.apply { local.updateHomework(listOf(homework.apply {
isDone = !isDone isDone = !isDone
})) }))
} }
}
} }

View File

@ -1,29 +1,31 @@
package io.github.wulkanowy.data.repositories.logger package io.github.wulkanowy.data.repositories.logger
import android.content.Context import android.content.Context
import io.reactivex.Single import io.github.wulkanowy.utils.DispatchersProvider
import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import javax.inject.Inject import javax.inject.Inject
class LoggerRepository @Inject constructor(private val context: Context) { class LoggerRepository @Inject constructor(
private val context: Context,
private val dispatchers: DispatchersProvider
) {
fun getLastLogLines(): Single<List<String>> { suspend fun getLastLogLines(): List<String> {
return getLastModified() return getLastModified().readText().split("\n")
.map { it.readText() }
.map { it.split("\n") }
} }
fun getLogFiles(): Single<List<File>> { suspend fun getLogFiles(): List<File> {
return Single.fromCallable { return withContext(dispatchers.backgroundThread) {
File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter { File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter {
it.name.endsWith(".log") it.name.endsWith(".log")
} }!!
} }
} }
private fun getLastModified(): Single<File> { private suspend fun getLastModified(): File {
return Single.fromCallable { return withContext(dispatchers.backgroundThread) {
var lastModifiedTime = Long.MIN_VALUE var lastModifiedTime = Long.MIN_VALUE
var chosenFile: File? = null var chosenFile: File? = null
File(context.filesDir.absolutePath).listFiles(File::isFile)?.forEach { file -> File(context.filesDir.absolutePath).listFiles(File::isFile)?.forEach { file ->
@ -33,7 +35,7 @@ class LoggerRepository @Inject constructor(private val context: Context) {
} }
} }
if (chosenFile == null) throw FileNotFoundException("Log file not found") if (chosenFile == null) throw FileNotFoundException("Log file not found")
chosenFile chosenFile!!
} }
} }
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.luckynumber
import io.github.wulkanowy.data.db.dao.LuckyNumberDao import io.github.wulkanowy.data.db.dao.LuckyNumberDao
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.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,19 +10,19 @@ import javax.inject.Singleton
@Singleton @Singleton
class LuckyNumberLocal @Inject constructor(private val luckyNumberDb: LuckyNumberDao) { class LuckyNumberLocal @Inject constructor(private val luckyNumberDb: LuckyNumberDao) {
fun saveLuckyNumber(luckyNumber: LuckyNumber) { suspend fun saveLuckyNumber(luckyNumber: LuckyNumber?) {
luckyNumberDb.insertAll(listOf(luckyNumber)) luckyNumberDb.insertAll(listOfNotNull(luckyNumber))
} }
fun updateLuckyNumber(luckyNumber: LuckyNumber) { suspend fun updateLuckyNumber(luckyNumber: LuckyNumber?) {
luckyNumberDb.updateAll(listOf(luckyNumber)) luckyNumberDb.updateAll(listOfNotNull(luckyNumber))
} }
fun deleteLuckyNumber(luckyNumber: LuckyNumber) { suspend fun deleteLuckyNumber(luckyNumber: LuckyNumber?) {
luckyNumberDb.deleteAll(listOf(luckyNumber)) luckyNumberDb.deleteAll(listOfNotNull(luckyNumber))
} }
fun getLuckyNumber(student: Student, date: LocalDate): Maybe<LuckyNumber> { suspend fun getLuckyNumber(student: Student, date: LocalDate): LuckyNumber? {
return luckyNumberDb.load(student.studentId, date) return luckyNumberDb.load(student.studentId, date)
} }
} }

View File

@ -4,7 +4,6 @@ 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.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -12,8 +11,8 @@ import javax.inject.Singleton
@Singleton @Singleton
class LuckyNumberRemote @Inject constructor(private val sdk: Sdk) { class LuckyNumberRemote @Inject constructor(private val sdk: Sdk) {
fun getLuckyNumber(student: Student): Maybe<LuckyNumber> { suspend fun getLuckyNumber(student: Student): LuckyNumber? {
return sdk.init(student).getLuckyNumber(student.schoolShortName).map { return sdk.init(student).getLuckyNumber(student.schoolShortName)?.let {
LuckyNumber( LuckyNumber(
studentId = student.studentId, studentId = student.studentId,
date = LocalDate.now(), date = LocalDate.now(),

View File

@ -1,54 +1,42 @@
package io.github.wulkanowy.data.repositories.luckynumber 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.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 org.threeten.bp.LocalDate.now
import io.reactivex.Maybe
import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class LuckyNumberRepository @Inject constructor( class LuckyNumberRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: LuckyNumberLocal, private val local: LuckyNumberLocal,
private val remote: LuckyNumberRemote private val remote: LuckyNumberRemote
) { ) {
fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): Maybe<LuckyNumber> { suspend fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): LuckyNumber? {
return local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh } return local.getLuckyNumber(student, now())?.takeIf { !forceRefresh } ?: run {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getLuckyNumber(student)
.flatMapMaybe { val old = local.getLuckyNumber(student, now())
if (it) remote.getLuckyNumber(student)
else Maybe.error(UnknownHostException())
}.flatMap { new ->
local.getLuckyNumber(student, LocalDate.now())
.doOnSuccess { old ->
if (new != old) { if (new != old) {
local.deleteLuckyNumber(old) old?.let { local.deleteLuckyNumber(it) }
local.saveLuckyNumber(new.apply { local.saveLuckyNumber(new?.apply {
if (notify) isNotified = false if (notify) isNotified = false
}) })
} }
}
.doOnComplete { local.saveLuckyNumber(new?.apply {
local.saveLuckyNumber(new.apply {
if (notify) isNotified = false if (notify) isNotified = false
}) })
local.getLuckyNumber(student, now())
} }
}.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) },
{ local.getLuckyNumber(student, LocalDate.now()) })
)
} }
fun getNotNotifiedLuckyNumber(student: Student): Maybe<LuckyNumber> { suspend fun getNotNotifiedLuckyNumber(student: Student): LuckyNumber? {
return local.getLuckyNumber(student, LocalDate.now()).filter { !it.isNotified } return local.getLuckyNumber(student, now())
} }
fun updateLuckyNumber(luckyNumber: LuckyNumber): Completable { suspend fun updateLuckyNumber(luckyNumber: LuckyNumber) {
return Completable.fromCallable { local.updateLuckyNumber(luckyNumber) } local.updateLuckyNumber(luckyNumber)
} }
} }

View File

@ -7,8 +7,6 @@ import io.github.wulkanowy.data.db.entities.MessageAttachment
import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.MessageWithAttachment
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED
import io.reactivex.Maybe
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -18,30 +16,30 @@ class MessageLocal @Inject constructor(
private val messageAttachmentDao: MessageAttachmentDao private val messageAttachmentDao: MessageAttachmentDao
) { ) {
fun saveMessages(messages: List<Message>) { suspend fun saveMessages(messages: List<Message>) {
messagesDb.insertAll(messages) messagesDb.insertAll(messages)
} }
fun updateMessages(messages: List<Message>) { suspend fun updateMessages(messages: List<Message>) {
messagesDb.updateAll(messages) messagesDb.updateAll(messages)
} }
fun deleteMessages(messages: List<Message>) { suspend fun deleteMessages(messages: List<Message>) {
messagesDb.deleteAll(messages) messagesDb.deleteAll(messages)
} }
fun getMessageWithAttachment(student: Student, message: Message): Single<MessageWithAttachment> { suspend fun getMessageWithAttachment(student: Student, message: Message): MessageWithAttachment {
return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId) return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId)
} }
fun saveMessageAttachments(attachments: List<MessageAttachment>) { suspend fun saveMessageAttachments(attachments: List<MessageAttachment>) {
messageAttachmentDao.insertAttachments(attachments) messageAttachmentDao.insertAttachments(attachments)
} }
fun getMessages(student: Student, folder: MessageFolder): Maybe<List<Message>> { suspend fun getMessages(student: Student, folder: MessageFolder): List<Message> {
return when (folder) { return when (folder) {
TRASHED -> messagesDb.loadDeleted(student.id.toInt()) TRASHED -> messagesDb.loadDeleted(student.id.toInt())
else -> messagesDb.loadAll(student.id.toInt(), folder.id) else -> messagesDb.loadAll(student.id.toInt(), folder.id)
}.filter { it.isNotEmpty() } }
} }
} }

View File

@ -9,7 +9,6 @@ import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.sdk.pojo.Folder import io.github.wulkanowy.sdk.pojo.Folder
import io.github.wulkanowy.sdk.pojo.SentMessage import io.github.wulkanowy.sdk.pojo.SentMessage
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDateTime.now import org.threeten.bp.LocalDateTime.now
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -18,9 +17,8 @@ import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient
@Singleton @Singleton
class MessageRemote @Inject constructor(private val sdk: Sdk) { class MessageRemote @Inject constructor(private val sdk: Sdk) {
fun getMessages(student: Student, semester: Semester, folder: MessageFolder): Single<List<Message>> { suspend fun getMessages(student: Student, semester: Semester, folder: MessageFolder): List<Message> {
return sdk.init(student).getMessages(Folder.valueOf(folder.name), semester.start.atStartOfDay(), semester.end.atStartOfDay()).map { messages -> return sdk.init(student).getMessages(Folder.valueOf(folder.name), semester.start.atStartOfDay(), semester.end.atStartOfDay()).map {
messages.map {
Message( Message(
studentId = student.id.toInt(), studentId = student.id.toInt(),
realId = it.id ?: 0, realId = it.id ?: 0,
@ -40,10 +38,9 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
fun getMessagesContentDetails(student: Student, message: Message, markAsRead: Boolean = false): Single<Pair<String, List<MessageAttachment>>> { suspend fun getMessagesContentDetails(student: Student, message: Message, markAsRead: Boolean = false): Pair<String, List<MessageAttachment>> {
return sdk.init(student).getMessageDetails(message.messageId, message.folderId, markAsRead, message.realId).map { details -> return sdk.init(student).getMessageDetails(message.messageId, message.folderId, markAsRead, message.realId).let { details ->
details.content to details.attachments.map { details.content to details.attachments.map {
MessageAttachment( MessageAttachment(
realId = it.id, realId = it.id,
@ -56,7 +53,7 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) {
} }
} }
fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): Single<SentMessage> { suspend fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): SentMessage {
return sdk.init(student).sendMessage( return sdk.init(student).sendMessage(
subject = subject, subject = subject,
content = content, content = content,
@ -74,7 +71,7 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
fun deleteMessage(student: Student, message: Message): Single<Boolean> { suspend fun deleteMessage(student: Student, message: Message): Boolean {
return sdk.init(student).deleteMessages(listOf(message.messageId to message.folderId)) return sdk.init(student).deleteMessages(listOf(message.messageId to message.folderId))
} }
} }

View File

@ -1,7 +1,5 @@
package io.github.wulkanowy.data.repositories.message package io.github.wulkanowy.data.repositories.message
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.MessageWithAttachment
import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Recipient
@ -10,96 +8,75 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED
import io.github.wulkanowy.sdk.pojo.SentMessage import io.github.wulkanowy.sdk.pojo.SentMessage
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Completable
import io.reactivex.Single
import timber.log.Timber import timber.log.Timber
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class MessageRepository @Inject constructor( class MessageRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: MessageLocal, private val local: MessageLocal,
private val remote: MessageRemote private val remote: MessageRemote
) { ) {
fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single<List<Message>> { suspend fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): List<Message> {
return local.getMessages(student, folder).filter { !forceRefresh } return local.getMessages(student, folder).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getMessages(student, semester, folder)
.flatMap { val old = local.getMessages(student, folder)
if (it) remote.getMessages(student, semester, folder)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getMessages(student, folder).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteMessages(old.uniqueSubtract(new)) local.deleteMessages(old.uniqueSubtract(new))
local.saveMessages(new.uniqueSubtract(old) local.saveMessages(new.uniqueSubtract(old).onEach {
.onEach {
it.isNotified = !notify it.isNotified = !notify
}) })
local.getMessages(student, folder)
} }
}.flatMap { local.getMessages(student, folder).toSingle(emptyList()) }
)
} }
fun getMessage(student: Student, message: Message, markAsRead: Boolean = false): Single<MessageWithAttachment> { suspend fun getMessage(student: Student, message: Message, markAsRead: Boolean = false): MessageWithAttachment {
return local.getMessageWithAttachment(student, message) return local.getMessageWithAttachment(student, message).let {
.filter { if (it.message.content.isNotEmpty().also { status ->
it.message.content.isNotEmpty().also { status ->
Timber.d("Message content in db empty: ${!status}") Timber.d("Message content in db empty: ${!status}")
} && !it.message.unread } && !it.message.unread) {
return@let it
} }
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
.flatMap { val dbMessage = local.getMessageWithAttachment(student, message)
if (it) local.getMessageWithAttachment(student, message)
else Single.error(UnknownHostException()) val (downloadedMessage, attachments) = remote.getMessagesContentDetails(student, dbMessage.message, markAsRead)
}
.flatMap { dbMessage ->
remote.getMessagesContentDetails(student, dbMessage.message, markAsRead).doOnSuccess { (downloadedMessage, attachments) ->
local.updateMessages(listOf(dbMessage.message.copy(unread = !markAsRead).apply { local.updateMessages(listOf(dbMessage.message.copy(unread = !markAsRead).apply {
id = dbMessage.message.id id = dbMessage.message.id
content = content.ifBlank { downloadedMessage } content = content.ifBlank { downloadedMessage }
})) }))
local.saveMessageAttachments(attachments) local.saveMessageAttachments(attachments)
Timber.d("Message ${message.messageId} with blank content: ${dbMessage.message.content.isBlank()}, marked as read") Timber.d("Message ${message.messageId} with blank content: ${dbMessage.message.content.isBlank()}, marked as read")
}
}.flatMap {
local.getMessageWithAttachment(student, message) local.getMessageWithAttachment(student, message)
} }
)
} }
fun getNotNotifiedMessages(student: Student): Single<List<Message>> { suspend fun getNotNotifiedMessages(student: Student): List<Message> {
return local.getMessages(student, RECEIVED) return local.getMessages(student, RECEIVED)
.map { it.filter { message -> !message.isNotified && message.unread } } .filter { message -> !message.isNotified && message.unread }
.toSingle(emptyList())
} }
fun updateMessages(messages: List<Message>): Completable { suspend fun updateMessages(messages: List<Message>) {
return Completable.fromCallable { local.updateMessages(messages) } return local.updateMessages(messages)
} }
fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): Single<SentMessage> { suspend fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): SentMessage {
return ReactiveNetwork.checkInternetConnectivity(settings) return remote.sendMessage(student, subject, content, recipients)
.flatMap {
if (it) remote.sendMessage(student, subject, content, recipients)
else Single.error(UnknownHostException())
}
} }
fun deleteMessage(student: Student, message: Message): Single<Boolean> { suspend fun deleteMessage(student: Student, message: Message): Boolean {
return ReactiveNetwork.checkInternetConnectivity(settings) val delete = remote.deleteMessage(student, message)
.flatMap {
if (it) remote.deleteMessage(student, message)
else Single.error(UnknownHostException())
}
.doOnSuccess {
if (!message.removed) local.updateMessages(listOf(message.copy(removed = true).apply { if (!message.removed) local.updateMessages(listOf(message.copy(removed = true).apply {
id = message.id id = message.id
content = message.content content = message.content
})) }))
else local.deleteMessages(listOf(message)) else local.deleteMessages(listOf(message))
}
return delete // TODO: wtf
} }
} }

View File

@ -3,22 +3,21 @@ package io.github.wulkanowy.data.repositories.mobiledevice
import io.github.wulkanowy.data.db.dao.MobileDeviceDao import io.github.wulkanowy.data.db.dao.MobileDeviceDao
import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.db.entities.MobileDevice
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class MobileDeviceLocal @Inject constructor(private val mobileDb: MobileDeviceDao) { class MobileDeviceLocal @Inject constructor(private val mobileDb: MobileDeviceDao) {
fun saveDevices(devices: List<MobileDevice>) { suspend fun saveDevices(devices: List<MobileDevice>) {
mobileDb.insertAll(devices) mobileDb.insertAll(devices)
} }
fun deleteDevices(devices: List<MobileDevice>) { suspend fun deleteDevices(devices: List<MobileDevice>) {
mobileDb.deleteAll(devices) mobileDb.deleteAll(devices)
} }
fun getDevices(semester: Semester): Maybe<List<MobileDevice>> { suspend fun getDevices(semester: Semester): List<MobileDevice> {
return mobileDb.loadAll(semester.studentId).filter { it.isNotEmpty() } return mobileDb.loadAll(semester.studentId)
} }
} }

View File

@ -6,18 +6,16 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.pojos.MobileDeviceToken import io.github.wulkanowy.data.pojos.MobileDeviceToken
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class MobileDeviceRemote @Inject constructor(private val sdk: Sdk) { class MobileDeviceRemote @Inject constructor(private val sdk: Sdk) {
fun getDevices(student: Student, semester: Semester): Single<List<MobileDevice>> { suspend fun getDevices(student: Student, semester: Semester): List<MobileDevice> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getRegisteredDevices() .getRegisteredDevices()
.map { devices -> .map {
devices.map {
MobileDevice( MobileDevice(
studentId = semester.studentId, studentId = semester.studentId,
date = it.createDate, date = it.createDate,
@ -26,17 +24,16 @@ class MobileDeviceRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Single<Boolean> { suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Boolean {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.unregisterDevice(device.deviceId) .unregisterDevice(device.deviceId)
} }
fun getToken(student: Student, semester: Semester): Single<MobileDeviceToken> { suspend fun getToken(student: Student, semester: Semester): MobileDeviceToken {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getToken() .getToken()
.map { .let {
MobileDeviceToken( MobileDeviceToken(
token = it.token, token = it.token,
symbol = it.symbol, symbol = it.symbol,

View File

@ -1,53 +1,36 @@
package io.github.wulkanowy.data.repositories.mobiledevice package io.github.wulkanowy.data.repositories.mobiledevice
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.db.entities.MobileDevice
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.pojos.MobileDeviceToken import io.github.wulkanowy.data.pojos.MobileDeviceToken
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class MobileDeviceRepository @Inject constructor( class MobileDeviceRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: MobileDeviceLocal, private val local: MobileDeviceLocal,
private val remote: MobileDeviceRemote private val remote: MobileDeviceRemote
) { ) {
fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean = false): Single<List<MobileDevice>> { suspend fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean = false): List<MobileDevice> {
return local.getDevices(semester).filter { !forceRefresh } return local.getDevices(semester).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getDevices(student, semester)
.flatMap { val old = local.getDevices(semester)
if (it) remote.getDevices(student, semester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getDevices(semester).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteDevices(old uniqueSubtract new) local.deleteDevices(old uniqueSubtract new)
local.saveDevices(new uniqueSubtract old) local.saveDevices(new uniqueSubtract old)
}
}
).flatMap { local.getDevices(semester).toSingle(emptyList()) }
}
fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Single<Boolean> { local.getDevices(semester)
return ReactiveNetwork.checkInternetConnectivity(settings)
.flatMap {
if (it) remote.unregisterDevice(student, semester, device)
else Single.error(UnknownHostException())
} }
} }
fun getToken(student: Student, semester: Semester): Single<MobileDeviceToken> { suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Boolean {
return ReactiveNetwork.checkInternetConnectivity(settings) return remote.unregisterDevice(student, semester, device)
.flatMap {
if (it) remote.getToken(student, semester)
else Single.error(UnknownHostException())
} }
suspend fun getToken(student: Student, semester: Semester): MobileDeviceToken {
return remote.getToken(student, semester)
} }
} }

View File

@ -3,26 +3,25 @@ package io.github.wulkanowy.data.repositories.note
import io.github.wulkanowy.data.db.dao.NoteDao import io.github.wulkanowy.data.db.dao.NoteDao
import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Note
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class NoteLocal @Inject constructor(private val noteDb: NoteDao) { class NoteLocal @Inject constructor(private val noteDb: NoteDao) {
fun saveNotes(notes: List<Note>) { suspend fun saveNotes(notes: List<Note>) {
noteDb.insertAll(notes) noteDb.insertAll(notes)
} }
fun updateNotes(notes: List<Note>) { suspend fun updateNotes(notes: List<Note>) {
noteDb.updateAll(notes) noteDb.updateAll(notes)
} }
fun deleteNotes(notes: List<Note>) { suspend fun deleteNotes(notes: List<Note>) {
noteDb.deleteAll(notes) noteDb.deleteAll(notes)
} }
fun getNotes(student: Student): Maybe<List<Note>> { suspend fun getNotes(student: Student): List<Note> {
return noteDb.loadAll(student.studentId).filter { it.isNotEmpty() } return noteDb.loadAll(student.studentId)
} }
} }

View File

@ -5,18 +5,16 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class NoteRemote @Inject constructor(private val sdk: Sdk) { class NoteRemote @Inject constructor(private val sdk: Sdk) {
fun getNotes(student: Student, semester: Semester): Single<List<Note>> { suspend fun getNotes(student: Student, semester: Semester): List<Note> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getNotes(semester.semesterId) .getNotes(semester.semesterId)
.map { notes -> .map {
notes.map {
Note( Note(
studentId = semester.studentId, studentId = semester.studentId,
date = it.date, date = it.date,
@ -30,5 +28,4 @@ class NoteRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,54 +1,44 @@
package io.github.wulkanowy.data.repositories.note package io.github.wulkanowy.data.repositories.note
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Note
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Completable
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class NoteRepository @Inject constructor( class NoteRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: NoteLocal, private val local: NoteLocal,
private val remote: NoteRemote private val remote: NoteRemote
) { ) {
fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Single<List<Note>> { suspend fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): List<Note> {
return local.getNotes(student).filter { !forceRefresh } return local.getNotes(student).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getNotes(student, semester)
.flatMap { val old = local.getNotes(student)
if (it) remote.getNotes(student, semester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getNotes(student).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteNotes(old.uniqueSubtract(new)) local.deleteNotes(old.uniqueSubtract(new))
local.saveNotes(new.uniqueSubtract(old) local.saveNotes(new.uniqueSubtract(old).onEach {
.onEach {
if (it.date >= student.registrationDate.toLocalDate()) it.apply { if (it.date >= student.registrationDate.toLocalDate()) it.apply {
isRead = false isRead = false
if (notify) isNotified = false if (notify) isNotified = false
} }
}) })
local.getNotes(student)
} }
}.flatMap { local.getNotes(student).toSingle(emptyList()) })
} }
fun getNotNotifiedNotes(student: Student): Single<List<Note>> { suspend fun getNotNotifiedNotes(student: Student): List<Note> {
return local.getNotes(student).map { it.filter { note -> !note.isNotified } }.toSingle(emptyList()) return local.getNotes(student).filter { note -> !note.isNotified }
} }
fun updateNote(note: Note): Completable { suspend fun updateNote(note: Note) {
return Completable.fromCallable { local.updateNotes(listOf(note)) } return local.updateNotes(listOf(note))
} }
fun updateNotes(notes: List<Note>): Completable { suspend fun updateNotes(notes: List<Note>) {
return Completable.fromCallable { local.updateNotes(notes) } return local.updateNotes(notes)
} }
} }

View File

@ -4,22 +4,21 @@ import io.github.wulkanowy.data.db.dao.RecipientDao
import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Recipient
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class RecipientLocal @Inject constructor(private val recipientDb: RecipientDao) { class RecipientLocal @Inject constructor(private val recipientDb: RecipientDao) {
fun getRecipients(student: Student, role: Int, unit: ReportingUnit): Maybe<List<Recipient>> { suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List<Recipient> {
return recipientDb.load(student.studentId, role, unit.realId).filter { it.isNotEmpty() } return recipientDb.load(student.studentId, role, unit.realId)
} }
fun saveRecipients(recipients: List<Recipient>): List<Long> { suspend fun saveRecipients(recipients: List<Recipient>): List<Long> {
return recipientDb.insertAll(recipients) return recipientDb.insertAll(recipients)
} }
fun deleteRecipients(recipients: List<Recipient>) { suspend fun deleteRecipients(recipients: List<Recipient>) {
recipientDb.deleteAll(recipients) recipientDb.deleteAll(recipients)
} }
} }

View File

@ -6,7 +6,6 @@ import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient
@ -14,18 +13,14 @@ import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient
@Singleton @Singleton
class RecipientRemote @Inject constructor(private val sdk: Sdk) { class RecipientRemote @Inject constructor(private val sdk: Sdk) {
fun getRecipients(student: Student, role: Int, unit: ReportingUnit): Single<List<Recipient>> { suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List<Recipient> {
return sdk.init(student).getRecipients(unit.realId, role) return sdk.init(student).getRecipients(unit.realId, role)
.map { recipients -> .map { it.toRecipient() }
recipients.map { it.toRecipient() }
}
} }
fun getMessageRecipients(student: Student, message: Message): Single<List<Recipient>> { suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId) return sdk.init(student).getMessageRecipients(message.messageId, message.senderId)
.map { recipients -> .map { it.toRecipient() }
recipients.map { it.toRecipient() }
}
} }
private fun SdkRecipient.toRecipient(): Recipient { private fun SdkRecipient.toRecipient(): Recipient {

View File

@ -1,47 +1,32 @@
package io.github.wulkanowy.data.repositories.recipient package io.github.wulkanowy.data.repositories.recipient
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Recipient
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class RecipientRepository @Inject constructor( class RecipientRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: RecipientLocal, private val local: RecipientLocal,
private val remote: RecipientRemote private val remote: RecipientRemote
) { ) {
fun getRecipients(student: Student, role: Int, unit: ReportingUnit, forceRefresh: Boolean = false): Single<List<Recipient>> { suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit, forceRefresh: Boolean = false): List<Recipient> {
return local.getRecipients(student, role, unit).filter { !forceRefresh } return local.getRecipients(student, role, unit).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getRecipients(student, role, unit)
.flatMap { val old = local.getRecipients(student, role, unit)
if (it) remote.getRecipients(student, role, unit)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getRecipients(student, role, unit).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteRecipients(old.uniqueSubtract(new)) local.deleteRecipients(old.uniqueSubtract(new))
local.saveRecipients(new.uniqueSubtract(old)) local.saveRecipients(new.uniqueSubtract(old))
local.getRecipients(student, role, unit)
} }
}.flatMap {
local.getRecipients(student, role, unit).toSingle(emptyList())
}
)
} }
fun getMessageRecipients(student: Student, message: Message): Single<List<Recipient>> { suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
return ReactiveNetwork.checkInternetConnectivity(settings) return remote.getMessageRecipients(student, message)
.flatMap {
if (it) remote.getMessageRecipients(student, message)
else Single.error(UnknownHostException())
}
} }
} }

View File

@ -1,18 +1,17 @@
package io.github.wulkanowy.data.repositories.recover package io.github.wulkanowy.data.repositories.recover
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class RecoverRemote @Inject constructor(private val sdk: Sdk) { class RecoverRemote @Inject constructor(private val sdk: Sdk) {
fun getReCaptchaSiteKey(host: String, symbol: String): Single<Pair<String, String>> { suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair<String, String> {
return sdk.getPasswordResetCaptchaCode(host, symbol) return sdk.getPasswordResetCaptchaCode(host, symbol)
} }
fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single<String> { suspend fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): String {
return sdk.sendPasswordResetRequest(url, symbol, email, reCaptchaResponse) return sdk.sendPasswordResetRequest(url, symbol, email, reCaptchaResponse)
} }
} }

View File

@ -1,26 +1,16 @@
package io.github.wulkanowy.data.repositories.recover package io.github.wulkanowy.data.repositories.recover
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class RecoverRepository @Inject constructor(private val settings: InternetObservingSettings, private val remote: RecoverRemote) { class RecoverRepository @Inject constructor(private val remote: RecoverRemote) {
fun getReCaptchaSiteKey(host: String, symbol: String): Single<Pair<String, String>> { suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair<String, String> {
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { return remote.getReCaptchaSiteKey(host, symbol)
if (it) remote.getReCaptchaSiteKey(host, symbol)
else Single.error(UnknownHostException())
}
} }
fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single<String> { suspend fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): String {
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { return remote.sendRecoverRequest(url, symbol, email, reCaptchaResponse)
if (it) remote.sendRecoverRequest(url, symbol, email, reCaptchaResponse)
else Single.error(UnknownHostException())
}
} }
} }

View File

@ -3,26 +3,25 @@ package io.github.wulkanowy.data.repositories.reportingunit
import io.github.wulkanowy.data.db.dao.ReportingUnitDao import io.github.wulkanowy.data.db.dao.ReportingUnitDao
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ReportingUnitLocal @Inject constructor(private val reportingUnitDb: ReportingUnitDao) { class ReportingUnitLocal @Inject constructor(private val reportingUnitDb: ReportingUnitDao) {
fun getReportingUnits(student: Student): Maybe<List<ReportingUnit>> { suspend fun getReportingUnits(student: Student): List<ReportingUnit> {
return reportingUnitDb.load(student.studentId).filter { it.isNotEmpty() } return reportingUnitDb.load(student.studentId)
} }
fun getReportingUnit(student: Student, unitId: Int): Maybe<ReportingUnit> { suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit? {
return reportingUnitDb.loadOne(student.studentId, unitId) return reportingUnitDb.loadOne(student.studentId, unitId)
} }
fun saveReportingUnits(reportingUnits: List<ReportingUnit>): List<Long> { suspend fun saveReportingUnits(reportingUnits: List<ReportingUnit>): List<Long> {
return reportingUnitDb.insertAll(reportingUnits) return reportingUnitDb.insertAll(reportingUnits)
} }
fun deleteReportingUnits(reportingUnits: List<ReportingUnit>) { suspend fun deleteReportingUnits(reportingUnits: List<ReportingUnit>) {
reportingUnitDb.deleteAll(reportingUnits) reportingUnitDb.deleteAll(reportingUnits)
} }
} }

View File

@ -4,16 +4,14 @@ import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ReportingUnitRemote @Inject constructor(private val sdk: Sdk) { class ReportingUnitRemote @Inject constructor(private val sdk: Sdk) {
fun getReportingUnits(student: Student): Single<List<ReportingUnit>> { suspend fun getReportingUnits(student: Student): List<ReportingUnit> {
return sdk.init(student).getReportingUnits().map { return sdk.init(student).getReportingUnits().map { unit ->
it.map { unit ->
ReportingUnit( ReportingUnit(
studentId = sdk.studentId, studentId = sdk.studentId,
realId = unit.id, realId = unit.id,
@ -24,5 +22,4 @@ class ReportingUnitRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,48 +1,34 @@
package io.github.wulkanowy.data.repositories.reportingunit package io.github.wulkanowy.data.repositories.reportingunit
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.ReportingUnit
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Maybe
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class ReportingUnitRepository @Inject constructor( class ReportingUnitRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: ReportingUnitLocal, private val local: ReportingUnitLocal,
private val remote: ReportingUnitRemote private val remote: ReportingUnitRemote
) { ) {
fun getReportingUnits(student: Student, forceRefresh: Boolean = false): Single<List<ReportingUnit>> { suspend fun getReportingUnits(student: Student, forceRefresh: Boolean = false): List<ReportingUnit> {
return local.getReportingUnits(student).filter { !forceRefresh } return local.getReportingUnits(student).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getReportingUnits(student)
.flatMap { val old = local.getReportingUnits(student)
if (it) remote.getReportingUnits(student)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getReportingUnits(student).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteReportingUnits(old.uniqueSubtract(new)) local.deleteReportingUnits(old.uniqueSubtract(new))
local.saveReportingUnits(new.uniqueSubtract(old)) local.saveReportingUnits(new.uniqueSubtract(old))
local.getReportingUnits(student)
} }
}.flatMap { local.getReportingUnits(student).toSingle(emptyList()) }
)
} }
fun getReportingUnit(student: Student, unitId: Int): Maybe<ReportingUnit> { suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit {
return local.getReportingUnit(student, unitId) return local.getReportingUnit(student, unitId) ?: run {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) getReportingUnits(student, true)
.flatMap {
if (it) getReportingUnits(student, true) return local.getReportingUnit(student, unitId)!!
else Single.error(UnknownHostException())
}.flatMapMaybe {
local.getReportingUnit(student, unitId)
} }
)
} }
} }

View File

@ -3,20 +3,19 @@ package io.github.wulkanowy.data.repositories.school
import io.github.wulkanowy.data.db.dao.SchoolDao import io.github.wulkanowy.data.db.dao.SchoolDao
import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.data.db.entities.School
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
class SchoolLocal @Inject constructor(private val schoolDb: SchoolDao) { class SchoolLocal @Inject constructor(private val schoolDb: SchoolDao) {
fun saveSchool(school: School) { suspend fun saveSchool(school: School) {
schoolDb.insertAll(listOf(school)) schoolDb.insertAll(listOf(school))
} }
fun deleteSchool(school: School) { suspend fun deleteSchool(school: School) {
schoolDb.deleteAll(listOf(school)) schoolDb.deleteAll(listOf(school))
} }
fun getSchool(semester: Semester): Maybe<School> { suspend fun getSchool(semester: Semester): School? {
return schoolDb.load(semester.studentId, semester.classId) return schoolDb.load(semester.studentId, semester.classId)
} }
} }

View File

@ -5,15 +5,14 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
class SchoolRemote @Inject constructor(private val sdk: Sdk) { class SchoolRemote @Inject constructor(private val sdk: Sdk) {
fun getSchoolInfo(student: Student, semester: Semester): Single<School> { suspend fun getSchoolInfo(student: Student, semester: Semester): School {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getSchool() .getSchool()
.map { .let {
School( School(
studentId = semester.studentId, studentId = semester.studentId,
classId = semester.classId, classId = semester.classId,

View File

@ -1,42 +1,29 @@
package io.github.wulkanowy.data.repositories.school package io.github.wulkanowy.data.repositories.school
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.data.db.entities.School
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SchoolRepository @Inject constructor( class SchoolRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: SchoolLocal, private val local: SchoolLocal,
private val remote: SchoolRemote private val remote: SchoolRemote
) { ) {
fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean = false): Maybe<School> { suspend fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean = false): School {
return local.getSchool(semester).filter { !forceRefresh } return local.getSchool(semester).takeIf { it != null && !forceRefresh } ?: run {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getSchoolInfo(student, semester)
.flatMap { val old = local.getSchool(semester)
if (it) remote.getSchoolInfo(student, semester)
else Single.error(UnknownHostException()) if (new != old && old != null) {
}.flatMapMaybe { new ->
local.getSchool(semester)
.doOnSuccess { old ->
if (new != old) {
local.deleteSchool(old) local.deleteSchool(old)
local.saveSchool(new) local.saveSchool(new)
} }
}
.doOnComplete {
local.saveSchool(new) local.saveSchool(new)
local.getSchool(semester)!!
} }
}.flatMap({ local.getSchool(semester) }, { Maybe.error(it) },
{ local.getSchool(semester) })
)
} }
} }

View File

@ -3,22 +3,21 @@ package io.github.wulkanowy.data.repositories.semester
import io.github.wulkanowy.data.db.dao.SemesterDao import io.github.wulkanowy.data.db.dao.SemesterDao
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SemesterLocal @Inject constructor(private val semesterDb: SemesterDao) { class SemesterLocal @Inject constructor(private val semesterDb: SemesterDao) {
fun saveSemesters(semesters: List<Semester>) { suspend fun saveSemesters(semesters: List<Semester>) {
semesterDb.insertAll(semesters) semesterDb.insertAll(semesters)
} }
fun deleteSemesters(semesters: List<Semester>) { suspend fun deleteSemesters(semesters: List<Semester>) {
semesterDb.deleteAll(semesters) semesterDb.deleteAll(semesters)
} }
fun getSemesters(student: Student): Maybe<List<Semester>> { suspend fun getSemesters(student: Student): List<Semester> {
return semesterDb.loadAll(student.studentId, student.classId).filter { it.isNotEmpty() } return semesterDb.loadAll(student.studentId, student.classId)
} }
} }

View File

@ -4,16 +4,14 @@ import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SemesterRemote @Inject constructor(private val sdk: Sdk) { class SemesterRemote @Inject constructor(private val sdk: Sdk) {
fun getSemesters(student: Student): Single<List<Semester>> { suspend fun getSemesters(student: Student): List<Semester> {
return sdk.init(student).getSemesters().map { semesters -> return sdk.init(student).getSemesters().map {
semesters.map {
Semester( Semester(
studentId = student.studentId, studentId = student.studentId,
diaryId = it.diaryId, diaryId = it.diaryId,
@ -28,5 +26,4 @@ class SemesterRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,47 +1,42 @@
package io.github.wulkanowy.data.repositories.semester package io.github.wulkanowy.data.repositories.semester
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.getCurrentOrLast import io.github.wulkanowy.utils.getCurrentOrLast
import io.github.wulkanowy.utils.isCurrent import io.github.wulkanowy.utils.isCurrent
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SemesterRepository @Inject constructor( class SemesterRepository @Inject constructor(
private val remote: SemesterRemote, private val remote: SemesterRemote,
private val local: SemesterLocal, private val local: SemesterLocal
private val settings: InternetObservingSettings
) { ) {
fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): Single<List<Semester>> { suspend fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): List<Semester> {
return local.getSemesters(student).filter { !forceRefresh }.filter { semesters -> return local.getSemesters(student).let { semesters ->
when { semesters.filter {
!forceRefresh && when {
Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0 Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0
refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent } refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent }
else -> true else -> true
} }
}.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) }
.flatMap { }.ifEmpty {
if (it) remote.getSemesters(student) val new = remote.getSemesters(student)
else Single.error(UnknownHostException())
}.flatMap { new ->
if (new.isEmpty()) throw IllegalArgumentException("Empty semester list!") if (new.isEmpty()) throw IllegalArgumentException("Empty semester list!")
local.getSemesters(student).toSingle(emptyList()).doOnSuccess { old -> val old = local.getSemesters(student)
local.deleteSemesters(old.uniqueSubtract(new)) local.deleteSemesters(old.uniqueSubtract(new))
local.saveSemesters(new.uniqueSubtract(old)) local.saveSemesters(new.uniqueSubtract(old))
local.getSemesters(student)
} }
}.flatMap { local.getSemesters(student).toSingle(emptyList()) })
} }
fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Single<Semester> { suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Semester {
return getSemesters(student, forceRefresh).map { it.getCurrentOrLast() } return getSemesters(student, forceRefresh).getCurrentOrLast()
} }
} }

View File

@ -6,9 +6,6 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.security.decrypt import io.github.wulkanowy.utils.security.decrypt
import io.github.wulkanowy.utils.security.encrypt import io.github.wulkanowy.utils.security.encrypt
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -18,47 +15,41 @@ class StudentLocal @Inject constructor(
private val context: Context private val context: Context
) { ) {
fun saveStudents(students: List<Student>): Single<List<Long>> { suspend fun saveStudents(students: List<Student>): List<Long> {
return Single.fromCallable { return studentDb.insertAll(students.map {
studentDb.insertAll(students.map {
if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) it.copy(password = encrypt(it.password, context)) if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) it.copy(password = encrypt(it.password, context))
else it else it
}) })
} }
}
fun getStudents(decryptPass: Boolean): Maybe<List<Student>> { suspend fun getStudents(decryptPass: Boolean): List<Student> {
return studentDb.loadAll() return studentDb.loadAll().map {
.map { list -> list.map { it.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } }
.filter { it.isNotEmpty() }
}
fun getStudentById(id: Int): Maybe<Student> {
return studentDb.loadById(id).map {
it.apply {
if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
}
}
}
fun getCurrentStudent(decryptPass: Boolean): Maybe<Student> {
return studentDb.loadCurrent().map {
it.apply { it.apply {
if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
} }
} }
} }
fun setCurrentStudent(student: Student): Completable { suspend fun getStudentById(id: Int): Student? {
return Completable.fromCallable { return studentDb.loadById(id)?.apply {
studentDb.run { if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
}
}
suspend fun getCurrentStudent(decryptPass: Boolean): Student? {
return studentDb.loadCurrent()?.apply {
if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
}
}
suspend fun setCurrentStudent(student: Student) {
return studentDb.run {
resetCurrent() resetCurrent()
updateCurrent(student.id) updateCurrent(student.id)
} }
} }
}
fun logoutStudent(student: Student): Completable { suspend fun logoutStudent(student: Student) {
return Completable.fromCallable { studentDb.delete(student) } return studentDb.delete(student)
} }
} }

View File

@ -2,7 +2,6 @@ package io.github.wulkanowy.data.repositories.student
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.reactivex.Single
import org.threeten.bp.LocalDateTime.now import org.threeten.bp.LocalDateTime.now
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -38,15 +37,15 @@ class StudentRemote @Inject constructor(private val sdk: Sdk) {
} }
} }
fun getStudentsMobileApi(token: String, pin: String, symbol: String): Single<List<Student>> { suspend fun getStudentsMobileApi(token: String, pin: String, symbol: String): List<Student> {
return sdk.getStudentsFromMobileApi(token, pin, symbol, "").map { mapStudents(it, "", "") } return mapStudents(sdk.getStudentsFromMobileApi(token, pin, symbol, ""), "", "")
} }
fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single<List<Student>> { suspend fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): List<Student> {
return sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol).map { mapStudents(it, email, password) } return mapStudents(sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol), email, password)
} }
fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single<List<Student>> { suspend fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): List<Student> {
return sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol).map { mapStudents(it, email, password) } return mapStudents(sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol), email, password)
} }
} }

View File

@ -1,73 +1,53 @@
package io.github.wulkanowy.data.repositories.student package io.github.wulkanowy.data.repositories.student
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.github.wulkanowy.data.exceptions.NoCurrentStudentException
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class StudentRepository @Inject constructor( class StudentRepository @Inject constructor(
private val local: StudentLocal, private val local: StudentLocal,
private val remote: StudentRemote, private val remote: StudentRemote
private val settings: InternetObservingSettings
) { ) {
fun isStudentSaved(): Single<Boolean> = local.getStudents(false).isEmpty.map { !it } suspend fun isStudentSaved(): Boolean = local.getStudents(false).isNotEmpty()
fun isCurrentStudentSet(): Single<Boolean> = local.getCurrentStudent(false).isEmpty.map { !it } suspend fun isCurrentStudentSet(): Boolean = local.getCurrentStudent(false)?.isCurrent ?: false
fun getStudentsApi(pin: String, symbol: String, token: String): Single<List<Student>> { suspend fun getStudentsApi(pin: String, symbol: String, token: String): List<Student> {
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { return remote.getStudentsMobileApi(token, pin, symbol)
if (it) remote.getStudentsMobileApi(token, pin, symbol)
else Single.error(UnknownHostException("No internet connection"))
}
} }
fun getStudentsScrapper(email: String, password: String, endpoint: String, symbol: String): Single<List<Student>> { suspend fun getStudentsScrapper(email: String, password: String, endpoint: String, symbol: String): List<Student> {
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { return remote.getStudentsScrapper(email, password, endpoint, symbol)
if (it) remote.getStudentsScrapper(email, password, endpoint, symbol)
else Single.error(UnknownHostException("No internet connection"))
}
} }
fun getStudentsHybrid(email: String, password: String, endpoint: String, symbol: String): Single<List<Student>> { suspend fun getStudentsHybrid(email: String, password: String, endpoint: String, symbol: String): List<Student> {
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { return remote.getStudentsHybrid(email, password, endpoint, symbol)
if (it) remote.getStudentsHybrid(email, password, endpoint, symbol)
else Single.error(UnknownHostException("No internet connection"))
}
} }
fun getSavedStudents(decryptPass: Boolean = true): Single<List<Student>> { suspend fun getSavedStudents(decryptPass: Boolean = true): List<Student> {
return local.getStudents(decryptPass).toSingle(emptyList()) return local.getStudents(decryptPass)
} }
fun getStudentById(id: Int): Single<Student> { suspend fun getStudentById(id: Int): Student {
return local.getStudentById(id) return local.getStudentById(id) ?: throw NoCurrentStudentException()
.switchIfEmpty(Maybe.error(NoCurrentStudentException()))
.toSingle()
} }
fun getCurrentStudent(decryptPass: Boolean = true): Single<Student> { suspend fun getCurrentStudent(decryptPass: Boolean = true): Student {
return local.getCurrentStudent(decryptPass) return local.getCurrentStudent(decryptPass) ?: throw NoCurrentStudentException()
.switchIfEmpty(Maybe.error(NoCurrentStudentException()))
.toSingle()
} }
fun saveStudents(students: List<Student>): Single<List<Long>> { suspend fun saveStudents(students: List<Student>): List<Long> {
return local.saveStudents(students) return local.saveStudents(students)
} }
fun switchStudent(student: Student): Completable { suspend fun switchStudent(student: Student) {
return local.setCurrentStudent(student) return local.setCurrentStudent(student)
} }
fun logoutStudent(student: Student): Completable { suspend fun logoutStudent(student: Student) {
return local.logoutStudent(student) return local.logoutStudent(student)
} }
} }

View File

@ -3,23 +3,21 @@ package io.github.wulkanowy.data.repositories.subject
import io.github.wulkanowy.data.db.dao.SubjectDao import io.github.wulkanowy.data.db.dao.SubjectDao
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.db.entities.Subject
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SubjectLocal @Inject constructor(private val subjectDao: SubjectDao) { class SubjectLocal @Inject constructor(private val subjectDao: SubjectDao) {
fun getSubjects(semester: Semester): Maybe<List<Subject>> { suspend fun getSubjects(semester: Semester): List<Subject> {
return subjectDao.loadAll(semester.diaryId, semester.studentId) return subjectDao.loadAll(semester.diaryId, semester.studentId)
.filter { it.isNotEmpty() }
} }
fun saveSubjects(subjects: List<Subject>) { suspend fun saveSubjects(subjects: List<Subject>) {
subjectDao.insertAll(subjects) subjectDao.insertAll(subjects)
} }
fun deleteSubjects(subjects: List<Subject>) { suspend fun deleteSubjects(subjects: List<Subject>) {
subjectDao.deleteAll(subjects) subjectDao.deleteAll(subjects)
} }
} }

View File

@ -5,18 +5,16 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.db.entities.Subject
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SubjectRemote @Inject constructor(private val sdk: Sdk) { class SubjectRemote @Inject constructor(private val sdk: Sdk) {
fun getSubjects(student: Student, semester: Semester): Single<List<Subject>> { suspend fun getSubjects(student: Student, semester: Semester): List<Subject> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getSubjects() .getSubjects()
.map { subjects -> .map {
subjects.map {
Subject( Subject(
studentId = semester.studentId, studentId = semester.studentId,
diaryId = semester.diaryId, diaryId = semester.diaryId,
@ -25,5 +23,4 @@ class SubjectRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,38 +1,27 @@
package io.github.wulkanowy.data.repositories.subject package io.github.wulkanowy.data.repositories.subject
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.db.entities.Subject
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SubjectRepository @Inject constructor( class SubjectRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: SubjectLocal, private val local: SubjectLocal,
private val remote: SubjectRemote private val remote: SubjectRemote
) { ) {
fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false): Single<List<Subject>> { suspend fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false): List<Subject> {
return local.getSubjects(semester).filter { !forceRefresh } return local.getSubjects(semester).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getSubjects(student, semester)
.flatMap { val old = local.getSubjects(semester)
if (it) remote.getSubjects(student, semester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getSubjects(semester)
.toSingle(emptyList())
.doOnSuccess { old ->
local.deleteSubjects(old.uniqueSubtract(new)) local.deleteSubjects(old.uniqueSubtract(new))
local.saveSubjects(new.uniqueSubtract(old)) local.saveSubjects(new.uniqueSubtract(old))
local.getSubjects(semester)
} }
}.flatMap {
local.getSubjects(semester).toSingle(emptyList())
})
} }
} }

View File

@ -3,20 +3,19 @@ package io.github.wulkanowy.data.repositories.teacher
import io.github.wulkanowy.data.db.dao.TeacherDao import io.github.wulkanowy.data.db.dao.TeacherDao
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.data.db.entities.Teacher
import io.reactivex.Maybe
import javax.inject.Inject import javax.inject.Inject
class TeacherLocal @Inject constructor(private val teacherDb: TeacherDao) { class TeacherLocal @Inject constructor(private val teacherDb: TeacherDao) {
fun saveTeachers(teachers: List<Teacher>) { suspend fun saveTeachers(teachers: List<Teacher>) {
teacherDb.insertAll(teachers) teacherDb.insertAll(teachers)
} }
fun deleteTeachers(teachers: List<Teacher>) { suspend fun deleteTeachers(teachers: List<Teacher>) {
teacherDb.deleteAll(teachers) teacherDb.deleteAll(teachers)
} }
fun getTeachers(semester: Semester): Maybe<List<Teacher>> { suspend fun getTeachers(semester: Semester): List<Teacher> {
return teacherDb.loadAll(semester.studentId, semester.classId).filter { it.isNotEmpty() } return teacherDb.loadAll(semester.studentId, semester.classId)
} }
} }

View File

@ -5,18 +5,16 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.data.db.entities.Teacher
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class TeacherRemote @Inject constructor(private val sdk: Sdk) { class TeacherRemote @Inject constructor(private val sdk: Sdk) {
fun getTeachers(student: Student, semester: Semester): Single<List<Teacher>> { suspend fun getTeachers(student: Student, semester: Semester): List<Teacher> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getTeachers(semester.semesterId) .getTeachers(semester.semesterId)
.map { teachers -> .map {
teachers.map {
Teacher( Teacher(
studentId = semester.studentId, studentId = semester.studentId,
name = it.name, name = it.name,
@ -26,5 +24,4 @@ class TeacherRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,35 +1,27 @@
package io.github.wulkanowy.data.repositories.teacher package io.github.wulkanowy.data.repositories.teacher
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.data.db.entities.Teacher
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class TeacherRepository @Inject constructor( class TeacherRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: TeacherLocal, private val local: TeacherLocal,
private val remote: TeacherRemote private val remote: TeacherRemote
) { ) {
fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean = false): Single<List<Teacher>> { suspend fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean = false): List<Teacher> {
return local.getTeachers(semester).filter { !forceRefresh } return local.getTeachers(semester).filter { !forceRefresh }.ifEmpty {
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) val new = remote.getTeachers(student, semester)
.flatMap { val old = local.getTeachers(semester)
if (it) remote.getTeachers(student, semester)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getTeachers(semester).toSingle(emptyList())
.doOnSuccess { old ->
local.deleteTeachers(old.uniqueSubtract(new)) local.deleteTeachers(old.uniqueSubtract(new))
local.saveTeachers(new.uniqueSubtract(old)) local.saveTeachers(new.uniqueSubtract(old))
local.getTeachers(semester)
} }
}.flatMap { local.getTeachers(semester).toSingle(emptyList()) })
} }
} }

View File

@ -3,7 +3,6 @@ package io.github.wulkanowy.data.repositories.timetable
import io.github.wulkanowy.data.db.dao.TimetableDao import io.github.wulkanowy.data.db.dao.TimetableDao
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.db.entities.Timetable
import io.reactivex.Maybe
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -11,15 +10,15 @@ import javax.inject.Singleton
@Singleton @Singleton
class TimetableLocal @Inject constructor(private val timetableDb: TimetableDao) { class TimetableLocal @Inject constructor(private val timetableDb: TimetableDao) {
fun saveTimetable(timetables: List<Timetable>) { suspend fun saveTimetable(timetables: List<Timetable>) {
timetableDb.insertAll(timetables) timetableDb.insertAll(timetables)
} }
fun deleteTimetable(timetables: List<Timetable>) { suspend fun deleteTimetable(timetables: List<Timetable>) {
timetableDb.deleteAll(timetables) timetableDb.deleteAll(timetables)
} }
fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Timetable>> { suspend fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Timetable> {
return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() } return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
} }
} }

View File

@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.db.entities.Timetable
import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.init import io.github.wulkanowy.utils.init
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -13,11 +12,10 @@ import javax.inject.Singleton
@Singleton @Singleton
class TimetableRemote @Inject constructor(private val sdk: Sdk) { class TimetableRemote @Inject constructor(private val sdk: Sdk) {
fun getTimetable(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single<List<Timetable>> { suspend fun getTimetable(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Timetable> {
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
.getTimetable(startDate, endDate) .getTimetable(startDate, endDate)
.map { lessons -> .map {
lessons.map {
Timetable( Timetable(
studentId = semester.studentId, studentId = semester.studentId,
diaryId = semester.diaryId, diaryId = semester.diaryId,
@ -39,5 +37,4 @@ class TimetableRemote @Inject constructor(private val sdk: Sdk) {
) )
} }
} }
}
} }

View File

@ -1,38 +1,28 @@
package io.github.wulkanowy.data.repositories.timetable package io.github.wulkanowy.data.repositories.timetable
import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.db.entities.Timetable
import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.monday
import io.github.wulkanowy.utils.sunday
import io.github.wulkanowy.utils.uniqueSubtract import io.github.wulkanowy.utils.uniqueSubtract
import io.reactivex.Single
import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class TimetableRepository @Inject constructor( class TimetableRepository @Inject constructor(
private val settings: InternetObservingSettings,
private val local: TimetableLocal, private val local: TimetableLocal,
private val remote: TimetableRemote, private val remote: TimetableRemote,
private val schedulerHelper: TimetableNotificationSchedulerHelper private val schedulerHelper: TimetableNotificationSchedulerHelper
) { ) {
fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single<List<Timetable>> { suspend fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List<Timetable> {
return Single.fromCallable { start.monday to end.sunday }.flatMap { (monday, sunday) -> return local.getTimetable(semester, start.monday, start.sunday).filter { !forceRefresh }.ifEmpty {
local.getTimetable(semester, monday, sunday).filter { !forceRefresh } val new = remote.getTimetable(student, semester, start.monday, start.sunday)
.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { val old = local.getTimetable(semester, start.monday, start.sunday)
if (it) remote.getTimetable(student, semester, monday, sunday)
else Single.error(UnknownHostException())
}.flatMap { new ->
local.getTimetable(semester, monday, sunday)
.toSingle(emptyList())
.doOnSuccess { old ->
local.deleteTimetable(old.uniqueSubtract(new).also { schedulerHelper.cancelScheduled(it) }) local.deleteTimetable(old.uniqueSubtract(new).also { schedulerHelper.cancelScheduled(it) })
local.saveTimetable(new.uniqueSubtract(old).also { schedulerHelper.scheduleNotifications(it, student) }.map { item -> local.saveTimetable(new.uniqueSubtract(old).also { schedulerHelper.scheduleNotifications(it, student) }.map { item ->
item.also { new -> item.also { new ->
@ -44,10 +34,8 @@ class TimetableRepository @Inject constructor(
} }
} }
}) })
}
}.flatMap { local.getTimetable(semester, start.monday, start.sunday)
local.getTimetable(semester, monday, sunday).toSingle(emptyList()) }.filter { it.date in start..end }.also { schedulerHelper.scheduleNotifications(it, student) }
}).map { list -> list.filter { it.date in start..end }.also { schedulerHelper.scheduleNotifications(it, student) } }
}
} }
} }

Some files were not shown because too many files have changed in this diff Show More