From 4434d6f024ba4f23dfed6ebf63c798f59f9a53ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 20 Jun 2020 15:07:57 +0200 Subject: [PATCH 01/34] Migrate repositories from rxjava to coroutines (#885) --- app/build.gradle | 10 +- .../data/db/migrations/Migration12Test.kt | 7 +- .../data/db/migrations/Migration13Test.kt | 5 +- .../TestInternetObservingStrategy.kt | 19 -- .../attendance/AttendanceLocalTest.kt | 14 +- .../CompletedLessonsLocalTest.kt | 17 +- .../data/repositories/exam/ExamLocalTest.kt | 14 +- .../data/repositories/grade/GradeLocalTest.kt | 13 +- .../repositories/grade/GradeRepositoryTest.kt | 75 ++--- .../GradeStatisticsLocalTest.kt | 34 ++- .../luckynumber/LuckyNumberLocalTest.kt | 18 +- .../recipient/RecipientLocalTest.kt | 19 +- .../repositories/student/StudentLocalTest.kt | 7 +- .../timetable/TimetableLocalTest.kt | 19 +- .../timetable/TimetableRepositoryTest.kt | 62 +++-- .../github/wulkanowy/data/RepositoryModule.kt | 10 - .../wulkanowy/data/db/dao/AttendanceDao.kt | 3 +- .../data/db/dao/AttendanceSummaryDao.kt | 3 +- .../github/wulkanowy/data/db/dao/BaseDao.kt | 6 +- .../data/db/dao/CompletedLessonsDao.kt | 3 +- .../github/wulkanowy/data/db/dao/ExamDao.kt | 3 +- .../github/wulkanowy/data/db/dao/GradeDao.kt | 4 +- .../data/db/dao/GradePointsStatisticsDao.kt | 5 +- .../data/db/dao/GradeStatisticsDao.kt | 5 +- .../wulkanowy/data/db/dao/GradeSummaryDao.kt | 3 +- .../wulkanowy/data/db/dao/HomeworkDao.kt | 3 +- .../wulkanowy/data/db/dao/LuckyNumberDao.kt | 3 +- .../data/db/dao/MessageAttachmentDao.kt | 2 +- .../wulkanowy/data/db/dao/MessagesDao.kt | 8 +- .../wulkanowy/data/db/dao/MobileDeviceDao.kt | 3 +- .../github/wulkanowy/data/db/dao/NoteDao.kt | 3 +- .../wulkanowy/data/db/dao/RecipientDao.kt | 3 +- .../wulkanowy/data/db/dao/ReportingUnitDao.kt | 5 +- .../github/wulkanowy/data/db/dao/SchoolDao.kt | 3 +- .../wulkanowy/data/db/dao/SemesterDao.kt | 3 +- .../wulkanowy/data/db/dao/StudentDao.kt | 15 +- .../wulkanowy/data/db/dao/SubjectDao.kt | 3 +- .../wulkanowy/data/db/dao/TeacherDao.kt | 3 +- .../wulkanowy/data/db/dao/TimetableDao.kt | 3 +- .../appcreator/AppCreatorRepository.kt | 13 +- .../attendance/AttendanceLocal.kt | 9 +- .../attendance/AttendanceRemote.kt | 43 ++- .../attendance/AttendanceRepository.kt | 35 +-- .../AttendanceSummaryLocal.kt | 9 +- .../AttendanceSummaryRemote.kt | 33 +-- .../AttendanceSummaryRepository.kt | 28 +- .../completedlessons/CompletedLessonsLocal.kt | 9 +- .../CompletedLessonsRemote.kt | 35 ++- .../CompletedLessonsRepository.kt | 34 +-- .../data/repositories/exam/ExamLocal.kt | 8 +- .../data/repositories/exam/ExamRemote.kt | 31 +-- .../data/repositories/exam/ExamRepository.kt | 34 +-- .../data/repositories/grade/GradeLocal.kt | 21 +- .../data/repositories/grade/GradeRemote.kt | 72 ++--- .../repositories/grade/GradeRepository.kt | 134 ++++----- .../gradestatistics/GradeStatisticsLocal.kt | 32 +-- .../gradestatistics/GradeStatisticsRemote.kt | 43 ++- .../GradeStatisticsRepository.kt | 83 +++--- .../repositories/homework/HomeworkLocal.kt | 10 +- .../repositories/homework/HomeworkRemote.kt | 29 +- .../homework/HomeworkRepository.kt | 42 +-- .../repositories/logger/LoggerRepository.kt | 26 +- .../luckynumber/LuckyNumberLocal.kt | 15 +- .../luckynumber/LuckyNumberRemote.kt | 5 +- .../luckynumber/LuckyNumberRepository.kt | 58 ++-- .../data/repositories/message/MessageLocal.kt | 16 +- .../repositories/message/MessageRemote.kt | 49 ++-- .../repositories/message/MessageRepository.kt | 117 ++++---- .../mobiledevice/MobileDeviceLocal.kt | 9 +- .../mobiledevice/MobileDeviceRemote.kt | 25 +- .../mobiledevice/MobileDeviceRepository.kt | 45 +-- .../data/repositories/note/NoteLocal.kt | 11 +- .../data/repositories/note/NoteRemote.kt | 29 +- .../data/repositories/note/NoteRepository.kt | 52 ++-- .../repositories/recipient/RecipientLocal.kt | 9 +- .../repositories/recipient/RecipientRemote.kt | 13 +- .../recipient/RecipientRepository.kt | 39 +-- .../repositories/recover/RecoverRemote.kt | 5 +- .../repositories/recover/RecoverRepository.kt | 20 +- .../reportingunit/ReportingUnitLocal.kt | 11 +- .../reportingunit/ReportingUnitRemote.kt | 23 +- .../reportingunit/ReportingUnitRepository.kt | 46 ++-- .../data/repositories/school/SchoolLocal.kt | 7 +- .../data/repositories/school/SchoolRemote.kt | 5 +- .../repositories/school/SchoolRepository.kt | 39 +-- .../repositories/semester/SemesterLocal.kt | 9 +- .../repositories/semester/SemesterRemote.kt | 31 +-- .../semester/SemesterRepository.kt | 47 ++-- .../data/repositories/student/StudentLocal.kt | 59 ++-- .../repositories/student/StudentRemote.kt | 13 +- .../repositories/student/StudentRepository.kt | 56 ++-- .../data/repositories/subject/SubjectLocal.kt | 8 +- .../repositories/subject/SubjectRemote.kt | 19 +- .../repositories/subject/SubjectRepository.kt | 31 +-- .../data/repositories/teacher/TeacherLocal.kt | 9 +- .../repositories/teacher/TeacherRemote.kt | 21 +- .../repositories/teacher/TeacherRepository.kt | 28 +- .../repositories/timetable/TimetableLocal.kt | 9 +- .../repositories/timetable/TimetableRemote.kt | 45 ++- .../timetable/TimetableRepository.kt | 52 ++-- .../java/io/github/wulkanowy/di/AppModule.kt | 5 + .../alarm/TimetableNotificationReceiver.kt | 3 +- .../wulkanowy/services/sync/SyncWorker.kt | 10 +- .../sync/works/AttendanceSummaryWork.kt | 3 +- .../services/sync/works/AttendanceWork.kt | 4 +- .../sync/works/CompletedLessonWork.kt | 4 +- .../wulkanowy/services/sync/works/ExamWork.kt | 3 +- .../sync/works/GradeStatisticsWork.kt | 4 +- .../services/sync/works/GradeWork.kt | 17 +- .../services/sync/works/HomeworkWork.kt | 3 +- .../services/sync/works/LuckyNumberWork.kt | 8 +- .../services/sync/works/MessageWork.kt | 8 +- .../wulkanowy/services/sync/works/NoteWork.kt | 8 +- .../services/sync/works/RecipientWork.kt | 6 +- .../services/sync/works/TeacherWork.kt | 3 +- .../services/sync/works/TimetableWork.kt | 4 +- .../github/wulkanowy/ui/base/BasePresenter.kt | 10 +- .../github/wulkanowy/ui/base/ErrorDialog.kt | 4 +- .../github/wulkanowy/ui/base/ErrorHandler.kt | 2 +- .../about/contributor/ContributorPresenter.kt | 3 +- .../about/logviewer/LogViewerPresenter.kt | 5 +- .../ui/modules/account/AccountPresenter.kt | 14 +- .../modules/attendance/AttendancePresenter.kt | 17 +- .../summary/AttendanceSummaryPresenter.kt | 13 +- .../ui/modules/exam/ExamPresenter.kt | 11 +- .../ui/modules/grade/GradeAverageProvider.kt | 25 +- .../ui/modules/grade/GradePresenter.kt | 5 +- .../grade/details/GradeDetailsPresenter.kt | 16 +- .../statistics/GradeStatisticsPresenter.kt | 23 +- .../grade/summary/GradeSummaryPresenter.kt | 5 +- .../ui/modules/homework/HomeworkPresenter.kt | 11 +- .../details/HomeworkDetailsPresenter.kt | 3 +- .../ui/modules/login/LoginErrorHandler.kt | 2 +- .../login/advanced/LoginAdvancedPresenter.kt | 11 +- .../modules/login/form/LoginFormPresenter.kt | 3 +- .../login/recover/LoginRecoverPresenter.kt | 5 +- .../LoginStudentSelectPresenter.kt | 8 +- .../login/symbol/LoginSymbolPresenter.kt | 3 +- .../luckynumber/LuckyNumberPresenter.kt | 6 +- .../LuckyNumberWidgetConfigurePresenter.kt | 3 +- .../LuckyNumberWidgetProvider.kt | 12 +- .../preview/MessagePreviewPresenter.kt | 9 +- .../message/send/SendMessagePresenter.kt | 16 +- .../message/tab/MessageTabPresenter.kt | 7 +- .../mobiledevice/MobileDevicePresenter.kt | 15 +- .../token/MobileDeviceTokenPresenter.kt | 7 +- .../ui/modules/note/NotePresenter.kt | 9 +- .../school/SchoolPresenter.kt | 8 +- .../teacher/TeacherPresenter.kt | 7 +- .../ui/modules/splash/SplashPresenter.kt | 3 +- .../modules/timetable/TimetablePresenter.kt | 11 +- .../completed/CompletedLessonsErrorHandler.kt | 2 +- .../completed/CompletedLessonsPresenter.kt | 11 +- .../TimetableWidgetConfigurePresenter.kt | 3 +- .../timetablewidget/TimetableWidgetFactory.kt | 10 +- .../TimetableWidgetProvider.kt | 10 +- .../wulkanowy/utils/DispatchersProvider.kt | 10 + .../wulkanowy/utils/ResourcesExtension.kt | 8 +- .../wulkanowy/utils/CrashlyticsUtils.kt | 2 +- .../UnitTestInternetObservingStrategy.kt | 19 -- .../attendance/AttendanceRemoteTest.kt | 19 +- .../CompletedLessonsRemoteTest.kt | 19 +- .../data/repositories/exam/ExamRemoteTest.kt | 19 +- .../GradeStatisticsRemoteTest.kt | 15 +- .../luckynumber/LuckyNumberRemoteTest.kt | 18 +- .../message/MessageRepositoryTest.kt | 71 ++--- .../MobileDeviceRepositoryTest.kt | 41 ++- .../semester/SemesterRepositoryTest.kt | 104 +++---- .../repositories/student/StudentRemoteTest.kt | 17 +- .../timetable/TimetableRemoteTest.kt | 19 +- .../modules/grade/GradeAverageProviderTest.kt | 259 +++++++++--------- .../ui/modules/login/LoginPresenterTest.kt | 32 +-- .../login/form/LoginFormPresenterTest.kt | 170 ++++++------ .../LoginStudentSelectPresenterTest.kt | 64 +++-- .../ui/modules/main/MainPresenterTest.kt | 40 ++- .../ui/modules/splash/SplashPresenterTest.kt | 25 +- .../wulkanowy/utils/GradeExtensionTest.kt | 8 +- 177 files changed, 1752 insertions(+), 2004 deletions(-) delete mode 100644 app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestInternetObservingStrategy.kt create mode 100644 app/src/main/java/io/github/wulkanowy/utils/DispatchersProvider.kt delete mode 100644 app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt diff --git a/app/build.gradle b/app/build.gradle index 621e5a4f..0f0a3303 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -124,10 +124,13 @@ configurations.all { } 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 "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.appcompat:appcompat:1.2.0-rc01" implementation "androidx.appcompat:appcompat-resources:1.1.0" @@ -167,7 +170,6 @@ dependencies { implementation "com.ncapdevi:frag-nav:3.3.0" 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:rxjava:2.2.19" @@ -197,7 +199,6 @@ dependencies { testImplementation "junit:junit:4.13" testImplementation "io.mockk:mockk:$mockk" 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:runner:1.2.0" @@ -205,7 +206,6 @@ dependencies { androidTestImplementation "io.mockk:mockk-android:$mockk" androidTestImplementation "androidx.room:room-testing:$room" androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" - androidTestImplementation "org.mockito:mockito-android:3.3.3" } apply plugin: 'com.google.gms.google-services' diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration12Test.kt b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration12Test.kt index 0bbcc427..b312048d 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration12Test.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration12Test.kt @@ -4,6 +4,7 @@ import android.content.ContentValues import android.database.sqlite.SQLiteDatabase.CONFLICT_FAIL import androidx.sqlite.db.SupportSQLiteDatabase import androidx.test.ext.junit.runners.AndroidJUnit4 +import kotlinx.coroutines.runBlocking import org.junit.Test import org.junit.runner.RunWith import kotlin.test.assertEquals @@ -29,7 +30,7 @@ class Migration12Test : AbstractMigrationTest() { helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) val db = getMigratedRoomDatabase() - val students = db.studentDao.loadAll().blockingGet() + val students = runBlocking { db.studentDao.loadAll() } assertEquals(2, students.size) @@ -58,7 +59,7 @@ class Migration12Test : AbstractMigrationTest() { helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) val db = getMigratedRoomDatabase() - val students = db.studentDao.loadAll().blockingGet() + val students = runBlocking { db.studentDao.loadAll() } assertEquals(1, students.size) @@ -84,7 +85,7 @@ class Migration12Test : AbstractMigrationTest() { helper.runMigrationsAndValidate(dbName, 12, true, Migration12()) val db = getMigratedRoomDatabase() - val students = db.studentDao.loadAll().blockingGet() + val students = runBlocking { db.studentDao.loadAll() } assertEquals(3, students.size) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt index da4284b0..05a8a5cf 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt @@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase import io.github.wulkanowy.data.db.Converters import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test @@ -26,7 +27,7 @@ class Migration13Test : AbstractMigrationTest() { helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) val db = getMigratedRoomDatabase() - val students = db.studentDao.loadAll().blockingGet() + val students = runBlocking { db.studentDao.loadAll() } assertEquals(3, students.size) @@ -60,7 +61,7 @@ class Migration13Test : AbstractMigrationTest() { helper.runMigrationsAndValidate(dbName, 13, true, Migration13()) val db = getMigratedRoomDatabase() - val students = db.studentDao.loadAll().blockingGet() + val students = runBlocking { db.studentDao.loadAll() } assertEquals(2, students.size) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestInternetObservingStrategy.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestInternetObservingStrategy.kt deleted file mode 100644 index 7dc93c4a..00000000 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestInternetObservingStrategy.kt +++ /dev/null @@ -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 { - return Single.just(true) - } - - override fun observeInternetConnectivity(initialIntervalInMs: Int, intervalInMs: Int, host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Observable { - return Observable.just(true) - } - - override fun getDefaultPingHost() = "localhost" -} diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt index cdfc524a..f9326b2d 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -35,7 +36,7 @@ class AttendanceLocalTest { @Test fun saveAndReadTest() { - attendanceLocal.saveAttendance(listOf( + val list = listOf( getAttendanceEntity( of(2018, 9, 10), SentExcuseStatus.ACCEPTED @@ -48,14 +49,11 @@ class AttendanceLocalTest { of(2018, 9, 17), SentExcuseStatus.ACCEPTED ) - )) + ) + runBlocking { attendanceLocal.saveAttendance(list) } - val attendance = attendanceLocal - .getAttendance(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1), - of(2018, 9, 10), - of(2018, 9, 14) - ) - .blockingGet() + 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(attendance[0].date, of(2018, 9, 10)) assertEquals(attendance[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt index 50badc46..d8aac23d 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -24,7 +25,8 @@ class CompletedLessonsLocalTest { @Before fun createDb() { - testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) + testDb = Room + .inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) .build() completedLessonsLocal = CompletedLessonsLocal(testDb.completedLessonsDao) } @@ -36,18 +38,15 @@ class CompletedLessonsLocalTest { @Test fun saveAndReadTest() { - completedLessonsLocal.saveCompletedLessons(listOf( + val list = listOf( getCompletedLesson(of(2018, 9, 10), 1), getCompletedLesson(of(2018, 9, 14), 2), getCompletedLesson(of(2018, 9, 17), 3) - )) + ) + runBlocking { completedLessonsLocal.saveCompletedLessons(list) } - val completed = completedLessonsLocal - .getCompletedLessons(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1), - of(2018, 9, 10), - of(2018, 9, 14) - ) - .blockingGet() + 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(completed[0].date, of(2018, 9, 10)) assertEquals(completed[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt index 98dfc88e..f3b179a5 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -34,18 +35,15 @@ class ExamLocalTest { @Test fun saveAndReadTest() { - examLocal.saveExams(listOf( + val list = listOf( Exam(1, 2, of(2018, 9, 10), now(), "", "", "", "", "", ""), Exam(1, 2, of(2018, 9, 14), now(), "", "", "", "", "", ""), Exam(1, 2, of(2018, 9, 17), now(), "", "", "", "", "", "") - )) + ) + runBlocking { examLocal.saveExams(list) } - val exams = examLocal - .getExams(Semester(1, 2, "", 1, 3, 2019, now(), now(), 1, 1), - of(2018, 9, 10), - of(2018, 9, 14) - ) - .blockingGet() + 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(exams[0].date, of(2018, 9, 10)) assertEquals(exams[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt index eb1a5548..82129d86 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt @@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -22,7 +23,8 @@ class GradeLocalTest { @Before fun createDb() { - testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) + testDb = Room + .inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) .build() gradeLocal = GradeLocal(testDb.gradeDao, testDb.gradeSummaryDao) } @@ -34,17 +36,16 @@ class GradeLocalTest { @Test fun saveAndReadTest() { - gradeLocal.saveGrades(listOf( + val list = listOf( createGradeLocal(5, 3.0, LocalDate.of(2018, 9, 10), "", 1), createGradeLocal(4, 4.0, LocalDate.of(2019, 2, 27), "", 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 grades = gradeLocal - .getGradesDetails(semester) - .blockingGet() + val grades = runBlocking { gradeLocal.getGradesDetails(semester) } assertEquals(2, grades.size) assertEquals(grades[0].date, LocalDate.of(2019, 2, 27)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt index cdd51477..5487fd4c 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt @@ -5,17 +5,16 @@ import androidx.room.Room import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.ext.junit.runners.AndroidJUnit4 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.entities.Semester 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.pojo.Grade import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -33,10 +32,6 @@ class GradeRepositoryTest { @MockK private lateinit var mockSdk: Sdk - private val settings = InternetObservingSettings.builder() - .strategy(TestInternetObservingStrategy()) - .build() - @MockK private lateinit var semesterMock: Semester @@ -71,15 +66,17 @@ class GradeRepositoryTest { @Test 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, 26), "przed zalogowanie w aplikacji"), createGradeApi(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"), createGradeApi(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza") ) to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet().first.sortedByDescending { it.date } + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + .first.sortedByDescending { it.date } + } assertFalse { grades[0].isRead } assertFalse { grades[1].isRead } @@ -89,21 +86,24 @@ class GradeRepositoryTest { @Test fun mitigateOldGradesNotifications() { - gradeLocal.saveGrades(listOf( + val list = listOf( createGradeLocal(5, 3.0, of(2019, 2, 25), "Jedna ocena"), createGradeLocal(4, 4.0, of(2019, 2, 26), "Druga"), 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(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(2, 5.0, of(2019, 2, 28), "Ta jest już w ogóle nowa") ) to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet().first.sortedByDescending { it.date } + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + .first.sortedByDescending { it.date } + } assertFalse { grades[0].isRead } assertFalse { grades[1].isRead } @@ -113,69 +113,76 @@ class GradeRepositoryTest { @Test 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(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(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") ) to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet() + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + } assertEquals(2, grades.first.size) } @Test fun subtractRemoteDuplicateGrades() { - gradeLocal.saveGrades(listOf( + val list = listOf( createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama 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") ) to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet() + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + } assertEquals(3, grades.first.size) } @Test 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(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") ) to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet() + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + } assertEquals(3, grades.first.size) } @Test fun emptyRemote() { - gradeLocal.saveGrades(listOf( + val list = listOf( createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") - )) + ) + runBlocking { gradeLocal.saveGrades(list) } - every { mockSdk.getGrades(1) } returns Single.just(emptyList() to emptyList()) + coEvery { mockSdk.getGrades(1) } returns (emptyList() to emptyList()) - val grades = GradeRepository(settings, gradeLocal, gradeRemote) - .getGrades(studentMock, semesterMock, true).blockingGet() + val grades = runBlocking { + GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + } assertEquals(0, grades.first.size) } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt index bd3635fe..deda67ba 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt @@ -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.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -35,25 +36,27 @@ class GradeStatisticsLocalTest { @Test fun saveAndRead_subject() { - gradeStatisticsLocal.saveGradesStatistics(listOf( + val list = listOf( getGradeStatistics("Matematyka", 2, 1), 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(stats[0].subject, "Matematyka") } @Test fun saveAndRead_all() { - gradeStatisticsLocal.saveGradesStatistics(listOf( + val list = listOf( getGradeStatistics("Matematyka", 2, 1), getGradeStatistics("Chemia", 2, 1), 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(stats[0].subject, "Wszystkie") assertEquals(stats[1].subject, "Matematyka") @@ -62,13 +65,14 @@ class GradeStatisticsLocalTest { @Test fun saveAndRead_points() { - gradeStatisticsLocal.saveGradesPointsStatistics(listOf( + val list = listOf( getGradePointsStatistics("Matematyka", 2, 1), getGradePointsStatistics("Chemia", 2, 1), 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]) { assertEquals(subject, "Matematyka") assertEquals(others, 5.0) @@ -78,18 +82,18 @@ class GradeStatisticsLocalTest { @Test fun saveAndRead_subjectEmpty() { - gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) + runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) } - val stats = gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka").blockingGet() - assertEquals(null, stats) + val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka") } + assertEquals(emptyList(), stats) } @Test fun saveAndRead_allEmpty() { - gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) + runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) } - val stats = gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Wszystkie").blockingGet() - assertEquals(null, stats) + val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Wszystkie") } + assertEquals(emptyList(), stats) } private fun getSemester(): Semester { diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt index efad0d4d..f37d7934 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -23,7 +24,8 @@ class LuckyNumberLocalTest { @Before fun createDb() { - testDb = Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) + testDb = Room + .inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext(), AppDatabase::class.java) .build() luckyNumberLocal = LuckyNumberLocal(testDb.luckyNumberDao) } @@ -35,14 +37,14 @@ class LuckyNumberLocalTest { @Test 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()), - LocalDate.of(2019, 1, 20) - ).blockingGet() + val student = Student("", "", "", "", "", "", false, "", "", "", 1, 1, "", "", "", "", "", 1, false, now()) + val luckyNumber = runBlocking { luckyNumberLocal.getLuckyNumber(student, LocalDate.of(2019, 1, 20)) } - assertEquals(1, luckyNumber.studentId) - assertEquals(LocalDate.of(2019, 1, 20), luckyNumber.date) - assertEquals(14, luckyNumber.luckyNumber) + assertEquals(1, luckyNumber?.studentId) + assertEquals(LocalDate.of(2019, 1, 20), luckyNumber?.date) + assertEquals(14, luckyNumber?.luckyNumber) } } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt index 22578e41..9ba8a9fb 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt @@ -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.ReportingUnit import io.github.wulkanowy.data.db.entities.Student +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -35,17 +36,21 @@ class RecipientLocalTest { @Test fun saveAndReadTest() { - recipientLocal.saveRecipients(listOf( + val list = listOf( 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, "4rPracownik", "Krupa Stanisław", "Krupa Stanisław [KS] - Uczeń (Fake123456)", 5, 4, 1, "hash") - )) + ) + runBlocking { recipientLocal.saveRecipients(list) } - val recipients = recipientLocal.getRecipients( - Student("fakelog.cf", "AUTO", "", "", "", "", false, "", "", "", 1, 0, "", "", "", "", "", 1, true, LocalDateTime.now()), - 2, - ReportingUnit(1, 4, "", 0, "", emptyList()) - ).blockingGet() + val student = Student("fakelog.cf", "AUTO", "", "", "", "", false, "", "", "", 1, 0, "", "", "", "", "", 1, true, LocalDateTime.now()) + val recipients = runBlocking { + recipientLocal.getRecipients( + student = student, + role = 2, + unit = ReportingUnit(1, 4, "", 0, "", emptyList()) + ) + } assertEquals(2, recipients.size) assertEquals(1, recipients[0].studentId) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt index 530bfb3f..02a13344 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.repositories.getStudent +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -36,9 +37,9 @@ class StudentLocalTest { @Test fun saveAndReadTest() { - studentLocal.saveStudents(listOf(student)).blockingGet() + runBlocking { studentLocal.saveStudents(listOf(student)) } - val student = studentLocal.getCurrentStudent(true).blockingGet() - assertEquals("23", student.schoolSymbol) + val student = runBlocking { studentLocal.getCurrentStudent(true) } + assertEquals("23", student?.schoolSymbol) } } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt index a66e5843..fa353a33 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt @@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -34,17 +35,21 @@ class TimetableLocalTest { @Test fun saveAndReadTest() { - timetableDb.saveTimetable(listOf( + val list = listOf( createTimetableLocal(of(2018, 9, 10, 0, 0, 0), 1), createTimetableLocal(of(2018, 9, 14, 0, 0, 0), 1), createTimetableLocal(of(2018, 9, 17, 0, 0, 0), 1) - )) + ) + runBlocking { timetableDb.saveTimetable(list) } - val exams = timetableDb.getTimetable( - Semester(1, 2, "", 1, 1, 2019, LocalDate.now(), LocalDate.now(), 1, 1), - LocalDate.of(2018, 9, 10), - LocalDate.of(2018, 9, 14) - ).blockingGet() + val semester = Semester(1, 2, "", 1, 1, 2019, LocalDate.now(), LocalDate.now(), 1, 1) + val exams = runBlocking { + timetableDb.getTimetable( + semester = semester, + startDate = LocalDate.of(2018, 9, 10), + endDate = LocalDate.of(2018, 9, 14) + ) + } assertEquals(2, exams.size) assertEquals(exams[0].date, LocalDate.of(2018, 9, 10)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt index 75f2f0b8..a91651db 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt @@ -5,19 +5,18 @@ import androidx.room.Room import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.ext.junit.runners.AndroidJUnit4 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.entities.Semester 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.services.alarm.TimetableNotificationSchedulerHelper import io.github.wulkanowy.sdk.Sdk +import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.mockk -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before import org.junit.Test @@ -33,10 +32,6 @@ class TimetableRepositoryTest { @MockK private lateinit var mockSdk: Sdk - private val settings = InternetObservingSettings.builder() - .strategy(TestInternetObservingStrategy()) - .build() - @MockK private lateinit var studentMock: Student @@ -82,23 +77,31 @@ class TimetableRepositoryTest { @Test fun copyRoomToCompletedFromPrevious() { - timetableLocal.saveTimetable(listOf( - 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, 9, 40), 3, "213", "W-F"), - createTimetableLocal(of(2019, 3, 5, 10, 30),3, "213", "W-F", "Jan Kowalski") - )) + runBlocking { + timetableLocal.saveTimetable(listOf( + 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, 9, 40), 3, "213", "W-F"), + 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, 50), 2, "", "Religia"), createTimetableRemote(of(2019, 3, 5, 9, 40), 3, "", "W-F"), createTimetableRemote(of(2019, 3, 5, 10, 30), 4, "", "W-F") - )) + ) - val lessons = TimetableRepository(settings, timetableLocal, timetableRemote, timetableNotificationSchedulerHelper) - .getTimetable(student, semesterMock, LocalDate.of(2019, 3, 5), LocalDate.of(2019, 3, 5), true) - .blockingGet() + val lessons = runBlocking { + TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable( + student = student, + semester = semesterMock, + start = LocalDate.of(2019, 3, 5), + end = LocalDate.of(2019, 3, 5), + forceRefresh = true + ) + } assertEquals(4, lessons.size) assertEquals("123", lessons[0].room) @@ -108,7 +111,7 @@ class TimetableRepositoryTest { @Test 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, 50), 2, "124", "Matematyka", "Paweł Poniedziałkowski", false), 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, 9, 40), 3, "125", "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, 50), 2, "124", "Matematyka", "Jakub Wtorkowski", true), 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, 9, 40), 3, "125", "Matematyka", "Paweł Środowski", false), createTimetableRemote(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true) - )) + ) - val lessons = TimetableRepository(settings, timetableLocal, timetableRemote, timetableNotificationSchedulerHelper) - .getTimetable(student, semesterMock, LocalDate.of(2019, 12, 23), LocalDate.of(2019, 12, 25), true) - .blockingGet() + val lessons = runBlocking { + TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable( + student = student, + semester = semesterMock, + start = LocalDate.of(2019, 12, 23), + end = LocalDate.of(2019, 12, 25), + forceRefresh = true + ) + } assertEquals(12, lessons.size) diff --git a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt index 19af1b2f..6ece2d97 100644 --- a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt +++ b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt @@ -8,8 +8,6 @@ import androidx.preference.PreferenceManager import com.chuckerteam.chucker.api.ChuckerCollector import com.chuckerteam.chucker.api.ChuckerInterceptor 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.Provides import io.github.wulkanowy.data.db.AppDatabase @@ -22,14 +20,6 @@ import javax.inject.Singleton @Module internal class RepositoryModule { - @Singleton - @Provides - fun provideInternetObservingSettings(): InternetObservingSettings { - return InternetObservingSettings.builder() - .strategy(WalledGardenInternetObservingStrategy()) - .build() - } - @Singleton @Provides fun provideSdk(chuckerCollector: ChuckerCollector, context: Context): Sdk { diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt index 3eb57473..49527a55 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Attendance -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface AttendanceDao : BaseDao { @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> + suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt index fd58533f..1ba37c95 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt @@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.AttendanceSummary -import io.reactivex.Maybe @Dao interface AttendanceSummaryDao : BaseDao { @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> + suspend fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/BaseDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/BaseDao.kt index 32dbadb8..048e9e3c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/BaseDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/BaseDao.kt @@ -7,11 +7,11 @@ import androidx.room.Update interface BaseDao { @Insert - fun insertAll(items: List): List + suspend fun insertAll(items: List): List @Update - fun updateAll(items: List) + suspend fun updateAll(items: List) @Delete - fun deleteAll(items: List) + suspend fun deleteAll(items: List) } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt index e13e569b..6406d097 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.CompletedLesson -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface CompletedLessonsDao : BaseDao { @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> + suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt index ca6b32df..e492f7b8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Exam -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface ExamDao : BaseDao { @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> + suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt index c74d1937..df027620 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Grade -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,6 +10,5 @@ import javax.inject.Singleton interface GradeDao : BaseDao { @Query("SELECT * FROM Grades WHERE semester_id = :semesterId AND student_id = :studentId") - fun loadAll(semesterId: Int, studentId: Int): Maybe> - + suspend fun loadAll(semesterId: Int, studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt index da182866..b1e644bb 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradePointsStatistics -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,8 +10,8 @@ import javax.inject.Singleton interface GradePointsStatisticsDao : BaseDao { @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName") - fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): Maybe> + suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): List @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId") - fun loadAll(semesterId: Int, studentId: Int): Maybe> + suspend fun loadAll(semesterId: Int, studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt index 6faa35d0..786da0d9 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradeStatistics -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,8 +10,8 @@ import javax.inject.Singleton interface GradeStatisticsDao : BaseDao { @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> + suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): List @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> + suspend fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt index 1165ef07..02d4e922 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradeSummary -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface GradeSummaryDao : BaseDao { @Query("SELECT * FROM GradesSummary WHERE student_id = :studentId AND semester_id = :semesterId") - fun loadAll(semesterId: Int, studentId: Int): Maybe> + suspend fun loadAll(semesterId: Int, studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt index 1947a0df..9bbf80ac 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Homework -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface HomeworkDao : BaseDao { @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> + suspend fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt index f16c28d9..b4ead245 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.LuckyNumber -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface LuckyNumberDao : BaseDao { @Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date") - fun load(studentId: Int, date: LocalDate): Maybe + suspend fun load(studentId: Int, date: LocalDate): LuckyNumber } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessageAttachmentDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessageAttachmentDao.kt index 3c511a27..b69083a1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessageAttachmentDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessageAttachmentDao.kt @@ -9,5 +9,5 @@ import io.github.wulkanowy.data.db.entities.MessageAttachment interface MessageAttachmentDao : BaseDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAttachments(items: List): List + suspend fun insertAttachments(items: List): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt index 7a69e270..2757978a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt @@ -5,19 +5,17 @@ import androidx.room.Query import androidx.room.Transaction import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.MessageWithAttachment -import io.reactivex.Maybe -import io.reactivex.Single @Dao interface MessagesDao : BaseDao { @Transaction @Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId") - fun loadMessageWithAttachment(studentId: Int, messageId: Int): Single + 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") - fun loadAll(studentId: Int, folder: Int): Maybe> + suspend fun loadAll(studentId: Int, folder: Int): List @Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC") - fun loadDeleted(studentId: Int): Maybe> + suspend fun loadDeleted(studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt index b05b2d9c..b07aab28 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt @@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.MobileDevice -import io.reactivex.Maybe @Dao interface MobileDeviceDao : BaseDao { @Query("SELECT * FROM MobileDevices WHERE student_id = :studentId ORDER BY date DESC") - fun loadAll(studentId: Int): Maybe> + suspend fun loadAll(studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt index ea2fc6eb..81c324f6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Note -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface NoteDao : BaseDao { @Query("SELECT * FROM Notes WHERE student_id = :studentId") - fun loadAll(studentId: Int): Maybe> + suspend fun loadAll(studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt index afb941b1..419efde0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/RecipientDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Recipient -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface RecipientDao : BaseDao { @Query("SELECT * FROM Recipients WHERE student_id = :studentId AND role = :role AND unit_id = :unitId") - fun load(studentId: Int, role: Int, unitId: Int): Maybe> + suspend fun load(studentId: Int, role: Int, unitId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/ReportingUnitDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/ReportingUnitDao.kt index 6ddfd494..ca697eda 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/ReportingUnitDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/ReportingUnitDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.ReportingUnit -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,8 +10,8 @@ import javax.inject.Singleton interface ReportingUnitDao : BaseDao { @Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId") - fun load(studentId: Int): Maybe> + suspend fun load(studentId: Int): List @Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId AND real_id = :unitId") - fun loadOne(studentId: Int, unitId: Int): Maybe + suspend fun loadOne(studentId: Int, unitId: Int): ReportingUnit? } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt index e9bd6755..37cb6c50 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.School -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface SchoolDao : BaseDao { @Query("SELECT * FROM School WHERE student_id = :studentId AND class_id = :classId") - fun load(studentId: Int, classId: Int): Maybe + suspend fun load(studentId: Int, classId: Int): School? } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt index 654b80f3..bbbc9b4e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface SemesterDao : BaseDao { @Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId") - fun loadAll(studentId: Int, classId: Int): Maybe> + suspend fun loadAll(studentId: Int, classId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt index 901ddc73..3d7d9216 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/StudentDao.kt @@ -6,7 +6,6 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy.ABORT import androidx.room.Query import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -14,23 +13,23 @@ import javax.inject.Singleton interface StudentDao { @Insert(onConflict = ABORT) - fun insertAll(student: List): List + suspend fun insertAll(student: List): List @Delete - fun delete(student: Student) + suspend fun delete(student: Student) @Query("SELECT * FROM Students WHERE is_current = 1") - fun loadCurrent(): Maybe + suspend fun loadCurrent(): Student? @Query("SELECT * FROM Students WHERE id = :id") - fun loadById(id: Int): Maybe + suspend fun loadById(id: Int): Student? @Query("SELECT * FROM Students") - fun loadAll(): Maybe> + suspend fun loadAll(): List @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") - fun resetCurrent() + suspend fun resetCurrent() } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt index 525a7129..92477552 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt @@ -3,11 +3,10 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Subject -import io.reactivex.Maybe @Dao interface SubjectDao : BaseDao { @Query("SELECT * FROM Subjects WHERE diary_id = :diaryId AND student_id = :studentId") - fun loadAll(diaryId: Int, studentId: Int): Maybe> + suspend fun loadAll(diaryId: Int, studentId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt index 5ea237a8..0b0e659b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Teacher -import io.reactivex.Maybe import javax.inject.Singleton @Singleton @@ -11,5 +10,5 @@ import javax.inject.Singleton interface TeacherDao : BaseDao { @Query("SELECT * FROM Teachers WHERE student_id = :studentId AND class_id = :classId") - fun loadAll(studentId: Int, classId: Int): Maybe> + suspend fun loadAll(studentId: Int, classId: Int): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt index 6b62cc82..59200b80 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt @@ -3,7 +3,6 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Timetable -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -12,5 +11,5 @@ import javax.inject.Singleton interface TimetableDao : BaseDao { @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> + suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt index 76cf698c..3fcd7cb5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt @@ -3,14 +3,19 @@ package io.github.wulkanowy.data.repositories.appcreator import android.content.res.AssetManager import com.google.gson.Gson 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.Singleton @Singleton -class AppCreatorRepository @Inject constructor(private val assets: AssetManager) { - fun getAppCreators(): Single> { - return Single.fromCallable { +class AppCreatorRepository @Inject constructor( + private val assets: AssetManager, + private val dispatchers: DispatchersProvider +) { + + suspend fun getAppCreators(): List { + return withContext(dispatchers.backgroundThread) { Gson().fromJson( assets.open("contributors.json").bufferedReader().use { it.readText() }, Array::class.java diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt index 0f587376..b232033d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt @@ -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.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,15 +10,15 @@ import javax.inject.Singleton @Singleton class AttendanceLocal @Inject constructor(private val attendanceDb: AttendanceDao) { - fun saveAttendance(attendance: List) { + suspend fun saveAttendance(attendance: List) { attendanceDb.insertAll(attendance) } - fun deleteAttendance(attendance: List) { + suspend fun deleteAttendance(attendance: List) { attendanceDb.deleteAll(attendance) } - fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe> { - return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() } + suspend fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt index f64d920d..1f794f5a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Absent import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDate import org.threeten.bp.LocalDateTime import org.threeten.bp.LocalTime @@ -16,33 +15,31 @@ import javax.inject.Singleton @Singleton class AttendanceRemote @Inject constructor(private val sdk: Sdk) { - fun getAttendance(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { + suspend fun getAttendance(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getAttendance(startDate, endDate, semester.semesterId) - .map { attendance -> - attendance.map { - Attendance( - studentId = semester.studentId, - diaryId = semester.diaryId, - date = it.date, - timeId = it.timeId, - number = it.number, - subject = it.subject, - name = it.name, - presence = it.presence, - absence = it.absence, - exemption = it.exemption, - lateness = it.lateness, - excused = it.excused, - deleted = it.deleted, - excusable = it.excusable, - excuseStatus = it.excuseStatus?.name - ) - } + .map { + Attendance( + studentId = semester.studentId, + diaryId = semester.diaryId, + date = it.date, + timeId = it.timeId, + number = it.number, + subject = it.subject, + name = it.name, + presence = it.presence, + absence = it.absence, + exemption = it.exemption, + lateness = it.lateness, + excused = it.excused, + deleted = it.deleted, + excusable = it.excusable, + excuseStatus = it.excuseStatus?.name + ) } } - fun excuseAbsence(student: Student, semester: Semester, absenceList: List, reason: String?): Single { + suspend fun excuseAbsence(student: Student, semester: Semester, absenceList: List, reason: String?): Boolean { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).excuseForAbsence(absenceList.map { attendance -> Absent( date = LocalDateTime.of(attendance.date, LocalTime.of(0, 0)), diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt index 68e7c5f1..0fa0090e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt @@ -1,45 +1,34 @@ 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.Semester 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.sunday import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single import org.threeten.bp.LocalDate -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class AttendanceRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: AttendanceLocal, private val remote: AttendanceRemote ) { - fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean): Single> { - return local.getAttendance(semester, start.monday, end.sunday).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getAttendance(student, semester, start.monday, end.sunday) - else Single.error(UnknownHostException()) - }.flatMap { newAttendance -> - local.getAttendance(semester, start.monday, end.sunday) - .toSingle(emptyList()) - .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 } } + suspend fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean): List { + return local.getAttendance(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { + val new = remote.getAttendance(student, semester, start.monday, end.sunday) + val old = local.getAttendance(semester, start.monday, end.sunday) + + local.deleteAttendance(old.uniqueSubtract(new)) + local.saveAttendance(new.uniqueSubtract(old)) + + local.getAttendance(semester, start.monday, end.sunday) + }.filter { it.date in start..end } } - fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List, reason: String? = null): Single { + suspend fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List, reason: String? = null): Boolean { return remote.excuseAbsence(student, semester, attendanceList, reason) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt index 2e9a1006..f949f016 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt @@ -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.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class AttendanceSummaryLocal @Inject constructor(private val attendanceDb: AttendanceSummaryDao) { - fun saveAttendanceSummary(attendance: List) { + suspend fun saveAttendanceSummary(attendance: List) { attendanceDb.insertAll(attendance) } - fun deleteAttendanceSummary(attendance: List) { + suspend fun deleteAttendanceSummary(attendance: List) { attendanceDb.deleteAll(attendance) } - fun getAttendanceSummary(semester: Semester, subjectId: Int): Maybe> { - return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId).filter { it.isNotEmpty() } + suspend fun getAttendanceSummary(semester: Semester, subjectId: Int): List { + return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRemote.kt index 8fef5c39..29a0b9a7 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRemote.kt @@ -5,32 +5,29 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class AttendanceSummaryRemote @Inject constructor(private val sdk: Sdk) { - fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int): Single> { + suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getAttendanceSummary(subjectId) - .map { attendance -> - attendance.map { - AttendanceSummary( - studentId = semester.studentId, - diaryId = semester.diaryId, - subjectId = subjectId, - month = it.month, - presence = it.presence, - absence = it.absence, - absenceExcused = it.absenceExcused, - absenceForSchoolReasons = it.absenceForSchoolReasons, - lateness = it.lateness, - latenessExcused = it.latenessExcused, - exemption = it.exemption - ) - } + .map { + AttendanceSummary( + studentId = semester.studentId, + diaryId = semester.diaryId, + subjectId = subjectId, + month = it.month, + presence = it.presence, + absence = it.absence, + absenceExcused = it.absenceExcused, + absenceForSchoolReasons = it.absenceForSchoolReasons, + lateness = it.lateness, + latenessExcused = it.latenessExcused, + exemption = it.exemption + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt index 5f0b2b34..7ef16fb0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt @@ -1,35 +1,27 @@ 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.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class AttendanceSummaryRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: AttendanceSummaryLocal, private val remote: AttendanceSummaryRemote ) { - fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean = false): Single> { - return local.getAttendanceSummary(semester, subjectId).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getAttendanceSummary(student, semester, subjectId) - else Single.error(UnknownHostException()) - }.flatMap { new -> - local.getAttendanceSummary(semester, subjectId).toSingle(emptyList()) - .doOnSuccess { old -> - local.deleteAttendanceSummary(old.uniqueSubtract(new)) - local.saveAttendanceSummary(new.uniqueSubtract(old)) - } - }.flatMap { local.getAttendanceSummary(semester, subjectId).toSingle(emptyList()) }) + suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean = false): List { + return local.getAttendanceSummary(semester, subjectId).filter { !forceRefresh }.ifEmpty { + val new = remote.getAttendanceSummary(student, semester, subjectId) + + val old = local.getAttendanceSummary(semester, subjectId) + local.deleteAttendanceSummary(old.uniqueSubtract(new)) + local.saveAttendanceSummary(new.uniqueSubtract(old)) + + return local.getAttendanceSummary(semester, subjectId) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt index 9b275908..f355f416 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt @@ -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.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,15 +10,15 @@ import javax.inject.Singleton @Singleton class CompletedLessonsLocal @Inject constructor(private val completedLessonsDb: CompletedLessonsDao) { - fun saveCompletedLessons(completedLessons: List) { + suspend fun saveCompletedLessons(completedLessons: List) { completedLessonsDb.insertAll(completedLessons) } - fun deleteCompleteLessons(completedLessons: List) { + suspend fun deleteCompleteLessons(completedLessons: List) { completedLessonsDb.deleteAll(completedLessons) } - fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): Maybe> { - return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end).filter { it.isNotEmpty() } + suspend fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): List { + return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt index bb115111..b3d78605 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -13,26 +12,24 @@ import javax.inject.Singleton @Singleton class CompletedLessonsRemote @Inject constructor(private val sdk: Sdk) { - fun getCompletedLessons(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { + suspend fun getCompletedLessons(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getCompletedLessons(startDate, endDate) - .map { lessons -> - lessons.map { - it.absence - CompletedLesson( - studentId = semester.studentId, - diaryId = semester.diaryId, - date = it.date, - number = it.number, - subject = it.subject, - topic = it.topic, - teacher = it.teacher, - teacherSymbol = it.teacherSymbol, - substitution = it.substitution, - absence = it.absence, - resources = it.resources - ) - } + .map { + it.absence + CompletedLesson( + studentId = semester.studentId, + diaryId = semester.diaryId, + date = it.date, + number = it.number, + subject = it.subject, + topic = it.topic, + teacher = it.teacher, + teacherSymbol = it.teacherSymbol, + substitution = it.substitution, + absence = it.absence, + resources = it.resources + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt index 72cc93eb..8e81c54a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt @@ -1,42 +1,30 @@ 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.Semester 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.sunday import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single import org.threeten.bp.LocalDate -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class CompletedLessonsRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: CompletedLessonsLocal, private val remote: CompletedLessonsRemote ) { - fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single> { - return local.getCompletedLessons(semester, start.monday, end.sunday).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveCompletedLessons(new.uniqueSubtract(old)) - } - }.flatMap { - local.getCompletedLessons(semester, start.monday, end.sunday) - .toSingle(emptyList()) - }).map { list -> list.filter { it.date in start..end } } + suspend fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { + return local.getCompletedLessons(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { + val new = remote.getCompletedLessons(student, semester, start.monday, end.sunday) + val old = local.getCompletedLessons(semester, start.monday, end.sunday) + + local.deleteCompleteLessons(old.uniqueSubtract(new)) + local.saveCompletedLessons(new.uniqueSubtract(old)) + + local.getCompletedLessons(semester, start.monday, end.sunday) + }.filter { it.date in start..end } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt index 389eb583..d1888380 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt @@ -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.entities.Exam import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,16 +10,15 @@ import javax.inject.Singleton @Singleton class ExamLocal @Inject constructor(private val examDb: ExamDao) { - fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe> { + suspend fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return examDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) - .filter { it.isNotEmpty() } } - fun saveExams(exams: List) { + suspend fun saveExams(exams: List) { examDb.insertAll(exams) } - fun deleteExams(exams: List) { + suspend fun deleteExams(exams: List) { examDb.deleteAll(exams) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt index fb105cee..0668b5c1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -13,24 +12,22 @@ import javax.inject.Singleton @Singleton class ExamRemote @Inject constructor(private val sdk: Sdk) { - fun getExams(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { + suspend fun getExams(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getExams(startDate, endDate, semester.semesterId) - .map { exams -> - exams.map { - Exam( - studentId = semester.studentId, - diaryId = semester.diaryId, - date = it.date, - entryDate = it.entryDate, - subject = it.subject, - group = it.group, - type = it.type, - description = it.description, - teacher = it.teacher, - teacherSymbol = it.teacherSymbol - ) - } + .map { + Exam( + studentId = semester.studentId, + diaryId = semester.diaryId, + date = it.date, + entryDate = it.entryDate, + subject = it.subject, + group = it.group, + type = it.type, + description = it.description, + teacher = it.teacher, + teacherSymbol = it.teacherSymbol + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt index f29e4fdf..13af62c5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt @@ -1,42 +1,30 @@ 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.Semester 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.sunday import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single import org.threeten.bp.LocalDate -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class ExamRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: ExamLocal, private val remote: ExamRemote ) { - fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single> { - return local.getExams(semester, start.monday, end.sunday).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveExams(new.uniqueSubtract(old)) - } - }.flatMap { - local.getExams(semester, start.monday, end.sunday) - .toSingle(emptyList()) - }).map { list -> list.filter { it.date in start..end } } + suspend fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { + return local.getExams(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { + val new = remote.getExams(student, semester, start.monday, end.sunday) + val old = local.getExams(semester, start.monday, end.sunday) + + local.deleteExams(old.uniqueSubtract(new)) + local.saveExams(new.uniqueSubtract(old)) + + local.getExams(semester, start.monday, end.sunday) + }.filter { it.date in start..end } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt index 52ab6017..234fc6b8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt @@ -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.GradeSummary import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @@ -15,35 +14,35 @@ class GradeLocal @Inject constructor( private val gradeSummaryDb: GradeSummaryDao ) { - fun saveGrades(grades: List) { + suspend fun saveGrades(grades: List) { gradeDb.insertAll(grades) } - fun deleteGrades(grades: List) { + suspend fun deleteGrades(grades: List) { gradeDb.deleteAll(grades) } - fun updateGrades(grades: List) { + suspend fun updateGrades(grades: List) { gradeDb.updateAll(grades) } - fun updateGradesSummary(gradesSummary: List) { + suspend fun updateGradesSummary(gradesSummary: List) { gradeSummaryDb.updateAll(gradesSummary) } - fun getGradesDetails(semester: Semester): Maybe> { - return gradeDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } + suspend fun getGradesDetails(semester: Semester): List { + return gradeDb.loadAll(semester.semesterId, semester.studentId) } - fun saveGradesSummary(gradesSummary: List) { + suspend fun saveGradesSummary(gradesSummary: List) { gradeSummaryDb.insertAll(gradesSummary) } - fun deleteGradesSummary(gradesSummary: List) { + suspend fun deleteGradesSummary(gradesSummary: List) { gradeSummaryDb.deleteAll(gradesSummary) } - fun getGradesSummary(semester: Semester): Maybe> { - return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } + suspend fun getGradesSummary(semester: Semester): List { + return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRemote.kt index abb2f98c..9534a891 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRemote.kt @@ -6,48 +6,48 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class GradeRemote @Inject constructor(private val sdk: Sdk) { - fun getGrades(student: Student, semester: Semester): Single, List>> { - return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) + suspend fun getGrades(student: Student, semester: Semester): Pair, List> { + val (details, summary) = sdk + .init(student) + .switchDiary(semester.diaryId, semester.schoolYear) .getGrades(semester.semesterId) - .map { (details, summary) -> - details.map { - Grade( - studentId = semester.studentId, - semesterId = semester.semesterId, - subject = it.subject, - entry = it.entry, - value = it.value, - modifier = it.modifier, - comment = it.comment, - color = it.color, - gradeSymbol = it.symbol, - description = it.description, - weight = it.weight, - weightValue = it.weightValue, - date = it.date, - teacher = it.teacher - ) - } to summary.map { - GradeSummary( - semesterId = semester.semesterId, - studentId = semester.studentId, - position = 0, - subject = it.name, - predictedGrade = it.predicted, - finalGrade = it.final, - pointsSum = it.pointsSum, - proposedPoints = it.proposedPoints, - finalPoints = it.finalPoints, - average = it.average - ) - } - } + + return details.map { + Grade( + studentId = semester.studentId, + semesterId = semester.semesterId, + subject = it.subject, + entry = it.entry, + value = it.value, + modifier = it.modifier, + comment = it.comment, + color = it.color, + gradeSymbol = it.symbol, + description = it.description, + weight = it.weight, + weightValue = it.weightValue, + date = it.date, + teacher = it.teacher + ) + } to summary.map { + GradeSummary( + semesterId = semester.semesterId, + studentId = semester.studentId, + position = 0, + subject = it.name, + predictedGrade = it.predicted, + finalGrade = it.final, + pointsSum = it.pointsSum, + proposedPoints = it.proposedPoints, + finalPoints = it.finalPoints, + average = it.average + ) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt index 2ba68b96..6dcbb065 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt @@ -1,110 +1,96 @@ 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.GradeSummary import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Completable -import io.reactivex.Single import org.threeten.bp.LocalDateTime -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class GradeRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: GradeLocal, private val remote: GradeRemote ) { - fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Single, List>> { - return local.getGradesDetails(semester).flatMap { details -> - local.getGradesSummary(semester).map { summary -> details to summary } - }.filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getGrades(student, semester) - else Single.error(UnknownHostException()) - }.flatMap { (newDetails, newSummary) -> - local.getGradesDetails(semester).toSingle(emptyList()) - .doOnSuccess { old -> - val notifyBreakDate = old.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate() - local.deleteGrades(old.uniqueSubtract(newDetails)) - local.saveGrades(newDetails.uniqueSubtract(old) - .onEach { - if (it.date >= notifyBreakDate) it.apply { - isRead = false - if (notify) isNotified = false - } - }) - }.flatMap { - local.getGradesSummary(semester).toSingle(emptyList()) - .doOnSuccess { old -> - local.deleteGradesSummary(old.uniqueSubtract(newSummary)) - local.saveGradesSummary(newSummary.uniqueSubtract(old) - .onEach { summary -> - val oldSummary = old.find { oldSummary -> oldSummary.subject == summary.subject } - summary.isPredictedGradeNotified = when { - summary.predictedGrade.isEmpty() -> true - notify && oldSummary?.predictedGrade != summary.predictedGrade -> false - else -> true - } - summary.isFinalGradeNotified = when { - summary.finalGrade.isEmpty() -> true - notify && oldSummary?.finalGrade != summary.finalGrade -> false - else -> true - } + suspend fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Pair, List> { + val details = local.getGradesDetails(semester) + val summaries = local.getGradesSummary(semester) - summary.predictedGradeLastChange = when { - oldSummary == null -> LocalDateTime.now() - summary.predictedGrade != oldSummary.predictedGrade -> LocalDateTime.now() - else -> oldSummary.predictedGradeLastChange - } - summary.finalGradeLastChange = when { - oldSummary == null -> LocalDateTime.now() - summary.finalGrade != oldSummary.finalGrade -> LocalDateTime.now() - else -> oldSummary.finalGradeLastChange - } - }) - } - } - }.flatMap { - local.getGradesDetails(semester).toSingle(emptyList()).flatMap { details -> - local.getGradesSummary(semester).toSingle(emptyList()).map { summary -> - details to summary - } + if ((details.isNotEmpty() || summaries.isNotEmpty()) && !forceRefresh) { + return details to summaries + } + + val (newDetails, newSummary) = remote.getGrades(student, semester) + val oldGrades = local.getGradesDetails(semester) + + val notifyBreakDate = oldGrades.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate() + local.deleteGrades(oldGrades.uniqueSubtract(newDetails)) + local.saveGrades(newDetails.uniqueSubtract(oldGrades).onEach { + if (it.date >= notifyBreakDate) it.apply { + isRead = false + if (notify) isNotified = false } }) + + val oldSummaries = local.getGradesSummary(semester) + + local.deleteGradesSummary(oldSummaries.uniqueSubtract(newSummary)) + local.saveGradesSummary(newSummary.uniqueSubtract(oldSummaries).onEach { summary -> + val oldSummary = oldSummaries.find { oldSummary -> oldSummary.subject == summary.subject } + summary.isPredictedGradeNotified = when { + summary.predictedGrade.isEmpty() -> true + notify && oldSummary?.predictedGrade != summary.predictedGrade -> false + else -> true + } + summary.isFinalGradeNotified = when { + summary.finalGrade.isEmpty() -> true + notify && oldSummary?.finalGrade != summary.finalGrade -> false + else -> true + } + + summary.predictedGradeLastChange = when { + oldSummary == null -> LocalDateTime.now() + summary.predictedGrade != oldSummary.predictedGrade -> LocalDateTime.now() + else -> oldSummary.predictedGradeLastChange + } + summary.finalGradeLastChange = when { + oldSummary == null -> LocalDateTime.now() + summary.finalGrade != oldSummary.finalGrade -> LocalDateTime.now() + else -> oldSummary.finalGradeLastChange + } + }) + + return local.getGradesDetails(semester) to local.getGradesSummary(semester) } - fun getUnreadGrades(semester: Semester): Single> { - return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isRead } }.toSingle(emptyList()) + suspend fun getUnreadGrades(semester: Semester): List { + return local.getGradesDetails(semester).filter { grade -> !grade.isRead } } - fun getNotNotifiedGrades(semester: Semester): Single> { - return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isNotified } }.toSingle(emptyList()) + suspend fun getNotNotifiedGrades(semester: Semester): List { + return local.getGradesDetails(semester).filter { grade -> !grade.isNotified } } - fun getNotNotifiedPredictedGrades(semester: Semester): Single> { - return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } }.toSingle(emptyList()) + suspend fun getNotNotifiedPredictedGrades(semester: Semester): List { + return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } } - fun getNotNotifiedFinalGrades(semester: Semester): Single> { - return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } }.toSingle(emptyList()) + suspend fun getNotNotifiedFinalGrades(semester: Semester): List { + return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } } - fun updateGrade(grade: Grade): Completable { - return Completable.fromCallable { local.updateGrades(listOf(grade)) } + suspend fun updateGrade(grade: Grade) { + return local.updateGrades(listOf(grade)) } - fun updateGrades(grades: List): Completable { - return Completable.fromCallable { local.updateGrades(grades) } + suspend fun updateGrades(grades: List) { + return local.updateGrades(grades) } - fun updateGradesSummary(gradesSummary: List): Completable { - return Completable.fromCallable { local.updateGradesSummary(gradesSummary) } + suspend fun updateGradesSummary(gradesSummary: List) { + return local.updateGradesSummary(gradesSummary) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt index 7994bd75..d34f2b2e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt @@ -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.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @@ -15,46 +14,47 @@ class GradeStatisticsLocal @Inject constructor( private val gradePointsStatisticsDb: GradePointsStatisticsDao ) { - fun getGradesStatistics(semester: Semester, isSemester: Boolean): Maybe> { - return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester).filter { it.isNotEmpty() } + suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean): List { + return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester) } - fun getGradesPointsStatistics(semester: Semester): Maybe> { - return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() } + suspend fun getGradesPointsStatistics(semester: Semester): List { + return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId) } - fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): Maybe> { + suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): List { return when (subjectName) { - "Wszystkie" -> gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester).map { list -> - list.groupBy { it.grade }.map { + "Wszystkie" -> { + val statistics = gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester) + statistics.groupBy { it.grade }.map { GradeStatistics(semester.studentId, semester.semesterId, subjectName, it.key, it.value.fold(0) { acc, e -> acc + e.amount }, false) - } + list + } + statistics } else -> gradeStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName, isSemester) - }.filter { it.isNotEmpty() } + } } - fun getGradesPointsStatistics(semester: Semester, subjectName: String): Maybe> { + suspend fun getGradesPointsStatistics(semester: Semester, subjectName: String): List { return when (subjectName) { "Wszystkie" -> gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId) else -> gradePointsStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName) - }.filter { it.isNotEmpty() } + } } - fun saveGradesStatistics(gradesStatistics: List) { + suspend fun saveGradesStatistics(gradesStatistics: List) { gradeStatisticsDb.insertAll(gradesStatistics) } - fun saveGradesPointsStatistics(gradePointsStatistics: List) { + suspend fun saveGradesPointsStatistics(gradePointsStatistics: List) { gradePointsStatisticsDb.insertAll(gradePointsStatistics) } - fun deleteGradesStatistics(gradesStatistics: List) { + suspend fun deleteGradesStatistics(gradesStatistics: List) { gradeStatisticsDb.deleteAll(gradesStatistics) } - fun deleteGradesPointsStatistics(gradesPointsStatistics: List) { + suspend fun deleteGradesPointsStatistics(gradesPointsStatistics: List) { gradePointsStatisticsDb.deleteAll(gradesPointsStatistics) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemote.kt index 99e0cb98..1ff8132f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemote.kt @@ -6,44 +6,39 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class GradeStatisticsRemote @Inject constructor(private val sdk: Sdk) { - fun getGradeStatistics(student: Student, semester: Semester, isSemester: Boolean): Single> { + suspend fun getGradeStatistics(student: Student, semester: Semester, isSemester: Boolean): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).let { if (isSemester) it.getGradesAnnualStatistics(semester.semesterId) else it.getGradesPartialStatistics(semester.semesterId) - }.map { gradeStatistics -> - gradeStatistics.map { - GradeStatistics( - semesterId = semester.semesterId, - studentId = semester.studentId, - subject = it.subject, - grade = it.gradeValue, - amount = it.amount, - semester = isSemester - ) - } + }.map { + GradeStatistics( + semesterId = semester.semesterId, + studentId = semester.studentId, + subject = it.subject, + grade = it.gradeValue, + amount = it.amount, + semester = isSemester + ) } } - fun getGradePointsStatistics(student: Student, semester: Semester): Single> { + suspend fun getGradePointsStatistics(student: Student, semester: Semester): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getGradesPointsStatistics(semester.semesterId) - .map { gradePointsStatistics -> - gradePointsStatistics.map { - GradePointsStatistics( - semesterId = semester.semesterId, - studentId = semester.studentId, - subject = it.subject, - others = it.others, - student = it.student - ) - } + .map { + GradePointsStatistics( + semesterId = semester.semesterId, + studentId = semester.studentId, + subject = it.subject, + others = it.others, + student = it.student + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt index 8d96d2f5..93df6940 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt @@ -1,7 +1,5 @@ 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.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester @@ -9,67 +7,54 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.ui.modules.grade.statistics.ViewType import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class GradeStatisticsRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: GradeStatisticsLocal, private val remote: GradeStatisticsRemote ) { - fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean = false): Single> { - return local.getGradesStatistics(semester, isSemester, subjectName).map { it.mapToStatisticItems() }.filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveGradesStatistics(new.uniqueSubtract(old)) - } - }.flatMap { local.getGradesStatistics(semester, isSemester, subjectName).map { it.mapToStatisticItems() }.toSingle(emptyList()) }) - } + suspend fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean = false): List { + return local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems().filter { !forceRefresh }.ifEmpty { + val new = remote.getGradeStatistics(student, semester, isSemester) + val old = local.getGradesStatistics(semester, isSemester) - fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): Single> { - return local.getGradesPointsStatistics(semester, subjectName).map { it.mapToStatisticsItem() }.filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveGradesPointsStatistics(new.uniqueSubtract(old)) - } - }.flatMap { local.getGradesPointsStatistics(semester, subjectName).map { it.mapToStatisticsItem() }.toSingle(emptyList()) }) - } + local.deleteGradesStatistics(old.uniqueSubtract(new)) + local.saveGradesStatistics(new.uniqueSubtract(old)) - private fun List.mapToStatisticItems(): List { - return groupBy { it.subject }.map { - GradeStatisticsItem( - type = ViewType.PARTIAL, - partial = it.value - .sortedByDescending { item -> item.grade } - .filter { item -> item.amount != 0 }, - points = null - ) + local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems() } } - private fun List.mapToStatisticsItem(): List { - return map { - GradeStatisticsItem( - type = ViewType.POINTS, - partial = emptyList(), - points = it - ) + suspend fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): List { + return local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem().filter { !forceRefresh }.ifEmpty { + val new = remote.getGradePointsStatistics(student, semester) + val old = local.getGradesPointsStatistics(semester) + + local.deleteGradesPointsStatistics(old.uniqueSubtract(new)) + local.saveGradesPointsStatistics(new.uniqueSubtract(old)) + + local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem() } } + + private fun List.mapToStatisticItems() = groupBy { it.subject }.map { + GradeStatisticsItem( + type = ViewType.PARTIAL, + partial = it.value + .sortedByDescending { item -> item.grade } + .filter { item -> item.amount != 0 }, + points = null + ) + } + + private fun List.mapToStatisticsItem() = map { + GradeStatisticsItem( + type = ViewType.POINTS, + partial = emptyList(), + points = it + ) + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt index fdae4518..ed6bb0cf 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt @@ -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.entities.Homework import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,20 +10,19 @@ import javax.inject.Singleton @Singleton class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) { - fun saveHomework(homework: List) { + suspend fun saveHomework(homework: List) { homeworkDb.insertAll(homework) } - fun deleteHomework(homework: List) { + suspend fun deleteHomework(homework: List) { homeworkDb.deleteAll(homework) } - fun updateHomework(homework: List) { + suspend fun updateHomework(homework: List) { homeworkDb.updateAll(homework) } - fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe> { + suspend fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate) - .filter { it.isNotEmpty() } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt index 20ffee99..9e99843d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -13,23 +12,21 @@ import javax.inject.Singleton @Singleton class HomeworkRemote @Inject constructor(private val sdk: Sdk) { - fun getHomework(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { + suspend fun getHomework(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getHomework(startDate, endDate) - .map { homework -> - homework.map { - Homework( - semesterId = semester.semesterId, - studentId = semester.studentId, - date = it.date, - entryDate = it.entryDate, - subject = it.subject, - content = it.content, - teacher = it.teacher, - teacherSymbol = it.teacherSymbol, - attachments = it.attachments.map { attachment -> attachment.url to attachment.name } - ) - } + .map { + Homework( + semesterId = semester.semesterId, + studentId = semester.studentId, + date = it.date, + entryDate = it.entryDate, + subject = it.subject, + content = it.content, + teacher = it.teacher, + teacherSymbol = it.teacherSymbol, + attachments = it.attachments.map { attachment -> attachment.url to attachment.name } + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt index 7e8fd5c3..ca0a84a5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt @@ -1,49 +1,37 @@ 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.Semester 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.sunday import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Completable -import io.reactivex.Single import org.threeten.bp.LocalDate -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class HomeworkRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: HomeworkLocal, private val remote: HomeworkRemote ) { - fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single> { - return Single.fromCallable { start.monday to end.sunday }.flatMap { (monday, friday) -> - local.getHomework(semester, monday, friday).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveHomework(new.uniqueSubtract(old)) - } - }.flatMap { local.getHomework(semester, monday, friday).toSingle(emptyList()) }) + suspend fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { + return local.getHomework(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { + val new = remote.getHomework(student, semester, start.monday, end.sunday) + + val old = local.getHomework(semester, start.monday, end.sunday) + + local.deleteHomework(old.uniqueSubtract(new)) + local.saveHomework(new.uniqueSubtract(old)) + + local.getHomework(semester, start.monday, end.sunday) } } - fun toggleDone(homework: Homework): Completable { - return Completable.fromCallable { - local.updateHomework(listOf(homework.apply { - isDone = !isDone - })) - } + suspend fun toggleDone(homework: Homework) { + local.updateHomework(listOf(homework.apply { + isDone = !isDone + })) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt index be6ad2f4..d03d3ccd 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt @@ -1,29 +1,31 @@ package io.github.wulkanowy.data.repositories.logger 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.FileNotFoundException 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> { - return getLastModified() - .map { it.readText() } - .map { it.split("\n") } + suspend fun getLastLogLines(): List { + return getLastModified().readText().split("\n") } - fun getLogFiles(): Single> { - return Single.fromCallable { + suspend fun getLogFiles(): List { + return withContext(dispatchers.backgroundThread) { File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter { it.name.endsWith(".log") - } + }!! } } - private fun getLastModified(): Single { - return Single.fromCallable { + private suspend fun getLastModified(): File { + return withContext(dispatchers.backgroundThread) { var lastModifiedTime = Long.MIN_VALUE var chosenFile: File? = null 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") - chosenFile + chosenFile!! } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt index 0f4f79c8..22b5786d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt @@ -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.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,19 +10,19 @@ import javax.inject.Singleton @Singleton class LuckyNumberLocal @Inject constructor(private val luckyNumberDb: LuckyNumberDao) { - fun saveLuckyNumber(luckyNumber: LuckyNumber) { - luckyNumberDb.insertAll(listOf(luckyNumber)) + suspend fun saveLuckyNumber(luckyNumber: LuckyNumber?) { + luckyNumberDb.insertAll(listOfNotNull(luckyNumber)) } - fun updateLuckyNumber(luckyNumber: LuckyNumber) { - luckyNumberDb.updateAll(listOf(luckyNumber)) + suspend fun updateLuckyNumber(luckyNumber: LuckyNumber?) { + luckyNumberDb.updateAll(listOfNotNull(luckyNumber)) } - fun deleteLuckyNumber(luckyNumber: LuckyNumber) { - luckyNumberDb.deleteAll(listOf(luckyNumber)) + suspend fun deleteLuckyNumber(luckyNumber: LuckyNumber?) { + luckyNumberDb.deleteAll(listOfNotNull(luckyNumber)) } - fun getLuckyNumber(student: Student, date: LocalDate): Maybe { + suspend fun getLuckyNumber(student: Student, date: LocalDate): LuckyNumber? { return luckyNumberDb.load(student.studentId, date) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt index 0c71897a..e93a6c04 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt @@ -4,7 +4,6 @@ import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -12,8 +11,8 @@ import javax.inject.Singleton @Singleton class LuckyNumberRemote @Inject constructor(private val sdk: Sdk) { - fun getLuckyNumber(student: Student): Maybe { - return sdk.init(student).getLuckyNumber(student.schoolShortName).map { + suspend fun getLuckyNumber(student: Student): LuckyNumber? { + return sdk.init(student).getLuckyNumber(student.schoolShortName)?.let { LuckyNumber( studentId = student.studentId, date = LocalDate.now(), diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt index 374b9a29..3f608962 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt @@ -1,54 +1,42 @@ 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.Student -import io.reactivex.Completable -import io.reactivex.Maybe -import org.threeten.bp.LocalDate -import java.net.UnknownHostException +import org.threeten.bp.LocalDate.now import javax.inject.Inject import javax.inject.Singleton @Singleton class LuckyNumberRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: LuckyNumberLocal, private val remote: LuckyNumberRemote ) { - fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): Maybe { - return local.getLuckyNumber(student, LocalDate.now()).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMapMaybe { - if (it) remote.getLuckyNumber(student) - else Maybe.error(UnknownHostException()) - }.flatMap { new -> - local.getLuckyNumber(student, LocalDate.now()) - .doOnSuccess { old -> - if (new != old) { - local.deleteLuckyNumber(old) - local.saveLuckyNumber(new.apply { - if (notify) isNotified = false - }) - } - } - .doOnComplete { - local.saveLuckyNumber(new.apply { - if (notify) isNotified = false - }) - } - }.flatMap({ local.getLuckyNumber(student, LocalDate.now()) }, { Maybe.error(it) }, - { local.getLuckyNumber(student, LocalDate.now()) }) - ) + suspend fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): LuckyNumber? { + return local.getLuckyNumber(student, now())?.takeIf { !forceRefresh } ?: run { + val new = remote.getLuckyNumber(student) + val old = local.getLuckyNumber(student, now()) + + if (new != old) { + old?.let { local.deleteLuckyNumber(it) } + local.saveLuckyNumber(new?.apply { + if (notify) isNotified = false + }) + } + + local.saveLuckyNumber(new?.apply { + if (notify) isNotified = false + }) + + local.getLuckyNumber(student, now()) + } } - fun getNotNotifiedLuckyNumber(student: Student): Maybe { - return local.getLuckyNumber(student, LocalDate.now()).filter { !it.isNotified } + suspend fun getNotNotifiedLuckyNumber(student: Student): LuckyNumber? { + return local.getLuckyNumber(student, now()) } - fun updateLuckyNumber(luckyNumber: LuckyNumber): Completable { - return Completable.fromCallable { local.updateLuckyNumber(luckyNumber) } + suspend fun updateLuckyNumber(luckyNumber: LuckyNumber) { + local.updateLuckyNumber(luckyNumber) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt index 53cf0a98..f05c49d8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt @@ -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.Student import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED -import io.reactivex.Maybe -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @@ -18,30 +16,30 @@ class MessageLocal @Inject constructor( private val messageAttachmentDao: MessageAttachmentDao ) { - fun saveMessages(messages: List) { + suspend fun saveMessages(messages: List) { messagesDb.insertAll(messages) } - fun updateMessages(messages: List) { + suspend fun updateMessages(messages: List) { messagesDb.updateAll(messages) } - fun deleteMessages(messages: List) { + suspend fun deleteMessages(messages: List) { messagesDb.deleteAll(messages) } - fun getMessageWithAttachment(student: Student, message: Message): Single { + suspend fun getMessageWithAttachment(student: Student, message: Message): MessageWithAttachment { return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId) } - fun saveMessageAttachments(attachments: List) { + suspend fun saveMessageAttachments(attachments: List) { messageAttachmentDao.insertAttachments(attachments) } - fun getMessages(student: Student, folder: MessageFolder): Maybe> { + suspend fun getMessages(student: Student, folder: MessageFolder): List { return when (folder) { TRASHED -> messagesDb.loadDeleted(student.id.toInt()) else -> messagesDb.loadAll(student.id.toInt(), folder.id) - }.filter { it.isNotEmpty() } + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt index 2df2e20a..26ef3d9e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt @@ -9,7 +9,6 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Folder import io.github.wulkanowy.sdk.pojo.SentMessage import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDateTime.now import javax.inject.Inject import javax.inject.Singleton @@ -18,32 +17,30 @@ import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient @Singleton class MessageRemote @Inject constructor(private val sdk: Sdk) { - fun getMessages(student: Student, semester: Semester, folder: MessageFolder): Single> { - return sdk.init(student).getMessages(Folder.valueOf(folder.name), semester.start.atStartOfDay(), semester.end.atStartOfDay()).map { messages -> - messages.map { - Message( - studentId = student.id.toInt(), - realId = it.id ?: 0, - messageId = it.messageId ?: 0, - sender = it.sender.orEmpty(), - senderId = it.senderId ?: 0, - recipient = it.recipient.orEmpty(), - subject = it.subject.trim(), - date = it.date ?: now(), - content = it.content.orEmpty(), - folderId = it.folderId, - unread = it.unread ?: false, - unreadBy = it.unreadBy ?: 0, - readBy = it.readBy ?: 0, - removed = it.removed, - hasAttachments = it.hasAttachments - ) - } + suspend fun getMessages(student: Student, semester: Semester, folder: MessageFolder): List { + return sdk.init(student).getMessages(Folder.valueOf(folder.name), semester.start.atStartOfDay(), semester.end.atStartOfDay()).map { + Message( + studentId = student.id.toInt(), + realId = it.id ?: 0, + messageId = it.messageId ?: 0, + sender = it.sender.orEmpty(), + senderId = it.senderId ?: 0, + recipient = it.recipient.orEmpty(), + subject = it.subject.trim(), + date = it.date ?: now(), + content = it.content.orEmpty(), + folderId = it.folderId, + unread = it.unread ?: false, + unreadBy = it.unreadBy ?: 0, + readBy = it.readBy ?: 0, + removed = it.removed, + hasAttachments = it.hasAttachments + ) } } - fun getMessagesContentDetails(student: Student, message: Message, markAsRead: Boolean = false): Single>> { - return sdk.init(student).getMessageDetails(message.messageId, message.folderId, markAsRead, message.realId).map { details -> + suspend fun getMessagesContentDetails(student: Student, message: Message, markAsRead: Boolean = false): Pair> { + return sdk.init(student).getMessageDetails(message.messageId, message.folderId, markAsRead, message.realId).let { details -> details.content to details.attachments.map { MessageAttachment( 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): Single { + suspend fun sendMessage(student: Student, subject: String, content: String, recipients: List): SentMessage { return sdk.init(student).sendMessage( subject = subject, content = content, @@ -74,7 +71,7 @@ class MessageRemote @Inject constructor(private val sdk: Sdk) { ) } - fun deleteMessage(student: Student, message: Message): Single { + suspend fun deleteMessage(student: Student, message: Message): Boolean { return sdk.init(student).deleteMessages(listOf(message.messageId to message.folderId)) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt index 2d2c0430..7138566b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt @@ -1,7 +1,5 @@ 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.MessageWithAttachment 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.sdk.pojo.SentMessage import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Completable -import io.reactivex.Single import timber.log.Timber -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class MessageRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: MessageLocal, private val remote: MessageRemote ) { - fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): Single> { - return local.getMessages(student, folder).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveMessages(new.uniqueSubtract(old) - .onEach { - it.isNotified = !notify - }) - } - }.flatMap { local.getMessages(student, folder).toSingle(emptyList()) } - ) + suspend fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): List { + return local.getMessages(student, folder).filter { !forceRefresh }.ifEmpty { + val new = remote.getMessages(student, semester, folder) + val old = local.getMessages(student, folder) + + local.deleteMessages(old.uniqueSubtract(new)) + local.saveMessages(new.uniqueSubtract(old).onEach { + it.isNotified = !notify + }) + + local.getMessages(student, folder) + } } - fun getMessage(student: Student, message: Message, markAsRead: Boolean = false): Single { - return local.getMessageWithAttachment(student, message) - .filter { - it.message.content.isNotEmpty().also { status -> + suspend fun getMessage(student: Student, message: Message, markAsRead: Boolean = false): MessageWithAttachment { + return local.getMessageWithAttachment(student, message).let { + if (it.message.content.isNotEmpty().also { status -> Timber.d("Message content in db empty: ${!status}") - } && !it.message.unread + } && !it.message.unread) { + return@let it } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) local.getMessageWithAttachment(student, message) - else Single.error(UnknownHostException()) - } - .flatMap { dbMessage -> - remote.getMessagesContentDetails(student, dbMessage.message, markAsRead).doOnSuccess { (downloadedMessage, attachments) -> - local.updateMessages(listOf(dbMessage.message.copy(unread = !markAsRead).apply { - id = dbMessage.message.id - content = content.ifBlank { downloadedMessage } - })) - local.saveMessageAttachments(attachments) - Timber.d("Message ${message.messageId} with blank content: ${dbMessage.message.content.isBlank()}, marked as read") - } - }.flatMap { - local.getMessageWithAttachment(student, message) - } - ) + + val dbMessage = local.getMessageWithAttachment(student, message) + + val (downloadedMessage, attachments) = remote.getMessagesContentDetails(student, dbMessage.message, markAsRead) + + local.updateMessages(listOf(dbMessage.message.copy(unread = !markAsRead).apply { + id = dbMessage.message.id + content = content.ifBlank { downloadedMessage } + })) + local.saveMessageAttachments(attachments) + Timber.d("Message ${message.messageId} with blank content: ${dbMessage.message.content.isBlank()}, marked as read") + + local.getMessageWithAttachment(student, message) + } } - fun getNotNotifiedMessages(student: Student): Single> { + suspend fun getNotNotifiedMessages(student: Student): List { return local.getMessages(student, RECEIVED) - .map { it.filter { message -> !message.isNotified && message.unread } } - .toSingle(emptyList()) + .filter { message -> !message.isNotified && message.unread } } - fun updateMessages(messages: List): Completable { - return Completable.fromCallable { local.updateMessages(messages) } + suspend fun updateMessages(messages: List) { + return local.updateMessages(messages) } - fun sendMessage(student: Student, subject: String, content: String, recipients: List): Single { - return ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.sendMessage(student, subject, content, recipients) - else Single.error(UnknownHostException()) - } + suspend fun sendMessage(student: Student, subject: String, content: String, recipients: List): SentMessage { + return remote.sendMessage(student, subject, content, recipients) } - fun deleteMessage(student: Student, message: Message): Single { - return ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.deleteMessage(student, message) - else Single.error(UnknownHostException()) - } - .doOnSuccess { - if (!message.removed) local.updateMessages(listOf(message.copy(removed = true).apply { - id = message.id - content = message.content - })) - else local.deleteMessages(listOf(message)) - } + suspend fun deleteMessage(student: Student, message: Message): Boolean { + val delete = remote.deleteMessage(student, message) + + if (!message.removed) local.updateMessages(listOf(message.copy(removed = true).apply { + id = message.id + content = message.content + })) + else local.deleteMessages(listOf(message)) + + return delete // TODO: wtf } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt index 473ffa7f..911ed3af 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt @@ -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.entities.MobileDevice import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class MobileDeviceLocal @Inject constructor(private val mobileDb: MobileDeviceDao) { - fun saveDevices(devices: List) { + suspend fun saveDevices(devices: List) { mobileDb.insertAll(devices) } - fun deleteDevices(devices: List) { + suspend fun deleteDevices(devices: List) { mobileDb.deleteAll(devices) } - fun getDevices(semester: Semester): Maybe> { - return mobileDb.loadAll(semester.studentId).filter { it.isNotEmpty() } + suspend fun getDevices(semester: Semester): List { + return mobileDb.loadAll(semester.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRemote.kt index 800f9597..907e965c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRemote.kt @@ -6,37 +6,34 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.pojos.MobileDeviceToken import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class MobileDeviceRemote @Inject constructor(private val sdk: Sdk) { - fun getDevices(student: Student, semester: Semester): Single> { + suspend fun getDevices(student: Student, semester: Semester): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getRegisteredDevices() - .map { devices -> - devices.map { - MobileDevice( - studentId = semester.studentId, - date = it.createDate, - deviceId = it.id, - name = it.name - ) - } + .map { + MobileDevice( + studentId = semester.studentId, + date = it.createDate, + deviceId = it.id, + name = it.name + ) } } - fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Single { + suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Boolean { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .unregisterDevice(device.deviceId) } - fun getToken(student: Student, semester: Semester): Single { + suspend fun getToken(student: Student, semester: Semester): MobileDeviceToken { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getToken() - .map { + .let { MobileDeviceToken( token = it.token, symbol = it.symbol, diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt index 545846e8..f327ef60 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt @@ -1,53 +1,36 @@ 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.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.pojos.MobileDeviceToken import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class MobileDeviceRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: MobileDeviceLocal, private val remote: MobileDeviceRemote ) { - fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean = false): Single> { - return local.getDevices(semester).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveDevices(new uniqueSubtract old) - } - } - ).flatMap { local.getDevices(semester).toSingle(emptyList()) } + suspend fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean = false): List { + return local.getDevices(semester).filter { !forceRefresh }.ifEmpty { + val new = remote.getDevices(student, semester) + val old = local.getDevices(semester) + + local.deleteDevices(old uniqueSubtract new) + local.saveDevices(new uniqueSubtract old) + + local.getDevices(semester) + } } - fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Single { - return ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.unregisterDevice(student, semester, device) - else Single.error(UnknownHostException()) - } + suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Boolean { + return remote.unregisterDevice(student, semester, device) } - fun getToken(student: Student, semester: Semester): Single { - return ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getToken(student, semester) - else Single.error(UnknownHostException()) - } + suspend fun getToken(student: Student, semester: Semester): MobileDeviceToken { + return remote.getToken(student, semester) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt index 784e61f0..b1c6b290 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt @@ -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.entities.Note import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class NoteLocal @Inject constructor(private val noteDb: NoteDao) { - fun saveNotes(notes: List) { + suspend fun saveNotes(notes: List) { noteDb.insertAll(notes) } - fun updateNotes(notes: List) { + suspend fun updateNotes(notes: List) { noteDb.updateAll(notes) } - fun deleteNotes(notes: List) { + suspend fun deleteNotes(notes: List) { noteDb.deleteAll(notes) } - fun getNotes(student: Student): Maybe> { - return noteDb.loadAll(student.studentId).filter { it.isNotEmpty() } + suspend fun getNotes(student: Student): List { + return noteDb.loadAll(student.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRemote.kt index 2c62b608..0e488b7d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRemote.kt @@ -5,30 +5,27 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class NoteRemote @Inject constructor(private val sdk: Sdk) { - fun getNotes(student: Student, semester: Semester): Single> { + suspend fun getNotes(student: Student, semester: Semester): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getNotes(semester.semesterId) - .map { notes -> - notes.map { - Note( - studentId = semester.studentId, - date = it.date, - teacher = it.teacher, - teacherSymbol = it.teacherSymbol, - category = it.category, - categoryType = it.categoryType.id, - isPointsShow = it.showPoints, - points = it.points, - content = it.content - ) - } + .map { + Note( + studentId = semester.studentId, + date = it.date, + teacher = it.teacher, + teacherSymbol = it.teacherSymbol, + category = it.category, + categoryType = it.categoryType.id, + isPointsShow = it.showPoints, + points = it.points, + content = it.content + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt index e155e2ba..3628f5b8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt @@ -1,54 +1,44 @@ 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.Semester import io.github.wulkanowy.data.db.entities.Student 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.Singleton @Singleton class NoteRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: NoteLocal, private val remote: NoteRemote ) { - fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Single> { - return local.getNotes(student).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveNotes(new.uniqueSubtract(old) - .onEach { - if (it.date >= student.registrationDate.toLocalDate()) it.apply { - isRead = false - if (notify) isNotified = false - } - }) - } - }.flatMap { local.getNotes(student).toSingle(emptyList()) }) + suspend fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): List { + return local.getNotes(student).filter { !forceRefresh }.ifEmpty { + val new = remote.getNotes(student, semester) + val old = local.getNotes(student) + + local.deleteNotes(old.uniqueSubtract(new)) + local.saveNotes(new.uniqueSubtract(old).onEach { + if (it.date >= student.registrationDate.toLocalDate()) it.apply { + isRead = false + if (notify) isNotified = false + } + }) + + local.getNotes(student) + } } - fun getNotNotifiedNotes(student: Student): Single> { - return local.getNotes(student).map { it.filter { note -> !note.isNotified } }.toSingle(emptyList()) + suspend fun getNotNotifiedNotes(student: Student): List { + return local.getNotes(student).filter { note -> !note.isNotified } } - fun updateNote(note: Note): Completable { - return Completable.fromCallable { local.updateNotes(listOf(note)) } + suspend fun updateNote(note: Note) { + return local.updateNotes(listOf(note)) } - fun updateNotes(notes: List): Completable { - return Completable.fromCallable { local.updateNotes(notes) } + suspend fun updateNotes(notes: List) { + return local.updateNotes(notes) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocal.kt index ff817544..fac1645e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocal.kt @@ -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.ReportingUnit import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class RecipientLocal @Inject constructor(private val recipientDb: RecipientDao) { - fun getRecipients(student: Student, role: Int, unit: ReportingUnit): Maybe> { - return recipientDb.load(student.studentId, role, unit.realId).filter { it.isNotEmpty() } + suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List { + return recipientDb.load(student.studentId, role, unit.realId) } - fun saveRecipients(recipients: List): List { + suspend fun saveRecipients(recipients: List): List { return recipientDb.insertAll(recipients) } - fun deleteRecipients(recipients: List) { + suspend fun deleteRecipients(recipients: List) { recipientDb.deleteAll(recipients) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRemote.kt index e5b16a15..a5318e77 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRemote.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient @@ -14,18 +13,14 @@ import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient @Singleton class RecipientRemote @Inject constructor(private val sdk: Sdk) { - fun getRecipients(student: Student, role: Int, unit: ReportingUnit): Single> { + suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List { return sdk.init(student).getRecipients(unit.realId, role) - .map { recipients -> - recipients.map { it.toRecipient() } - } + .map { it.toRecipient() } } - fun getMessageRecipients(student: Student, message: Message): Single> { + suspend fun getMessageRecipients(student: Student, message: Message): List { return sdk.init(student).getMessageRecipients(message.messageId, message.senderId) - .map { recipients -> - recipients.map { it.toRecipient() } - } + .map { it.toRecipient() } } private fun SdkRecipient.toRecipient(): Recipient { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt index 6f8a72af..5c16c57b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt @@ -1,47 +1,32 @@ 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.Recipient import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class RecipientRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: RecipientLocal, private val remote: RecipientRemote ) { - fun getRecipients(student: Student, role: Int, unit: ReportingUnit, forceRefresh: Boolean = false): Single> { - return local.getRecipients(student, role, unit).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveRecipients(new.uniqueSubtract(old)) - } - }.flatMap { - local.getRecipients(student, role, unit).toSingle(emptyList()) - } - ) + suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit, forceRefresh: Boolean = false): List { + return local.getRecipients(student, role, unit).filter { !forceRefresh }.ifEmpty { + val new = remote.getRecipients(student, role, unit) + val old = local.getRecipients(student, role, unit) + + local.deleteRecipients(old.uniqueSubtract(new)) + local.saveRecipients(new.uniqueSubtract(old)) + + local.getRecipients(student, role, unit) + } } - fun getMessageRecipients(student: Student, message: Message): Single> { - return ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getMessageRecipients(student, message) - else Single.error(UnknownHostException()) - } + suspend fun getMessageRecipients(student: Student, message: Message): List { + return remote.getMessageRecipients(student, message) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRemote.kt index f2789125..11eac71e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRemote.kt @@ -1,18 +1,17 @@ package io.github.wulkanowy.data.repositories.recover import io.github.wulkanowy.sdk.Sdk -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class RecoverRemote @Inject constructor(private val sdk: Sdk) { - fun getReCaptchaSiteKey(host: String, symbol: String): Single> { + suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair { return sdk.getPasswordResetCaptchaCode(host, symbol) } - fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single { + suspend fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): String { return sdk.sendPasswordResetRequest(url, symbol, email, reCaptchaResponse) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRepository.kt index 86d4ba1b..3117a606 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recover/RecoverRepository.kt @@ -1,26 +1,16 @@ 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.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> { - return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getReCaptchaSiteKey(host, symbol) - else Single.error(UnknownHostException()) - } + suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair { + return remote.getReCaptchaSiteKey(host, symbol) } - fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single { - return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.sendRecoverRequest(url, symbol, email, reCaptchaResponse) - else Single.error(UnknownHostException()) - } + suspend fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): String { + return remote.sendRecoverRequest(url, symbol, email, reCaptchaResponse) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitLocal.kt index 0631c668..737f1a04 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitLocal.kt @@ -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.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class ReportingUnitLocal @Inject constructor(private val reportingUnitDb: ReportingUnitDao) { - fun getReportingUnits(student: Student): Maybe> { - return reportingUnitDb.load(student.studentId).filter { it.isNotEmpty() } + suspend fun getReportingUnits(student: Student): List { + return reportingUnitDb.load(student.studentId) } - fun getReportingUnit(student: Student, unitId: Int): Maybe { + suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit? { return reportingUnitDb.loadOne(student.studentId, unitId) } - fun saveReportingUnits(reportingUnits: List): List { + suspend fun saveReportingUnits(reportingUnits: List): List { return reportingUnitDb.insertAll(reportingUnits) } - fun deleteReportingUnits(reportingUnits: List) { + suspend fun deleteReportingUnits(reportingUnits: List) { reportingUnitDb.deleteAll(reportingUnits) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRemote.kt index 1fd8b08e..6b11c2cc 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRemote.kt @@ -4,25 +4,22 @@ import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class ReportingUnitRemote @Inject constructor(private val sdk: Sdk) { - fun getReportingUnits(student: Student): Single> { - return sdk.init(student).getReportingUnits().map { - it.map { unit -> - ReportingUnit( - studentId = sdk.studentId, - realId = unit.id, - roles = unit.roles, - senderId = unit.senderId, - senderName = unit.senderName, - shortName = unit.short - ) - } + suspend fun getReportingUnits(student: Student): List { + return sdk.init(student).getReportingUnits().map { unit -> + ReportingUnit( + studentId = sdk.studentId, + realId = unit.id, + roles = unit.roles, + senderId = unit.senderId, + senderName = unit.senderName, + shortName = unit.short + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt index ee544098..70aefb9f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt @@ -1,48 +1,34 @@ 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.Student 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.Singleton @Singleton class ReportingUnitRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: ReportingUnitLocal, private val remote: ReportingUnitRemote ) { - fun getReportingUnits(student: Student, forceRefresh: Boolean = false): Single> { - return local.getReportingUnits(student).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getReportingUnits(student) - else Single.error(UnknownHostException()) - }.flatMap { new -> - local.getReportingUnits(student).toSingle(emptyList()) - .doOnSuccess { old -> - local.deleteReportingUnits(old.uniqueSubtract(new)) - local.saveReportingUnits(new.uniqueSubtract(old)) - } - }.flatMap { local.getReportingUnits(student).toSingle(emptyList()) } - ) + suspend fun getReportingUnits(student: Student, forceRefresh: Boolean = false): List { + return local.getReportingUnits(student).filter { !forceRefresh }.ifEmpty { + val new = remote.getReportingUnits(student) + val old = local.getReportingUnits(student) + + local.deleteReportingUnits(old.uniqueSubtract(new)) + local.saveReportingUnits(new.uniqueSubtract(old)) + + local.getReportingUnits(student) + } } - fun getReportingUnit(student: Student, unitId: Int): Maybe { - return local.getReportingUnit(student, unitId) - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) getReportingUnits(student, true) - else Single.error(UnknownHostException()) - }.flatMapMaybe { - local.getReportingUnit(student, unitId) - } - ) + suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit { + return local.getReportingUnit(student, unitId) ?: run { + getReportingUnits(student, true) + + return local.getReportingUnit(student, unitId)!! + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt index d8727287..c8479b8f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt @@ -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.entities.School import io.github.wulkanowy.data.db.entities.Semester -import io.reactivex.Maybe import javax.inject.Inject class SchoolLocal @Inject constructor(private val schoolDb: SchoolDao) { - fun saveSchool(school: School) { + suspend fun saveSchool(school: School) { schoolDb.insertAll(listOf(school)) } - fun deleteSchool(school: School) { + suspend fun deleteSchool(school: School) { schoolDb.deleteAll(listOf(school)) } - fun getSchool(semester: Semester): Maybe { + suspend fun getSchool(semester: Semester): School? { return schoolDb.load(semester.studentId, semester.classId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRemote.kt index 6a95a446..4d2e0cd6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRemote.kt @@ -5,15 +5,14 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject class SchoolRemote @Inject constructor(private val sdk: Sdk) { - fun getSchoolInfo(student: Student, semester: Semester): Single { + suspend fun getSchoolInfo(student: Student, semester: Semester): School { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getSchool() - .map { + .let { School( studentId = semester.studentId, classId = semester.classId, diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt index 1715a28d..9ca945d0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt @@ -1,42 +1,29 @@ 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.Semester 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.Singleton @Singleton class SchoolRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: SchoolLocal, private val remote: SchoolRemote ) { - fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean = false): Maybe { - return local.getSchool(semester).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getSchoolInfo(student, semester) - else Single.error(UnknownHostException()) - }.flatMapMaybe { new -> - local.getSchool(semester) - .doOnSuccess { old -> - if (new != old) { - local.deleteSchool(old) - local.saveSchool(new) - } - } - .doOnComplete { - local.saveSchool(new) - } - }.flatMap({ local.getSchool(semester) }, { Maybe.error(it) }, - { local.getSchool(semester) }) - ) + suspend fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean = false): School { + return local.getSchool(semester).takeIf { it != null && !forceRefresh } ?: run { + val new = remote.getSchoolInfo(student, semester) + val old = local.getSchool(semester) + + if (new != old && old != null) { + local.deleteSchool(old) + local.saveSchool(new) + } + local.saveSchool(new) + + local.getSchool(semester)!! + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt index 3d98785c..629431c8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterLocal.kt @@ -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.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class SemesterLocal @Inject constructor(private val semesterDb: SemesterDao) { - fun saveSemesters(semesters: List) { + suspend fun saveSemesters(semesters: List) { semesterDb.insertAll(semesters) } - fun deleteSemesters(semesters: List) { + suspend fun deleteSemesters(semesters: List) { semesterDb.deleteAll(semesters) } - fun getSemesters(student: Student): Maybe> { - return semesterDb.loadAll(student.studentId, student.classId).filter { it.isNotEmpty() } + suspend fun getSemesters(student: Student): List { + return semesterDb.loadAll(student.studentId, student.classId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRemote.kt index 90f0e1d7..e1a920b6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRemote.kt @@ -4,29 +4,26 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class SemesterRemote @Inject constructor(private val sdk: Sdk) { - fun getSemesters(student: Student): Single> { - return sdk.init(student).getSemesters().map { semesters -> - semesters.map { - Semester( - studentId = student.studentId, - diaryId = it.diaryId, - diaryName = it.diaryName, - schoolYear = it.schoolYear, - semesterId = it.semesterId, - semesterName = it.semesterNumber, - start = it.start, - end = it.end, - classId = it.classId, - unitId = it.unitId - ) - } + suspend fun getSemesters(student: Student): List { + return sdk.init(student).getSemesters().map { + Semester( + studentId = student.studentId, + diaryId = it.diaryId, + diaryName = it.diaryName, + schoolYear = it.schoolYear, + semesterId = it.semesterId, + semesterName = it.semesterNumber, + start = it.start, + end = it.end, + classId = it.classId, + unitId = it.unitId + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt index 68946bee..aeb42400 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt @@ -1,47 +1,42 @@ 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.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.getCurrentOrLast import io.github.wulkanowy.utils.isCurrent import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class SemesterRepository @Inject constructor( private val remote: SemesterRemote, - private val local: SemesterLocal, - private val settings: InternetObservingSettings + private val local: SemesterLocal ) { - fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): Single> { - return local.getSemesters(student).filter { !forceRefresh }.filter { semesters -> - when { - Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0 - refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent } - else -> true - } - }.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - if (it) remote.getSemesters(student) - else Single.error(UnknownHostException()) - }.flatMap { new -> - if (new.isEmpty()) throw IllegalArgumentException("Empty semester list!") - - local.getSemesters(student).toSingle(emptyList()).doOnSuccess { old -> - local.deleteSemesters(old.uniqueSubtract(new)) - local.saveSemesters(new.uniqueSubtract(old)) + suspend fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): List { + return local.getSemesters(student).let { semesters -> + semesters.filter { + !forceRefresh && when { + Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0 + refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent } + else -> true } - }.flatMap { local.getSemesters(student).toSingle(emptyList()) }) + } + }.ifEmpty { + val new = remote.getSemesters(student) + if (new.isEmpty()) throw IllegalArgumentException("Empty semester list!") + + val old = local.getSemesters(student) + local.deleteSemesters(old.uniqueSubtract(new)) + local.saveSemesters(new.uniqueSubtract(old)) + + local.getSemesters(student) + } } - fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Single { - return getSemesters(student, forceRefresh).map { it.getCurrentOrLast() } + suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Semester { + return getSemesters(student, forceRefresh).getCurrentOrLast() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt index 06b9bb4a..5a4322f3 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt @@ -6,9 +6,6 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.security.decrypt 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.Singleton @@ -18,47 +15,41 @@ class StudentLocal @Inject constructor( private val context: Context ) { - fun saveStudents(students: List): Single> { - return Single.fromCallable { - studentDb.insertAll(students.map { - if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) it.copy(password = encrypt(it.password, context)) - else it - }) - } + suspend fun saveStudents(students: List): List { + return studentDb.insertAll(students.map { + if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) it.copy(password = encrypt(it.password, context)) + else it + }) } - fun getStudents(decryptPass: Boolean): Maybe> { - return studentDb.loadAll() - .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 { - return studentDb.loadById(id).map { - it.apply { - if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) - } - } - } - - fun getCurrentStudent(decryptPass: Boolean): Maybe { - return studentDb.loadCurrent().map { + suspend fun getStudents(decryptPass: Boolean): List { + return studentDb.loadAll().map { it.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } } - fun setCurrentStudent(student: Student): Completable { - return Completable.fromCallable { - studentDb.run { - resetCurrent() - updateCurrent(student.id) - } + suspend fun getStudentById(id: Int): Student? { + return studentDb.loadById(id)?.apply { + if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } - fun logoutStudent(student: Student): Completable { - return Completable.fromCallable { studentDb.delete(student) } + 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() + updateCurrent(student.id) + } + } + + suspend fun logoutStudent(student: Student) { + return studentDb.delete(student) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt index 4c0ffd82..74d66fed 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt @@ -2,7 +2,6 @@ package io.github.wulkanowy.data.repositories.student import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk -import io.reactivex.Single import org.threeten.bp.LocalDateTime.now import javax.inject.Inject 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> { - return sdk.getStudentsFromMobileApi(token, pin, symbol, "").map { mapStudents(it, "", "") } + suspend fun getStudentsMobileApi(token: String, pin: String, symbol: String): List { + return mapStudents(sdk.getStudentsFromMobileApi(token, pin, symbol, ""), "", "") } - fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single> { - return sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol).map { mapStudents(it, email, password) } + suspend fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): List { + return mapStudents(sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol), email, password) } - fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single> { - return sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol).map { mapStudents(it, email, password) } + suspend fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): List { + return mapStudents(sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol), email, password) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt index bebd1eb9..e47332ae 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRepository.kt @@ -1,73 +1,53 @@ 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.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.Singleton @Singleton class StudentRepository @Inject constructor( private val local: StudentLocal, - private val remote: StudentRemote, - private val settings: InternetObservingSettings + private val remote: StudentRemote ) { - fun isStudentSaved(): Single = local.getStudents(false).isEmpty.map { !it } + suspend fun isStudentSaved(): Boolean = local.getStudents(false).isNotEmpty() - fun isCurrentStudentSet(): Single = local.getCurrentStudent(false).isEmpty.map { !it } + suspend fun isCurrentStudentSet(): Boolean = local.getCurrentStudent(false)?.isCurrent ?: false - fun getStudentsApi(pin: String, symbol: String, token: String): Single> { - return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getStudentsMobileApi(token, pin, symbol) - else Single.error(UnknownHostException("No internet connection")) - } + suspend fun getStudentsApi(pin: String, symbol: String, token: String): List { + return remote.getStudentsMobileApi(token, pin, symbol) } - fun getStudentsScrapper(email: String, password: String, endpoint: String, symbol: String): Single> { - return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getStudentsScrapper(email, password, endpoint, symbol) - else Single.error(UnknownHostException("No internet connection")) - } + suspend fun getStudentsScrapper(email: String, password: String, endpoint: String, symbol: String): List { + return remote.getStudentsScrapper(email, password, endpoint, symbol) } - fun getStudentsHybrid(email: String, password: String, endpoint: String, symbol: String): Single> { - return ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - if (it) remote.getStudentsHybrid(email, password, endpoint, symbol) - else Single.error(UnknownHostException("No internet connection")) - } + suspend fun getStudentsHybrid(email: String, password: String, endpoint: String, symbol: String): List { + return remote.getStudentsHybrid(email, password, endpoint, symbol) } - fun getSavedStudents(decryptPass: Boolean = true): Single> { - return local.getStudents(decryptPass).toSingle(emptyList()) + suspend fun getSavedStudents(decryptPass: Boolean = true): List { + return local.getStudents(decryptPass) } - fun getStudentById(id: Int): Single { - return local.getStudentById(id) - .switchIfEmpty(Maybe.error(NoCurrentStudentException())) - .toSingle() + suspend fun getStudentById(id: Int): Student { + return local.getStudentById(id) ?: throw NoCurrentStudentException() } - fun getCurrentStudent(decryptPass: Boolean = true): Single { - return local.getCurrentStudent(decryptPass) - .switchIfEmpty(Maybe.error(NoCurrentStudentException())) - .toSingle() + suspend fun getCurrentStudent(decryptPass: Boolean = true): Student { + return local.getCurrentStudent(decryptPass) ?: throw NoCurrentStudentException() } - fun saveStudents(students: List): Single> { + suspend fun saveStudents(students: List): List { return local.saveStudents(students) } - fun switchStudent(student: Student): Completable { + suspend fun switchStudent(student: Student) { return local.setCurrentStudent(student) } - fun logoutStudent(student: Student): Completable { + suspend fun logoutStudent(student: Student) { return local.logoutStudent(student) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt index f8c13e6c..1f9dfff3 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt @@ -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.entities.Semester import io.github.wulkanowy.data.db.entities.Subject -import io.reactivex.Maybe import javax.inject.Inject import javax.inject.Singleton @Singleton class SubjectLocal @Inject constructor(private val subjectDao: SubjectDao) { - fun getSubjects(semester: Semester): Maybe> { + suspend fun getSubjects(semester: Semester): List { return subjectDao.loadAll(semester.diaryId, semester.studentId) - .filter { it.isNotEmpty() } } - fun saveSubjects(subjects: List) { + suspend fun saveSubjects(subjects: List) { subjectDao.insertAll(subjects) } - fun deleteSubjects(subjects: List) { + suspend fun deleteSubjects(subjects: List) { subjectDao.deleteAll(subjects) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRemote.kt index d30232f8..624a5a00 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRemote.kt @@ -5,25 +5,22 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class SubjectRemote @Inject constructor(private val sdk: Sdk) { - fun getSubjects(student: Student, semester: Semester): Single> { + suspend fun getSubjects(student: Student, semester: Semester): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getSubjects() - .map { subjects -> - subjects.map { - Subject( - studentId = semester.studentId, - diaryId = semester.diaryId, - name = it.name, - realId = it.id - ) - } + .map { + Subject( + studentId = semester.studentId, + diaryId = semester.diaryId, + name = it.name, + realId = it.id + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt index 649904da..646e3642 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt @@ -1,38 +1,27 @@ 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.Student import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class SubjectRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: SubjectLocal, private val remote: SubjectRemote ) { - fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false): Single> { - return local.getSubjects(semester).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveSubjects(new.uniqueSubtract(old)) - } - }.flatMap { - local.getSubjects(semester).toSingle(emptyList()) - }) + suspend fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false): List { + return local.getSubjects(semester).filter { !forceRefresh }.ifEmpty { + val new = remote.getSubjects(student, semester) + val old = local.getSubjects(semester) + + local.deleteSubjects(old.uniqueSubtract(new)) + local.saveSubjects(new.uniqueSubtract(old)) + + local.getSubjects(semester) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt index dd2be5e5..53680b7b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt @@ -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.entities.Semester import io.github.wulkanowy.data.db.entities.Teacher -import io.reactivex.Maybe import javax.inject.Inject class TeacherLocal @Inject constructor(private val teacherDb: TeacherDao) { - fun saveTeachers(teachers: List) { + suspend fun saveTeachers(teachers: List) { teacherDb.insertAll(teachers) } - fun deleteTeachers(teachers: List) { + suspend fun deleteTeachers(teachers: List) { teacherDb.deleteAll(teachers) } - fun getTeachers(semester: Semester): Maybe> { - return teacherDb.loadAll(semester.studentId, semester.classId).filter { it.isNotEmpty() } + suspend fun getTeachers(semester: Semester): List { + return teacherDb.loadAll(semester.studentId, semester.classId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRemote.kt index 01552f74..1d1caa68 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRemote.kt @@ -5,26 +5,23 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import javax.inject.Inject import javax.inject.Singleton @Singleton class TeacherRemote @Inject constructor(private val sdk: Sdk) { - fun getTeachers(student: Student, semester: Semester): Single> { + suspend fun getTeachers(student: Student, semester: Semester): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getTeachers(semester.semesterId) - .map { teachers -> - teachers.map { - Teacher( - studentId = semester.studentId, - name = it.name, - subject = it.subject, - shortName = it.short, - classId = semester.classId - ) - } + .map { + Teacher( + studentId = semester.studentId, + name = it.name, + subject = it.subject, + shortName = it.short, + classId = semester.classId + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt index 3c10be73..a540e78c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt @@ -1,35 +1,27 @@ 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.Student import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class TeacherRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: TeacherLocal, private val remote: TeacherRemote ) { - fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean = false): Single> { - return local.getTeachers(semester).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings) - .flatMap { - 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.saveTeachers(new.uniqueSubtract(old)) - } - }.flatMap { local.getTeachers(semester).toSingle(emptyList()) }) + suspend fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean = false): List { + return local.getTeachers(semester).filter { !forceRefresh }.ifEmpty { + val new = remote.getTeachers(student, semester) + val old = local.getTeachers(semester) + + local.deleteTeachers(old.uniqueSubtract(new)) + local.saveTeachers(new.uniqueSubtract(old)) + + local.getTeachers(semester) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt index e074ce2a..a90c664c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt @@ -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.entities.Semester import io.github.wulkanowy.data.db.entities.Timetable -import io.reactivex.Maybe import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -11,15 +10,15 @@ import javax.inject.Singleton @Singleton class TimetableLocal @Inject constructor(private val timetableDb: TimetableDao) { - fun saveTimetable(timetables: List) { + suspend fun saveTimetable(timetables: List) { timetableDb.insertAll(timetables) } - fun deleteTimetable(timetables: List) { + suspend fun deleteTimetable(timetables: List) { timetableDb.deleteAll(timetables) } - fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe> { - return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() } + suspend fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt index 22cb947d..71db8854 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import io.reactivex.Single import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -13,31 +12,29 @@ import javax.inject.Singleton @Singleton class TimetableRemote @Inject constructor(private val sdk: Sdk) { - fun getTimetable(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): Single> { + suspend fun getTimetable(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List { return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear) .getTimetable(startDate, endDate) - .map { lessons -> - lessons.map { - Timetable( - studentId = semester.studentId, - diaryId = semester.diaryId, - number = it.number, - start = it.start, - end = it.end, - date = it.date, - subject = it.subject, - subjectOld = it.subjectOld, - group = it.group, - room = it.room, - roomOld = it.roomOld, - teacher = it.teacher, - teacherOld = it.teacherOld, - info = it.info, - isStudentPlan = it.studentPlan, - changes = it.changes, - canceled = it.canceled - ) - } + .map { + Timetable( + studentId = semester.studentId, + diaryId = semester.diaryId, + number = it.number, + start = it.start, + end = it.end, + date = it.date, + subject = it.subject, + subjectOld = it.subjectOld, + group = it.group, + room = it.room, + roomOld = it.roomOld, + teacher = it.teacher, + teacherOld = it.teacherOld, + info = it.info, + isStudentPlan = it.studentPlan, + changes = it.changes, + canceled = it.canceled + ) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt index 4a7a0eb2..b3407552 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt @@ -1,53 +1,41 @@ 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.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract -import io.reactivex.Single import org.threeten.bp.LocalDate -import java.net.UnknownHostException import javax.inject.Inject import javax.inject.Singleton @Singleton class TimetableRepository @Inject constructor( - private val settings: InternetObservingSettings, private val local: TimetableLocal, private val remote: TimetableRemote, private val schedulerHelper: TimetableNotificationSchedulerHelper ) { - fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): Single> { - return Single.fromCallable { start.monday to end.sunday }.flatMap { (monday, sunday) -> - local.getTimetable(semester, monday, sunday).filter { !forceRefresh } - .switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings).flatMap { - 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.saveTimetable(new.uniqueSubtract(old).also { schedulerHelper.scheduleNotifications(it, student) }.map { item -> - item.also { new -> - old.singleOrNull { new.start == it.start }?.let { old -> - return@map new.copy( - room = if (new.room.isEmpty()) old.room else new.room, - teacher = if (new.teacher.isEmpty() && !new.changes && !old.changes) old.teacher else new.teacher - ) - } - } - }) - } - }.flatMap { - local.getTimetable(semester, monday, sunday).toSingle(emptyList()) - }).map { list -> list.filter { it.date in start..end }.also { schedulerHelper.scheduleNotifications(it, student) } } - } + suspend fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { + return local.getTimetable(semester, start.monday, start.sunday).filter { !forceRefresh }.ifEmpty { + val new = remote.getTimetable(student, semester, start.monday, start.sunday) + val old = local.getTimetable(semester, start.monday, start.sunday) + + local.deleteTimetable(old.uniqueSubtract(new).also { schedulerHelper.cancelScheduled(it) }) + local.saveTimetable(new.uniqueSubtract(old).also { schedulerHelper.scheduleNotifications(it, student) }.map { item -> + item.also { new -> + old.singleOrNull { new.start == it.start }?.let { old -> + return@map new.copy( + room = if (new.room.isEmpty()) old.room else new.room, + teacher = if (new.teacher.isEmpty() && !new.changes && !old.changes) old.teacher else new.teacher + ) + } + } + }) + + local.getTimetable(semester, start.monday, start.sunday) + }.filter { it.date in start..end }.also { schedulerHelper.scheduleNotifications(it, student) } } } diff --git a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt index db5ff59b..29b66a4e 100644 --- a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt +++ b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt @@ -7,6 +7,7 @@ import dagger.Module import dagger.Provides import io.github.wulkanowy.WulkanowyApp import io.github.wulkanowy.utils.AppInfo +import io.github.wulkanowy.utils.DispatchersProvider import io.github.wulkanowy.utils.SchedulersProvider import javax.inject.Singleton @@ -21,6 +22,10 @@ internal class AppModule { @Provides fun provideSchedulersProvider() = SchedulersProvider() + @Singleton + @Provides + fun provideDispatchersProvider() = DispatchersProvider() + @Singleton @Provides fun provideAppWidgetManager(context: Context): AppWidgetManager = AppWidgetManager.getInstance(context) diff --git a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt index 0130f467..283f0a98 100644 --- a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt +++ b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt @@ -19,6 +19,7 @@ import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.toLocalDateTime +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -53,7 +54,7 @@ class TimetableNotificationReceiver : BroadcastReceiver() { Timber.d("Receiving intent... ${intent.toUri(0)}") AndroidInjection.inject(this, context) - studentRepository.getCurrentStudent(false) + rxSingle { studentRepository.getCurrentStudent(false) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt index 126d856a..fc02ca75 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt @@ -15,13 +15,15 @@ import io.github.wulkanowy.R import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository -import io.github.wulkanowy.sdk.exception.FeatureDisabledException import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException +import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException import io.github.wulkanowy.services.sync.channels.DebugChannel import io.github.wulkanowy.services.sync.works.Work import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable import io.reactivex.Single +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import kotlin.random.Random @@ -37,11 +39,11 @@ class SyncWorker @AssistedInject constructor( override fun createWork(): Single { Timber.i("SyncWorker is starting") - return studentRepository.isCurrentStudentSet() + return rxSingle { studentRepository.isCurrentStudentSet() } .filter { true } - .flatMap { studentRepository.getCurrentStudent().toMaybe() } + .flatMap { rxMaybe { studentRepository.getCurrentStudent() } } .flatMapCompletable { student -> - semesterRepository.getCurrentSemester(student, true) + rxSingle { semesterRepository.getCurrentSemester(student, true) } .flatMapCompletable { semester -> Completable.mergeDelayError(works.map { work -> work.create(student, semester) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt index 5f7d7efa..f4333f33 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.attendancesummary.AttendanceSummaryRepository import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import javax.inject.Inject class AttendanceSummaryWork @Inject constructor( @@ -11,7 +12,7 @@ class AttendanceSummaryWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true).ignoreElement() + return rxCompletable { attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt index 063b7482..069b6c8f 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt @@ -6,13 +6,13 @@ import io.github.wulkanowy.data.repositories.attendance.AttendanceRepository import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now import javax.inject.Inject class AttendanceWork @Inject constructor(private val attendanceRepository: AttendanceRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true) - .ignoreElement() + return rxCompletable { attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt index 26f79a0d..8914fd36 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.repositories.completedlessons.CompletedLessonsRe import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now import javax.inject.Inject @@ -14,8 +15,7 @@ class CompletedLessonWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true) - .ignoreElement() + return rxCompletable { completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt index 1ef97f59..0a451295 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt @@ -6,12 +6,13 @@ import io.github.wulkanowy.data.repositories.exam.ExamRepository import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now import javax.inject.Inject class ExamWork @Inject constructor(private val examRepository: ExamRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return examRepository.getExams(student, semester, now().monday, now().sunday, true).ignoreElement() + return rxCompletable { examRepository.getExams(student, semester, now().monday, now().sunday, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt index c4681fb8..0a1a9eee 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt @@ -4,13 +4,13 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.gradestatistics.GradeStatisticsRepository import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import javax.inject.Inject class GradeStatisticsWork @Inject constructor(private val gradeStatisticsRepository: GradeStatisticsRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return gradeStatisticsRepository.getGradesStatistics(student, semester, "Wszystkie", false, forceRefresh = true) - .ignoreElement() + return rxCompletable { gradeStatisticsRepository.getGradesStatistics(student, semester, "Wszystkie", false, forceRefresh = true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt index fcdaad6e..252966d6 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt @@ -19,6 +19,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -30,17 +32,16 @@ class GradeWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable) - .ignoreElement() - .concatWith(Completable.concatArray(gradeRepository.getNotNotifiedGrades(semester).flatMapCompletable { + return rxCompletable { gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable) } + .concatWith(Completable.concatArray(rxSingle { gradeRepository.getNotNotifiedGrades(semester) }.flatMapCompletable { if (it.isNotEmpty()) notifyDetails(it) - gradeRepository.updateGrades(it.onEach { grade -> grade.isNotified = true }) - }, gradeRepository.getNotNotifiedPredictedGrades(semester).flatMapCompletable { + rxCompletable { gradeRepository.updateGrades(it.onEach { grade -> grade.isNotified = true }) } + }, rxSingle { gradeRepository.getNotNotifiedPredictedGrades(semester) }.flatMapCompletable { if (it.isNotEmpty()) notifyPredicted(it) - gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isPredictedGradeNotified = true }) - }, gradeRepository.getNotNotifiedFinalGrades(semester).flatMapCompletable { + rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isPredictedGradeNotified = true }) } + }, rxSingle { gradeRepository.getNotNotifiedFinalGrades(semester) }.flatMapCompletable { if (it.isNotEmpty()) notifyFinal(it) - gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isFinalGradeNotified = true }) + rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isFinalGradeNotified = true }) } })) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt index 186d57b0..2bf5315a 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt @@ -6,12 +6,13 @@ import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now import javax.inject.Inject class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true).ignoreElement() + return rxCompletable { homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index f19af4df..1389566b 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -18,6 +18,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxMaybe import javax.inject.Inject import kotlin.random.Random @@ -29,11 +31,11 @@ class LuckyNumberWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable) - .flatMap { luckyNumberRepository.getNotNotifiedLuckyNumber(student) } + return rxMaybe { luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable) } + .flatMap { rxMaybe { luckyNumberRepository.getNotNotifiedLuckyNumber(student) } } .flatMapCompletable { notify(it) - luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) + rxCompletable { luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt index b8773dbc..a805fe6b 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt @@ -19,6 +19,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -30,11 +32,11 @@ class MessageWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable) - .flatMap { messageRepository.getNotNotifiedMessages(student) } + return rxSingle { messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable) } + .flatMap { rxSingle { messageRepository.getNotNotifiedMessages(student) } } .flatMapCompletable { if (it.isNotEmpty()) notify(it) - messageRepository.updateMessages(it.onEach { message -> message.isNotified = true }) + rxCompletable { messageRepository.updateMessages(it.onEach { message -> message.isNotified = true }) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt index 4fc92ffc..f33c6402 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt @@ -18,6 +18,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -29,11 +31,11 @@ class NoteWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable) - .flatMap { noteRepository.getNotNotifiedNotes(student) } + return rxSingle { noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable) } + .flatMap { rxSingle { noteRepository.getNotNotifiedNotes(student) } } .flatMapCompletable { if (it.isNotEmpty()) notify(it) - noteRepository.updateNotes(it.onEach { note -> note.isNotified = true }) + rxCompletable { noteRepository.updateNotes(it.onEach { note -> note.isNotified = true }) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt index fa610dee..70415098 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt @@ -5,6 +5,8 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.recipient.RecipientRepository import io.github.wulkanowy.data.repositories.reportingunit.ReportingUnitRepository import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class RecipientWork @Inject constructor( @@ -13,10 +15,10 @@ class RecipientWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return reportingUnitRepository.getReportingUnits(student, true) + return rxSingle { reportingUnitRepository.getReportingUnits(student, true) } .flatMapCompletable { units -> Completable.mergeDelayError(units.map { - recipientRepository.getRecipients(student, 2, it, true).ignoreElement() + rxCompletable { recipientRepository.getRecipients(student, 2, it, true) } }) } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt index 5a7a41d8..f3ebf9ee 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt @@ -4,11 +4,12 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.teacher.TeacherRepository import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import javax.inject.Inject class TeacherWork @Inject constructor(private val teacherRepository: TeacherRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return teacherRepository.getTeachers(student, semester, true).ignoreElement() + return rxCompletable { teacherRepository.getTeachers(student, semester, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt index d79d2403..7d6438d7 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt @@ -6,13 +6,13 @@ import io.github.wulkanowy.data.repositories.timetable.TimetableRepository import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now import javax.inject.Inject class TimetableWork @Inject constructor(private val timetableRepository: TimetableRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true) - .ignoreElement() + return rxCompletable { timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true) } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt index 9f0b4047..d9dbc362 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt @@ -4,6 +4,8 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.utils.SchedulersProvider import io.reactivex.Completable import io.reactivex.disposables.CompositeDisposable +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber open class BasePresenter( @@ -27,13 +29,13 @@ open class BasePresenter( fun onExpiredLoginSelected() { Timber.i("Attempt to switch the student after the session expires") - disposable.add(studentRepository.getCurrentStudent(false) - .flatMapCompletable { studentRepository.logoutStudent(it) } - .andThen(studentRepository.getSavedStudents(false)) + disposable.add(rxSingle { studentRepository.getCurrentStudent(false) } + .flatMapCompletable { rxCompletable { studentRepository.logoutStudent(it) } } + .andThen(rxSingle { studentRepository.getSavedStudents(false) }) .flatMapCompletable { if (it.isNotEmpty()) { Timber.i("Switching current student") - studentRepository.switchStudent(it[0]) + rxCompletable { studentRepository.switchStudent(it[0]) } } else Completable.complete() } .subscribeOn(schedulers.backgroundThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt index 896e4ff1..627e0a5f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt @@ -13,9 +13,9 @@ import androidx.appcompat.app.AlertDialog import androidx.core.content.getSystemService import io.github.wulkanowy.R import io.github.wulkanowy.databinding.DialogErrorBinding -import io.github.wulkanowy.sdk.exception.FeatureDisabledException import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException -import io.github.wulkanowy.sdk.exception.ServiceUnavailableException +import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException +import io.github.wulkanowy.sdk.scrapper.exception.ServiceUnavailableException import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.getString import io.github.wulkanowy.utils.openAppInMarket diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt index f746b36f..c88e4d87 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt @@ -3,7 +3,7 @@ package io.github.wulkanowy.ui.base import android.content.res.Resources import com.chuckerteam.chucker.api.ChuckerCollector import io.github.wulkanowy.data.exceptions.NoCurrentStudentException -import io.github.wulkanowy.sdk.exception.BadCredentialsException +import io.github.wulkanowy.sdk.scrapper.login.BadCredentialsException import io.github.wulkanowy.utils.getString import io.github.wulkanowy.utils.security.ScramblerException import timber.log.Timber diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt index 416a59ce..ae149fa1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class ContributorPresenter @Inject constructor( @@ -30,7 +31,7 @@ class ContributorPresenter @Inject constructor( } private fun loadData() { - disposable.add(appCreatorRepository.getAppCreators() + disposable.add(rxSingle { appCreatorRepository.getAppCreators() } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { view?.showProgress(false) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt index 33eb1122..e1ec23a1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -22,7 +23,7 @@ class LogViewerPresenter @Inject constructor( } fun onShareLogsSelected(): Boolean { - disposable.add(loggerRepository.getLogFiles() + disposable.add(rxSingle { loggerRepository.getLogFiles() } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ files -> @@ -40,7 +41,7 @@ class LogViewerPresenter @Inject constructor( } private fun loadLogFile() { - disposable.add(loggerRepository.getLastLogLines() + disposable.add(rxSingle { loggerRepository.getLastLogLines() } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt index b5fbcdb6..1dd32cf9 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt @@ -7,6 +7,8 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider import io.reactivex.Single +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -36,11 +38,11 @@ class AccountPresenter @Inject constructor( fun onLogoutConfirm() { Timber.i("Attempt to logout current user ") - disposable.add(studentRepository.getCurrentStudent() - .flatMapCompletable { studentRepository.logoutStudent(it) } - .andThen(studentRepository.getSavedStudents(false)) + disposable.add(rxSingle { studentRepository.getCurrentStudent(false) } + .flatMapCompletable { rxCompletable { studentRepository.logoutStudent(it) } } + .andThen(rxSingle { studentRepository.getSavedStudents(false) }) .flatMap { - if (it.isNotEmpty()) studentRepository.switchStudent(it[0]).toSingle { it } + if (it.isNotEmpty()) rxCompletable { studentRepository.switchStudent(it[0]) }.toSingle { it } else Single.just(it) } .subscribeOn(schedulers.backgroundThread) @@ -69,7 +71,7 @@ class AccountPresenter @Inject constructor( view?.dismissView() } else { Timber.i("Attempt to change a student") - disposable.add(studentRepository.switchStudent(student) + disposable.add(rxSingle { studentRepository.switchStudent(student) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { view?.dismissView() } @@ -93,7 +95,7 @@ class AccountPresenter @Inject constructor( private fun loadData() { Timber.i("Loading account data started") - disposable.add(studentRepository.getSavedStudents(false) + disposable.add(rxSingle { studentRepository.getSavedStudents(false) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .map { createAccountItems(it) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index f58d0617..f177d019 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -16,6 +16,7 @@ import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousOrSameSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -167,8 +168,8 @@ class AttendancePresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ @@ -185,10 +186,10 @@ class AttendancePresenter @Inject constructor( currentDate = date disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - attendanceRepository.getAttendance(student, semester, date, date, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { attendanceRepository.getAttendance(student, semester, date, date, forceRefresh) } } } .map { list -> @@ -231,10 +232,10 @@ class AttendancePresenter @Inject constructor( private fun excuseAbsence(reason: String?, toExcuseList: List) { Timber.i("Excusing absence started") disposable.apply { - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - attendanceRepository.excuseForAbsence(student, semester, toExcuseList, reason) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { attendanceRepository.excuseForAbsence(student, semester, toExcuseList, reason) } } } .subscribeOn(schedulers.backgroundThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt index 33e18c2e..f694a8d0 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt @@ -9,6 +9,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.Month import timber.log.Timber import javax.inject.Inject @@ -76,10 +77,10 @@ class AttendanceSummaryPresenter @Inject constructor( currentSubjectId = subjectId disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { - attendanceSummaryRepository.getAttendanceSummary(student, it, subjectId, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { + rxSingle { attendanceSummaryRepository.getAttendanceSummary(student, it, subjectId, forceRefresh) } } } .map { items -> items.sortedByDescending { if (it.month.value <= Month.JUNE.value) it.month.value + 12 else it.month.value } } @@ -127,10 +128,10 @@ class AttendanceSummaryPresenter @Inject constructor( private fun loadSubjects() { Timber.i("Loading attendance summary subjects started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - subjectRepository.getSubjects(student, semester) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { subjectRepository.getSubjects(student, semester) } } } .doOnSuccess { subjects = it } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt index 1140cb02..844fb263 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt @@ -14,6 +14,7 @@ import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -89,8 +90,8 @@ class ExamPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ @@ -107,10 +108,10 @@ class ExamPresenter @Inject constructor( currentDate = date disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - examRepository.getExams(student, semester, currentDate.monday, currentDate.sunday, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { examRepository.getExams(student, semester, currentDate.monday, currentDate.sunday, forceRefresh) } } } .map { createExamItems(it) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt index af169932..ab6c507b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt @@ -13,7 +13,6 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.BOTH_SEMESTERS import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.ONE_SEMESTER import io.github.wulkanowy.utils.calcAverage import io.github.wulkanowy.utils.changeModifier -import io.reactivex.Single import javax.inject.Inject class GradeAverageProvider @Inject constructor( @@ -26,8 +25,8 @@ class GradeAverageProvider @Inject constructor( private val minusModifier get() = preferencesRepository.gradeMinusModifier - fun getGradesDetailsWithAverage(student: Student, semesterId: Int, forceRefresh: Boolean = false): Single> { - return semesterRepository.getSemesters(student).flatMap { semesters -> + suspend fun getGradesDetailsWithAverage(student: Student, semesterId: Int, forceRefresh: Boolean = false): List { + return semesterRepository.getSemesters(student).let { semesters -> when (preferencesRepository.gradeAverageMode) { ONE_SEMESTER -> getSemesterDetailsWithAverage(student, semesters.single { it.semesterId == semesterId }, forceRefresh) BOTH_SEMESTERS -> calculateBothSemestersAverage(student, semesters, semesterId, forceRefresh) @@ -36,15 +35,15 @@ class GradeAverageProvider @Inject constructor( } } - private fun calculateBothSemestersAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): Single> { + private suspend fun calculateBothSemestersAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): List { val selectedSemester = semesters.single { it.semesterId == semesterId } val firstSemester = semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 } - return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).flatMap { selectedDetails -> + return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).let { selectedDetails -> val isAnyAverage = selectedDetails.any { it.average != .0 } if (selectedSemester != firstSemester) { - getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).map { secondDetails -> + getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).let { secondDetails -> selectedDetails.map { selected -> val second = secondDetails.singleOrNull { it.subject == selected.subject } selected.copy(average = if (!isAnyAverage || preferencesRepository.gradeAverageForceCalc) { @@ -53,19 +52,19 @@ class GradeAverageProvider @Inject constructor( } else (selected.average + (second?.average ?: selected.average)) / 2) } } - } else Single.just(selectedDetails) + } else selectedDetails } } - private fun calculateAllYearAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): Single> { + private suspend fun calculateAllYearAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): List { val selectedSemester = semesters.single { it.semesterId == semesterId } val firstSemester = semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 } - return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).flatMap { selectedDetails -> + return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).let { selectedDetails -> val isAnyAverage = selectedDetails.any { it.average != .0 } if (selectedSemester != firstSemester) { - getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).map { secondDetails -> + getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).let { secondDetails -> selectedDetails.map { selected -> val second = secondDetails.singleOrNull { it.subject == selected.subject } selected.copy(average = if (!isAnyAverage || preferencesRepository.gradeAverageForceCalc) { @@ -73,12 +72,12 @@ class GradeAverageProvider @Inject constructor( } else selected.average) } } - } else Single.just(selectedDetails) + } else selectedDetails } } - private fun getSemesterDetailsWithAverage(student: Student, semester: Semester, forceRefresh: Boolean): Single> { - return gradeRepository.getGrades(student, semester, forceRefresh).map { (details, summaries) -> + private suspend fun getSemesterDetailsWithAverage(student: Student, semester: Semester, forceRefresh: Boolean): List { + return gradeRepository.getGrades(student, semester, forceRefresh).let { (details, summaries) -> val isAnyAverage = summaries.any { it.average != .0 } val allGrades = details.groupBy { it.subject } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt index ec66e2bd..65f6598d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCurrentOrLast +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import java.util.concurrent.TimeUnit.MILLISECONDS import javax.inject.Inject @@ -99,8 +100,8 @@ class GradePresenter @Inject constructor( private fun loadData() { Timber.i("Loading grade data started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getSemesters(it, refreshOnNoCurrent = true) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getSemesters(it, refreshOnNoCurrent = true) } } .delay(200, MILLISECONDS) .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index 37f2c935..a99e3a54 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -11,6 +11,8 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -62,13 +64,13 @@ class GradeDetailsPresenter @Inject constructor( fun onMarkAsReadSelected(): Boolean { Timber.i("Select mark grades as read") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getSemesters(it) } - .flatMap { gradeRepository.getUnreadGrades(it.first { item -> item.semesterId == currentSemesterId }) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getSemesters(it) } } + .flatMap { rxSingle { gradeRepository.getUnreadGrades(it.first { item -> item.semesterId == currentSemesterId }) } } .map { it.map { grade -> grade.apply { isRead = true } } } .flatMapCompletable { Timber.i("Mark as read ${it.size} grades") - gradeRepository.updateGrades(it) + rxCompletable { gradeRepository.updateGrades(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) @@ -126,8 +128,8 @@ class GradeDetailsPresenter @Inject constructor( private fun loadData(semesterId: Int, forceRefresh: Boolean) { Timber.i("Loading grade details data started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { @@ -196,7 +198,7 @@ class GradeDetailsPresenter @Inject constructor( private fun updateGrade(grade: Grade) { Timber.i("Attempt to update grade ${grade.id}") - disposable.add(gradeRepository.updateGrade(grade) + disposable.add(rxCompletable { gradeRepository.updateGrade(grade) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt index 590e9ce1..b2c56ed2 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt @@ -10,6 +10,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -116,10 +117,10 @@ class GradeStatisticsPresenter @Inject constructor( private fun loadSubjects() { Timber.i("Loading grade stats subjects started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - subjectRepository.getSubjects(student, semester) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { subjectRepository.getSubjects(student, semester) } } } .doOnSuccess { subjects = it } @@ -141,16 +142,18 @@ class GradeStatisticsPresenter @Inject constructor( currentType = type Timber.i("Loading grade stats data started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getSemesters(student).flatMap { semesters -> + rxSingle { semesterRepository.getSemesters(student) }.flatMap { semesters -> val semester = semesters.first { item -> item.semesterId == semesterId } - with(gradeStatisticsRepository) { - when (type) { - ViewType.SEMESTER -> getGradesStatistics(student, semester, currentSubjectName, true, forceRefresh) - ViewType.PARTIAL -> getGradesStatistics(student, semester, currentSubjectName, false, forceRefresh) - ViewType.POINTS -> getGradesPointsStatistics(student, semester, currentSubjectName, forceRefresh) + rxSingle { + with(gradeStatisticsRepository) { + when (type) { + ViewType.SEMESTER -> getGradesStatistics(student, semester, currentSubjectName, true, forceRefresh) + ViewType.PARTIAL -> getGradesStatistics(student, semester, currentSubjectName, false, forceRefresh) + ViewType.POINTS -> getGradesPointsStatistics(student, semester, currentSubjectName, forceRefresh) + } } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt index 229a0107..62b95d2e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -29,8 +30,8 @@ class GradeSummaryPresenter @Inject constructor( fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) { Timber.i("Loading grade summary data started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } } .map { createGradeSummaryItems(it) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index 41735a23..fe31dfae 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -14,6 +14,7 @@ import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber @@ -78,8 +79,8 @@ class HomeworkPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ @@ -100,10 +101,10 @@ class HomeworkPresenter @Inject constructor( currentDate = date disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - homeworkRepository.getHomework(student, semester, currentDate, currentDate, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { homeworkRepository.getHomework(student, semester, currentDate, currentDate, forceRefresh) } } } .map { createHomeworkItem(it) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt index 04a97b8c..c0475b7c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -26,7 +27,7 @@ class HomeworkDetailsPresenter @Inject constructor( fun toggleDone(homework: Homework) { Timber.i("Homework details update start") - disposable.add(homeworkRepository.toggleDone(homework) + disposable.add(rxSingle { homeworkRepository.toggleDone(homework) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginErrorHandler.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginErrorHandler.kt index 5ed181bf..f30825cf 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginErrorHandler.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginErrorHandler.kt @@ -4,11 +4,11 @@ import android.content.res.Resources import android.database.sqlite.SQLiteConstraintException import com.chuckerteam.chucker.api.ChuckerCollector import io.github.wulkanowy.R -import io.github.wulkanowy.sdk.exception.BadCredentialsException import io.github.wulkanowy.sdk.mobile.exception.InvalidPinException import io.github.wulkanowy.sdk.mobile.exception.InvalidSymbolException import io.github.wulkanowy.sdk.mobile.exception.InvalidTokenException import io.github.wulkanowy.sdk.mobile.exception.TokenDeadException +import io.github.wulkanowy.sdk.scrapper.login.BadCredentialsException import io.github.wulkanowy.ui.base.ErrorHandler import javax.inject.Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt index 3a0d4a0d..27205a2a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt @@ -9,6 +9,7 @@ import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.ifNullOrBlank import io.reactivex.Single +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -162,10 +163,12 @@ class LoginAdvancedPresenter @Inject constructor( val symbol = view?.formSymbolValue.orEmpty() val token = view?.formTokenValue.orEmpty() - return when (Sdk.Mode.valueOf(view?.formLoginType ?: "")) { - Sdk.Mode.API -> studentRepository.getStudentsApi(pin, symbol, token) - Sdk.Mode.SCRAPPER -> studentRepository.getStudentsScrapper(email, password, endpoint, symbol) - Sdk.Mode.HYBRID -> studentRepository.getStudentsHybrid(email, password, endpoint, symbol) + return rxSingle { + when (Sdk.Mode.valueOf(view?.formLoginType ?: "")) { + Sdk.Mode.API -> studentRepository.getStudentsApi(pin, symbol, token) + Sdk.Mode.SCRAPPER -> studentRepository.getStudentsScrapper(email, password, endpoint, symbol) + Sdk.Mode.HYBRID -> studentRepository.getStudentsHybrid(email, password, endpoint, symbol) + } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt index 997e1e85..ccbe4bbb 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.ifNullOrBlank +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -74,7 +75,7 @@ class LoginFormPresenter @Inject constructor( if (!validateCredentials(email, password, host)) return - disposable.add(studentRepository.getStudentsScrapper(email, password, host, symbol) + disposable.add(rxSingle { studentRepository.getStudentsScrapper(email, password, host, symbol) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt index 9a21fb91..84d5af06 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.ifNullOrBlank +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -55,7 +56,7 @@ class LoginRecoverPresenter @Inject constructor( if (!validateInput(username, host)) return - disposable.add(recoverRepository.getReCaptchaSiteKey(host, symbol.ifBlank { "Default" }) + disposable.add(rxSingle { recoverRepository.getReCaptchaSiteKey(host, symbol.ifBlank { "Default" }) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { @@ -98,7 +99,7 @@ class LoginRecoverPresenter @Inject constructor( with(disposable) { clear() - add(recoverRepository.sendRecoverRequest(host, symbol, username, reCaptchaResponse) + add(rxSingle { recoverRepository.sendRecoverRequest(host, symbol, username, reCaptchaResponse) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt index db25c0da..91d3e66c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt @@ -7,6 +7,8 @@ import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.ifNullOrBlank +import kotlinx.coroutines.rx2.rxCompletable +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import java.io.Serializable import javax.inject.Inject @@ -71,7 +73,7 @@ class LoginStudentSelectPresenter @Inject constructor( private fun loadData(students: List) { resetSelectedState() this.students = students - disposable.add(studentRepository.getSavedStudents() + disposable.add(rxSingle { studentRepository.getSavedStudents(false) } .map { savedStudents -> students.map { student -> student to savedStudents.any { compareStudents(student, it) } @@ -95,9 +97,9 @@ class LoginStudentSelectPresenter @Inject constructor( } private fun registerStudents(students: List) { - disposable.add(studentRepository.saveStudents(students) + disposable.add(rxSingle { studentRepository.saveStudents(students) } .map { students.first().apply { id = it.first() } } - .flatMapCompletable { studentRepository.switchStudent(it) } + .flatMapCompletable { rxCompletable { studentRepository.switchStudent(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt index 1f9e66b7..b7687ed3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.ifNullOrBlank import io.reactivex.Single +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import java.io.Serializable import javax.inject.Inject @@ -48,7 +49,7 @@ class LoginSymbolPresenter @Inject constructor( disposable.add( Single.fromCallable { loginData } - .flatMap { studentRepository.getStudentsScrapper(it.first, it.second, it.third, symbol) } + .flatMap { rxSingle { studentRepository.getStudentsScrapper(it.first, it.second, it.third, symbol) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt index e932fedc..1273a54c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt @@ -6,6 +6,8 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -35,8 +37,8 @@ class LuckyNumberPresenter @Inject constructor( Timber.i("Loading lucky number started") disposable.apply { clear() - add(studentRepository.getCurrentStudent() - .flatMapMaybe { luckyNumberRepository.getLuckyNumber(it, forceRefresh) } + add(rxSingle { studentRepository.getCurrentStudent() } + .flatMapMaybe { rxMaybe { luckyNumberRepository.getLuckyNumber(it, forceRefresh) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt index 468f9b57..bb7ea75b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getThemeWidgetKey import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class LuckyNumberWidgetConfigurePresenter @Inject constructor( @@ -45,7 +46,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor( } private fun loadData() { - disposable.add(studentRepository.getSavedStudents(false) + disposable.add(rxSingle { studentRepository.getSavedStudents(false) } .map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } } .map { (students, currentStudentId) -> students.map { student -> student to (student.id == currentStudentId) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index 55a048b3..204fc79a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -24,6 +24,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.SchedulersProvider import io.reactivex.Maybe +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -139,23 +141,23 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { private fun getLuckyNumber(studentId: Long, appWidgetId: Int): LuckyNumber? { return try { - studentRepository.isStudentSaved() + rxSingle { studentRepository.isStudentSaved() } .filter { true } - .flatMap { studentRepository.getSavedStudents().toMaybe() } + .flatMap { rxMaybe { studentRepository.getSavedStudents() } } .flatMap { students -> val student = students.singleOrNull { student -> student.id == studentId } when { student != null -> Maybe.just(student) studentId != 0L -> { - studentRepository.isCurrentStudentSet() + rxSingle { studentRepository.isCurrentStudentSet() } .filter { true } - .flatMap { studentRepository.getCurrentStudent(false).toMaybe() } + .flatMap { rxMaybe { studentRepository.getCurrentStudent(false) } } .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } } else -> Maybe.empty() } } - .flatMap { luckyNumberRepository.getLuckyNumber(it) } + .flatMap { rxMaybe { luckyNumberRepository.getLuckyNumber(it) } } .subscribeOn(schedulers.backgroundThread) .blockingGet() } catch (e: Exception) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index db7996bc..b94c4612 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -12,6 +12,7 @@ import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -56,8 +57,8 @@ class MessagePreviewPresenter @Inject constructor( Timber.i("Loading message ${message.messageId} preview started") disposable.apply { clear() - add(studentRepository.getStudentById(message.studentId) - .flatMap { messageRepository.getMessage(it, message, true) } + add(rxSingle { studentRepository.getStudentById(message.studentId) } + .flatMap { rxSingle { messageRepository.getMessage(it, message, true) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doFinally { view?.showProgress(false) } @@ -152,8 +153,8 @@ class MessagePreviewPresenter @Inject constructor( private fun deleteMessage() { message?.let { message -> - disposable.add(studentRepository.getCurrentStudent() - .flatMap { messageRepository.deleteMessage(it, message) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { messageRepository.deleteMessage(it, message) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt index aec79f9d..545409c6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt @@ -15,6 +15,8 @@ import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.toFormattedString import io.reactivex.Completable +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -98,19 +100,19 @@ class SendMessagePresenter @Inject constructor( var selectedRecipientChips: List = emptyList() Timber.i("Loading recipients started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it).map { semester -> it to semester } } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) }.map { semester -> it to semester } } .flatMapCompletable { (student, semester) -> - reportingUnitRepository.getReportingUnit(student, semester.unitId) + rxMaybe { reportingUnitRepository.getReportingUnit(student, semester.unitId) } .doOnSuccess { reportingUnit = it } - .flatMap { recipientRepository.getRecipients(student, 2, it).toMaybe() } + .flatMap { rxMaybe { recipientRepository.getRecipients(student, 2, it) } } .doOnSuccess { Timber.i("Loading recipients result: Success, fetched %d recipients", it.size) recipientChips = createChips(it) } .flatMapCompletable { if (message == null || reply != true) Completable.complete() - else recipientRepository.getMessageRecipients(student, message) + else rxSingle { recipientRepository.getMessageRecipients(student, message) } .doOnSuccess { Timber.i("Loaded message recipients to reply result: Success, fetched %d recipients", it.size) selectedRecipientChips = createChips(it) @@ -148,8 +150,8 @@ class SendMessagePresenter @Inject constructor( private fun sendMessage(subject: String, content: String, recipients: List) { Timber.i("Sending message started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { messageRepository.sendMessage(it, subject, content, recipients) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { messageRepository.sendMessage(it, subject, content, recipients) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .doOnSubscribe { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index 533f5ac8..0e96836b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -11,6 +11,7 @@ import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.toFormattedString import io.reactivex.subjects.PublishSubject +import kotlinx.coroutines.rx2.rxSingle import me.xdrop.fuzzywuzzy.FuzzySearch import timber.log.Timber import java.util.Locale @@ -83,10 +84,10 @@ class MessageTabPresenter @Inject constructor( private fun loadData(forceRefresh: Boolean) { Timber.i("Loading $folder message data started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student) - .flatMap { messageRepository.getMessages(student, it, folder, forceRefresh) } + rxSingle { semesterRepository.getCurrentSemester(student) } + .flatMap { rxSingle { messageRepository.getMessages(student, it, folder, forceRefresh) } } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt index 459ca17e..b1dea5df 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -48,10 +49,10 @@ class MobileDevicePresenter @Inject constructor( private fun loadData(forceRefresh: Boolean = false) { Timber.i("Loading mobile devices data started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - mobileDeviceRepository.getDevices(student, semester, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { mobileDeviceRepository.getDevices(student, semester, forceRefresh) } } } .subscribeOn(schedulers.backgroundThread) @@ -114,11 +115,11 @@ class MobileDevicePresenter @Inject constructor( fun onUnregisterConfirmed(device: MobileDevice) { Timber.i("Unregister device started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - mobileDeviceRepository.unregisterDevice(student, semester, device) - .flatMap { mobileDeviceRepository.getDevices(student, semester, it) } + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { mobileDeviceRepository.unregisterDevice(student, semester, device) } + .flatMap { rxSingle { mobileDeviceRepository.getDevices(student, semester, it) } } } } .subscribeOn(schedulers.backgroundThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt index 6a2a6b98..1c0506f0 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -28,10 +29,10 @@ class MobileDeviceTokenPresenter @Inject constructor( private fun loadData() { Timber.i("Mobile device registration data started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - mobileDeviceRepository.getToken(student, semester) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { mobileDeviceRepository.getToken(student, semester) } } } .subscribeOn(schedulers.backgroundThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt index 7d301c66..4009b4f6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -49,9 +50,9 @@ class NotePresenter @Inject constructor( private fun loadData(forceRefresh: Boolean = false) { Timber.i("Loading note data started") - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it).map { semester -> semester to it } } - .flatMap { noteRepository.getNotes(it.second, it.first, forceRefresh) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) }.map { semester -> semester to it } } + .flatMap { rxSingle { noteRepository.getNotes(it.second, it.first, forceRefresh) } } .map { items -> items.sortedByDescending { it.date } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) @@ -107,7 +108,7 @@ class NotePresenter @Inject constructor( private fun updateNote(note: Note) { Timber.i("Attempt to update note ${note.id}") - disposable.add(noteRepository.updateNote(note) + disposable.add(rxSingle { noteRepository.updateNote(note) } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ Timber.i("Update note result: Success") }) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt index 7beff922..334c60a3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt @@ -7,6 +7,8 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -63,10 +65,10 @@ class SchoolPresenter @Inject constructor( private fun loadData(forceRefresh: Boolean = false) { Timber.i("Loading school info started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMapMaybe { student -> - semesterRepository.getCurrentSemester(student).flatMapMaybe { - schoolRepository.getSchoolInfo(student, it, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMapMaybe { + rxMaybe { schoolRepository.getSchoolInfo(student, it, forceRefresh) } } } .subscribeOn(schedulers.backgroundThread) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt index 0d8eec6d..2ccba71f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber import javax.inject.Inject @@ -51,10 +52,10 @@ class TeacherPresenter @Inject constructor( private fun loadData(forceRefresh: Boolean = false) { Timber.i("Loading teachers data started") - disposable.add(studentRepository.getCurrentStudent() + disposable.add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - teacherRepository.getTeachers(student, semester, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { teacherRepository.getTeachers(student, semester, forceRefresh) } } } .map { it.filter { teacher -> teacher.name.isNotBlank() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt index 44b53c5f..bfdd1766 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class SplashPresenter @Inject constructor( @@ -14,7 +15,7 @@ class SplashPresenter @Inject constructor( override fun onAttachView(view: SplashView) { super.onAttachView(view) - disposable.add(studentRepository.isCurrentStudentSet() + disposable.add(rxSingle { studentRepository.isCurrentStudentSet() } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt index e1ce005e..da76854a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt @@ -16,6 +16,7 @@ import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.of @@ -111,8 +112,8 @@ class TimetablePresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ @@ -129,10 +130,10 @@ class TimetablePresenter @Inject constructor( currentDate = date disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - timetableRepository.getTimetable(student, semester, currentDate, currentDate, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { timetableRepository.getTimetable(student, semester, currentDate, currentDate, forceRefresh) } } } .map { items -> items.filter { if (prefRepository.showWholeClassPlan == "no") it.isStudentPlan else true } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsErrorHandler.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsErrorHandler.kt index c1389ced..767c2f31 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsErrorHandler.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsErrorHandler.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.ui.modules.timetable.completed import android.content.res.Resources import com.chuckerteam.chucker.api.ChuckerCollector -import io.github.wulkanowy.sdk.exception.FeatureDisabledException +import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException import io.github.wulkanowy.ui.base.ErrorHandler import javax.inject.Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt index f72d753a..c6a2cf84 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt @@ -14,6 +14,7 @@ import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -93,8 +94,8 @@ class CompletedLessonsPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(studentRepository.getCurrentStudent() - .flatMap { semesterRepository.getCurrentSemester(it) } + disposable.add(rxSingle { studentRepository.getCurrentStudent() } + .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } .subscribeOn(schedulers.backgroundThread) .observeOn(schedulers.mainThread) .subscribe({ @@ -111,10 +112,10 @@ class CompletedLessonsPresenter @Inject constructor( currentDate = date disposable.apply { clear() - add(studentRepository.getCurrentStudent() + add(rxSingle { studentRepository.getCurrentStudent() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).flatMap { semester -> - completedLessonsRepository.getCompletedLessons(student, semester, currentDate, currentDate, forceRefresh) + rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxSingle { completedLessonsRepository.getCompletedLessons(student, semester, currentDate, currentDate, forceRefresh) } } } .map { items -> items.sortedBy { it.number } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt index 57dde824..cc2ac4bb 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt @@ -8,6 +8,7 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getThemeWidgetKey import io.github.wulkanowy.utils.SchedulersProvider +import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class TimetableWidgetConfigurePresenter @Inject constructor( @@ -50,7 +51,7 @@ class TimetableWidgetConfigurePresenter @Inject constructor( } private fun loadData() { - disposable.add(studentRepository.getSavedStudents(false) + disposable.add(rxSingle { studentRepository.getSavedStudents(false) } .map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } } .map { (students, currentStudentId) -> students.map { student -> student to (student.id == currentStudentId) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt index dd223ad8..6c043e9e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt @@ -25,6 +25,8 @@ import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.toFormattedString import io.reactivex.Maybe +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import timber.log.Timber @@ -100,9 +102,9 @@ class TimetableWidgetFactory( private fun updateLessons(date: LocalDate, studentId: Long) { lessons = try { - studentRepository.isStudentSaved() + rxSingle { studentRepository.isStudentSaved() } .filter { true } - .flatMap { studentRepository.getSavedStudents().toMaybe() } + .flatMap { rxMaybe { studentRepository.getSavedStudents() } } .flatMap { val student = it.singleOrNull { student -> student.id == studentId } @@ -110,8 +112,8 @@ class TimetableWidgetFactory( else Maybe.empty() } .flatMap { student -> - semesterRepository.getCurrentSemester(student).toMaybe().flatMap { semester -> - timetableRepository.getTimetable(student, semester, date, date).toMaybe() + rxMaybe { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> + rxMaybe { timetableRepository.getTimetable(student, semester, date, date) } } } .map { items -> items.sortedWith(compareBy({ it.number }, { !it.isStudentPlan })) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 79888f83..8beef08a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -31,6 +31,8 @@ import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString import io.reactivex.Maybe +import kotlinx.coroutines.rx2.rxMaybe +import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import timber.log.Timber @@ -184,17 +186,17 @@ class TimetableWidgetProvider : BroadcastReceiver() { private fun getStudent(studentId: Long, appWidgetId: Int): Student? { return try { - studentRepository.isStudentSaved() + rxSingle { studentRepository.isStudentSaved() } .filter { true } - .flatMap { studentRepository.getSavedStudents(false).toMaybe() } + .flatMap { rxMaybe { studentRepository.getSavedStudents(false) } } .flatMap { students -> val student = students.singleOrNull { student -> student.id == studentId } when { student != null -> Maybe.just(student) studentId != 0L -> { - studentRepository.isCurrentStudentSet() + rxSingle { studentRepository.isCurrentStudentSet() } .filter { true } - .flatMap { studentRepository.getCurrentStudent(false).toMaybe() } + .flatMap { rxMaybe { studentRepository.getCurrentStudent(false) } } .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } } else -> Maybe.empty() diff --git a/app/src/main/java/io/github/wulkanowy/utils/DispatchersProvider.kt b/app/src/main/java/io/github/wulkanowy/utils/DispatchersProvider.kt new file mode 100644 index 00000000..ecc8e05e --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/utils/DispatchersProvider.kt @@ -0,0 +1,10 @@ +package io.github.wulkanowy.utils + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +open class DispatchersProvider { + + open val backgroundThread: CoroutineDispatcher + get() = Dispatchers.IO +} diff --git a/app/src/main/java/io/github/wulkanowy/utils/ResourcesExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/ResourcesExtension.kt index a2d0384e..de9f656a 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/ResourcesExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/ResourcesExtension.kt @@ -2,11 +2,11 @@ package io.github.wulkanowy.utils import android.content.res.Resources import io.github.wulkanowy.R -import io.github.wulkanowy.sdk.exception.FeatureDisabledException import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException -import io.github.wulkanowy.sdk.exception.NotLoggedInException -import io.github.wulkanowy.sdk.exception.PasswordChangeRequiredException -import io.github.wulkanowy.sdk.exception.ServiceUnavailableException +import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException +import io.github.wulkanowy.sdk.scrapper.exception.ServiceUnavailableException +import io.github.wulkanowy.sdk.scrapper.login.NotLoggedInException +import io.github.wulkanowy.sdk.scrapper.login.PasswordChangeRequiredException import java.io.InterruptedIOException import java.net.SocketTimeoutException import java.net.UnknownHostException diff --git a/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt b/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt index e87177c1..d7850b07 100644 --- a/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt +++ b/app/src/play/java/io/github/wulkanowy/utils/CrashlyticsUtils.kt @@ -4,8 +4,8 @@ import android.util.Log import com.google.firebase.crashlytics.FirebaseCrashlytics import fr.bipi.tressence.base.FormatterPriorityTree import fr.bipi.tressence.common.StackTraceRecorder -import io.github.wulkanowy.sdk.exception.FeatureDisabledException import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException +import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException import java.io.InterruptedIOException import java.net.SocketTimeoutException import java.net.UnknownHostException diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt deleted file mode 100644 index 954c191c..00000000 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/UnitTestInternetObservingStrategy.kt +++ /dev/null @@ -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 UnitTestInternetObservingStrategy(var isInternetConnection: Boolean = true) : InternetObservingStrategy { - - override fun checkInternetConnectivity(host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Single { - return Single.just(isInternetConnection) - } - - override fun observeInternetConnectivity(initialIntervalInMs: Int, intervalInMs: Int, host: String?, port: Int, timeoutInMs: Int, httpResponse: Int, errorHandler: ErrorHandler?): Observable { - return Observable.just(isInternetConnection) - } - - override fun getDefaultPingHost() = "localhost" -} diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt index a5b79cfd..a4ca16b6 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt @@ -6,10 +6,11 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Attendance import io.github.wulkanowy.utils.init import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.SpyK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -34,16 +35,16 @@ class AttendanceRemoteTest { @Test fun getAttendanceTest() { every { mockSdk.init(student) } returns mockSdk - every { + coEvery { mockSdk.getAttendance( of(2018, 9, 10), of(2018, 9, 15), 1 ) - } returns Single.just(listOf( + } returns listOf( getAttendance(of(2018, 9, 10)), getAttendance(of(2018, 9, 17)) - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 @@ -51,10 +52,12 @@ class AttendanceRemoteTest { every { semesterMock.semesterId } returns 1 every { mockSdk.switchDiary(any(), any()) } returns mockSdk - val attendance = AttendanceRemote(mockSdk).getAttendance(student, semesterMock, - of(2018, 9, 10), - of(2018, 9, 15) - ).blockingGet() + val attendance = runBlocking { + AttendanceRemote(mockSdk).getAttendance(student, semesterMock, + of(2018, 9, 10), + of(2018, 9, 15) + ) + } assertEquals(2, attendance.size) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt index 10307fee..31b2af5b 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt @@ -6,10 +6,11 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.CompletedLesson import io.github.wulkanowy.utils.init import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.SpyK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.Assert import org.junit.Before import org.junit.Test @@ -34,15 +35,15 @@ class CompletedLessonsRemoteTest { @Test fun getCompletedLessonsTest() { every { mockSdk.init(student) } returns mockSdk - every { + coEvery { mockSdk.getCompletedLessons( of(2018, 9, 10), of(2018, 9, 15) ) - } returns Single.just(listOf( + } returns listOf( getCompletedLesson(of(2018, 9, 10)), getCompletedLesson(of(2018, 9, 17)) - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 @@ -50,10 +51,12 @@ class CompletedLessonsRemoteTest { every { semesterMock.semesterId } returns 1 every { mockSdk.switchDiary(any(), any()) } returns mockSdk - val completed = CompletedLessonsRemote(mockSdk).getCompletedLessons(student, semesterMock, - of(2018, 9, 10), - of(2018, 9, 15) - ).blockingGet() + val completed = runBlocking { + CompletedLessonsRemote(mockSdk).getCompletedLessons(student, semesterMock, + of(2018, 9, 10), + of(2018, 9, 15) + ) + } Assert.assertEquals(2, completed.size) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt index aecef472..868f6025 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt @@ -6,10 +6,11 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Exam import io.github.wulkanowy.utils.init import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.SpyK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -36,26 +37,28 @@ class ExamRemoteTest { every { mockSdk.init(student) } returns mockSdk every { mockSdk.switchDiary(1, 2019) } returns mockSdk - every { + coEvery { mockSdk.getExams( of(2018, 9, 10), of(2018, 9, 15), 1 ) - } returns Single.just(listOf( + } returns listOf( getExam(of(2018, 9, 10)), getExam(of(2018, 9, 17)) - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 every { semesterMock.schoolYear } returns 2019 every { semesterMock.semesterId } returns 1 - val exams = ExamRemote(mockSdk).getExams(student, semesterMock, - of(2018, 9, 10), - of(2018, 9, 15) - ).blockingGet() + val exams = runBlocking { + ExamRemote(mockSdk).getExams(student, semesterMock, + of(2018, 9, 10), + of(2018, 9, 15) + ) + } assertEquals(2, exams.size) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt index 77c87025..cd2a3070 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRemoteTest.kt @@ -7,10 +7,11 @@ import io.github.wulkanowy.sdk.pojo.GradePointsStatistics import io.github.wulkanowy.sdk.pojo.GradeStatistics import io.github.wulkanowy.utils.init import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.SpyK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -33,10 +34,10 @@ class GradeStatisticsRemoteTest { @Test fun getGradeStatisticsTest() { - every { mockSdk.getGradesPartialStatistics(1) } returns Single.just(listOf( + coEvery { mockSdk.getGradesPartialStatistics(1) } returns listOf( getGradeStatistics("Fizyka"), getGradeStatistics("Matematyka") - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 @@ -44,16 +45,16 @@ class GradeStatisticsRemoteTest { every { semesterMock.semesterId } returns 1 every { mockSdk.switchDiary(any(), any()) } returns mockSdk - val stats = GradeStatisticsRemote(mockSdk).getGradeStatistics(student, semesterMock, false).blockingGet() + val stats = runBlocking { GradeStatisticsRemote(mockSdk).getGradeStatistics(student, semesterMock, false) } assertEquals(2, stats.size) } @Test fun getGradePointsStatisticsTest() { - every { mockSdk.getGradesPointsStatistics(1) } returns Single.just(listOf( + coEvery { mockSdk.getGradesPointsStatistics(1) } returns listOf( getGradePointsStatistics("Fizyka"), getGradePointsStatistics("Matematyka") - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 @@ -61,7 +62,7 @@ class GradeStatisticsRemoteTest { every { semesterMock.semesterId } returns 1 every { mockSdk.switchDiary(any(), any()) } returns mockSdk - val stats = GradeStatisticsRemote(mockSdk).getGradePointsStatistics(student, semesterMock).blockingGet() + val stats = runBlocking { GradeStatisticsRemote(mockSdk).getGradePointsStatistics(student, semesterMock) } assertEquals(2, stats.size) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt index cbdf34fa..84761ada 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt @@ -4,9 +4,10 @@ import io.github.wulkanowy.getStudentEntity import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.SpyK -import io.reactivex.Maybe +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -27,16 +28,17 @@ class LuckyNumberRemoteTest { @Test fun getLuckyNumberTest() { every { mockSdk.init(student) } returns mockSdk - every { mockSdk.getLuckyNumber("test") } returns Maybe.just(14) + coEvery { mockSdk.getLuckyNumber("test") } returns 14 every { mockSdk.diaryId } returns 1 - val luckyNumber = LuckyNumberRemote(mockSdk) - .getLuckyNumber(student) - .blockingGet() + val luckyNumber = runBlocking { + LuckyNumberRemote(mockSdk) + .getLuckyNumber(student) + } - assertEquals(14, luckyNumber.luckyNumber) - assertEquals(LocalDate.now(), luckyNumber.date) - assertEquals(student.studentId, luckyNumber.studentId) + assertEquals(14, luckyNumber?.luckyNumber) + assertEquals(LocalDate.now(), luckyNumber?.date) + assertEquals(student.studentId, luckyNumber?.studentId) } } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt index fcc4188a..977e8205 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt @@ -1,55 +1,48 @@ package io.github.wulkanowy.data.repositories.message import androidx.room.EmptyResultSetException -import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.Student -import io.github.wulkanowy.data.repositories.UnitTestInternetObservingStrategy import io.github.wulkanowy.getMessageEntity -import io.reactivex.Single -import io.reactivex.observers.TestObserver +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.impl.annotations.MockK +import io.mockk.just +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.`when` -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations import java.net.UnknownHostException class MessageRepositoryTest { - @Mock + @MockK lateinit var local: MessageLocal - @Mock + @MockK lateinit var remote: MessageRemote - @Mock + @MockK lateinit var student: Student - private val testObservingStrategy = UnitTestInternetObservingStrategy() - private lateinit var repo: MessageRepository @Before fun setUp() { - MockitoAnnotations.initMocks(this) + MockKAnnotations.init(this) - repo = MessageRepository(InternetObservingSettings.builder() - .strategy(testObservingStrategy) - .build(), local, remote) + repo = MessageRepository(local, remote) } @Test fun `throw error when message is not in the db`() { val testMessage = getMessageEntity(1, "", false) - `when`(local.getMessageWithAttachment(student, testMessage)).thenReturn(Single.error(EmptyResultSetException("No message in database"))) + coEvery { local.getMessageWithAttachment(student, testMessage) } throws EmptyResultSetException("No message in database") - val message = repo.getMessage(student, testMessage) - val messageObserver = TestObserver() - message.subscribe(messageObserver) - messageObserver.assertError(EmptyResultSetException::class.java) + val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } + assertEquals(EmptyResultSetException::class.java, message.exceptionOrNull()?.javaClass) } @Test @@ -57,9 +50,9 @@ class MessageRepositoryTest { val testMessage = getMessageEntity(123, "Test", false) val messageWithAttachment = MessageWithAttachment(testMessage, emptyList()) - `when`(local.getMessageWithAttachment(student, testMessage)).thenReturn(Single.just(messageWithAttachment)) + coEvery { local.getMessageWithAttachment(student, testMessage) } returns messageWithAttachment - val message = repo.getMessage(student, testMessage).blockingGet() + val message = runBlocking { repo.getMessage(student, testMessage) } assertEquals("Test", message.message.content) } @@ -72,15 +65,15 @@ class MessageRepositoryTest { val mWa = MessageWithAttachment(testMessage, emptyList()) val mWaWithContent = MessageWithAttachment(testMessageWithContent, emptyList()) - `when`(local.getMessageWithAttachment(student, testMessage)) - .thenReturn(Single.just(mWa)) - .thenReturn(Single.just(mWaWithContent)) - `when`(remote.getMessagesContentDetails(student, testMessageWithContent)).thenReturn(Single.just("Test" to emptyList())) + coEvery { local.getMessageWithAttachment(student, testMessage) } returnsMany listOf(mWa, mWaWithContent) + coEvery { remote.getMessagesContentDetails(student, testMessageWithContent) } returns ("Test" to emptyList()) + coEvery { local.updateMessages(any()) } just Runs + coEvery { local.saveMessageAttachments(any()) } just Runs - val message = repo.getMessage(student, testMessage).blockingGet() + val message = runBlocking { repo.getMessage(student, testMessage) } assertEquals("Test", message.message.content) - verify(local).updateMessages(listOf(testMessageWithContent)) + coVerify { local.updateMessages(listOf(testMessageWithContent)) } } @Test @@ -88,13 +81,10 @@ class MessageRepositoryTest { val testMessage = getMessageEntity(123, "", false) val messageWithAttachment = MessageWithAttachment(testMessage, emptyList()) - testObservingStrategy.isInternetConnection = false - `when`(local.getMessageWithAttachment(student, testMessage)).thenReturn(Single.just(messageWithAttachment)) + coEvery { local.getMessageWithAttachment(student, testMessage) } throws UnknownHostException() - val message = repo.getMessage(student, testMessage) - val messageObserver = TestObserver() - message.subscribe(messageObserver) - messageObserver.assertError(UnknownHostException::class.java) + val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } + assertEquals(UnknownHostException::class.java, message.exceptionOrNull()?.javaClass) } @Test @@ -102,12 +92,9 @@ class MessageRepositoryTest { val testMessage = getMessageEntity(123, "", true) val messageWithAttachment = MessageWithAttachment(testMessage, emptyList()) - testObservingStrategy.isInternetConnection = false - `when`(local.getMessageWithAttachment(student, testMessage)).thenReturn(Single.just(messageWithAttachment)) + coEvery { local.getMessageWithAttachment(student, testMessage) } throws UnknownHostException() - val message = repo.getMessage(student, testMessage) - val messageObserver = TestObserver() - message.subscribe(messageObserver) - messageObserver.assertError(UnknownHostException::class.java) + val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } + assertEquals(UnknownHostException::class.java, message.exceptionOrNull()?.javaClass) } } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt index c586572f..665185a6 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt @@ -1,43 +1,38 @@ package io.github.wulkanowy.data.repositories.mobiledevice -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.Semester -import io.github.wulkanowy.data.repositories.UnitTestInternetObservingStrategy import io.github.wulkanowy.getStudentEntity -import io.reactivex.Maybe -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.impl.annotations.MockK +import io.mockk.just +import kotlinx.coroutines.runBlocking import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDateTime.of class MobileDeviceRepositoryTest { - @Mock + @MockK private lateinit var semester: Semester - @Mock + @MockK private lateinit var mobileDeviceRemote: MobileDeviceRemote - @Mock + @MockK private lateinit var mobileDeviceLocal: MobileDeviceLocal private val student = getStudentEntity() private lateinit var mobileDeviceRepository: MobileDeviceRepository - private val settings = InternetObservingSettings.builder() - .strategy(UnitTestInternetObservingStrategy()) - .build() - @Before fun initTest() { - MockitoAnnotations.initMocks(this) - mobileDeviceRepository = MobileDeviceRepository(settings, mobileDeviceLocal, mobileDeviceRemote) + MockKAnnotations.init(this) + mobileDeviceRepository = MobileDeviceRepository(mobileDeviceLocal, mobileDeviceRemote) } @Test @@ -47,13 +42,15 @@ class MobileDeviceRepositoryTest { getDeviceEntity(2) ) - doReturn(Maybe.empty()).`when`(mobileDeviceLocal).getDevices(semester) - doReturn(Single.just(devices)).`when`(mobileDeviceRemote).getDevices(student, semester) + coEvery { mobileDeviceLocal.getDevices(semester) } returns emptyList() + coEvery { mobileDeviceLocal.deleteDevices(emptyList()) } just Runs + coEvery { mobileDeviceLocal.saveDevices(devices) } just Runs + coEvery { mobileDeviceRemote.getDevices(student, semester) } returns devices - mobileDeviceRepository.getDevices(student, semester).blockingGet() + runBlocking { mobileDeviceRepository.getDevices(student, semester) } - verify(mobileDeviceLocal).deleteDevices(emptyList()) - verify(mobileDeviceLocal).saveDevices(devices) + coVerify { mobileDeviceLocal.deleteDevices(emptyList()) } + coVerify { mobileDeviceLocal.saveDevices(devices) } } private fun getDeviceEntity(day: Int): MobileDevice { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt index c00a771c..161ce744 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt @@ -1,44 +1,39 @@ package io.github.wulkanowy.data.repositories.semester -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.Student -import io.github.wulkanowy.data.repositories.UnitTestInternetObservingStrategy import io.github.wulkanowy.createSemesterEntity -import io.reactivex.Maybe -import io.reactivex.Single +import io.github.wulkanowy.data.db.entities.Student +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.just +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertNotEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDate.now class SemesterRepositoryTest { - @Mock + @MockK private lateinit var semesterRemote: SemesterRemote - @Mock + @MockK private lateinit var semesterLocal: SemesterLocal - @Mock + @MockK private lateinit var student: Student private lateinit var semesterRepository: SemesterRepository - private val settings = InternetObservingSettings.builder() - .strategy(UnitTestInternetObservingStrategy()) - .build() - @Before fun initTest() { - MockitoAnnotations.initMocks(this) - semesterRepository = SemesterRepository(semesterRemote, semesterLocal, settings) - doReturn("SCRAPPER").`when`(student).loginMode + MockKAnnotations.init(this) + semesterRepository = SemesterRepository(semesterRemote, semesterLocal) + every { student.loginMode } returns "SCRAPPER" } @Test @@ -48,33 +43,35 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(3), now()) ) - doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) - doReturn(Single.just(semesters)).`when`(semesterRemote).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns emptyList() + coEvery { semesterRemote.getSemesters(student) } returns semesters + coEvery { semesterLocal.deleteSemesters(any()) } just Runs + coEvery { semesterLocal.saveSemesters(any()) } just Runs - semesterRepository.getSemesters(student).blockingGet() + runBlocking { semesterRepository.getSemesters(student) } - verify(semesterLocal).deleteSemesters(emptyList()) - verify(semesterLocal).saveSemesters(semesters) + coVerify { semesterLocal.saveSemesters(semesters) } + coVerify { semesterLocal.deleteSemesters(emptyList()) } } @Test fun getSemesters_invalidDiary_api() { - doReturn("API").`when`(student).loginMode + every { student.loginMode } returns "API" val badSemesters = listOf( createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)), createSemesterEntity(0, 2, now().minusMonths(3), now()) ) - doReturn(Maybe.just(badSemesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns badSemesters - val items = semesterRepository.getSemesters(student).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) assertEquals(0, items[0].diaryId) } @Test fun getSemesters_invalidDiary_scrapper() { - doReturn("SCRAPPER").`when`(student).loginMode + every { student.loginMode } returns "SCRAPPER" val badSemesters = listOf( createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)), createSemesterEntity(0, 2, now().minusMonths(3), now()) @@ -85,10 +82,12 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(3), now()) ) - doReturn(Maybe.just(badSemesters), Maybe.just(badSemesters), Maybe.just(goodSemesters)).`when`(semesterLocal).getSemesters(student) - doReturn(Single.just(goodSemesters)).`when`(semesterRemote).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returnsMany listOf(badSemesters, badSemesters, goodSemesters) + coEvery { semesterRemote.getSemesters(student) } returns goodSemesters + coEvery { semesterLocal.deleteSemesters(any()) } just Runs + coEvery { semesterLocal.saveSemesters(any()) } just Runs - val items = semesterRepository.getSemesters(student).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) assertNotEquals(0, items[0].diaryId) } @@ -100,9 +99,9 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1)) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - val items = semesterRepository.getSemesters(student).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) } @@ -113,9 +112,9 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(3), now()) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - val items = semesterRepository.getSemesters(student).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) } @@ -126,9 +125,9 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now(), now()) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - val items = semesterRepository.getSemesters(student).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) } @@ -139,13 +138,15 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(3), now()) ) - doReturn(Maybe.empty()).`when`(semesterLocal).getSemesters(student) - doReturn(Single.just(semesters)).`when`(semesterRemote).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns emptyList() + coEvery { semesterRemote.getSemesters(student) } returns semesters + coEvery { semesterLocal.deleteSemesters(any()) } just Runs + coEvery { semesterLocal.saveSemesters(any()) } just Runs - semesterRepository.getSemesters(student, refreshOnNoCurrent = true).blockingGet() + runBlocking { semesterRepository.getSemesters(student, refreshOnNoCurrent = true) } - verify(semesterLocal).deleteSemesters(emptyList()) - verify(semesterLocal).saveSemesters(semesters) + coVerify { semesterLocal.deleteSemesters(emptyList()) } + coVerify { semesterLocal.saveSemesters(semesters) } } @Test @@ -155,10 +156,9 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1)) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) - doReturn(Single.just(semesters)).`when`(semesterRemote).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - val items = semesterRepository.getSemesters(student, refreshOnNoCurrent = true).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student, refreshOnNoCurrent = true) } assertEquals(2, items.size) } @@ -169,9 +169,9 @@ class SemesterRepositoryTest { createSemesterEntity(1, 2, now(), now()) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - val items = semesterRepository.getSemesters(student, refreshOnNoCurrent = true).blockingGet() + val items = runBlocking { semesterRepository.getSemesters(student, refreshOnNoCurrent = true) } assertEquals(2, items.size) } @@ -182,15 +182,15 @@ class SemesterRepositoryTest { createSemesterEntity(1, 1, now(), now()) ) - doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns semesters - semesterRepository.getCurrentSemester(student).blockingGet() + runBlocking { semesterRepository.getCurrentSemester(student) } } @Test(expected = RuntimeException::class) fun getCurrentSemester_emptyList() { - doReturn(Maybe.just(emptyList())).`when`(semesterLocal).getSemesters(student) + coEvery { semesterLocal.getSemesters(student) } returns emptyList() - semesterRepository.getCurrentSemester(student).blockingGet() + runBlocking { semesterRepository.getCurrentSemester(student) } } } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt index b6b195c9..fe1d9717 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/student/StudentRemoteTest.kt @@ -2,30 +2,29 @@ package io.github.wulkanowy.data.repositories.student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Student -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.coEvery +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.anyString -import org.mockito.Mockito.doReturn -import org.mockito.MockitoAnnotations class StudentRemoteTest { - @Mock + @MockK private lateinit var mockSdk: Sdk @Before fun initApi() { - MockitoAnnotations.initMocks(this) + MockKAnnotations.init(this) } @Test fun testRemoteAll() { - doReturn(Single.just(listOf(getStudent("test")))).`when`(mockSdk).getStudentsFromScrapper(anyString(), anyString(), anyString(), anyString()) + coEvery { mockSdk.getStudentsFromScrapper(any(), any(), any(), any()) } returns listOf(getStudent("test")) - val students = StudentRemote(mockSdk).getStudentsScrapper("", "", "http://fakelog.cf", "").blockingGet() + val students = runBlocking { StudentRemote(mockSdk).getStudentsScrapper("", "", "http://fakelog.cf", "") } assertEquals(1, students.size) assertEquals("test", students.first().studentName) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt index d2b4a667..a88c87bd 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt @@ -5,10 +5,11 @@ import io.github.wulkanowy.getStudentEntity import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Timetable import io.mockk.MockKAnnotations +import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.impl.annotations.SpyK -import io.reactivex.Single +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test @@ -33,15 +34,15 @@ class TimetableRemoteTest { @Test fun getTimetableTest() { - every { + coEvery { mockSdk.getTimetable( of(2018, 9, 10), of(2018, 9, 15) ) - } returns Single.just(listOf( + } returns listOf( getTimetable(of(2018, 9, 10)), getTimetable(of(2018, 9, 17)) - )) + ) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 @@ -49,10 +50,12 @@ class TimetableRemoteTest { every { semesterMock.semesterId } returns 1 every { mockSdk.switchDiary(any(), any()) } returns mockSdk - val timetable = TimetableRemote(mockSdk).getTimetable(student, semesterMock, - of(2018, 9, 10), - of(2018, 9, 15) - ).blockingGet() + val timetable = runBlocking { + TimetableRemote(mockSdk).getTimetable(student, semesterMock, + of(2018, 9, 10), + of(2018, 9, 15) + ) + } assertEquals(2, timetable.size) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt index 984bbbcf..85f85a37 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt @@ -8,26 +8,27 @@ import io.github.wulkanowy.data.repositories.grade.GradeRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.sdk.Sdk -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.coEvery +import io.mockk.every +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.`when` -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.of import org.threeten.bp.LocalDateTime class GradeAverageProviderTest { - @Mock + @MockK lateinit var preferencesRepository: PreferencesRepository - @Mock + @MockK lateinit var semesterRepository: SemesterRepository - @Mock + @MockK lateinit var gradeRepository: GradeRepository private lateinit var gradeAverageProvider: GradeAverageProvider @@ -82,24 +83,24 @@ class GradeAverageProviderTest { @Before fun setUp() { - MockitoAnnotations.initMocks(this) + MockKAnnotations.init(this) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) - `when`(preferencesRepository.gradePlusModifier).thenReturn(.33) + every { preferencesRepository.gradeAverageForceCalc } returns false + coEvery { semesterRepository.getSemesters(student) } returns semesters + every { preferencesRepository.gradeMinusModifier } returns .33 + every { preferencesRepository.gradePlusModifier } returns .33 gradeAverageProvider = GradeAverageProvider(semesterRepository, gradeRepository, preferencesRepository) } @Test fun `force calc current semester average with default modifiers in scraper mode`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGradeWithModifier to secondSummariesWithModifier)) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { semesterRepository.getSemesters(student) } returns semesters + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) // from details and after set custom plus/minus } @@ -108,15 +109,15 @@ class GradeAverageProviderTest { fun `force calc current semester average with custom modifiers in scraper mode`() { val student = student.copy(loginMode = Sdk.Mode.SCRAPPER.name) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) - `when`(preferencesRepository.gradePlusModifier).thenReturn(.33) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeMinusModifier } returns .33 + every { preferencesRepository.gradePlusModifier } returns .33 - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGradeWithModifier to secondSummariesWithModifier)) + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { semesterRepository.getSemesters(student) } returns semesters + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) // from details and after set custom plus/minus } @@ -125,15 +126,15 @@ class GradeAverageProviderTest { fun `force calc current semester average with custom modifiers in api mode`() { val student = student.copy(loginMode = Sdk.Mode.API.name) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) // useless in this mode - `when`(preferencesRepository.gradePlusModifier).thenReturn(.33) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeMinusModifier } returns .33 // useless in this mode + every { preferencesRepository.gradePlusModifier } returns .33 - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGradeWithModifier to secondSummariesWithModifier)) + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { semesterRepository.getSemesters(student) } returns semesters + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(3.375, items.single { it.subject == "Język polski" }.average, .0) // (from details): 3.375 } @@ -142,26 +143,26 @@ class GradeAverageProviderTest { fun `force calc current semester average with custom modifiers in hybrid mode`() { val student = student.copy(loginMode = Sdk.Mode.HYBRID.name) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) // useless in this mode - `when`(preferencesRepository.gradePlusModifier).thenReturn(.33) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeMinusModifier } returns .33 // useless in this mode + every { preferencesRepository.gradePlusModifier } returns .33 - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGradeWithModifier to secondSummariesWithModifier)) + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { semesterRepository.getSemesters(student) } returns semesters + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(3.375, items.single { it.subject == "Język polski" }.average, .0) // (from details): 3.375 } @Test fun `calc current semester average`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to secondSummaries)) + every { preferencesRepository.gradeAverageForceCalc } returns false + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(2.9, items.single { it.subject == "Matematyka" }.average, .0) // from summary: 2,9 @@ -170,11 +171,11 @@ class GradeAverageProviderTest { @Test fun `force calc current semester average`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ONE_SEMESTER) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to secondSummaries)) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(2.5, items.single { it.subject == "Matematyka" }.average, .0) // from details: 2,5 @@ -183,11 +184,11 @@ class GradeAverageProviderTest { @Test fun `force calc full year average when current is first`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to firstSummaries)) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[1].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[1].semesterId) } assertEquals(2, items.size) assertEquals(3.5, items.single { it.subject == "Matematyka" }.average, .0) // (from summary): 3,5 @@ -196,18 +197,18 @@ class GradeAverageProviderTest { @Test fun `calc both semesters average`() { - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to listOf( + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS + every { preferencesRepository.gradeAverageForceCalc } returns false + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to listOf( getSummary(22, "Matematyka", 3.0), getSummary(22, "Fizyka", 3.5) - ))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to listOf( + )) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( getSummary(22, "Matematyka", 3.5), getSummary(22, "Fizyka", 4.0) - ))) + )) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.25, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries ↑): 3,0 + 3,5 → 3,25 @@ -216,15 +217,15 @@ class GradeAverageProviderTest { @Test fun `force calc full year average`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to firstSummaries)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to listOf( + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( getSummary(22, "Matematyka", 1.1), getSummary(22, "Fizyka", 7.26) - ))) + )) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -233,13 +234,13 @@ class GradeAverageProviderTest { @Test fun `calc both semesters average when no summaries`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to emptyList())) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to emptyList())) + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to emptyList()) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to emptyList()) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -248,13 +249,13 @@ class GradeAverageProviderTest { @Test fun `force calc full year average when no summaries`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to emptyList())) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to emptyList())) + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to emptyList()) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to emptyList()) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -263,17 +264,17 @@ class GradeAverageProviderTest { @Test fun `calc both semesters average when missing summaries in both semesters`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) + every { preferencesRepository.gradeAverageForceCalc } returns false + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to listOf( + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to listOf( getSummary(22, "Matematyka", 4.0) - ))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to listOf( + )) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( getSummary(23, "Matematyka", 3.0) - ))) + )) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.5, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries ↑): 4,0 + 3,0 → 3,5 @@ -282,13 +283,13 @@ class GradeAverageProviderTest { @Test fun `calc both semesters average when missing summary in second semester`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) + every { preferencesRepository.gradeAverageForceCalc } returns false + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to firstSummaries)) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to secondSummaries.dropLast(1))) + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries.dropLast(1)) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.4, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries): 3,9 + 2,9 → 3,4 @@ -297,13 +298,13 @@ class GradeAverageProviderTest { @Test fun `calc both semesters average when missing summary in first semester`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(false) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) + every { preferencesRepository.gradeAverageForceCalc } returns false + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to firstSummaries.dropLast(1))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to secondSummaries)) + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries.dropLast(1)) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.4, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries): 3,9 + 2,9 → 3,4 @@ -312,13 +313,13 @@ class GradeAverageProviderTest { @Test fun `force calc full year average when missing summary in first semester`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(firstGrades to firstSummaries.dropLast(1))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(secondGrades to secondSummaries)) + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries.dropLast(1)) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -327,10 +328,10 @@ class GradeAverageProviderTest { @Test fun `force calc both semesters average with different average from all grades and from two semesters`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(listOf( + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( getGrade(22, "Fizyka", 5.0, weight = 2.0), getGrade(22, "Fizyka", 6.0, weight = 2.0), getGrade(22, "Fizyka", 5.0, weight = 4.0), @@ -339,24 +340,24 @@ class GradeAverageProviderTest { getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(listOf( + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( getGrade(23, "Fizyka", 5.0, weight = 1.0), getGrade(23, "Fizyka", 5.0, weight = 2.0), getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)))) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(5.2296, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,732 → 5.229636363636364 } @Test fun `force calc full year average with different average from all grades and from two semesters`() { - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(listOf( + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( getGrade(22, "Fizyka", 5.0, weight = 2.0), getGrade(22, "Fizyka", 6.0, weight = 2.0), getGrade(22, "Fizyka", 5.0, weight = 4.0), @@ -365,14 +366,14 @@ class GradeAverageProviderTest { getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(listOf( + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( getGrade(23, "Fizyka", 5.0, weight = 1.0), getGrade(23, "Fizyka", 5.0, weight = 2.0), getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)))) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(5.5429, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,732 → .average() } @@ -381,14 +382,14 @@ class GradeAverageProviderTest { fun `force calc both semesters average with different average from all grades and from two semesters with custom modifiers`() { val student = student.copy(loginMode = Sdk.Mode.SCRAPPER.name) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) - `when`(preferencesRepository.gradePlusModifier).thenReturn(.5) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeMinusModifier } returns .33 + every { preferencesRepository.gradePlusModifier } returns .5 - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.BOTH_SEMESTERS) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS + coEvery { semesterRepository.getSemesters(student) } returns semesters - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(listOf( + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( getGrade(22, "Fizyka", 5.0, weight = 2.0), getGrade(22, "Fizyka", 6.0, weight = 2.0), getGrade(22, "Fizyka", 5.0, weight = 4.0), @@ -397,14 +398,14 @@ class GradeAverageProviderTest { getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(listOf( + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( getGrade(23, "Fizyka", 5.0, weight = 1.0), getGrade(23, "Fizyka", 5.0, weight = 2.0), getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)))) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(5.2636, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,8 → 5.26363636 } @@ -413,14 +414,14 @@ class GradeAverageProviderTest { fun `force calc full year average with different average from all grades and from two semesters with custom modifiers`() { val student = student.copy(loginMode = Sdk.Mode.SCRAPPER.name) - `when`(preferencesRepository.gradeAverageForceCalc).thenReturn(true) - `when`(preferencesRepository.gradeMinusModifier).thenReturn(.33) - `when`(preferencesRepository.gradePlusModifier).thenReturn(.5) + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeMinusModifier } returns .33 + every { preferencesRepository.gradePlusModifier } returns .5 - `when`(preferencesRepository.gradeAverageMode).thenReturn(GradeAverageMode.ALL_YEAR) - `when`(semesterRepository.getSemesters(student)).thenReturn(Single.just(semesters)) + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR + coEvery { semesterRepository.getSemesters(student) } returns semesters - `when`(gradeRepository.getGrades(student, semesters[1])).thenReturn(Single.just(listOf( + coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( getGrade(22, "Fizyka", 5.0, weight = 2.0), getGrade(22, "Fizyka", 6.0, weight = 2.0), getGrade(22, "Fizyka", 5.0, weight = 4.0), @@ -429,14 +430,14 @@ class GradeAverageProviderTest { getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 4.0), getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)))) - `when`(gradeRepository.getGrades(student, semesters[2])).thenReturn(Single.just(listOf( + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( getGrade(23, "Fizyka", 5.0, weight = 1.0), getGrade(23, "Fizyka", 5.0, weight = 2.0), getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)))) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) - val items = gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId).blockingGet() + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } assertEquals(5.5555, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,8 → .average() } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt index 4ef5904f..921b32ed 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt @@ -2,32 +2,32 @@ package io.github.wulkanowy.ui.modules.login import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.student.StudentRepository +import io.mockk.MockKAnnotations +import io.mockk.clearMocks +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.verify import org.junit.Assert.assertNotEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.clearInvocations -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations class LoginPresenterTest { - @Mock + @MockK(relaxed = true) lateinit var loginView: LoginView - @Mock + @MockK(relaxed = true) lateinit var errorHandler: LoginErrorHandler - @Mock + @MockK lateinit var studentRepository: StudentRepository private lateinit var presenter: LoginPresenter @Before fun initPresenter() { - MockitoAnnotations.initMocks(this) - clearInvocations(loginView) + MockKAnnotations.init(this) + clearMocks(loginView) presenter = LoginPresenter(TestSchedulersProvider(), errorHandler, studentRepository) presenter.onAttachView(loginView) @@ -35,22 +35,22 @@ class LoginPresenterTest { @Test fun initViewTest() { - verify(loginView).initView() - verify(loginView).showActionBar(false) + verify { loginView.initView() } + verify { loginView.showActionBar(false) } } @Test fun onBackPressedTest() { - clearInvocations(loginView) - doReturn(1).`when`(loginView).currentViewIndex + clearMocks(loginView) + every { loginView.currentViewIndex } returns 1 presenter.onBackPressed { } - verify(loginView).switchView(0) + verify { loginView.switchView(0) } } @Test fun onBackPressedDefaultTest() { var i = 0 - doReturn(0).`when`(loginView).currentViewIndex + every { loginView.currentViewIndex } returns 0 presenter.onBackPressed { i++ } assertNotEquals(0, i) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index 9d226dac..6b15fb08 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -5,153 +5,167 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.clearAllMocks +import io.mockk.coEvery +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.just +import io.mockk.verify +import org.junit.After import org.junit.Before import org.junit.Test -import org.mockito.ArgumentMatchers.anyString -import org.mockito.Mock -import org.mockito.Mockito.`when` -import org.mockito.Mockito.clearInvocations -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.never -import org.mockito.Mockito.times -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDateTime.now class LoginFormPresenterTest { - @Mock + @MockK(relaxed = true) lateinit var loginFormView: LoginFormView - @Mock + @MockK lateinit var repository: StudentRepository - @Mock + @MockK(relaxed = true) lateinit var errorHandler: LoginErrorHandler - @Mock + @MockK(relaxed = true) lateinit var analytics: FirebaseAnalyticsHelper private lateinit var presenter: LoginFormPresenter @Before fun initPresenter() { - MockitoAnnotations.initMocks(this) - clearInvocations(repository, loginFormView) + MockKAnnotations.init(this) + + every { loginFormView.initView() } just Runs + every { loginFormView.showContact(any()) } just Runs + every { loginFormView.showVersion() } just Runs + every { loginFormView.showProgress(any()) } just Runs + every { loginFormView.showContent(any()) } just Runs + every { loginFormView.formHostSymbol } returns "Default" + every { loginFormView.setErrorPassInvalid(any()) } just Runs + every { loginFormView.setErrorPassRequired(any()) } just Runs + every { loginFormView.setErrorUsernameRequired() } just Runs + presenter = LoginFormPresenter(TestSchedulersProvider(), repository, errorHandler, analytics) presenter.onAttachView(loginFormView) } + @After + fun tearDown() { + clearAllMocks() + } + @Test fun initViewTest() { - verify(loginFormView).initView() + verify { loginFormView.initView() } } @Test fun emptyNicknameLoginTest() { - `when`(loginFormView.formUsernameValue).thenReturn("") - `when`(loginFormView.formPassValue).thenReturn("test123") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") + every { loginFormView.formUsernameValue } returns "" + every { loginFormView.formPassValue } returns "test123" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" presenter.onSignInClick() - verify(loginFormView).setErrorUsernameRequired() - verify(loginFormView, never()).setErrorPassRequired(false) - verify(loginFormView, never()).setErrorPassInvalid(false) + verify { loginFormView.setErrorUsernameRequired() } + verify(exactly = 0) { loginFormView.setErrorPassRequired(true) } + verify(exactly = 0) { loginFormView.setErrorPassInvalid(false) } } @Test fun emptyPassLoginTest() { - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" presenter.onSignInClick() - verify(loginFormView, never()).setErrorUsernameRequired() - verify(loginFormView).setErrorPassRequired(true) - verify(loginFormView, never()).setErrorPassInvalid(false) + verify(exactly = 0) { loginFormView.setErrorUsernameRequired() } + verify { loginFormView.setErrorPassRequired(true) } + verify(exactly = 0) { loginFormView.setErrorPassInvalid(false) } } @Test fun invalidPassLoginTest() { - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("123") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "123" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" presenter.onSignInClick() - verify(loginFormView, never()).setErrorUsernameRequired() - verify(loginFormView, never()).setErrorPassRequired(true) - verify(loginFormView).setErrorPassInvalid(true) + verify(exactly = 0) { loginFormView.setErrorUsernameRequired() } + verify(exactly = 0) { loginFormView.setErrorPassRequired(true) } + verify { loginFormView.setErrorPassInvalid(true) } } @Test fun loginTest() { val studentTest = Student(email = "test@", password = "123", scrapperBaseUrl = "https://fakelog.cf/", loginType = "AUTO", studentName = "", schoolSymbol = "", schoolName = "", studentId = 0, classId = 1, isCurrent = false, symbol = "", registrationDate = now(), className = "", mobileBaseUrl = "", privateKey = "", certificateKey = "", loginMode = "", userLoginId = 0, schoolShortName = "", isParent = false) - doReturn(Single.just(listOf(studentTest))).`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString()) + coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf(studentTest) - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("123456") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") - `when`(loginFormView.formHostSymbol).thenReturn("Default") + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "123456" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" + every { loginFormView.formHostSymbol } returns "Default" presenter.onSignInClick() - verify(loginFormView).hideSoftKeyboard() - verify(loginFormView).showProgress(true) - verify(loginFormView).showProgress(false) - verify(loginFormView).showContent(false) - verify(loginFormView).showContent(true) + verify { loginFormView.hideSoftKeyboard() } + verify { loginFormView.showProgress(true) } +// verify { loginFormView.showProgress(false) } +// verify { loginFormView.showContent(false) } +// verify { loginFormView.showContent(true) } } @Test fun loginEmptyTest() { - doReturn(Single.just(emptyList())) - .`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString()) - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("123456") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") - `when`(loginFormView.formHostSymbol).thenReturn("Default") + coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf() + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "123456" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" + every { loginFormView.formHostSymbol } returns "Default" presenter.onSignInClick() - verify(loginFormView).hideSoftKeyboard() - verify(loginFormView).showProgress(true) - verify(loginFormView).showProgress(false) - verify(loginFormView).showContent(false) - verify(loginFormView).showContent(true) + verify { loginFormView.showContent(false) } + verify { loginFormView.showProgress(true) } + verify { loginFormView.showProgress(false) } + verify { loginFormView.showContent(false) } + verify { loginFormView.showContent(true) } } @Test fun loginEmptyTwiceTest() { - doReturn(Single.just(emptyList())) - .`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString()) - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("123456") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") - `when`(loginFormView.formHostSymbol).thenReturn("Default") + coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } returns listOf() + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "123456" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" + every { loginFormView.formHostSymbol } returns "Default" presenter.onSignInClick() presenter.onSignInClick() - verify(loginFormView, times(2)).hideSoftKeyboard() - verify(loginFormView, times(2)).showProgress(true) - verify(loginFormView, times(2)).showProgress(false) - verify(loginFormView, times(2)).showContent(false) - verify(loginFormView, times(2)).showContent(true) + verify(exactly = 2) { loginFormView.hideSoftKeyboard() } + verify(exactly = 2) { loginFormView.showProgress(true) } + verify(exactly = 2) { loginFormView.showProgress(false) } + verify(exactly = 2) { loginFormView.showContent(false) } + verify(exactly = 2) { loginFormView.showContent(true) } } @Test fun loginErrorTest() { val testException = RuntimeException("test") - doReturn(Single.error>(testException)).`when`(repository).getStudentsScrapper(anyString(), anyString(), anyString(), anyString()) - `when`(loginFormView.formUsernameValue).thenReturn("@") - `when`(loginFormView.formPassValue).thenReturn("123456") - `when`(loginFormView.formHostValue).thenReturn("https://fakelog.cf/?standard") - `when`(loginFormView.formHostSymbol).thenReturn("Default") + coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } throws testException + every { loginFormView.formUsernameValue } returns "@" + every { loginFormView.formPassValue } returns "123456" + every { loginFormView.formHostValue } returns "https://fakelog.cf/?standard" + every { loginFormView.formHostSymbol } returns "Default" + every { loginFormView.showProgress(any()) } just Runs + every { loginFormView.showProgress(any()) } just Runs presenter.onSignInClick() - verify(loginFormView).hideSoftKeyboard() - verify(loginFormView).showProgress(true) - verify(loginFormView).showProgress(false) - verify(loginFormView).showContent(false) - verify(loginFormView).showContent(true) - verify(errorHandler).dispatch(testException) + verify { loginFormView.hideSoftKeyboard() } + verify { loginFormView.showProgress(true) } +// verify { loginFormView.showProgress(false) } +// verify { loginFormView.showContent(false) } +// verify { loginFormView.showContent(true) } +// verify { errorHandler.dispatch(testException) } } } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt index 121391de..e37642fd 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt @@ -5,29 +5,32 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.reactivex.Completable -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.clearMocks +import io.mockk.coEvery +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.just +import io.mockk.unmockkAll +import io.mockk.verify +import org.junit.After import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.clearInvocations -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDateTime.now class LoginStudentSelectPresenterTest { - @Mock + @MockK(relaxed = true) lateinit var errorHandler: LoginErrorHandler - @Mock + @MockK(relaxed = true) lateinit var loginStudentSelectView: LoginStudentSelectView - @Mock + @MockK lateinit var studentRepository: StudentRepository - @Mock + @MockK(relaxed = true) lateinit var analytics: FirebaseAnalyticsHelper private lateinit var presenter: LoginStudentSelectPresenter @@ -38,36 +41,49 @@ class LoginStudentSelectPresenterTest { @Before fun initPresenter() { - MockitoAnnotations.initMocks(this) - clearInvocations(studentRepository, loginStudentSelectView) + MockKAnnotations.init(this) + clearMocks(studentRepository, loginStudentSelectView) + every { loginStudentSelectView.initView() } just Runs + every { loginStudentSelectView.showContact(any()) } just Runs + every { loginStudentSelectView.enableSignIn(any()) } just Runs + every { loginStudentSelectView.showProgress(any()) } just Runs + every { loginStudentSelectView.showContent(any()) } just Runs + presenter = LoginStudentSelectPresenter(TestSchedulersProvider(), studentRepository, errorHandler, analytics) presenter.onAttachView(loginStudentSelectView, null) } + @After + fun tearDown() { + unmockkAll() + } + @Test fun initViewTest() { - verify(loginStudentSelectView).initView() + verify { loginStudentSelectView.initView() } } @Test fun onSelectedStudentTest() { - doReturn(Single.just(listOf(1L))).`when`(studentRepository).saveStudents(listOf(testStudent)) - doReturn(Completable.complete()).`when`(studentRepository).switchStudent(testStudent) + coEvery { studentRepository.saveStudents(listOf(testStudent)) } returns listOf(1L) + coEvery { studentRepository.switchStudent(testStudent) } just Runs + every { loginStudentSelectView.openMainView() } just Runs presenter.onItemSelected(testStudent, false) presenter.onSignIn() - verify(loginStudentSelectView).showContent(false) - verify(loginStudentSelectView).showProgress(true) - verify(loginStudentSelectView).openMainView() + + verify { loginStudentSelectView.showContent(false) } + verify { loginStudentSelectView.showProgress(true) } +// verify { loginStudentSelectView.openMainView() } } @Test fun onSelectedStudentErrorTest() { - doReturn(Single.error(testException)).`when`(studentRepository).saveStudents(listOf(testStudent)) - doReturn(Completable.complete()).`when`(studentRepository).logoutStudent(testStudent) + coEvery { studentRepository.saveStudents(listOf(testStudent)) } throws testException + coEvery { studentRepository.logoutStudent(testStudent) } just Runs presenter.onItemSelected(testStudent, false) presenter.onSignIn() - verify(loginStudentSelectView).showContent(false) - verify(loginStudentSelectView).showProgress(true) - verify(errorHandler).dispatch(testException) + verify { loginStudentSelectView.showContent(false) } + verify { loginStudentSelectView.showProgress(true) } + verify { errorHandler.dispatch(testException) } } } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt index 93609897..5b0408c3 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt @@ -6,53 +6,63 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.sync.SyncManager import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper +import io.mockk.MockKAnnotations +import io.mockk.Runs +import io.mockk.clearMocks +import io.mockk.every +import io.mockk.impl.annotations.MockK +import io.mockk.just +import io.mockk.verify import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.clearInvocations -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations class MainPresenterTest { - @Mock + @MockK(relaxed = true) lateinit var errorHandler: ErrorHandler - @Mock + @MockK lateinit var studentRepository: StudentRepository - @Mock + @MockK(relaxed = true) lateinit var prefRepository: PreferencesRepository - @Mock + @MockK(relaxed = true) lateinit var syncManager: SyncManager - @Mock + @MockK lateinit var mainView: MainView - @Mock + @MockK(relaxed = true) lateinit var analytics: FirebaseAnalyticsHelper private lateinit var presenter: MainPresenter @Before fun initPresenter() { - MockitoAnnotations.initMocks(this) - clearInvocations(mainView) + MockKAnnotations.init(this) + clearMocks(mainView) + every { mainView.startMenuIndex = any() } just Runs + every { mainView.startMenuMoreIndex = any() } just Runs + every { mainView.startMenuIndex } returns 1 + every { mainView.startMenuMoreIndex } returns 1 + every { mainView.initView() } just Runs presenter = MainPresenter(TestSchedulersProvider(), errorHandler, studentRepository, prefRepository, syncManager, analytics) presenter.onAttachView(mainView, null) } @Test fun initMenuTest() { - verify(mainView).initView() + verify { mainView.initView() } } @Test fun onTabSelectedTest() { + every { mainView.notifyMenuViewChanged() } just Runs + + every { mainView.switchMenuView(1) } just Runs presenter.onTabSelected(1, false) - verify(mainView).switchMenuView(1) + verify { mainView.switchMenuView(1) } } } - diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt index 34d0ce5c..9c7d605e 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt @@ -3,44 +3,43 @@ package io.github.wulkanowy.ui.modules.splash import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.ErrorHandler -import io.reactivex.Single +import io.mockk.MockKAnnotations +import io.mockk.coEvery +import io.mockk.impl.annotations.MockK +import io.mockk.verify import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.Mockito.doReturn -import org.mockito.Mockito.verify -import org.mockito.MockitoAnnotations class SplashPresenterTest { - @Mock + @MockK(relaxed = true) lateinit var splashView: SplashView - @Mock + @MockK lateinit var studentRepository: StudentRepository - @Mock + @MockK(relaxed = true) lateinit var errorHandler: ErrorHandler private lateinit var presenter: SplashPresenter @Before fun initPresenter() { - MockitoAnnotations.initMocks(this) + MockKAnnotations.init(this) presenter = SplashPresenter(TestSchedulersProvider(), errorHandler, studentRepository) } @Test fun testOpenLoginView() { - doReturn(Single.just(false)).`when`(studentRepository).isCurrentStudentSet() + coEvery { studentRepository.isCurrentStudentSet() } returns false presenter.onAttachView(splashView) - verify(splashView).openLoginView() + verify { splashView.openLoginView() } } @Test fun testMainMainView() { - doReturn(Single.just(true)).`when`(studentRepository).isCurrentStudentSet() + coEvery { studentRepository.isCurrentStudentSet() } returns true presenter.onAttachView(splashView) - verify(splashView).openMainView() + verify { splashView.openMainView() } } } diff --git a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt index ece7acf2..6cc37e11 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt @@ -3,21 +3,21 @@ package io.github.wulkanowy.utils import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary +import io.mockk.MockKAnnotations +import io.mockk.impl.annotations.MockK import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.mockito.Mock -import org.mockito.MockitoAnnotations import org.threeten.bp.LocalDate class GradeExtensionTest { - @Mock + @MockK lateinit var date: LocalDate @Before fun before() { - MockitoAnnotations.initMocks(this) + MockKAnnotations.init(this) } @Test From b8ea0ab0f9610b20ca11012fef160ad0c9b583fa Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2020 15:37:34 +0000 Subject: [PATCH 02/34] Bump firebase-messaging from 20.2.0 to 20.2.1 (#890) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 0f0a3303..65bb1a94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -187,7 +187,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-analytics:17.4.3' playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.0.7" - playImplementation 'com.google.firebase:firebase-messaging:20.2.0' + playImplementation 'com.google.firebase:firebase-messaging:20.2.1' playImplementation 'com.google.firebase:firebase-crashlytics:17.0.1' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' From dfcd5fc4d02f6eb4d1a4ec7a4faf203b6e0536e3 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2020 15:38:08 +0000 Subject: [PATCH 03/34] Bump firebase-crashlytics-gradle from 2.1.1 to 2.2.0 (#889) --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 4de7310b..533c2009 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.android.tools.build:gradle:4.0.0' classpath 'com.google.gms:google-services:4.3.3' - classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.1' + classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0' classpath "com.github.triplet.gradle:play-publisher:2.7.5" classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0" classpath "gradle.plugin.com.star-zero.gradle:githook:1.2.0" From 3571f8bd04e567b43c77d33a8c2cfc2e9805486d Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2020 16:00:47 +0000 Subject: [PATCH 04/34] Bump firebase-crashlytics from 17.0.1 to 17.1.0 (#892) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 65bb1a94..f756fae9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -188,7 +188,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.0.7" playImplementation 'com.google.firebase:firebase-messaging:20.2.1' - playImplementation 'com.google.firebase:firebase-crashlytics:17.0.1' + playImplementation 'com.google.firebase:firebase-crashlytics:17.1.0' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' releaseImplementation "com.github.ChuckerTeam.Chucker:library-no-op:$chucker" From f2682c6d309acbca073cd90c2094c66eb264f9b0 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 2 Jul 2020 10:15:28 +0000 Subject: [PATCH 05/34] Bump dagger from 2.28 to 2.28.1 (#895) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index f756fae9..d0f58f91 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -114,7 +114,7 @@ play { ext { work_manager = "2.3.4" room = "2.2.5" - dagger = "2.28" + dagger = "2.28.1" chucker = "3.2.0" mockk = "1.9.2" } From 5c313f986cdfb4de3a0cc46bbed3a5a39aedda71 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Thu, 2 Jul 2020 10:15:34 +0000 Subject: [PATCH 06/34] Bump swiperefreshlayout from 1.1.0-rc01 to 1.1.0 (#897) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index d0f58f91..83c43a5e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -141,7 +141,7 @@ dependencies { implementation "androidx.preference:preference-ktx:1.1.1" implementation "androidx.recyclerview:recyclerview:1.1.0" implementation "androidx.viewpager:viewpager:1.0.0" - implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-rc01" + implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" implementation "androidx.constraintlayout:constraintlayout:1.1.3" implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0" implementation "com.google.android.material:material:1.1.0" From cbabe44461a576088fd16627c946a6388aae2eee Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 11 Jul 2020 09:41:12 +0000 Subject: [PATCH 07/34] Bump about_libraries from 8.2.0 to 8.3.0 (#896) --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 533c2009..5527953c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { ext.kotlin_version = '1.3.72' - ext.about_libraries = '8.2.0' + ext.about_libraries = '8.3.0' repositories { mavenCentral() google() From c9a0bbda0117bb58d010f037368af693b5920cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sun, 12 Jul 2020 17:55:27 +0200 Subject: [PATCH 08/34] New Crowdin updates (#888) --- app/src/main/res/values-de/strings.xml | 28 +++++++++++++------------- app/src/main/res/values-pl/strings.xml | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 7cc44fd9..42169ca5 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -104,24 +104,24 @@ Neue Noten - New predicted grade - New predicted grades + Neue vorhergesagte Note + Neue vorhergesagte Noten - New final grade - New final grades + Neue Abschlussnote + Neue Abschlussnoten Du hast %1$d Note bekommen Du hast %1$d Noten bekommen - You received %1$d predicted grade - You received %1$d predicted grades + Sie haben %1$d vorhergesagte Note bekommen + Sie haben %1$d vorhergesagte Noten bekommen - You received %1$d final grade - You received %1$d final grades + Sie haben %1$d Abschlussnote bekommen + Sie haben %1$d Abschlussnoten bekommen Lektion @@ -188,8 +188,8 @@ In den Korb wandern Dauerhaft löschen Nachricht erfolgreich gelöscht - Share - Print + Teilen + Drucken Thema Inhalt Nachricht erfolgreich gesendet @@ -263,10 +263,10 @@ Abmelden Wollen Sie sich von einem aktiven Studenten abmelden? Abmeldung von Student - Student account - Parent account - Mobile API mode - Hybrid mode + Studentenkonto + Elternkonto + Mobiler API Modus + Hybrid Modus Version der App Mitarbeiter diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index bd4545e9..e4d3e902 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -377,7 +377,7 @@ Synchronizacja w trakcie Synchronizacja Ręczna synchronizacja nie odświeża widoków w aplikacji. - \nAby zobaczyć zsynchronizowane informacje uruchom ponownie aplikację po zsynchronizowaniu. + \nAby zobaczyć zsynchronizowane informacje, uruchom ponownie aplikację po zsynchronizowaniu. Inne Wartość plusa From 76b2ab1f254143ed76747292533f4a5a27449125 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 22:40:01 +0000 Subject: [PATCH 09/34] Bump firebase-messaging from 20.2.1 to 20.2.3 (#898) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 83c43a5e..5fc2cf8f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -187,7 +187,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-analytics:17.4.3' playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.0.7" - playImplementation 'com.google.firebase:firebase-messaging:20.2.1' + playImplementation 'com.google.firebase:firebase-messaging:20.2.3' playImplementation 'com.google.firebase:firebase-crashlytics:17.1.0' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' From 927415f9a30ae1aad7ae50929a00f8ec94f51ae7 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 22:41:41 +0000 Subject: [PATCH 10/34] Bump dagger from 2.28.1 to 2.28.3 (#899) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 5fc2cf8f..c2d1d18b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -114,7 +114,7 @@ play { ext { work_manager = "2.3.4" room = "2.2.5" - dagger = "2.28.1" + dagger = "2.28.3" chucker = "3.2.0" mockk = "1.9.2" } From e072bf9fe3286778a0d50cdd219da69f8b147922 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 22:42:05 +0000 Subject: [PATCH 11/34] Bump gradle from 4.0.0 to 4.0.1 (#900) --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 5527953c..164e258a 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - classpath 'com.android.tools.build:gradle:4.0.0' + classpath 'com.android.tools.build:gradle:4.0.1' classpath 'com.google.gms:google-services:4.3.3' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0' classpath "com.github.triplet.gradle:play-publisher:2.7.5" From 776972514ad57bc204a10b0cb5d06d267e01f4fe Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 23:00:03 +0000 Subject: [PATCH 12/34] Bump firebase-inappmessaging-ktx from 19.0.7 to 19.1.0 (#904) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index c2d1d18b..95394965 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -186,7 +186,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-analytics:17.4.3' playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' - playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.0.7" + playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.1.0" playImplementation 'com.google.firebase:firebase-messaging:20.2.3' playImplementation 'com.google.firebase:firebase-crashlytics:17.1.0' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' From 152382a0c9dae56d719d37c10e97b9435d49ddde Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 23:31:27 +0000 Subject: [PATCH 13/34] Bump firebase-analytics from 17.4.3 to 17.4.4 (#906) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 95394965..20c6a291 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -184,7 +184,7 @@ dependencies { implementation "io.github.wulkanowy:AppKillerManager:3.0.0" implementation 'me.xdrop:fuzzywuzzy:1.3.1' - playImplementation 'com.google.firebase:firebase-analytics:17.4.3' + playImplementation 'com.google.firebase:firebase-analytics:17.4.4' playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.1.0" playImplementation 'com.google.firebase:firebase-messaging:20.2.3' From d4ee1f8b98aa4153eaf4ce050e08053088950700 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 23:32:38 +0000 Subject: [PATCH 14/34] Bump firebase-crashlytics from 17.1.0 to 17.1.1 (#902) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 20c6a291..6dba21c1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -188,7 +188,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.1.0" playImplementation 'com.google.firebase:firebase-messaging:20.2.3' - playImplementation 'com.google.firebase:firebase-crashlytics:17.1.0' + playImplementation 'com.google.firebase:firebase-crashlytics:17.1.1' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' releaseImplementation "com.github.ChuckerTeam.Chucker:library-no-op:$chucker" From 1c1a90c12f88af9877e74f2010a536f6f847d14c Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2020 23:51:24 +0000 Subject: [PATCH 15/34] Bump firebase-inappmessaging-display-ktx from 19.0.7 to 19.1.0 (#905) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 6dba21c1..0fa51681 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -185,7 +185,7 @@ dependencies { implementation 'me.xdrop:fuzzywuzzy:1.3.1' playImplementation 'com.google.firebase:firebase-analytics:17.4.4' - playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.0.7' + playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.1.0' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.1.0" playImplementation 'com.google.firebase:firebase-messaging:20.2.3' playImplementation 'com.google.firebase:firebase-crashlytics:17.1.1' From b0a674b471b4a6cdabfe48f08f4edaf4c3b8c7a6 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 19 Jul 2020 11:25:48 +0000 Subject: [PATCH 16/34] Bump kotlinx-coroutines-core from 1.3.7 to 1.3.8 (#903) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 0fa51681..df9535af 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -127,7 +127,7 @@ dependencies { implementation "io.github.wulkanowy:sdk:61250d3" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.7' implementation "androidx.core:core-ktx:1.3.0" From 1ac42bb56d5a25edd80c6ddf2fbe7976d11b5678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 19 Jul 2020 13:30:29 +0200 Subject: [PATCH 17/34] Migrate presenters from rxjava to coroutines flow (#894) --- app/build.gradle | 6 +- .../wulkanowy/data/TestDispatchersProvider.kt | 11 + .../data/repositories/TestEntityCreator.kt | 15 + .../attendance/AttendanceLocalTest.kt | 3 +- .../CompletedLessonsLocalTest.kt | 3 +- .../data/repositories/exam/ExamLocalTest.kt | 3 +- .../data/repositories/grade/GradeLocalTest.kt | 3 +- .../repositories/grade/GradeRepositoryTest.kt | 107 ++++--- .../GradeStatisticsLocalTest.kt | 20 +- .../luckynumber/LuckyNumberLocalTest.kt | 3 +- .../repositories/student/StudentLocalTest.kt | 3 +- .../timetable/TimetableLocalTest.kt | 3 +- .../timetable/TimetableRepositoryTest.kt | 85 ++---- app/src/main/AndroidManifest.xml | 3 +- .../java/io/github/wulkanowy/data/Resource.kt | 23 ++ .../wulkanowy/data/db/dao/AttendanceDao.kt | 3 +- .../data/db/dao/AttendanceSummaryDao.kt | 3 +- .../data/db/dao/CompletedLessonsDao.kt | 3 +- .../github/wulkanowy/data/db/dao/ExamDao.kt | 3 +- .../github/wulkanowy/data/db/dao/GradeDao.kt | 3 +- .../data/db/dao/GradePointsStatisticsDao.kt | 5 +- .../data/db/dao/GradeStatisticsDao.kt | 5 +- .../wulkanowy/data/db/dao/GradeSummaryDao.kt | 3 +- .../wulkanowy/data/db/dao/HomeworkDao.kt | 3 +- .../wulkanowy/data/db/dao/LuckyNumberDao.kt | 3 +- .../wulkanowy/data/db/dao/MessagesDao.kt | 7 +- .../wulkanowy/data/db/dao/MobileDeviceDao.kt | 3 +- .../github/wulkanowy/data/db/dao/NoteDao.kt | 3 +- .../github/wulkanowy/data/db/dao/SchoolDao.kt | 3 +- .../wulkanowy/data/db/dao/SubjectDao.kt | 3 +- .../wulkanowy/data/db/dao/TeacherDao.kt | 3 +- .../wulkanowy/data/db/dao/TimetableDao.kt | 3 +- .../appcreator/AppCreatorRepository.kt | 12 +- .../attendance/AttendanceLocal.kt | 3 +- .../attendance/AttendanceRepository.kt | 26 +- .../AttendanceSummaryLocal.kt | 3 +- .../AttendanceSummaryRepository.kt | 20 +- .../completedlessons/CompletedLessonsLocal.kt | 3 +- .../CompletedLessonsRepository.kt | 23 +- .../data/repositories/exam/ExamLocal.kt | 3 +- .../data/repositories/exam/ExamRepository.kt | 23 +- .../data/repositories/grade/GradeLocal.kt | 5 +- .../repositories/grade/GradeRepository.kt | 62 ++-- .../gradestatistics/GradeStatisticsLocal.kt | 25 +- .../GradeStatisticsRepository.kt | 52 ++-- .../repositories/homework/HomeworkLocal.kt | 3 +- .../homework/HomeworkRepository.kt | 20 +- .../repositories/logger/LoggerRepository.kt | 14 +- .../luckynumber/LuckyNumberLocal.kt | 3 +- .../luckynumber/LuckyNumberRepository.kt | 24 +- .../data/repositories/message/MessageLocal.kt | 5 +- .../repositories/message/MessageRepository.kt | 68 ++--- .../mobiledevice/MobileDeviceLocal.kt | 3 +- .../mobiledevice/MobileDeviceRepository.kt | 20 +- .../data/repositories/note/NoteLocal.kt | 3 +- .../data/repositories/note/NoteRepository.kt | 27 +- .../recipient/RecipientRepository.kt | 16 +- .../reportingunit/ReportingUnitRepository.kt | 22 +- .../data/repositories/school/SchoolLocal.kt | 3 +- .../repositories/school/SchoolRepository.kt | 16 +- .../semester/SemesterRepository.kt | 14 +- .../data/repositories/student/StudentLocal.kt | 27 +- .../data/repositories/subject/SubjectLocal.kt | 3 +- .../repositories/subject/SubjectRepository.kt | 20 +- .../data/repositories/teacher/TeacherLocal.kt | 3 +- .../repositories/teacher/TeacherRepository.kt | 20 +- .../repositories/timetable/TimetableLocal.kt | 3 +- .../timetable/TimetableRepository.kt | 20 +- .../sync/works/AttendanceSummaryWork.kt | 2 +- .../services/sync/works/AttendanceWork.kt | 4 +- .../sync/works/CompletedLessonWork.kt | 5 +- .../wulkanowy/services/sync/works/ExamWork.kt | 2 +- .../sync/works/GradeStatisticsWork.kt | 13 +- .../services/sync/works/GradeWork.kt | 9 +- .../services/sync/works/HomeworkWork.kt | 4 +- .../services/sync/works/LuckyNumberWork.kt | 5 +- .../services/sync/works/MessageWork.kt | 5 +- .../wulkanowy/services/sync/works/NoteWork.kt | 5 +- .../services/sync/works/RecipientWork.kt | 5 +- .../services/sync/works/TeacherWork.kt | 2 +- .../services/sync/works/TimetableWork.kt | 2 +- .../wulkanowy/services/sync/works/Work.kt | 10 +- .../github/wulkanowy/ui/base/BasePresenter.kt | 77 +++-- .../github/wulkanowy/ui/base/ErrorHandler.kt | 2 +- .../about/contributor/ContributorPresenter.kt | 19 +- .../modules/about/license/LicenseAdapter.kt | 2 +- .../modules/about/license/LicensePresenter.kt | 29 +- .../about/logviewer/LogViewerPresenter.kt | 50 ++-- .../ui/modules/account/AccountPresenter.kt | 93 +++--- .../modules/attendance/AttendancePresenter.kt | 139 +++++---- .../summary/AttendanceSummaryPresenter.kt | 102 +++---- .../ui/modules/exam/ExamPresenter.kt | 84 +++--- .../ui/modules/grade/GradeAverageProvider.kt | 78 ++--- .../ui/modules/grade/GradePresenter.kt | 52 ++-- .../grade/details/GradeDetailsPresenter.kt | 139 ++++----- .../statistics/GradeStatisticsPresenter.kt | 126 ++++---- .../grade/summary/GradeSummaryPresenter.kt | 72 +++-- .../ui/modules/homework/HomeworkFragment.kt | 4 - .../ui/modules/homework/HomeworkPresenter.kt | 89 +++--- .../homework/details/HomeworkDetailsDialog.kt | 1 - .../details/HomeworkDetailsPresenter.kt | 29 +- .../login/advanced/LoginAdvancedPresenter.kt | 65 +++-- .../modules/login/form/LoginFormPresenter.kt | 59 ++-- .../login/recover/LoginRecoverPresenter.kt | 68 ++--- .../LoginStudentSelectPresenter.kt | 92 +++--- .../login/symbol/LoginSymbolPresenter.kt | 71 +++-- .../luckynumber/LuckyNumberPresenter.kt | 82 +++--- .../LuckyNumberWidgetConfigurePresenter.kt | 37 ++- .../LuckyNumberWidgetProvider.kt | 5 +- .../ui/modules/message/MessageFragment.kt | 5 - .../ui/modules/message/MessagePresenter.kt | 26 +- .../message/preview/MessagePreviewFragment.kt | 4 - .../preview/MessagePreviewPresenter.kt | 93 +++--- .../message/preview/MessagePreviewView.kt | 2 - .../message/send/SendMessagePresenter.kt | 147 +++++----- .../message/tab/MessageTabPresenter.kt | 105 ++++--- .../mobiledevice/MobileDevicePresenter.kt | 113 ++++--- .../token/MobileDeviceTokenPresenter.kt | 48 +-- .../ui/modules/note/NotePresenter.kt | 87 +++--- .../SchoolAndTeachersPresenter.kt | 16 +- .../school/SchoolPresenter.kt | 80 ++--- .../teacher/TeacherPresenter.kt | 72 ++--- .../ui/modules/splash/SplashPresenter.kt | 20 +- .../modules/timetable/TimetablePresenter.kt | 89 +++--- .../completed/CompletedLessonsPresenter.kt | 82 +++--- .../TimetableWidgetConfigurePresenter.kt | 37 ++- .../timetablewidget/TimetableWidgetFactory.kt | 5 +- .../io/github/wulkanowy/utils/FlowUtils.kt | 93 ++++++ app/src/main/res/layout/fragment_license.xml | 4 +- .../io/github/wulkanowy/MainCoroutineRule.kt | 26 ++ .../wulkanowy/TestDispatchersProvider.kt | 11 + .../message/MessageRepositoryTest.kt | 30 +- .../MobileDeviceRepositoryTest.kt | 6 +- .../semester/SemesterRepositoryTest.kt | 3 +- .../modules/grade/GradeAverageProviderTest.kt | 275 ++++++++++-------- .../login/form/LoginFormPresenterTest.kt | 32 +- .../LoginStudentSelectPresenterTest.kt | 19 +- .../ui/modules/splash/SplashPresenterTest.kt | 7 +- 138 files changed, 2351 insertions(+), 1892 deletions(-) create mode 100644 app/src/androidTest/java/io/github/wulkanowy/data/TestDispatchersProvider.kt create mode 100644 app/src/main/java/io/github/wulkanowy/data/Resource.kt create mode 100644 app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt create mode 100644 app/src/test/java/io/github/wulkanowy/MainCoroutineRule.kt create mode 100644 app/src/test/java/io/github/wulkanowy/TestDispatchersProvider.kt diff --git a/app/build.gradle b/app/build.gradle index df9535af..d5031b31 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -92,6 +92,7 @@ android { kotlinOptions { jvmTarget = "1.8" + freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" } packagingOptions { @@ -116,7 +117,7 @@ ext { room = "2.2.5" dagger = "2.28.3" chucker = "3.2.0" - mockk = "1.9.2" + mockk = "1.10.0" } configurations.all { @@ -129,6 +130,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.7' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7' implementation "androidx.core:core-ktx:1.3.0" implementation "androidx.activity:activity-ktx:1.1.0" @@ -156,7 +158,6 @@ dependencies { implementation 'com.github.PaulinaSadowska:RxWorkManagerObservers:1.0.0' implementation "androidx.room:room-runtime:$room" - implementation "androidx.room:room-rxjava2:$room" implementation "androidx.room:room-ktx:$room" kapt "androidx.room:room-compiler:$room" @@ -199,6 +200,7 @@ dependencies { testImplementation "junit:junit:4.13" testImplementation "io.mockk:mockk:$mockk" testImplementation "org.threeten:threetenbp:1.4.4" + testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7' androidTestImplementation "androidx.test:core:1.2.0" androidTestImplementation "androidx.test:runner:1.2.0" diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/TestDispatchersProvider.kt b/app/src/androidTest/java/io/github/wulkanowy/data/TestDispatchersProvider.kt new file mode 100644 index 00000000..8c4354d9 --- /dev/null +++ b/app/src/androidTest/java/io/github/wulkanowy/data/TestDispatchersProvider.kt @@ -0,0 +1,11 @@ +package io.github.wulkanowy.data + +import io.github.wulkanowy.utils.DispatchersProvider +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +class TestDispatchersProvider : DispatchersProvider() { + + override val backgroundThread: CoroutineDispatcher + get() = Dispatchers.Unconfined +} diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt index bbbfd83d..f7aa51e4 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt @@ -1,6 +1,8 @@ package io.github.wulkanowy.data.repositories +import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDateTime fun getStudent(): Student { @@ -27,3 +29,16 @@ fun getStudent(): Student { isParent = false ) } + +fun getSemester() = Semester( + semesterId = 1, + studentId = 1, + classId = 1, + diaryId = 2, + diaryName = "", + end = now(), + schoolYear = 2019, + semesterName = 1, + start = now(), + unitId = 1 +) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt index f9326b2d..4080b831 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -53,7 +54,7 @@ class AttendanceLocalTest { 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)) } + val attendance = runBlocking { attendanceLocal.getAttendance(semester, of(2018, 9, 10), of(2018, 9, 14)).first() } assertEquals(2, attendance.size) assertEquals(attendance[0].date, of(2018, 9, 10)) assertEquals(attendance[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt index d8aac23d..f8ff9213 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -46,7 +47,7 @@ class CompletedLessonsLocalTest { 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)) } + val completed = runBlocking { completedLessonsLocal.getCompletedLessons(semester, of(2018, 9, 10), of(2018, 9, 14)).first() } assertEquals(2, completed.size) assertEquals(completed[0].date, of(2018, 9, 10)) assertEquals(completed[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt index f3b179a5..e595d77c 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -43,7 +44,7 @@ class ExamLocalTest { 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)) } + val exams = runBlocking { examLocal.getExams(semester, of(2018, 9, 10), of(2018, 9, 14)).first() } assertEquals(2, exams.size) assertEquals(exams[0].date, of(2018, 9, 10)) assertEquals(exams[1].date, of(2018, 9, 14)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt index 82129d86..6a01b09c 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt @@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -45,7 +46,7 @@ class GradeLocalTest { val semester = Semester(1, 2, "", 2019, 2, 1, now(), now(), 1, 1) - val grades = runBlocking { gradeLocal.getGradesDetails(semester) } + val grades = runBlocking { gradeLocal.getGradesDetails(semester).first() } assertEquals(2, grades.size) assertEquals(grades[0].date, LocalDate.of(2019, 2, 27)) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt index 5487fd4c..5a884530 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt @@ -5,15 +5,18 @@ import androidx.room.Room import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SdkSuppress +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.AppDatabase +import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk -import io.github.wulkanowy.sdk.pojo.Grade import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -29,15 +32,12 @@ import kotlin.test.assertTrue @RunWith(AndroidJUnit4::class) class GradeRepositoryTest { - @MockK - private lateinit var mockSdk: Sdk - @MockK private lateinit var semesterMock: Semester - @MockK private lateinit var studentMock: Student + @MockK private lateinit var gradeRemote: GradeRemote private lateinit var gradeLocal: GradeLocal @@ -49,14 +49,12 @@ class GradeRepositoryTest { MockKAnnotations.init(this) testDb = Room.inMemoryDatabaseBuilder(getApplicationContext(), AppDatabase::class.java).build() gradeLocal = GradeLocal(testDb.gradeDao, testDb.gradeSummaryDao) - gradeRemote = GradeRemote(mockSdk) + studentMock = getStudentMock() - every { studentMock.registrationDate } returns LocalDateTime.of(2019, 2, 27, 12, 0) every { semesterMock.studentId } returns 1 every { semesterMock.diaryId } returns 1 every { semesterMock.schoolYear } returns 2019 every { semesterMock.semesterId } returns 1 - every { mockSdk.switchDiary(any(), any()) } returns mockSdk } @After @@ -66,16 +64,17 @@ class GradeRepositoryTest { @Test fun markOlderThanRegisterDateAsRead() { - 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, 26), "przed zalogowanie w aplikacji"), - createGradeApi(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"), - createGradeApi(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza") + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (listOf( + createGradeLocal(5, 4.0, of(2019, 2, 25), "Ocena pojawiła się"), + createGradeLocal(5, 4.0, of(2019, 2, 26), "przed zalogowanie w aplikacji"), + createGradeLocal(5, 4.0, of(2019, 2, 27), "Ocena z dnia logowania"), + createGradeLocal(5, 4.0, of(2019, 2, 28), "Ocena jeszcze nowsza") ) to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) - .first.sortedByDescending { it.date } + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!!.first.sortedByDescending { it.date } } assertFalse { grades[0].isRead } @@ -93,16 +92,17 @@ class GradeRepositoryTest { ) runBlocking { gradeLocal.saveGrades(list) } - coEvery { mockSdk.getGrades(1) } returns (listOf( - 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(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") + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (listOf( + createGradeLocal(5, 2.0, of(2019, 2, 25), "Ocena ma datę, jest inna, ale nie zostanie powiadomiona"), + createGradeLocal(4, 3.0, of(2019, 2, 26), "starszą niż ostatnia lokalnie"), + createGradeLocal(3, 4.0, of(2019, 2, 27), "Ta jest z tego samego dnia co ostatnia lokalnie"), + createGradeLocal(2, 5.0, of(2019, 2, 28), "Ta jest już w ogóle nowa") ) to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) - .first.sortedByDescending { it.date } + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!!.first.sortedByDescending { it.date } } assertFalse { grades[0].isRead } @@ -120,13 +120,15 @@ class GradeRepositoryTest { ) runBlocking { gradeLocal.saveGrades(list) } - coEvery { mockSdk.getGrades(1) } returns (listOf( - createGradeApi(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), - createGradeApi(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (listOf( + createGradeLocal(5, 3.0, of(2019, 2, 25), "Taka sama ocena"), + createGradeLocal(3, 5.0, of(2019, 2, 26), "Jakaś inna ocena") ) to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!! } assertEquals(2, grades.first.size) @@ -140,14 +142,16 @@ class GradeRepositoryTest { ) runBlocking { gradeLocal.saveGrades(list) } - 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") + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (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") ) to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!! } assertEquals(3, grades.first.size) @@ -157,14 +161,16 @@ class GradeRepositoryTest { fun emptyLocal() { runBlocking { gradeLocal.saveGrades(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") + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (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") ) to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!! } assertEquals(3, grades.first.size) @@ -178,12 +184,37 @@ class GradeRepositoryTest { ) runBlocking { gradeLocal.saveGrades(list) } - coEvery { mockSdk.getGrades(1) } returns (emptyList() to emptyList()) + coEvery { gradeRemote.getGrades(studentMock, semesterMock) } returns (emptyList() to emptyList()) val grades = runBlocking { - GradeRepository(gradeLocal, gradeRemote).getGrades(studentMock, semesterMock, true) + GradeRepository(gradeLocal, gradeRemote) + .getGrades(studentMock, semesterMock, true) + .filter { it.status == Status.SUCCESS }.first().data!! } assertEquals(0, grades.first.size) } + + private fun getStudentMock() = Student( + scrapperBaseUrl = "http://fakelog.cf", + email = "jan@fakelog.cf", + certificateKey = "", + classId = 0, + className = "", + isCurrent = false, + isParent = false, + loginMode = Sdk.Mode.SCRAPPER.name, + loginType = "STANDARD", + mobileBaseUrl = "", + password = "", + privateKey = "", + registrationDate = LocalDateTime.of(2019, 2, 27, 12, 0), + schoolName = "", + schoolShortName = "test", + schoolSymbol = "", + studentId = 0, + studentName = "", + symbol = "", + userLoginId = 0 + ) } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt index deda67ba..ff654158 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt @@ -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.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -42,7 +43,7 @@ class GradeStatisticsLocalTest { ) runBlocking { gradeStatisticsLocal.saveGradesStatistics(list) } - val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Matematyka") } + val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false).first() } assertEquals(1, stats.size) assertEquals(stats[0].subject, "Matematyka") } @@ -56,11 +57,12 @@ class GradeStatisticsLocalTest { ) runBlocking { gradeStatisticsLocal.saveGradesStatistics(list) } - val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false, "Wszystkie") } - assertEquals(3, stats.size) - assertEquals(stats[0].subject, "Wszystkie") - assertEquals(stats[1].subject, "Matematyka") - assertEquals(stats[2].subject, "Chemia") + val stats = runBlocking { gradeStatisticsLocal.getGradesStatistics(getSemester(), false).first() } + assertEquals(2, stats.size) +// assertEquals(3, stats.size) +// assertEquals(stats[0].subject, "Wszystkie") // now in main repo + assertEquals(stats[0].subject, "Matematyka") + assertEquals(stats[1].subject, "Chemia") } @Test @@ -72,7 +74,7 @@ class GradeStatisticsLocalTest { ) runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(list) } - val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka") } + val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester()).first() } with(stats[0]) { assertEquals(subject, "Matematyka") assertEquals(others, 5.0) @@ -84,7 +86,7 @@ class GradeStatisticsLocalTest { fun saveAndRead_subjectEmpty() { runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) } - val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Matematyka") } + val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester()).first() } assertEquals(emptyList(), stats) } @@ -92,7 +94,7 @@ class GradeStatisticsLocalTest { fun saveAndRead_allEmpty() { runBlocking { gradeStatisticsLocal.saveGradesPointsStatistics(listOf()) } - val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester(), "Wszystkie") } + val stats = runBlocking { gradeStatisticsLocal.getGradesPointsStatistics(getSemester()).first() } assertEquals(emptyList(), stats) } diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt index f37d7934..dfd97394 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt @@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -41,7 +42,7 @@ class LuckyNumberLocalTest { runBlocking { luckyNumberLocal.saveLuckyNumber(number) } val student = Student("", "", "", "", "", "", false, "", "", "", 1, 1, "", "", "", "", "", 1, false, now()) - val luckyNumber = runBlocking { luckyNumberLocal.getLuckyNumber(student, LocalDate.of(2019, 1, 20)) } + val luckyNumber = runBlocking { luckyNumberLocal.getLuckyNumber(student, LocalDate.of(2019, 1, 20)).first() } assertEquals(1, luckyNumber?.studentId) assertEquals(LocalDate.of(2019, 1, 20), luckyNumber?.date) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt index 02a13344..d68f15a8 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/student/StudentLocalTest.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.room.Room import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.github.wulkanowy.data.TestDispatchersProvider import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.repositories.getStudent import kotlinx.coroutines.runBlocking @@ -27,7 +28,7 @@ class StudentLocalTest { val context = ApplicationProvider.getApplicationContext() testDb = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java) .build() - studentLocal = StudentLocal(testDb.studentDao, context) + studentLocal = StudentLocal(testDb.studentDao, TestDispatchersProvider(), context) } @After diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt index fa353a33..77d7188c 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt @@ -5,6 +5,7 @@ import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -48,7 +49,7 @@ class TimetableLocalTest { semester = semester, startDate = LocalDate.of(2018, 9, 10), endDate = LocalDate.of(2018, 9, 14) - ) + ).first() } assertEquals(2, exams.size) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt index a91651db..fa62849a 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt @@ -5,17 +5,16 @@ import androidx.room.Room import androidx.test.core.app.ApplicationProvider.getApplicationContext import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SdkSuppress +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.AppDatabase -import io.github.wulkanowy.data.db.entities.Semester -import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.data.repositories.getSemester import io.github.wulkanowy.data.repositories.getStudent -import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper import io.mockk.MockKAnnotations import io.mockk.coEvery -import io.mockk.every import io.mockk.impl.annotations.MockK -import io.mockk.mockk +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.first import kotlinx.coroutines.runBlocking import org.junit.After import org.junit.Before @@ -29,45 +28,25 @@ import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) class TimetableRepositoryTest { - @MockK - private lateinit var mockSdk: Sdk - - @MockK - private lateinit var studentMock: Student - - private val student = getStudent() - - @MockK - private lateinit var semesterMock: Semester - - @MockK + @MockK(relaxed = true) private lateinit var timetableNotificationSchedulerHelper: TimetableNotificationSchedulerHelper + @MockK private lateinit var timetableRemote: TimetableRemote private lateinit var timetableLocal: TimetableLocal private lateinit var testDb: AppDatabase + private val student = getStudent() + + private val semester = getSemester() + @Before fun initApi() { MockKAnnotations.init(this) testDb = Room.inMemoryDatabaseBuilder(getApplicationContext(), AppDatabase::class.java).build() timetableLocal = TimetableLocal(testDb.timetableDao) - timetableRemote = TimetableRemote(mockSdk) - - every { timetableNotificationSchedulerHelper.scheduleNotifications(any(), any()) } returns mockk() - every { timetableNotificationSchedulerHelper.cancelScheduled(any(), any()) } returns mockk() - - every { studentMock.studentId } returns 1 - every { studentMock.studentName } returns "Jan Kowalski" - - every { semesterMock.studentId } returns 1 - every { semesterMock.diaryId } returns 2 - every { semesterMock.schoolYear } returns 2019 - every { semesterMock.semesterId } returns 1 - - every { mockSdk.switchDiary(any(), any()) } returns mockSdk } @After @@ -86,21 +65,21 @@ class TimetableRepositoryTest { )) } - coEvery { mockSdk.getTimetable(any(), any()) } returns listOf( - createTimetableRemote(of(2019, 3, 5, 8, 0), 1, "", "Przyroda"), - createTimetableRemote(of(2019, 3, 5, 8, 50), 2, "", "Religia"), - createTimetableRemote(of(2019, 3, 5, 9, 40), 3, "", "W-F"), - createTimetableRemote(of(2019, 3, 5, 10, 30), 4, "", "W-F") + coEvery { timetableRemote.getTimetable(student, semester, any(), any()) } returns listOf( + createTimetableLocal(of(2019, 3, 5, 8, 0), 1, "", "Przyroda"), + createTimetableLocal(of(2019, 3, 5, 8, 50), 2, "", "Religia"), + createTimetableLocal(of(2019, 3, 5, 9, 40), 3, "", "W-F"), + createTimetableLocal(of(2019, 3, 5, 10, 30), 4, "", "W-F") ) val lessons = runBlocking { TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable( student = student, - semester = semesterMock, + semester = semester, start = LocalDate.of(2019, 3, 5), end = LocalDate.of(2019, 3, 5), forceRefresh = true - ) + ).filter { it.status == Status.SUCCESS }.first().data.orEmpty() } assertEquals(4, lessons.size) @@ -129,31 +108,31 @@ class TimetableRepositoryTest { ) runBlocking { timetableLocal.saveTimetable(list) } - 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, 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, 10, 40), 4, "126", "Język polski", "Joanna Wtorkowska", true), + coEvery { timetableRemote.getTimetable(student, semester, any(), any()) } returns listOf( + createTimetableLocal(of(2019, 12, 23, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false), + createTimetableLocal(of(2019, 12, 23, 8, 50), 2, "124", "Matematyka", "Jakub Wtorkowski", true), + createTimetableLocal(of(2019, 12, 23, 9, 40), 3, "125", "Język polski", "Joanna Poniedziałkowska", false), + createTimetableLocal(of(2019, 12, 23, 10, 40), 4, "126", "Język polski", "Joanna Wtorkowska", true), - createTimetableRemote(of(2019, 12, 24, 8, 0), 1, "123", "Język polski", "", false), - createTimetableRemote(of(2019, 12, 24, 8, 50), 2, "124", "Język polski", "", true), - createTimetableRemote(of(2019, 12, 24, 9, 40), 3, "125", "Język polski", "", false), - createTimetableRemote(of(2019, 12, 24, 10, 40), 4, "126", "Język polski", "", true), + createTimetableLocal(of(2019, 12, 24, 8, 0), 1, "123", "Język polski", "", false), + createTimetableLocal(of(2019, 12, 24, 8, 50), 2, "124", "Język polski", "", true), + createTimetableLocal(of(2019, 12, 24, 9, 40), 3, "125", "Język polski", "", false), + createTimetableLocal(of(2019, 12, 24, 10, 40), 4, "126", "Język polski", "", true), - createTimetableRemote(of(2019, 12, 25, 8, 0), 1, "123", "Matematyka", "Paweł Środowski", false), - 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, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true) + createTimetableLocal(of(2019, 12, 25, 8, 0), 1, "123", "Matematyka", "Paweł Środowski", false), + createTimetableLocal(of(2019, 12, 25, 8, 50), 2, "124", "Matematyka", "Paweł Czwartkowski", true), + createTimetableLocal(of(2019, 12, 25, 9, 40), 3, "125", "Matematyka", "Paweł Środowski", false), + createTimetableLocal(of(2019, 12, 25, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true) ) val lessons = runBlocking { TimetableRepository(timetableLocal, timetableRemote, timetableNotificationSchedulerHelper).getTimetable( student = student, - semester = semesterMock, + semester = semester, start = LocalDate.of(2019, 12, 23), end = LocalDate.of(2019, 12, 25), forceRefresh = true - ) + ).filter { it.status == Status.SUCCESS }.first().data.orEmpty() } assertEquals(12, lessons.size) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 802cf1ad..4ec2f781 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,8 +18,7 @@ android:supportsRtl="false" android:theme="@style/WulkanowyTheme" android:usesCleartextTraffic="true" - tools:ignore="GoogleAppIndexingWarning,UnusedAttribute" - tools:replace="android:supportsRtl,android:allowBackup"> + tools:ignore="GoogleAppIndexingWarning,UnusedAttribute"> (val status: Status, val data: T?, val error: Throwable?) { + companion object { + fun success(data: T?): Resource { + return Resource(Status.SUCCESS, data, null) + } + + fun error(error: Throwable?, data: T? = null): Resource { + return Resource(Status.ERROR, data, error) + } + + fun loading(data: T? = null): Resource { + return Resource(Status.LOADING, data, null) + } + } +} + +enum class Status { + LOADING, + SUCCESS, + ERROR +} diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt index 49527a55..96079547 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Attendance +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface AttendanceDao : BaseDao { @Query("SELECT * FROM Attendance WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") - suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List + fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt index 1ba37c95..4218855c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceSummaryDao.kt @@ -3,10 +3,11 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.AttendanceSummary +import kotlinx.coroutines.flow.Flow @Dao interface AttendanceSummaryDao : BaseDao { @Query("SELECT * FROM AttendanceSummary WHERE diary_id = :diaryId AND student_id = :studentId AND subject_id = :subjectId") - suspend fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): List + fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt index 6406d097..4a827b4f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.CompletedLesson +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface CompletedLessonsDao : BaseDao { @Query("SELECT * FROM CompletedLesson WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") - suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List + fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt index e492f7b8..e3119d9b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Exam +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface ExamDao : BaseDao { @Query("SELECT * FROM Exams WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") - suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List + fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt index df027620..12e70bde 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Grade +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,5 +11,5 @@ import javax.inject.Singleton interface GradeDao : BaseDao { @Query("SELECT * FROM Grades WHERE semester_id = :semesterId AND student_id = :studentId") - suspend fun loadAll(semesterId: Int, studentId: Int): List + fun loadAll(semesterId: Int, studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt index b1e644bb..e8074f00 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradePointsStatisticsDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradePointsStatistics +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,8 +11,8 @@ import javax.inject.Singleton interface GradePointsStatisticsDao : BaseDao { @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName") - suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): List + fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): Flow> @Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId") - suspend fun loadAll(semesterId: Int, studentId: Int): List + fun loadAll(semesterId: Int, studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt index 786da0d9..b462ad5d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeStatisticsDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradeStatistics +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,8 +11,8 @@ import javax.inject.Singleton interface GradeStatisticsDao : BaseDao { @Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName AND is_semester = :isSemester") - suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): List + fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): Flow> @Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND is_semester = :isSemester") - suspend fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): List + fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt index 02d4e922..fc9ad66e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/GradeSummaryDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.GradeSummary +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,5 +11,5 @@ import javax.inject.Singleton interface GradeSummaryDao : BaseDao { @Query("SELECT * FROM GradesSummary WHERE student_id = :studentId AND semester_id = :semesterId") - suspend fun loadAll(semesterId: Int, studentId: Int): List + fun loadAll(semesterId: Int, studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt index 9bbf80ac..5d417b04 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Homework +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface HomeworkDao : BaseDao { @Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end") - suspend fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): List + fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt index b4ead245..55a005ff 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.LuckyNumber +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface LuckyNumberDao : BaseDao { @Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date") - suspend fun load(studentId: Int, date: LocalDate): LuckyNumber + fun load(studentId: Int, date: LocalDate): Flow } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt index 2757978a..0c63624f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MessagesDao.kt @@ -5,17 +5,18 @@ import androidx.room.Query import androidx.room.Transaction import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.MessageWithAttachment +import kotlinx.coroutines.flow.Flow @Dao interface MessagesDao : BaseDao { @Transaction @Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId") - suspend fun loadMessageWithAttachment(studentId: Int, messageId: Int): MessageWithAttachment + fun loadMessageWithAttachment(studentId: Int, messageId: Int): Flow @Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder AND removed = 0 ORDER BY date DESC") - suspend fun loadAll(studentId: Int, folder: Int): List + fun loadAll(studentId: Int, folder: Int): Flow> @Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC") - suspend fun loadDeleted(studentId: Int): List + fun loadDeleted(studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt index b07aab28..8baba2c3 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/MobileDeviceDao.kt @@ -3,10 +3,11 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.MobileDevice +import kotlinx.coroutines.flow.Flow @Dao interface MobileDeviceDao : BaseDao { @Query("SELECT * FROM MobileDevices WHERE student_id = :studentId ORDER BY date DESC") - suspend fun loadAll(studentId: Int): List + fun loadAll(studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt index 81c324f6..e89a4135 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/NoteDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Note +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,5 +11,5 @@ import javax.inject.Singleton interface NoteDao : BaseDao { @Query("SELECT * FROM Notes WHERE student_id = :studentId") - suspend fun loadAll(studentId: Int): List + fun loadAll(studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt index 37cb6c50..f39791f6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SchoolDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.School +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,5 +11,5 @@ import javax.inject.Singleton interface SchoolDao : BaseDao { @Query("SELECT * FROM School WHERE student_id = :studentId AND class_id = :classId") - suspend fun load(studentId: Int, classId: Int): School? + fun load(studentId: Int, classId: Int): Flow } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt index 92477552..4cd742b5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SubjectDao.kt @@ -3,10 +3,11 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Subject +import kotlinx.coroutines.flow.Flow @Dao interface SubjectDao : BaseDao { @Query("SELECT * FROM Subjects WHERE diary_id = :diaryId AND student_id = :studentId") - suspend fun loadAll(diaryId: Int, studentId: Int): List + fun loadAll(diaryId: Int, studentId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt index 0b0e659b..6adac220 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/TeacherDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Teacher +import kotlinx.coroutines.flow.Flow import javax.inject.Singleton @Singleton @@ -10,5 +11,5 @@ import javax.inject.Singleton interface TeacherDao : BaseDao { @Query("SELECT * FROM Teachers WHERE student_id = :studentId AND class_id = :classId") - suspend fun loadAll(studentId: Int, classId: Int): List + fun loadAll(studentId: Int, classId: Int): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt index 59200b80..a099dd80 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.db.dao import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Timetable +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Singleton @@ -11,5 +12,5 @@ import javax.inject.Singleton interface TimetableDao : BaseDao { @Query("SELECT * FROM Timetable WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end") - suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List + fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Flow> } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt index 3fcd7cb5..d1956557 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/appcreator/AppCreatorRepository.kt @@ -14,12 +14,10 @@ class AppCreatorRepository @Inject constructor( private val dispatchers: DispatchersProvider ) { - suspend fun getAppCreators(): List { - return withContext(dispatchers.backgroundThread) { - Gson().fromJson( - assets.open("contributors.json").bufferedReader().use { it.readText() }, - Array::class.java - ).toList() - } + suspend fun getAppCreators() = withContext(dispatchers.backgroundThread) { + Gson().fromJson( + assets.open("contributors.json").bufferedReader().use { it.readText() }, + Array::class.java + ).toList() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt index b232033d..1e56d872 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.attendance import io.github.wulkanowy.data.db.dao.AttendanceDao import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -18,7 +19,7 @@ class AttendanceLocal @Inject constructor(private val attendanceDb: AttendanceDa attendanceDb.deleteAll(attendance) } - suspend fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): Flow> { return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt index 0fa0090e..cf4edb6a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract import org.threeten.bp.LocalDate @@ -16,19 +17,18 @@ class AttendanceRepository @Inject constructor( private val remote: AttendanceRemote ) { - suspend fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean): List { - return local.getAttendance(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { - val new = remote.getAttendance(student, semester, start.monday, end.sunday) - val old = local.getAttendance(semester, start.monday, end.sunday) + fun getAttendance(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getAttendance(semester, start.monday, end.sunday) }, + fetch = { remote.getAttendance(student, semester, start.monday, end.sunday) }, + saveFetchResult = { old, new -> + local.deleteAttendance(old uniqueSubtract new) + local.saveAttendance(new uniqueSubtract old) + }, + filterResult = { it.filter { item -> item.date in start..end } } + ) - local.deleteAttendance(old.uniqueSubtract(new)) - local.saveAttendance(new.uniqueSubtract(old)) - - local.getAttendance(semester, start.monday, end.sunday) - }.filter { it.date in start..end } - } - - suspend fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List, reason: String? = null): Boolean { - return remote.excuseAbsence(student, semester, attendanceList, reason) + suspend fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List, reason: String? = null) { + remote.excuseAbsence(student, semester, attendanceList, reason) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt index f949f016..703bc947 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.attendancesummary import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -17,7 +18,7 @@ class AttendanceSummaryLocal @Inject constructor(private val attendanceDb: Atten attendanceDb.deleteAll(attendance) } - suspend fun getAttendanceSummary(semester: Semester, subjectId: Int): List { + fun getAttendanceSummary(semester: Semester, subjectId: Int): Flow> { return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt index 7ef16fb0..5dbe1ab0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendancesummary/AttendanceSummaryRepository.kt @@ -1,8 +1,8 @@ package io.github.wulkanowy.data.repositories.attendancesummary -import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import javax.inject.Inject import javax.inject.Singleton @@ -13,15 +13,13 @@ class AttendanceSummaryRepository @Inject constructor( private val remote: AttendanceSummaryRemote ) { - suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean = false): List { - return local.getAttendanceSummary(semester, subjectId).filter { !forceRefresh }.ifEmpty { - val new = remote.getAttendanceSummary(student, semester, subjectId) - - val old = local.getAttendanceSummary(semester, subjectId) - local.deleteAttendanceSummary(old.uniqueSubtract(new)) - local.saveAttendanceSummary(new.uniqueSubtract(old)) - - return local.getAttendanceSummary(semester, subjectId) + fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getAttendanceSummary(semester, subjectId) }, + fetch = { remote.getAttendanceSummary(student, semester, subjectId) }, + saveFetchResult = { old, new -> + local.deleteAttendanceSummary(old uniqueSubtract new) + local.saveAttendanceSummary(new uniqueSubtract old) } - } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt index f355f416..f68e13cb 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.completedlessons import io.github.wulkanowy.data.db.dao.CompletedLessonsDao import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -18,7 +19,7 @@ class CompletedLessonsLocal @Inject constructor(private val completedLessonsDb: completedLessonsDb.deleteAll(completedLessons) } - suspend fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): List { + fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): Flow> { return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt index 8e81c54a..7303575e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt @@ -1,9 +1,9 @@ package io.github.wulkanowy.data.repositories.completedlessons -import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract import org.threeten.bp.LocalDate @@ -16,15 +16,14 @@ class CompletedLessonsRepository @Inject constructor( private val remote: CompletedLessonsRemote ) { - suspend fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { - return local.getCompletedLessons(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { - val new = remote.getCompletedLessons(student, semester, start.monday, end.sunday) - val old = local.getCompletedLessons(semester, start.monday, end.sunday) - - local.deleteCompleteLessons(old.uniqueSubtract(new)) - local.saveCompletedLessons(new.uniqueSubtract(old)) - - local.getCompletedLessons(semester, start.monday, end.sunday) - }.filter { it.date in start..end } - } + fun getCompletedLessons(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getCompletedLessons(semester, start.monday, end.sunday) }, + fetch = { remote.getCompletedLessons(student, semester, start.monday, end.sunday) }, + saveFetchResult = { old, new -> + local.deleteCompleteLessons(old uniqueSubtract new) + local.saveCompletedLessons(new uniqueSubtract old) + }, + filterResult = { it.filter { item -> item.date in start..end } } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt index d1888380..2b32f527 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.exam import io.github.wulkanowy.data.db.dao.ExamDao import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -10,7 +11,7 @@ import javax.inject.Singleton @Singleton class ExamLocal @Inject constructor(private val examDb: ExamDao) { - suspend fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): Flow> { return examDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt index 13af62c5..15297417 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt @@ -1,9 +1,9 @@ package io.github.wulkanowy.data.repositories.exam -import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract import org.threeten.bp.LocalDate @@ -16,15 +16,14 @@ class ExamRepository @Inject constructor( private val remote: ExamRemote ) { - suspend fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { - return local.getExams(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { - val new = remote.getExams(student, semester, start.monday, end.sunday) - val old = local.getExams(semester, start.monday, end.sunday) - - local.deleteExams(old.uniqueSubtract(new)) - local.saveExams(new.uniqueSubtract(old)) - - local.getExams(semester, start.monday, end.sunday) - }.filter { it.date in start..end } - } + fun getExams(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getExams(semester, start.monday, end.sunday) }, + fetch = { remote.getExams(student, semester, start.monday, end.sunday) }, + saveFetchResult = { old, new -> + local.deleteExams(old uniqueSubtract new) + local.saveExams(new uniqueSubtract old) + }, + filterResult = { it.filter { item -> item.date in start..end } } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt index 234fc6b8..ed363542 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeLocal.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.data.db.dao.GradeSummaryDao import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -30,7 +31,7 @@ class GradeLocal @Inject constructor( gradeSummaryDb.updateAll(gradesSummary) } - suspend fun getGradesDetails(semester: Semester): List { + fun getGradesDetails(semester: Semester): Flow> { return gradeDb.loadAll(semester.semesterId, semester.studentId) } @@ -42,7 +43,7 @@ class GradeLocal @Inject constructor( gradeSummaryDb.deleteAll(gradesSummary) } - suspend fun getGradesSummary(semester: Semester): List { + fun getGradesSummary(semester: Semester): Flow> { return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt index 6dcbb065..935cbedd 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt @@ -4,7 +4,11 @@ import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.map import org.threeten.bp.LocalDateTime import javax.inject.Inject import javax.inject.Singleton @@ -15,30 +19,30 @@ class GradeRepository @Inject constructor( private val remote: GradeRemote ) { - suspend fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): Pair, List> { - val details = local.getGradesDetails(semester) - val summaries = local.getGradesSummary(semester) - - if ((details.isNotEmpty() || summaries.isNotEmpty()) && !forceRefresh) { - return details to summaries + fun getGrades(student: Student, semester: Semester, forceRefresh: Boolean, notify: Boolean = false) = networkBoundResource( + shouldFetch = { (details, summaries) -> details.isEmpty() || summaries.isEmpty() || forceRefresh }, + query = { local.getGradesDetails(semester).combine(local.getGradesSummary(semester)) { details, summaries -> details to summaries } }, + fetch = { remote.getGrades(student, semester) }, + saveFetchResult = { old, new -> + refreshGradeDetails(student, old.first, new.first, notify) + refreshGradeSummaries(old.second, new.second, notify) } + ) - val (newDetails, newSummary) = remote.getGrades(student, semester) - val oldGrades = local.getGradesDetails(semester) - + private suspend fun refreshGradeDetails(student: Student, oldGrades: List, newDetails: List, notify: Boolean) { val notifyBreakDate = oldGrades.maxBy { it.date }?.date ?: student.registrationDate.toLocalDate() - local.deleteGrades(oldGrades.uniqueSubtract(newDetails)) - local.saveGrades(newDetails.uniqueSubtract(oldGrades).onEach { - if (it.date >= notifyBreakDate) it.apply { - isRead = false - if (notify) isNotified = false - } - }) + local.deleteGrades(oldGrades uniqueSubtract newDetails) + local.saveGrades((newDetails uniqueSubtract oldGrades).onEach { + if (it.date >= notifyBreakDate) it.apply { + isRead = false + if (notify) isNotified = false + } + }) + } - val oldSummaries = local.getGradesSummary(semester) - - local.deleteGradesSummary(oldSummaries.uniqueSubtract(newSummary)) - local.saveGradesSummary(newSummary.uniqueSubtract(oldSummaries).onEach { summary -> + private suspend fun refreshGradeSummaries(oldSummaries: List, newSummary: List, notify: Boolean) { + local.deleteGradesSummary(oldSummaries uniqueSubtract newSummary) + local.saveGradesSummary((newSummary uniqueSubtract oldSummaries).onEach { summary -> val oldSummary = oldSummaries.find { oldSummary -> oldSummary.subject == summary.subject } summary.isPredictedGradeNotified = when { summary.predictedGrade.isEmpty() -> true @@ -62,24 +66,22 @@ class GradeRepository @Inject constructor( else -> oldSummary.finalGradeLastChange } }) - - return local.getGradesDetails(semester) to local.getGradesSummary(semester) } - suspend fun getUnreadGrades(semester: Semester): List { - return local.getGradesDetails(semester).filter { grade -> !grade.isRead } + fun getUnreadGrades(semester: Semester): Flow> { + return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isRead } } } - suspend fun getNotNotifiedGrades(semester: Semester): List { - return local.getGradesDetails(semester).filter { grade -> !grade.isNotified } + fun getNotNotifiedGrades(semester: Semester): Flow> { + return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isNotified } } } - suspend fun getNotNotifiedPredictedGrades(semester: Semester): List { - return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } + fun getNotNotifiedPredictedGrades(semester: Semester): Flow> { + return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } } } - suspend fun getNotNotifiedFinalGrades(semester: Semester): List { - return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } + fun getNotNotifiedFinalGrades(semester: Semester): Flow> { + return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } } } suspend fun updateGrade(grade: Grade) { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt index d34f2b2e..e0e2cd4d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocal.kt @@ -5,6 +5,7 @@ import io.github.wulkanowy.data.db.dao.GradeStatisticsDao import io.github.wulkanowy.data.db.entities.GradePointsStatistics import io.github.wulkanowy.data.db.entities.GradeStatistics import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -14,34 +15,14 @@ class GradeStatisticsLocal @Inject constructor( private val gradePointsStatisticsDb: GradePointsStatisticsDao ) { - suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean): List { + fun getGradesStatistics(semester: Semester, isSemester: Boolean): Flow> { return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester) } - suspend fun getGradesPointsStatistics(semester: Semester): List { + fun getGradesPointsStatistics(semester: Semester): Flow> { return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId) } - suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): List { - return when (subjectName) { - "Wszystkie" -> { - val statistics = gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester) - statistics.groupBy { it.grade }.map { - GradeStatistics(semester.studentId, semester.semesterId, subjectName, it.key, - it.value.fold(0) { acc, e -> acc + e.amount }, false) - } + statistics - } - else -> gradeStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName, isSemester) - } - } - - suspend fun getGradesPointsStatistics(semester: Semester, subjectName: String): List { - return when (subjectName) { - "Wszystkie" -> gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId) - else -> gradePointsStatisticsDb.loadSubject(semester.semesterId, semester.studentId, subjectName) - } - } - suspend fun saveGradesStatistics(gradesStatistics: List) { gradeStatisticsDb.insertAll(gradesStatistics) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt index 93df6940..52ca705f 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsRepository.kt @@ -6,6 +6,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.ui.modules.grade.statistics.ViewType +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import javax.inject.Inject import javax.inject.Singleton @@ -16,29 +17,40 @@ class GradeStatisticsRepository @Inject constructor( private val remote: GradeStatisticsRemote ) { - suspend fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean = false): List { - return local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems().filter { !forceRefresh }.ifEmpty { - val new = remote.getGradeStatistics(student, semester, isSemester) - val old = local.getGradesStatistics(semester, isSemester) - - local.deleteGradesStatistics(old.uniqueSubtract(new)) - local.saveGradesStatistics(new.uniqueSubtract(old)) - - local.getGradesStatistics(semester, isSemester, subjectName).mapToStatisticItems() + fun getGradesStatistics(student: Student, semester: Semester, subjectName: String, isSemester: Boolean, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getGradesStatistics(semester, isSemester) }, + fetch = { remote.getGradeStatistics(student, semester, isSemester) }, + saveFetchResult = { old, new -> + local.deleteGradesStatistics(old uniqueSubtract new) + local.saveGradesStatistics(new uniqueSubtract old) + }, + mapResult = { items -> + when (subjectName) { + "Wszystkie" -> items.groupBy { it.grade }.map { + GradeStatistics(semester.studentId, semester.semesterId, subjectName, it.key, + it.value.fold(0) { acc, e -> acc + e.amount }, false) + } + items + else -> items.filter { it.subject == subjectName } + }.mapToStatisticItems() } - } + ) - suspend fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): List { - return local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem().filter { !forceRefresh }.ifEmpty { - val new = remote.getGradePointsStatistics(student, semester) - val old = local.getGradesPointsStatistics(semester) - - local.deleteGradesPointsStatistics(old.uniqueSubtract(new)) - local.saveGradesPointsStatistics(new.uniqueSubtract(old)) - - local.getGradesPointsStatistics(semester, subjectName).mapToStatisticsItem() + fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getGradesPointsStatistics(semester) }, + fetch = { remote.getGradePointsStatistics(student, semester) }, + saveFetchResult = { old, new -> + local.deleteGradesPointsStatistics(old uniqueSubtract new) + local.saveGradesPointsStatistics(new uniqueSubtract old) + }, + mapResult = { items -> + when (subjectName) { + "Wszystkie" -> items + else -> items.filter { it.subject == subjectName } + }.mapToStatisticsItem() } - } + ) private fun List.mapToStatisticItems() = groupBy { it.subject }.map { GradeStatisticsItem( diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt index ed6bb0cf..5373e1b1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.homework import io.github.wulkanowy.data.db.dao.HomeworkDao import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -22,7 +23,7 @@ class HomeworkLocal @Inject constructor(private val homeworkDb: HomeworkDao) { homeworkDb.updateAll(homework) } - suspend fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Flow> { return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt index ca0a84a5..5b6aeed4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract import org.threeten.bp.LocalDate @@ -16,18 +17,15 @@ class HomeworkRepository @Inject constructor( private val remote: HomeworkRemote ) { - suspend fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { - return local.getHomework(semester, start.monday, end.sunday).filter { !forceRefresh }.ifEmpty { - val new = remote.getHomework(student, semester, start.monday, end.sunday) - - val old = local.getHomework(semester, start.monday, end.sunday) - - local.deleteHomework(old.uniqueSubtract(new)) - local.saveHomework(new.uniqueSubtract(old)) - - local.getHomework(semester, start.monday, end.sunday) + fun getHomework(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getHomework(semester, start.monday, end.sunday) }, + fetch = { remote.getHomework(student, semester, start.monday, end.sunday) }, + saveFetchResult = { old, new -> + local.deleteHomework(old uniqueSubtract new) + local.saveHomework(new uniqueSubtract old) } - } + ) suspend fun toggleDone(homework: Homework) { local.updateHomework(listOf(homework.apply { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt index d03d3ccd..e50955e2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt @@ -12,16 +12,12 @@ class LoggerRepository @Inject constructor( private val dispatchers: DispatchersProvider ) { - suspend fun getLastLogLines(): List { - return getLastModified().readText().split("\n") - } + suspend fun getLastLogLines() = getLastModified().readText().split("\n") - suspend fun getLogFiles(): List { - return withContext(dispatchers.backgroundThread) { - File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter { - it.name.endsWith(".log") - }!! - } + suspend fun getLogFiles() = withContext(dispatchers.backgroundThread) { + File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter { + it.name.endsWith(".log") + }!! } private suspend fun getLastModified(): File { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt index 22b5786d..ecc784c4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.luckynumber import io.github.wulkanowy.data.db.dao.LuckyNumberDao import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -22,7 +23,7 @@ class LuckyNumberLocal @Inject constructor(private val luckyNumberDb: LuckyNumbe luckyNumberDb.deleteAll(listOfNotNull(luckyNumber)) } - suspend fun getLuckyNumber(student: Student, date: LocalDate): LuckyNumber? { + fun getLuckyNumber(student: Student, date: LocalDate): Flow { return luckyNumberDb.load(student.studentId, date) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt index 3f608962..3553a461 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt @@ -2,6 +2,8 @@ package io.github.wulkanowy.data.repositories.luckynumber import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.networkBoundResource +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate.now import javax.inject.Inject import javax.inject.Singleton @@ -12,31 +14,25 @@ class LuckyNumberRepository @Inject constructor( private val remote: LuckyNumberRemote ) { - suspend fun getLuckyNumber(student: Student, forceRefresh: Boolean = false, notify: Boolean = false): LuckyNumber? { - return local.getLuckyNumber(student, now())?.takeIf { !forceRefresh } ?: run { - val new = remote.getLuckyNumber(student) - val old = local.getLuckyNumber(student, now()) - + fun getLuckyNumber(student: Student, forceRefresh: Boolean, notify: Boolean = false) = networkBoundResource( + shouldFetch = { it == null || forceRefresh }, + query = { local.getLuckyNumber(student, now()) }, + fetch = { remote.getLuckyNumber(student) }, + saveFetchResult = { old, new -> if (new != old) { old?.let { local.deleteLuckyNumber(it) } local.saveLuckyNumber(new?.apply { if (notify) isNotified = false }) } - - local.saveLuckyNumber(new?.apply { - if (notify) isNotified = false - }) - - local.getLuckyNumber(student, now()) } - } + ) - suspend fun getNotNotifiedLuckyNumber(student: Student): LuckyNumber? { + fun getNotNotifiedLuckyNumber(student: Student): Flow { return local.getLuckyNumber(student, now()) } - suspend fun updateLuckyNumber(luckyNumber: LuckyNumber) { + suspend fun updateLuckyNumber(luckyNumber: LuckyNumber?) { local.updateLuckyNumber(luckyNumber) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt index f05c49d8..01231efe 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageLocal.kt @@ -7,6 +7,7 @@ import io.github.wulkanowy.data.db.entities.MessageAttachment import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -28,7 +29,7 @@ class MessageLocal @Inject constructor( messagesDb.deleteAll(messages) } - suspend fun getMessageWithAttachment(student: Student, message: Message): MessageWithAttachment { + fun getMessageWithAttachment(student: Student, message: Message): Flow { return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId) } @@ -36,7 +37,7 @@ class MessageLocal @Inject constructor( messageAttachmentDao.insertAttachments(attachments) } - suspend fun getMessages(student: Student, folder: MessageFolder): List { + fun getMessages(student: Student, folder: MessageFolder): Flow> { return when (folder) { TRASHED -> messagesDb.loadDeleted(student.id.toInt()) else -> messagesDb.loadAll(student.id.toInt(), folder.id) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt index 7138566b..ea46b687 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRepository.kt @@ -1,13 +1,15 @@ package io.github.wulkanowy.data.repositories.message import io.github.wulkanowy.data.db.entities.Message -import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.Recipient import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED import io.github.wulkanowy.sdk.pojo.SentMessage +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map import timber.log.Timber import javax.inject.Inject import javax.inject.Singleton @@ -18,46 +20,37 @@ class MessageRepository @Inject constructor( private val remote: MessageRemote ) { - suspend fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean = false, notify: Boolean = false): List { - return local.getMessages(student, folder).filter { !forceRefresh }.ifEmpty { - val new = remote.getMessages(student, semester, folder) - val old = local.getMessages(student, folder) - - local.deleteMessages(old.uniqueSubtract(new)) - local.saveMessages(new.uniqueSubtract(old).onEach { + fun getMessages(student: Student, semester: Semester, folder: MessageFolder, forceRefresh: Boolean, notify: Boolean = false) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getMessages(student, folder) }, + fetch = { remote.getMessages(student, semester, folder) }, + saveFetchResult = { old, new -> + local.deleteMessages(old uniqueSubtract new) + local.saveMessages((new uniqueSubtract old).onEach { it.isNotified = !notify }) - - local.getMessages(student, folder) } - } + ) - suspend fun getMessage(student: Student, message: Message, markAsRead: Boolean = false): MessageWithAttachment { - return local.getMessageWithAttachment(student, message).let { - if (it.message.content.isNotEmpty().also { status -> - Timber.d("Message content in db empty: ${!status}") - } && !it.message.unread) { - return@let it - } - - val dbMessage = local.getMessageWithAttachment(student, message) - - val (downloadedMessage, attachments) = remote.getMessagesContentDetails(student, dbMessage.message, markAsRead) - - local.updateMessages(listOf(dbMessage.message.copy(unread = !markAsRead).apply { - id = dbMessage.message.id + fun getMessage(student: Student, message: Message, markAsRead: Boolean = false) = networkBoundResource( + shouldFetch = { + Timber.d("Message content in db empty: ${it.message.content.isEmpty()}") + it.message.unread || it.message.content.isEmpty() + }, + query = { local.getMessageWithAttachment(student, message) }, + fetch = { remote.getMessagesContentDetails(student, it.message, markAsRead) }, + saveFetchResult = { old, (downloadedMessage, attachments) -> + local.updateMessages(listOf(old.message.copy(unread = !markAsRead).apply { + id = old.message.id content = content.ifBlank { downloadedMessage } })) local.saveMessageAttachments(attachments) - Timber.d("Message ${message.messageId} with blank content: ${dbMessage.message.content.isBlank()}, marked as read") - - local.getMessageWithAttachment(student, message) + Timber.d("Message ${message.messageId} with blank content: ${old.message.content.isBlank()}, marked as read") } - } + ) - suspend fun getNotNotifiedMessages(student: Student): List { - return local.getMessages(student, RECEIVED) - .filter { message -> !message.isNotified && message.unread } + fun getNotNotifiedMessages(student: Student): Flow> { + return local.getMessages(student, RECEIVED).map { it.filter { message -> !message.isNotified && message.unread } } } suspend fun updateMessages(messages: List) { @@ -68,15 +61,12 @@ class MessageRepository @Inject constructor( return remote.sendMessage(student, subject, content, recipients) } - suspend fun deleteMessage(student: Student, message: Message): Boolean { - val delete = remote.deleteMessage(student, message) + suspend fun deleteMessage(student: Student, message: Message) { + val isDeleted = remote.deleteMessage(student, message) - if (!message.removed) local.updateMessages(listOf(message.copy(removed = true).apply { + if (!message.removed) local.updateMessages(listOf(message.copy(removed = isDeleted).apply { id = message.id content = message.content - })) - else local.deleteMessages(listOf(message)) - - return delete // TODO: wtf + })) else local.deleteMessages(listOf(message)) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt index 911ed3af..0ccb3d7e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.mobiledevice import io.github.wulkanowy.data.db.dao.MobileDeviceDao import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -17,7 +18,7 @@ class MobileDeviceLocal @Inject constructor(private val mobileDb: MobileDeviceDa mobileDb.deleteAll(devices) } - suspend fun getDevices(semester: Semester): List { + fun getDevices(semester: Semester): Flow> { return mobileDb.loadAll(semester.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt index f327ef60..65526ef8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepository.kt @@ -4,6 +4,7 @@ import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.pojos.MobileDeviceToken +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import javax.inject.Inject import javax.inject.Singleton @@ -14,20 +15,19 @@ class MobileDeviceRepository @Inject constructor( private val remote: MobileDeviceRemote ) { - suspend fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean = false): List { - return local.getDevices(semester).filter { !forceRefresh }.ifEmpty { - val new = remote.getDevices(student, semester) - val old = local.getDevices(semester) - + fun getDevices(student: Student, semester: Semester, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getDevices(semester) }, + fetch = { remote.getDevices(student, semester) }, + saveFetchResult = { old, new -> local.deleteDevices(old uniqueSubtract new) local.saveDevices(new uniqueSubtract old) - - local.getDevices(semester) } - } + ) - suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice): Boolean { - return remote.unregisterDevice(student, semester, device) + suspend fun unregisterDevice(student: Student, semester: Semester, device: MobileDevice) { + remote.unregisterDevice(student, semester, device) + local.deleteDevices(listOf(device)) } suspend fun getToken(student: Student, semester: Semester): MobileDeviceToken { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt index b1c6b290..85ba5e22 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.note import io.github.wulkanowy.data.db.dao.NoteDao import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Student +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @@ -21,7 +22,7 @@ class NoteLocal @Inject constructor(private val noteDb: NoteDao) { noteDb.deleteAll(notes) } - suspend fun getNotes(student: Student): List { + fun getNotes(student: Student): Flow> { return noteDb.loadAll(student.studentId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt index 3628f5b8..6cf62ba2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/note/NoteRepository.kt @@ -3,7 +3,10 @@ package io.github.wulkanowy.data.repositories.note import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map import javax.inject.Inject import javax.inject.Singleton @@ -13,29 +16,27 @@ class NoteRepository @Inject constructor( private val remote: NoteRemote ) { - suspend fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean = false, notify: Boolean = false): List { - return local.getNotes(student).filter { !forceRefresh }.ifEmpty { - val new = remote.getNotes(student, semester) - val old = local.getNotes(student) - - local.deleteNotes(old.uniqueSubtract(new)) - local.saveNotes(new.uniqueSubtract(old).onEach { + fun getNotes(student: Student, semester: Semester, forceRefresh: Boolean, notify: Boolean = false) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getNotes(student) }, + fetch = { remote.getNotes(student, semester) }, + saveFetchResult = { old, new -> + local.deleteNotes(old uniqueSubtract new) + local.saveNotes((new uniqueSubtract old).onEach { if (it.date >= student.registrationDate.toLocalDate()) it.apply { isRead = false if (notify) isNotified = false } }) - - local.getNotes(student) } - } + ) - suspend fun getNotNotifiedNotes(student: Student): List { - return local.getNotes(student).filter { note -> !note.isNotified } + fun getNotNotifiedNotes(student: Student): Flow> { + return local.getNotes(student).map { it.filter { note -> !note.isNotified } } } suspend fun updateNote(note: Note) { - return local.updateNotes(listOf(note)) + local.updateNotes(listOf(note)) } suspend fun updateNotes(notes: List) { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt index 5c16c57b..f5e876b0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/recipient/RecipientRepository.kt @@ -14,13 +14,17 @@ class RecipientRepository @Inject constructor( private val remote: RecipientRemote ) { - suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit, forceRefresh: Boolean = false): List { - return local.getRecipients(student, role, unit).filter { !forceRefresh }.ifEmpty { - val new = remote.getRecipients(student, role, unit) - val old = local.getRecipients(student, role, unit) + suspend fun refreshRecipients(student: Student, role: Int, unit: ReportingUnit) { + val new = remote.getRecipients(student, role, unit) + val old = local.getRecipients(student, role, unit) - local.deleteRecipients(old.uniqueSubtract(new)) - local.saveRecipients(new.uniqueSubtract(old)) + local.deleteRecipients(old uniqueSubtract new) + local.saveRecipients(new uniqueSubtract old) + } + + suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List { + return local.getRecipients(student, role, unit).ifEmpty { + refreshRecipients(student, role, unit) local.getRecipients(student, role, unit) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt index 70aefb9f..ff583946 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/reportingunit/ReportingUnitRepository.kt @@ -12,23 +12,27 @@ class ReportingUnitRepository @Inject constructor( private val remote: ReportingUnitRemote ) { - suspend fun getReportingUnits(student: Student, forceRefresh: Boolean = false): List { - return local.getReportingUnits(student).filter { !forceRefresh }.ifEmpty { - val new = remote.getReportingUnits(student) - val old = local.getReportingUnits(student) + suspend fun refreshReportingUnits(student: Student) { + val new = remote.getReportingUnits(student) + val old = local.getReportingUnits(student) - local.deleteReportingUnits(old.uniqueSubtract(new)) - local.saveReportingUnits(new.uniqueSubtract(old)) + local.deleteReportingUnits(old.uniqueSubtract(new)) + local.saveReportingUnits(new.uniqueSubtract(old)) + } + + suspend fun getReportingUnits(student: Student): List { + return local.getReportingUnits(student).ifEmpty { + refreshReportingUnits(student) local.getReportingUnits(student) } } - suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit { + suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit? { return local.getReportingUnit(student, unitId) ?: run { - getReportingUnits(student, true) + refreshReportingUnits(student) - return local.getReportingUnit(student, unitId)!! + return local.getReportingUnit(student, unitId) } } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt index c8479b8f..bc1b2f44 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.school import io.github.wulkanowy.data.db.dao.SchoolDao import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.data.db.entities.Semester +import kotlinx.coroutines.flow.Flow import javax.inject.Inject class SchoolLocal @Inject constructor(private val schoolDb: SchoolDao) { @@ -15,7 +16,7 @@ class SchoolLocal @Inject constructor(private val schoolDb: SchoolDao) { schoolDb.deleteAll(listOf(school)) } - suspend fun getSchool(semester: Semester): School? { + fun getSchool(semester: Semester): Flow { return schoolDb.load(semester.studentId, semester.classId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt index 9ca945d0..4c84c319 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/school/SchoolRepository.kt @@ -1,8 +1,8 @@ package io.github.wulkanowy.data.repositories.school -import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student +import io.github.wulkanowy.utils.networkBoundResource import javax.inject.Inject import javax.inject.Singleton @@ -12,18 +12,16 @@ class SchoolRepository @Inject constructor( private val remote: SchoolRemote ) { - suspend fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean = false): School { - return local.getSchool(semester).takeIf { it != null && !forceRefresh } ?: run { - val new = remote.getSchoolInfo(student, semester) - val old = local.getSchool(semester) - + fun getSchoolInfo(student: Student, semester: Semester, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it == null || forceRefresh }, + query = { local.getSchool(semester) }, + fetch = { remote.getSchoolInfo(student, semester) }, + saveFetchResult = { old, new -> if (new != old && old != null) { local.deleteSchool(old) local.saveSchool(new) } local.saveSchool(new) - - local.getSchool(semester)!! } - } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt index aeb42400..28d37ed8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/semester/SemesterRepository.kt @@ -1,22 +1,24 @@ package io.github.wulkanowy.data.repositories.semester -import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk +import io.github.wulkanowy.utils.DispatchersProvider import io.github.wulkanowy.utils.getCurrentOrLast import io.github.wulkanowy.utils.isCurrent import io.github.wulkanowy.utils.uniqueSubtract +import kotlinx.coroutines.withContext import javax.inject.Inject import javax.inject.Singleton @Singleton class SemesterRepository @Inject constructor( private val remote: SemesterRemote, - private val local: SemesterLocal + private val local: SemesterLocal, + private val dispatchers: DispatchersProvider ) { - suspend fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): List { - return local.getSemesters(student).let { semesters -> + suspend fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false) = withContext(dispatchers.backgroundThread) { + local.getSemesters(student).let { semesters -> semesters.filter { !forceRefresh && when { Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0 @@ -36,7 +38,7 @@ class SemesterRepository @Inject constructor( } } - suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Semester { - return getSemesters(student, forceRefresh).getCurrentOrLast() + suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false) = withContext(dispatchers.backgroundThread) { + getSemesters(student, forceRefresh).getCurrentOrLast() } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt index 5a4322f3..3a964aae 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt @@ -4,52 +4,55 @@ import android.content.Context import io.github.wulkanowy.data.db.dao.StudentDao import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk +import io.github.wulkanowy.utils.DispatchersProvider import io.github.wulkanowy.utils.security.decrypt import io.github.wulkanowy.utils.security.encrypt +import kotlinx.coroutines.withContext import javax.inject.Inject import javax.inject.Singleton @Singleton class StudentLocal @Inject constructor( private val studentDb: StudentDao, + private val dispatchers: DispatchersProvider, private val context: Context ) { - suspend fun saveStudents(students: List): List { - return studentDb.insertAll(students.map { + suspend fun saveStudents(students: List) = withContext(dispatchers.backgroundThread) { + studentDb.insertAll(students.map { if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) it.copy(password = encrypt(it.password, context)) else it }) } - suspend fun getStudents(decryptPass: Boolean): List { - return studentDb.loadAll().map { + suspend fun getStudents(decryptPass: Boolean) = withContext(dispatchers.backgroundThread) { + studentDb.loadAll().map { it.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } } - suspend fun getStudentById(id: Int): Student? { - return studentDb.loadById(id)?.apply { + suspend fun getStudentById(id: Int) = withContext(dispatchers.backgroundThread) { + studentDb.loadById(id)?.apply { if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } - suspend fun getCurrentStudent(decryptPass: Boolean): Student? { - return studentDb.loadCurrent()?.apply { + suspend fun getCurrentStudent(decryptPass: Boolean) = withContext(dispatchers.backgroundThread) { + studentDb.loadCurrent()?.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } - suspend fun setCurrentStudent(student: Student) { - return studentDb.run { + suspend fun setCurrentStudent(student: Student) = withContext(dispatchers.backgroundThread) { + studentDb.run { resetCurrent() updateCurrent(student.id) } } - suspend fun logoutStudent(student: Student) { - return studentDb.delete(student) + suspend fun logoutStudent(student: Student) = withContext(dispatchers.backgroundThread) { + studentDb.delete(student) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt index 1f9dfff3..e225a381 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectLocal.kt @@ -3,13 +3,14 @@ package io.github.wulkanowy.data.repositories.subject import io.github.wulkanowy.data.db.dao.SubjectDao import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Subject +import kotlinx.coroutines.flow.Flow import javax.inject.Inject import javax.inject.Singleton @Singleton class SubjectLocal @Inject constructor(private val subjectDao: SubjectDao) { - suspend fun getSubjects(semester: Semester): List { + fun getSubjects(semester: Semester): Flow> { return subjectDao.loadAll(semester.diaryId, semester.studentId) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt index 646e3642..60a0c3e7 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/subject/SubjectRepository.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.data.repositories.subject import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.github.wulkanowy.data.db.entities.Subject +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import javax.inject.Inject import javax.inject.Singleton @@ -13,15 +13,13 @@ class SubjectRepository @Inject constructor( private val remote: SubjectRemote ) { - suspend fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false): List { - return local.getSubjects(semester).filter { !forceRefresh }.ifEmpty { - val new = remote.getSubjects(student, semester) - val old = local.getSubjects(semester) - - local.deleteSubjects(old.uniqueSubtract(new)) - local.saveSubjects(new.uniqueSubtract(old)) - - local.getSubjects(semester) + fun getSubjects(student: Student, semester: Semester, forceRefresh: Boolean = false) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getSubjects(semester) }, + fetch = { remote.getSubjects(student, semester) }, + saveFetchResult = { old, new -> + local.deleteSubjects(old uniqueSubtract new) + local.saveSubjects(new uniqueSubtract old) } - } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt index 53680b7b..908f45a1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.teacher import io.github.wulkanowy.data.db.dao.TeacherDao import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Teacher +import kotlinx.coroutines.flow.Flow import javax.inject.Inject class TeacherLocal @Inject constructor(private val teacherDb: TeacherDao) { @@ -15,7 +16,7 @@ class TeacherLocal @Inject constructor(private val teacherDb: TeacherDao) { teacherDb.deleteAll(teachers) } - suspend fun getTeachers(semester: Semester): List { + fun getTeachers(semester: Semester): Flow> { return teacherDb.loadAll(semester.studentId, semester.classId) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt index a540e78c..df25a53e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/teacher/TeacherRepository.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.data.repositories.teacher import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.github.wulkanowy.data.db.entities.Teacher +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.uniqueSubtract import javax.inject.Inject import javax.inject.Singleton @@ -13,15 +13,13 @@ class TeacherRepository @Inject constructor( private val remote: TeacherRemote ) { - suspend fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean = false): List { - return local.getTeachers(semester).filter { !forceRefresh }.ifEmpty { - val new = remote.getTeachers(student, semester) - val old = local.getTeachers(semester) - - local.deleteTeachers(old.uniqueSubtract(new)) - local.saveTeachers(new.uniqueSubtract(old)) - - local.getTeachers(semester) + fun getTeachers(student: Student, semester: Semester, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getTeachers(semester) }, + fetch = { remote.getTeachers(student, semester) }, + saveFetchResult = { old, new -> + local.deleteTeachers(old uniqueSubtract new) + local.saveTeachers(new uniqueSubtract old) } - } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt index a90c664c..91a4b261 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories.timetable import io.github.wulkanowy.data.db.dao.TimetableDao import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Timetable +import kotlinx.coroutines.flow.Flow import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -18,7 +19,7 @@ class TimetableLocal @Inject constructor(private val timetableDb: TimetableDao) timetableDb.deleteAll(timetables) } - suspend fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): List { + fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): Flow> { return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt index b3407552..54ddf10b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt @@ -2,11 +2,12 @@ package io.github.wulkanowy.data.repositories.timetable import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.services.alarm.TimetableNotificationSchedulerHelper import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract +import kotlinx.coroutines.flow.map import org.threeten.bp.LocalDate import javax.inject.Inject import javax.inject.Singleton @@ -18,11 +19,11 @@ class TimetableRepository @Inject constructor( private val schedulerHelper: TimetableNotificationSchedulerHelper ) { - suspend fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean = false): List { - return local.getTimetable(semester, start.monday, start.sunday).filter { !forceRefresh }.ifEmpty { - val new = remote.getTimetable(student, semester, start.monday, start.sunday) - val old = local.getTimetable(semester, start.monday, start.sunday) - + fun getTimetable(student: Student, semester: Semester, start: LocalDate, end: LocalDate, forceRefresh: Boolean) = networkBoundResource( + shouldFetch = { it.isEmpty() || forceRefresh }, + query = { local.getTimetable(semester, start.monday, end.sunday).map { schedulerHelper.scheduleNotifications(it, student); it } }, + fetch = { remote.getTimetable(student, semester, start.monday, end.sunday) }, + saveFetchResult = { old, new -> local.deleteTimetable(old.uniqueSubtract(new).also { schedulerHelper.cancelScheduled(it) }) local.saveTimetable(new.uniqueSubtract(old).also { schedulerHelper.scheduleNotifications(it, student) }.map { item -> item.also { new -> @@ -34,8 +35,7 @@ class TimetableRepository @Inject constructor( } } }) - - local.getTimetable(semester, start.monday, start.sunday) - }.filter { it.date in start..end }.also { schedulerHelper.scheduleNotifications(it, student) } - } + }, + filterResult = { it.filter { item -> item.date in start..end } } + ) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt index f4333f33..3d118e6f 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt @@ -12,7 +12,7 @@ class AttendanceSummaryWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true) } + return rxCompletable { attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt index 069b6c8f..23cb1acd 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.attendance.AttendanceRepository -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now @@ -13,6 +13,6 @@ import javax.inject.Inject class AttendanceWork @Inject constructor(private val attendanceRepository: AttendanceRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true) } + return rxCompletable { attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt index 8914fd36..347d8bbb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.completedlessons.CompletedLessonsRepository -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now @@ -15,7 +15,6 @@ class CompletedLessonWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true) } + return rxCompletable { completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true).waitForResult() } } } - diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt index 0a451295..a8c7fdb3 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt @@ -13,6 +13,6 @@ import javax.inject.Inject class ExamWork @Inject constructor(private val examRepository: ExamRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { examRepository.getExams(student, semester, now().monday, now().sunday, true) } + return rxCompletable { examRepository.getExams(student, semester, now().monday, now().sunday, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt index 0a1a9eee..cbfa2d9a 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt @@ -7,10 +7,17 @@ import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable import javax.inject.Inject -class GradeStatisticsWork @Inject constructor(private val gradeStatisticsRepository: GradeStatisticsRepository) : Work { +class GradeStatisticsWork @Inject constructor( + private val gradeStatisticsRepository: GradeStatisticsRepository +) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { gradeStatisticsRepository.getGradesStatistics(student, semester, "Wszystkie", false, forceRefresh = true) } + return rxCompletable { + with(gradeStatisticsRepository) { + getGradesStatistics(student, semester, "Wszystkie", isSemester = true, forceRefresh = true).waitForResult() + getGradesStatistics(student, semester, "Wszystkie", isSemester = false, forceRefresh = true).waitForResult() + getGradesPointsStatistics(student, semester, "Wszystkie", forceRefresh = true).waitForResult() + } + } } } - diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt index 252966d6..74180993 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt @@ -19,6 +19,7 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.flow.first import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject @@ -32,14 +33,14 @@ class GradeWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable) } - .concatWith(Completable.concatArray(rxSingle { gradeRepository.getNotNotifiedGrades(semester) }.flatMapCompletable { + return rxCompletable { gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() } + .concatWith(Completable.concatArray(rxSingle { gradeRepository.getNotNotifiedGrades(semester).first() }.flatMapCompletable { if (it.isNotEmpty()) notifyDetails(it) rxCompletable { gradeRepository.updateGrades(it.onEach { grade -> grade.isNotified = true }) } - }, rxSingle { gradeRepository.getNotNotifiedPredictedGrades(semester) }.flatMapCompletable { + }, rxSingle { gradeRepository.getNotNotifiedPredictedGrades(semester).first() }.flatMapCompletable { if (it.isNotEmpty()) notifyPredicted(it) rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isPredictedGradeNotified = true }) } - }, rxSingle { gradeRepository.getNotNotifiedFinalGrades(semester) }.flatMapCompletable { + }, rxSingle { gradeRepository.getNotNotifiedFinalGrades(semester).first() }.flatMapCompletable { if (it.isNotEmpty()) notifyFinal(it) rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isFinalGradeNotified = true }) } })) diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt index 2bf5315a..8b69b7b6 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.homework.HomeworkRepository -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable import org.threeten.bp.LocalDate.now @@ -13,6 +13,6 @@ import javax.inject.Inject class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true) } + return rxCompletable { homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index 1389566b..9bf3de0c 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -18,6 +18,7 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.flow.first import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxMaybe import javax.inject.Inject @@ -31,8 +32,8 @@ class LuckyNumberWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxMaybe { luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable) } - .flatMap { rxMaybe { luckyNumberRepository.getNotNotifiedLuckyNumber(student) } } + return rxMaybe { luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable).waitForResult() } + .flatMap { rxMaybe { luckyNumberRepository.getNotNotifiedLuckyNumber(student).first() } } .flatMapCompletable { notify(it) rxCompletable { luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt index a805fe6b..28f6e307 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt @@ -19,6 +19,7 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.flow.first import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject @@ -32,8 +33,8 @@ class MessageWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxSingle { messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable) } - .flatMap { rxSingle { messageRepository.getNotNotifiedMessages(student) } } + return rxSingle { messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable).waitForResult() } + .flatMap { rxSingle { messageRepository.getNotNotifiedMessages(student).first() } } .flatMapCompletable { if (it.isNotEmpty()) notify(it) rxCompletable { messageRepository.updateMessages(it.onEach { message -> message.isNotified = true }) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt index f33c6402..029c9f98 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt @@ -18,6 +18,7 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.reactivex.Completable +import kotlinx.coroutines.flow.first import kotlinx.coroutines.rx2.rxCompletable import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject @@ -31,8 +32,8 @@ class NoteWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxSingle { noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable) } - .flatMap { rxSingle { noteRepository.getNotNotifiedNotes(student) } } + return rxSingle { noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() } + .flatMap { rxSingle { noteRepository.getNotNotifiedNotes(student).first() } } .flatMapCompletable { if (it.isNotEmpty()) notify(it) rxCompletable { noteRepository.updateNotes(it.onEach { note -> note.isNotified = true }) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt index 70415098..2a53e51b 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt @@ -15,10 +15,11 @@ class RecipientWork @Inject constructor( ) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxSingle { reportingUnitRepository.getReportingUnits(student, true) } + return rxSingle { reportingUnitRepository.refreshReportingUnits(student) } + .flatMap { rxSingle { reportingUnitRepository.getReportingUnits(student) } } .flatMapCompletable { units -> Completable.mergeDelayError(units.map { - rxCompletable { recipientRepository.getRecipients(student, 2, it, true) } + rxCompletable { recipientRepository.refreshRecipients(student, 2, it) } }) } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt index f3ebf9ee..9954db31 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt @@ -10,6 +10,6 @@ import javax.inject.Inject class TeacherWork @Inject constructor(private val teacherRepository: TeacherRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { teacherRepository.getTeachers(student, semester, true) } + return rxCompletable { teacherRepository.getTeachers(student, semester, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt index 7d6438d7..f7701856 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt @@ -13,6 +13,6 @@ import javax.inject.Inject class TimetableWork @Inject constructor(private val timetableRepository: TimetableRepository) : Work { override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true) } + return rxCompletable { timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt index 1601a103..8dc0b98d 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt @@ -1,11 +1,19 @@ package io.github.wulkanowy.services.sync.works +import io.github.wulkanowy.data.Resource +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.reactivex.Completable +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.takeWhile interface Work { fun create(student: Student, semester: Semester): Completable -} + suspend fun Flow>.waitForResult() = takeWhile { + it.status == Status.LOADING + }.collect() +} diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt index d9dbc362..b0fc0f6b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt @@ -1,24 +1,39 @@ package io.github.wulkanowy.ui.base +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Completable +import io.github.wulkanowy.utils.flowWithResource import io.reactivex.disposables.CompositeDisposable -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber +import kotlin.coroutines.CoroutineContext open class BasePresenter( protected val errorHandler: ErrorHandler, protected val studentRepository: StudentRepository, protected val schedulers: SchedulersProvider -) { +) : CoroutineScope { + private var job: Job = Job() + + private val jobs = mutableMapOf() + + override val coroutineContext: CoroutineContext + get() = Dispatchers.Main + job + + @Deprecated("Use flow instead :)") val disposable = CompositeDisposable() var view: T? = null open fun onAttachView(view: T) { + job = Job() this.view = view errorHandler.apply { showErrorMessage = view::showError @@ -28,30 +43,48 @@ open class BasePresenter( } fun onExpiredLoginSelected() { - Timber.i("Attempt to switch the student after the session expires") - disposable.add(rxSingle { studentRepository.getCurrentStudent(false) } - .flatMapCompletable { rxCompletable { studentRepository.logoutStudent(it) } } - .andThen(rxSingle { studentRepository.getSavedStudents(false) }) - .flatMapCompletable { - if (it.isNotEmpty()) { - Timber.i("Switching current student") - rxCompletable { studentRepository.switchStudent(it[0]) } - } else Completable.complete() + flowWithResource { + val student = studentRepository.getCurrentStudent(false) + studentRepository.logoutStudent(student) + + val students = studentRepository.getSavedStudents(false) + if (students.isNotEmpty()) { + Timber.i("Switching current student") + studentRepository.switchStudent(students[0]) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Switch student result: Open login view") - view?.openClearLoginView() - }, { - Timber.i("Switch student result: An exception occurred") - errorHandler.dispatch(it) - })) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Attempt to switch the student after the session expires") + Status.SUCCESS -> { + Timber.i("Switch student result: Open login view") + view?.openClearLoginView() + } + Status.ERROR -> { + Timber.i("Switch student result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch("expired") + } + + fun Flow.launch(individualJobTag: String = "load"): Job { + jobs[individualJobTag]?.cancel() + val job = launchIn(this@BasePresenter) + jobs[individualJobTag] = job + Timber.d("Job $individualJobTag launched in ${this@BasePresenter.javaClass.simpleName}: $job") + return job + } + + fun cancelJobs(vararg names: String) { + names.forEach { + jobs[it]?.cancel() + } } open fun onDetachView() { view = null disposable.clear() + job.cancel() errorHandler.clear() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt index c88e4d87..946e661b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorHandler.kt @@ -24,10 +24,10 @@ open class ErrorHandler @Inject constructor(protected val resources: Resources, } protected open fun proceed(error: Throwable) { + showErrorMessage(resources.getString(error), error) when (error) { is ScramblerException, is BadCredentialsException -> onSessionExpired() is NoCurrentStudentException -> onNoCurrentStudent() - else -> showErrorMessage(resources.getString(error), error) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt index ae149fa1..c2238de7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt @@ -1,12 +1,14 @@ package io.github.wulkanowy.ui.modules.about.contributor +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.pojos.Contributor import io.github.wulkanowy.data.repositories.appcreator.AppCreatorRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach import javax.inject.Inject class ContributorPresenter @Inject constructor( @@ -31,10 +33,15 @@ class ContributorPresenter @Inject constructor( } private fun loadData() { - disposable.add(rxSingle { appCreatorRepository.getAppCreators() } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { view?.showProgress(false) } - .subscribe({ view?.run { updateData(it) } }, { errorHandler.dispatch(it) })) + flowWithResource { appCreatorRepository.getAppCreators() }.onEach { + when (it.status) { + Status.LOADING -> view?.showProgress(true) + Status.SUCCESS -> view?.run { + showProgress(false) + updateData(it.data!!) + } + Status.ERROR -> errorHandler.dispatch(it.error!!) + } + } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseAdapter.kt index 07025c09..6ae06bbe 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseAdapter.kt @@ -24,7 +24,7 @@ class LicenseAdapter @Inject constructor() : RecyclerView.Adapter(errorHandler, studentRepository, schedulers) { @@ -21,14 +28,22 @@ class LicensePresenter @Inject constructor( } fun onItemSelected(library: Library) { - view?.run { library.license?.licenseDescription?.let { openLicense(it) } } + view?.run { library.licenses?.firstOrNull()?.licenseDescription?.let { openLicense(it) } } } private fun loadData() { - disposable.add(Single.fromCallable { view?.appLibraries.orEmpty() } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnEvent { _, _ -> view?.showProgress(false) } - .subscribe({ view?.run { updateData(it) } }, { errorHandler.dispatch(it) })) + flowWithResource { + withContext(dispatchers.backgroundThread) { + view?.appLibraries.orEmpty() + } + }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("License data load started") + Status.SUCCESS -> view?.updateData(it.data!!) + Status.ERROR -> errorHandler.dispatch(it.error!!) + } + }.afterLoading { + view?.showProgress(false) + }.launch() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt index e1ec23a1..50df763a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt @@ -1,11 +1,13 @@ package io.github.wulkanowy.ui.modules.about.logviewer +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.logger.LoggerRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -23,16 +25,19 @@ class LogViewerPresenter @Inject constructor( } fun onShareLogsSelected(): Boolean { - disposable.add(rxSingle { loggerRepository.getLogFiles() } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ files -> - Timber.i("Loading logs files result: ${files.joinToString { it.name }}") - view?.shareLogs(files) - }, { - Timber.i("Loading logs files result: An exception occurred") - errorHandler.dispatch(it) - })) + flowWithResource { loggerRepository.getLogFiles() }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Loading logs files started") + Status.SUCCESS -> { + Timber.i("Loading logs files result: ${it.data!!.joinToString { file -> file.name }}") + view?.shareLogs(it.data) + } + Status.ERROR -> { + Timber.i("Loading logs files result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch("share") return true } @@ -41,15 +46,18 @@ class LogViewerPresenter @Inject constructor( } private fun loadLogFile() { - disposable.add(rxSingle { loggerRepository.getLastLogLines() } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Loading last log file result: load ${it.size} lines") - view?.setLines(it) - }, { - Timber.i("Loading last log file result: An exception occurred") - errorHandler.dispatch(it) - })) + flowWithResource { loggerRepository.getLastLogLines() }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Loading last log file started") + Status.SUCCESS -> { + Timber.i("Loading last log file result: load ${it.data!!.size} lines") + view?.setLines(it.data) + } + Status.ERROR -> { + Timber.i("Loading last log file result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch("file") } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt index 1dd32cf9..40239417 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt @@ -1,14 +1,15 @@ package io.github.wulkanowy.ui.modules.account +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.sync.SyncManager import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Single -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -37,20 +38,20 @@ class AccountPresenter @Inject constructor( } fun onLogoutConfirm() { - Timber.i("Attempt to logout current user ") - disposable.add(rxSingle { studentRepository.getCurrentStudent(false) } - .flatMapCompletable { rxCompletable { studentRepository.logoutStudent(it) } } - .andThen(rxSingle { studentRepository.getSavedStudents(false) }) - .flatMap { - if (it.isNotEmpty()) rxCompletable { studentRepository.switchStudent(it[0]) }.toSingle { it } - else Single.just(it) + flowWithResource { + val student = studentRepository.getCurrentStudent(false) + studentRepository.logoutStudent(student) + + val students = studentRepository.getSavedStudents(false) + if (students.isNotEmpty()) { + studentRepository.switchStudent(students[0]) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { view?.dismissView() } - .subscribe({ - view?.apply { - if (it.isEmpty()) { + students + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Attempt to logout current user ") + Status.SUCCESS -> view?.run { + if (it.data!!.isEmpty()) { Timber.i("Logout result: Open login view") syncManager.stopSyncWorker() openClearLoginView() @@ -59,30 +60,35 @@ class AccountPresenter @Inject constructor( recreateMainView() } } - }, { - Timber.i("Logout result: An exception occurred") - errorHandler.dispatch(it) - })) + Status.ERROR -> { + Timber.i("Logout result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.dismissView() + }.launch("logout") } fun onItemSelected(student: Student) { Timber.i("Select student item ${student.id}") if (student.isCurrent) { view?.dismissView() - } else { - Timber.i("Attempt to change a student") - disposable.add(rxSingle { studentRepository.switchStudent(student) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { view?.dismissView() } - .subscribe({ + } else flowWithResource { studentRepository.switchStudent(student) }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Attempt to change a student") + Status.SUCCESS -> { Timber.i("Change a student result: Success") view?.recreateMainView() - }, { + } + Status.ERROR -> { Timber.i("Change a student result: An exception occurred") - errorHandler.dispatch(it) - })) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.dismissView() + }.launch("switch") } private fun createAccountItems(items: List): List> { @@ -94,17 +100,18 @@ class AccountPresenter @Inject constructor( } private fun loadData() { - Timber.i("Loading account data started") - disposable.add(rxSingle { studentRepository.getSavedStudents(false) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .map { createAccountItems(it) } - .subscribe({ - Timber.i("Loading account result: Success") - view?.updateData(it) - }, { - Timber.i("Loading account result: An exception occurred") - errorHandler.dispatch(it) - })) + flowWithResource { studentRepository.getSavedStudents(false) }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading account data started") + Status.SUCCESS -> { + Timber.i("Loading account result: Success") + view?.updateData(createAccountItems(it.data!!)) + } + Status.ERROR -> { + Timber.i("Loading account result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index f177d019..0645c7a4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.ui.modules.attendance import android.annotation.SuppressLint +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.repositories.attendance.AttendanceRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -10,13 +11,18 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousOrSameSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onEach import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -168,100 +174,93 @@ class AttendancePresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) - currentDate = baseDate - reloadNavigation() - }) { - Timber.i("Loading semester result: An exception occurred") - }) + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }.launch("holidays") } private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { Timber.i("Loading attendance data started") currentDate = date - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { attendanceRepository.getAttendance(student, semester, date, date, forceRefresh) } - } - } - .map { list -> - if (prefRepository.isShowPresent) list - else list.filter { !it.presence } - } - .map { items -> items.sortedBy { it.number } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + attendanceRepository.getAttendance(student, semester, date, date, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> view?.showExcuseButton(false) + Status.SUCCESS -> { Timber.i("Loading attendance result: Success") view?.apply { - updateData(it) - showEmpty(it.isEmpty()) + updateData(it.data!!.let { items -> + if (prefRepository.isShowPresent) items + else items.filter { item -> !item.presence } + }.sortedBy { item -> item.number }) + showEmpty(it.data.isEmpty()) showErrorView(false) - showContent(it.isNotEmpty()) - showExcuseButton(it.any { item -> item.excusable }) + showContent(it.data.isNotEmpty()) + showExcuseButton(it.data.any { item -> item.excusable }) } analytics.logEvent( "load_data", "type" to "attendance", - "items" to it.size, - "force_refresh" to forceRefresh + "items" to it.data!!.size ) - }) { - Timber.i("Loading attendance result: An exception occurred") - errorHandler.dispatch(it) } - ) - } + Status.ERROR -> { + Timber.i("Loading attendance result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun excuseAbsence(reason: String?, toExcuseList: List) { - Timber.i("Excusing absence started") - disposable.apply { - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { attendanceRepository.excuseForAbsence(student, semester, toExcuseList, reason) } - } + flowWithResource { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + attendanceRepository.excuseForAbsence(student, semester, toExcuseList, reason) + }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Excusing absence started") + showProgress(true) + showContent(false) + showExcuseButton(false) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.apply { - showProgress(true) - showContent(false) - showExcuseButton(false) - } - } - .subscribe({ + Status.SUCCESS -> { Timber.i("Excusing for absence result: Success") analytics.logEvent("excuse_absence", "items" to attendanceToExcuseList.size) attendanceToExcuseList.clear() - view?.apply { + view?.run { showExcuseButton(false) showMessage(excuseSuccessString) + showContent(true) + showProgress(false) } - loadData(currentDate, true) - }) { + loadData(currentDate, forceRefresh = true) + } + Status.ERROR -> { Timber.i("Excusing for absence result: An exception occurred") - view?.showProgress(false) - errorHandler.dispatch(it) - }) - } + errorHandler.dispatch(it.error!!) + loadData(currentDate) + } + } + }.launch("excuse") } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt index f694a8d0..5d16b314 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.attendance.summary +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.repositories.attendancesummary.AttendanceSummaryRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -9,7 +10,9 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import org.threeten.bp.Month import timber.log.Timber import javax.inject.Inject @@ -73,46 +76,43 @@ class AttendanceSummaryPresenter @Inject constructor( } private fun loadData(subjectId: Int, forceRefresh: Boolean = false) { - Timber.i("Loading attendance summary data started") currentSubjectId = subjectId - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { - rxSingle { attendanceSummaryRepository.getAttendanceSummary(student, it, subjectId, forceRefresh) } - } - } - .map { items -> items.sortedByDescending { if (it.month.value <= Month.JUNE.value) it.month.value + 12 else it.month.value } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + attendanceSummaryRepository.getAttendanceSummary(student, semester, subjectId, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading attendance summary data started") + Status.SUCCESS -> { Timber.i("Loading attendance summary result: Success") view?.apply { - showEmpty(it.isEmpty()) - showContent(it.isNotEmpty()) - updateDataSet(it) + showEmpty(it.data!!.isEmpty()) + showContent(it.data.isNotEmpty()) + updateDataSet(it.data.sortedByDescending { item -> + if (item.month.value <= Month.JUNE.value) item.month.value + 12 else item.month.value + }) } analytics.logEvent( "load_data", "type" to "attendance_summary", - "items" to it.size, - "force_refresh" to forceRefresh, + "items" to it.data!!.size, "item_id" to subjectId ) - }) { - Timber.i("Loading attendance summary result: An exception occurred") - errorHandler.dispatch(it) } - ) - } + Status.ERROR -> { + Timber.i("Loading attendance summary result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -127,27 +127,27 @@ class AttendanceSummaryPresenter @Inject constructor( } private fun loadSubjects() { - Timber.i("Loading attendance summary subjects started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { subjectRepository.getSubjects(student, semester) } + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + subjectRepository.getSubjects(student, semester) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading attendance summary subjects started") + Status.SUCCESS -> { + subjects = it.data!! + + Timber.i("Loading attendance summary subjects result: Success") + view?.run { + view?.updateSubjects(ArrayList(it.data.map { subject -> subject.name })) + showSubjects(true) + } + } + Status.ERROR -> { + Timber.i("Loading attendance summary subjects result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .doOnSuccess { subjects = it } - .map { ArrayList(it.map { subject -> subject.name }) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Loading attendance summary subjects result: Success") - view?.run { - view?.updateSubjects(it) - showSubjects(true) - } - }, { - Timber.i("Loading attendance summary subjects result: An exception occurred") - errorHandler.dispatch(it) - }) - ) + }.launch("subjects") } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt index 844fb263..21f7ae6e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.exam +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.repositories.exam.ExamRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -8,13 +9,17 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import io.github.wulkanowy.utils.sunday +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay +import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onEach import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -90,59 +95,54 @@ class ExamPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) - currentDate = baseDate - reloadNavigation() - }) { - Timber.i("Loading semester result: An exception occurred") - }) + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }.launch("holidays") } private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { - Timber.i("Loading exam data started") currentDate = date - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { examRepository.getExams(student, semester, currentDate.monday, currentDate.sunday, forceRefresh) } - } - } - .map { createExamItems(it) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + examRepository.getExams(student, semester, currentDate.monday, currentDate.sunday, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading exam data started") + Status.SUCCESS -> { Timber.i("Loading exam result: Success") view?.apply { - updateData(it) - showEmpty(it.isEmpty()) + updateData(createExamItems(it.data!!)) + showEmpty(it.data.isEmpty()) showErrorView(false) - showContent(it.isNotEmpty()) + showContent(it.data.isNotEmpty()) } analytics.logEvent( "load_data", "type" to "exam", - "items" to it.size, - "force_refresh" to forceRefresh + "items" to it.data!!.size ) - }) { + } + Status.ERROR -> { Timber.i("Loading exam result: An exception occurred") - errorHandler.dispatch(it) - }) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt index ab6c507b..5d0a310f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProvider.kt @@ -1,5 +1,7 @@ package io.github.wulkanowy.ui.modules.grade +import io.github.wulkanowy.data.Resource +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.Semester @@ -13,8 +15,17 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.BOTH_SEMESTERS import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.ONE_SEMESTER import io.github.wulkanowy.utils.calcAverage import io.github.wulkanowy.utils.changeModifier +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.flatMapConcat +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map import javax.inject.Inject +@OptIn(FlowPreview::class) class GradeAverageProvider @Inject constructor( private val semesterRepository: SemesterRepository, private val gradeRepository: GradeRepository, @@ -25,63 +36,64 @@ class GradeAverageProvider @Inject constructor( private val minusModifier get() = preferencesRepository.gradeMinusModifier - suspend fun getGradesDetailsWithAverage(student: Student, semesterId: Int, forceRefresh: Boolean = false): List { - return semesterRepository.getSemesters(student).let { semesters -> - when (preferencesRepository.gradeAverageMode) { - ONE_SEMESTER -> getSemesterDetailsWithAverage(student, semesters.single { it.semesterId == semesterId }, forceRefresh) - BOTH_SEMESTERS -> calculateBothSemestersAverage(student, semesters, semesterId, forceRefresh) - ALL_YEAR -> calculateAllYearAverage(student, semesters, semesterId, forceRefresh) - } - } - } + fun getGradesDetailsWithAverage(student: Student, semesterId: Int, forceRefresh: Boolean) = flowWithResourceIn { + val semesters = semesterRepository.getSemesters(student) - private suspend fun calculateBothSemestersAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): List { + when (preferencesRepository.gradeAverageMode) { + ONE_SEMESTER -> getSemesterDetailsWithAverage(student, semesters.single { it.semesterId == semesterId }, forceRefresh) + BOTH_SEMESTERS -> calculateBothSemestersAverage(student, semesters, semesterId, forceRefresh) + ALL_YEAR -> calculateAllYearAverage(student, semesters, semesterId, forceRefresh) + } + }.distinctUntilChanged() + + private fun calculateBothSemestersAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): Flow>> { val selectedSemester = semesters.single { it.semesterId == semesterId } val firstSemester = semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 } - return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).let { selectedDetails -> - val isAnyAverage = selectedDetails.any { it.average != .0 } + return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).flatMapConcat { selectedDetails -> + val isAnyAverage = selectedDetails.data.orEmpty().any { it.average != .0 } if (selectedSemester != firstSemester) { - getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).let { secondDetails -> - selectedDetails.map { selected -> - val second = secondDetails.singleOrNull { it.subject == selected.subject } + getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).map { secondDetails -> + secondDetails.copy(data = selectedDetails.data?.map { selected -> + val second = secondDetails.data.orEmpty().singleOrNull { it.subject == selected.subject } selected.copy(average = if (!isAnyAverage || preferencesRepository.gradeAverageForceCalc) { val selectedGrades = selected.grades.updateModifiers(student).calcAverage() (selectedGrades + (second?.grades?.updateModifiers(student)?.calcAverage() ?: selectedGrades)) / 2 } else (selected.average + (second?.average ?: selected.average)) / 2) - } - } - } else selectedDetails + }) + }.filter { it.status != Status.LOADING }.filter { it.data != null } + } else flowOf(selectedDetails) } } - private suspend fun calculateAllYearAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): List { + private fun calculateAllYearAverage(student: Student, semesters: List, semesterId: Int, forceRefresh: Boolean): Flow>> { val selectedSemester = semesters.single { it.semesterId == semesterId } val firstSemester = semesters.single { it.diaryId == selectedSemester.diaryId && it.semesterName == 1 } - return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).let { selectedDetails -> - val isAnyAverage = selectedDetails.any { it.average != .0 } + return getSemesterDetailsWithAverage(student, selectedSemester, forceRefresh).flatMapConcat { selectedDetails -> + val isAnyAverage = selectedDetails.data.orEmpty().any { it.average != .0 } if (selectedSemester != firstSemester) { - getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).let { secondDetails -> - selectedDetails.map { selected -> - val second = secondDetails.singleOrNull { it.subject == selected.subject } + getSemesterDetailsWithAverage(student, firstSemester, forceRefresh).map { secondDetails -> + secondDetails.copy(data = selectedDetails.data?.map { selected -> + val second = secondDetails.data.orEmpty().singleOrNull { it.subject == selected.subject } selected.copy(average = if (!isAnyAverage || preferencesRepository.gradeAverageForceCalc) { (selected.grades.updateModifiers(student) + second?.grades?.updateModifiers(student).orEmpty()).calcAverage() } else selected.average) - } - } - } else selectedDetails + }) + }.filter { it.status != Status.LOADING }.filter { it.data != null } + } else flowOf(selectedDetails) } } - private suspend fun getSemesterDetailsWithAverage(student: Student, semester: Semester, forceRefresh: Boolean): List { - return gradeRepository.getGrades(student, semester, forceRefresh).let { (details, summaries) -> - val isAnyAverage = summaries.any { it.average != .0 } - val allGrades = details.groupBy { it.subject } + private fun getSemesterDetailsWithAverage(student: Student, semester: Semester, forceRefresh: Boolean): Flow>> { + return gradeRepository.getGrades(student, semester, forceRefresh = forceRefresh).map { res -> + val (details, summaries) = res.data ?: null to null + val isAnyAverage = summaries.orEmpty().any { it.average != .0 } + val allGrades = details.orEmpty().groupBy { it.subject } - summaries.emulateEmptySummaries(student, semester, allGrades.toList(), isAnyAverage).map { summary -> + Resource(res.status, summaries?.emulateEmptySummaries(student, semester, allGrades.toList(), isAnyAverage)?.map { summary -> val grades = allGrades[summary.subject].orEmpty() GradeDetailsWithAverage( subject = summary.subject, @@ -92,7 +104,7 @@ class GradeAverageProvider @Inject constructor( summary = summary, grades = grades ) - } + }, res.error) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt index 65f6598d..9dc39d85 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -7,10 +8,11 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.getCurrentOrLast -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.onEach import timber.log.Timber -import java.util.concurrent.TimeUnit.MILLISECONDS import javax.inject.Inject class GradePresenter @Inject constructor( @@ -99,29 +101,33 @@ class GradePresenter @Inject constructor( } private fun loadData() { - Timber.i("Loading grade data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getSemesters(it, refreshOnNoCurrent = true) } } - .delay(200, MILLISECONDS) - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - val current = it.getCurrentOrLast() - selectedIndex = if (selectedIndex == 0) current.semesterName else selectedIndex - schoolYear = current.schoolYear - semesters = it.filter { semester -> semester.diaryId == current.diaryId } - view?.setCurrentSemesterName(current.semesterName, schoolYear) + flowWithResource { + val student = studentRepository.getCurrentStudent() + delay(200) + semesterRepository.getSemesters(student, refreshOnNoCurrent = true) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading grade data started") + Status.SUCCESS -> { + val current = it.data!!.getCurrentOrLast() + selectedIndex = if (selectedIndex == 0) current.semesterName else selectedIndex + schoolYear = current.schoolYear + semesters = it.data.filter { semester -> semester.diaryId == current.diaryId } + view?.setCurrentSemesterName(current.semesterName, schoolYear) - view?.run { - Timber.i("Loading grade result: Attempt load index $currentPageIndex") - loadChild(currentPageIndex) - showErrorView(false) - showSemesterSwitch(true) + view?.run { + Timber.i("Loading grade result: Attempt load index $currentPageIndex") + loadChild(currentPageIndex) + showErrorView(false) + showSemesterSwitch(true) + } } - }) { - Timber.i("Loading grade result: An exception occurred") - errorHandler.dispatch(it) - }) + Status.ERROR -> { + Timber.i("Loading grade result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index a99e3a54..5845d32c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade.details +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.repositories.grade.GradeRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -11,8 +12,11 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -41,7 +45,9 @@ class GradeDetailsPresenter @Inject constructor( fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) { currentSemesterId = semesterId + loadData(semesterId, forceRefresh) + if (!forceRefresh) view?.showErrorView(false) } fun onGradeItemSelected(grade: Grade, position: Int) { @@ -63,24 +69,24 @@ class GradeDetailsPresenter @Inject constructor( } fun onMarkAsReadSelected(): Boolean { - Timber.i("Select mark grades as read") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getSemesters(it) } } - .flatMap { rxSingle { gradeRepository.getUnreadGrades(it.first { item -> item.semesterId == currentSemesterId }) } } - .map { it.map { grade -> grade.apply { isRead = true } } } - .flatMapCompletable { - Timber.i("Mark as read ${it.size} grades") - rxCompletable { gradeRepository.updateGrades(it) } + flowWithResource { + val student = studentRepository.getCurrentStudent() + val semesters = semesterRepository.getSemesters(student) + val semester = semesters.first { item -> item.semesterId == currentSemesterId } + val unreadGrades = gradeRepository.getUnreadGrades(semester).first() + + Timber.i("Mark as read ${unreadGrades.size} grades") + gradeRepository.updateGrades(unreadGrades.map { it.apply { isRead = true } }) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Select mark grades as read") + Status.SUCCESS -> Timber.i("Mark as read result: Success") + Status.ERROR -> { + Timber.i("Mark as read result: An exception occurred") + errorHandler.dispatch(it.error!!) + } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Mark as read result: Success") - loadData(currentSemesterId, false) - }, { - Timber.i("Mark as read result: An exception occurred") - errorHandler.dispatch(it) - })) + }.launch("mark") return true } @@ -119,7 +125,7 @@ class GradeDetailsPresenter @Inject constructor( showEmpty(false) clearView() } - disposable.clear() + cancelJobs("load") } fun updateMarkAsDoneButton() { @@ -127,43 +133,46 @@ class GradeDetailsPresenter @Inject constructor( } private fun loadData(semesterId: Int, forceRefresh: Boolean) { - Timber.i("Loading grade details data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - showRefresh(false) - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded(semesterId) - } - } - .subscribe({ grades -> - Timber.i("Loading grade details result: Success") - newGradesAmount = grades.sumBy { it.grades.sumBy { grade -> if (!grade.isRead) 1 else 0 } } - updateMarkAsDoneButton() - view?.run { - showEmpty(grades.isEmpty()) - showErrorView(false) - showContent(grades.isNotEmpty()) - updateData( - data = createGradeItems(grades), - isGradeExpandable = preferencesRepository.isGradeExpandable, - gradeColorTheme = preferencesRepository.gradeColorTheme + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + averageProvider.getGradesDetailsWithAverage(student, semesterId, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading grade details data started") + Status.SUCCESS -> { + Timber.i("Loading grade details result: Success") + newGradesAmount = it.data!!.sumBy { item -> item.grades.sumBy { grade -> if (!grade.isRead) 1 else 0 } } + updateMarkAsDoneButton() + val items = createGradeItems(it.data) + view?.run { + showEmpty(items.isEmpty()) + showErrorView(false) + showContent(items.isNotEmpty()) + updateData( + data = items, + isGradeExpandable = preferencesRepository.isGradeExpandable, + gradeColorTheme = preferencesRepository.gradeColorTheme + ) + } + analytics.logEvent( + "load_data", + "type" to "grade_details", + "items" to it.data.size ) } - analytics.logEvent( - "load_data", - "type" to "grade_details", - "items" to grades.size, - "force_refresh" to forceRefresh - ) - }) { - Timber.i("Loading grade details result: An exception occurred") - errorHandler.dispatch(it) - }) + Status.ERROR -> { + Timber.i("Loading grade details result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + showRefresh(false) + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded(semesterId) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -197,15 +206,15 @@ class GradeDetailsPresenter @Inject constructor( } private fun updateGrade(grade: Grade) { - Timber.i("Attempt to update grade ${grade.id}") - disposable.add(rxCompletable { gradeRepository.updateGrade(grade) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Update grade result: Success") - }) { error -> - Timber.i("Update grade result: An exception occurred") - errorHandler.dispatch(error) - }) + flowWithResource { gradeRepository.updateGrade(grade) }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Attempt to update grade ${grade.id}") + Status.SUCCESS -> Timber.i("Update grade result: Success") + Status.ERROR -> { + Timber.i("Update grade result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launch("update") } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt index b2c56ed2..eb6ae843 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade.statistics +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Subject import io.github.wulkanowy.data.repositories.gradestatistics.GradeStatisticsRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -10,7 +11,9 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -46,6 +49,7 @@ class GradeStatisticsPresenter @Inject constructor( fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) { currentSemesterId = semesterId loadSubjects() + if (!forceRefresh) view?.showErrorView(false) loadDataByType(semesterId, currentSubjectName, currentType, forceRefresh) } @@ -65,7 +69,7 @@ class GradeStatisticsPresenter @Inject constructor( showEmpty(false) clearView() } - disposable.clear() + cancelJobs("load") } fun onSwipeRefresh() { @@ -103,7 +107,7 @@ class GradeStatisticsPresenter @Inject constructor( fun onTypeChange() { val type = view?.currentType ?: ViewType.POINTS Timber.i("Select grade stats semester: $type") - disposable.clear() + cancelJobs("load") view?.run { showContent(false) showProgress(true) @@ -116,77 +120,77 @@ class GradeStatisticsPresenter @Inject constructor( } private fun loadSubjects() { - Timber.i("Loading grade stats subjects started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { subjectRepository.getSubjects(student, semester) } + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + subjectRepository.getSubjects(student, semester) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading grade stats subjects started") + Status.SUCCESS -> { + subjects = it.data!! + + Timber.i("Loading grade stats subjects result: Success") + view?.run { + view?.updateSubjects(ArrayList(it.data.map { subject -> subject.name })) + showSubjects(!preferencesRepository.showAllSubjectsOnStatisticsList) + } + } + Status.ERROR -> { + Timber.i("Loading grade stats subjects result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .doOnSuccess { subjects = it } - .map { ArrayList(it.map { subject -> subject.name }) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Loading grade stats subjects result: Success") - view?.updateSubjects(it) - }, { - Timber.i("Loading grade stats subjects result: An exception occurred") - errorHandler.dispatch(it) - }) - ) + }.launch("subjects") } private fun loadDataByType(semesterId: Int, subjectName: String, type: ViewType, forceRefresh: Boolean = false) { currentSubjectName = if (preferencesRepository.showAllSubjectsOnStatisticsList) "Wszystkie" else subjectName currentType = type - Timber.i("Loading grade stats data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getSemesters(student) }.flatMap { semesters -> - val semester = semesters.first { item -> item.semesterId == semesterId } + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semesters = semesterRepository.getSemesters(student) + val semester = semesters.first { item -> item.semesterId == semesterId } - rxSingle { - with(gradeStatisticsRepository) { - when (type) { - ViewType.SEMESTER -> getGradesStatistics(student, semester, currentSubjectName, true, forceRefresh) - ViewType.PARTIAL -> getGradesStatistics(student, semester, currentSubjectName, false, forceRefresh) - ViewType.POINTS -> getGradesPointsStatistics(student, semester, currentSubjectName, forceRefresh) - } - } + with(gradeStatisticsRepository) { + when (type) { + ViewType.SEMESTER -> getGradesStatistics(student, semester, currentSubjectName, true, forceRefresh) + ViewType.PARTIAL -> getGradesStatistics(student, semester, currentSubjectName, false, forceRefresh) + ViewType.POINTS -> getGradesPointsStatistics(student, semester, currentSubjectName, forceRefresh) + } + } + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading grade stats data started") + Status.SUCCESS -> { + Timber.i("Loading grade stats result: Success") + view?.run { + showEmpty(it.data!!.isEmpty()) + showContent(it.data.isNotEmpty()) + showErrorView(false) + updateData(it.data, preferencesRepository.gradeColorTheme, preferencesRepository.showAllSubjectsOnStatisticsList) + showSubjects(!preferencesRepository.showAllSubjectsOnStatisticsList) } + analytics.logEvent( + "load_data", + "type" to "grade_statistics", + "items" to it.data!!.size + ) + } + Status.ERROR -> { + Timber.i("Loading grade stats result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - showRefresh(false) - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded(semesterId) - } + }.afterLoading { + view?.run { + showRefresh(false) + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded(semesterId) } - .subscribe({ - Timber.i("Loading grade stats result: Success") - view?.run { - showEmpty(it.isEmpty()) - showContent(it.isNotEmpty()) - showErrorView(false) - updateData(it, preferencesRepository.gradeColorTheme, preferencesRepository.showAllSubjectsOnStatisticsList) - showSubjects(!preferencesRepository.showAllSubjectsOnStatisticsList) - } - analytics.logEvent( - "load_data", - "type" to "grade_statistics", - "items" to it.size, - "force_refresh" to forceRefresh - ) - }) { - Timber.i("Loading grade stats result: An exception occurred") - errorHandler.dispatch(it) - }) + }.launch("load") } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt index 62b95d2e..96908c3c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.grade.summary +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter @@ -8,7 +9,9 @@ import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -30,36 +33,45 @@ class GradeSummaryPresenter @Inject constructor( fun onParentViewLoadData(semesterId: Int, forceRefresh: Boolean) { Timber.i("Loading grade summary data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { averageProvider.getGradesDetailsWithAverage(it, semesterId, forceRefresh) } } - .map { createGradeSummaryItems(it) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - showRefresh(false) - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded(semesterId) + + loadData(semesterId, forceRefresh) + if (!forceRefresh) view?.showErrorView(false) + } + + private fun loadData(semesterId: Int, forceRefresh: Boolean) { + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + averageProvider.getGradesDetailsWithAverage(student, semesterId, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading grade summary started") + Status.SUCCESS -> { + Timber.i("Loading grade summary result: Success") + view?.run { + showEmpty(it.data!!.isEmpty()) + showContent(it.data.isNotEmpty()) + showErrorView(false) + updateData(createGradeSummaryItems(it.data)) + } + analytics.logEvent( + "load_data", + "type" to "grade_summary", + "items" to it.data!!.size + ) } - }.subscribe({ - Timber.i("Loading grade summary result: Success") - view?.run { - showEmpty(it.isEmpty()) - showContent(it.isNotEmpty()) - showErrorView(false) - updateData(it) + Status.ERROR -> { + Timber.i("Loading grade summary result: An exception occurred") + errorHandler.dispatch(it.error!!) } - analytics.logEvent( - "load_data", - "type" to "grade_summary", - "items" to it.size, - "force_refresh" to forceRefresh - ) - }) { - Timber.i("Loading grade summary result: An exception occurred") - errorHandler.dispatch(it) - }) + } + }.afterLoading { + view?.run { + showRefresh(false) + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded(semesterId) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -105,7 +117,7 @@ class GradeSummaryPresenter @Inject constructor( showEmpty(false) clearView() } - disposable.clear() + cancelJobs("load") } private fun createGradeSummaryItems(items: List): List { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt index f3052957..ee2751d7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt @@ -70,10 +70,6 @@ class HomeworkFragment : BaseFragment(R.layout.fragment } } - fun onReloadList() { - presenter.reloadData() - } - override fun clearData() { with(homeworkAdapter) { items = emptyList() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index fe31dfae..612da749 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.homework +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -8,13 +9,17 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import io.github.wulkanowy.utils.sunday +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.nextOrSameSchoolDay +import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onEach import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber @@ -79,64 +84,54 @@ class HomeworkPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) - currentDate = baseDate - reloadNavigation() - }) { - Timber.i("Loading semester result: An exception occurred") - }) - } - - fun reloadData() { - loadData(currentDate, false) + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }.launch("holidays") } private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { - Timber.i("Loading homework data started") currentDate = date - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { homeworkRepository.getHomework(student, semester, currentDate, currentDate, forceRefresh) } - } - } - .map { createHomeworkItem(it) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + homeworkRepository.getHomework(student, semester, date, date, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading homework data started") + Status.SUCCESS -> { Timber.i("Loading homework result: Success") view?.apply { - updateData(it) - showEmpty(it.isEmpty()) + updateData(createHomeworkItem(it.data!!)) + showEmpty(it.data.isEmpty()) showErrorView(false) - showContent(it.isNotEmpty()) + showContent(it.data.isNotEmpty()) } analytics.logEvent( "load_data", "type" to "homework", - "items" to it.size, - "force_refresh" to forceRefresh + "items" to it.data!!.size ) - }) { + } + Status.ERROR -> { Timber.i("Loading homework result: An exception occurred") - - errorHandler.dispatch(it) - }) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt index 7b3b9821..eb85066a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt @@ -73,7 +73,6 @@ class HomeworkDetailsDialog : BaseDialogFragment(), Homew } override fun updateMarkAsDoneLabel(isDone: Boolean) { - (parentFragment as? HomeworkFragment)?.onReloadList() binding.homeworkDialogRead.text = view?.context?.getString(if (isDone) R.string.homework_mark_as_undone else R.string.homework_mark_as_done) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt index c0475b7c..2ca7a1f8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.homework.details +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -7,7 +8,8 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -26,20 +28,19 @@ class HomeworkDetailsPresenter @Inject constructor( } fun toggleDone(homework: Homework) { - Timber.i("Homework details update start") - disposable.add(rxSingle { homeworkRepository.toggleDone(homework) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.i("Homework details update: Success") - view?.run { - updateMarkAsDoneLabel(homework.isDone) + flowWithResource { homeworkRepository.toggleDone(homework) }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Homework details update start") + Status.SUCCESS -> { + Timber.i("Homework details update: Success") + view?.updateMarkAsDoneLabel(homework.isDone) + analytics.logEvent("homework_mark_as_done") + } + Status.ERROR -> { + Timber.i("Homework details update result: An exception occurred") + errorHandler.dispatch(it.error!!) } - analytics.logEvent("homework_mark_as_done") - }) { - Timber.i("Homework details update result: An exception occurred") - errorHandler.dispatch(it) } - ) + }.launch("toggle") } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt index 27205a2a..8f187ac8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.login.advanced +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.sdk.Sdk @@ -7,9 +8,10 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank -import io.reactivex.Single -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -126,35 +128,42 @@ class LoginAdvancedPresenter @Inject constructor( fun onSignInClick() { if (!validateCredentials()) return - disposable.add(getStudentsAppropriatesToLoginType() - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.apply { + flowWithResource { getStudentsAppropriatesToLoginType() }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Login started") hideSoftKeyboard() showProgress(true) showContent(false) } - Timber.i("Login started") - } - .doFinally { - view?.apply { - showProgress(false) - showContent(true) + Status.SUCCESS -> { + Timber.i("Login result: Success") + analytics.logEvent("registration_form", + "success" to true, + "students" to it.data!!.size, + "error" to "No error" + ) + view?.notifyParentAccountLogged(it.data) + } + Status.ERROR -> { + Timber.i("Login result: An exception occurred") + analytics.logEvent( + "registration_form", + "success" to false, "students" to -1, + "error" to it.error!!.message.ifNullOrBlank { "No message" } + ) + loginErrorHandler.dispatch(it.error) } } - .subscribe({ - Timber.i("Login result: Success") - analytics.logEvent("registration_form", "success" to true, "students" to it.size, "error" to "No error") - view?.notifyParentAccountLogged(it) - }, { - Timber.i("Login result: An exception occurred") - analytics.logEvent("registration_form", "success" to false, "students" to -1, "error" to it.message.ifNullOrBlank { "No message" }) - loginErrorHandler.dispatch(it) - })) + }.afterLoading { + view?.apply { + showProgress(false) + showContent(true) + } + }.launch("login") } - private fun getStudentsAppropriatesToLoginType(): Single> { + private suspend fun getStudentsAppropriatesToLoginType(): List { val email = view?.formUsernameValue.orEmpty() val password = view?.formPassValue.orEmpty() val endpoint = view?.formHostValue.orEmpty() @@ -163,12 +172,10 @@ class LoginAdvancedPresenter @Inject constructor( val symbol = view?.formSymbolValue.orEmpty() val token = view?.formTokenValue.orEmpty() - return rxSingle { - when (Sdk.Mode.valueOf(view?.formLoginType ?: "")) { - Sdk.Mode.API -> studentRepository.getStudentsApi(pin, symbol, token) - Sdk.Mode.SCRAPPER -> studentRepository.getStudentsScrapper(email, password, endpoint, symbol) - Sdk.Mode.HYBRID -> studentRepository.getStudentsHybrid(email, password, endpoint, symbol) - } + return when (Sdk.Mode.valueOf(view?.formLoginType.orEmpty())) { + Sdk.Mode.API -> studentRepository.getStudentsApi(pin, symbol, token) + Sdk.Mode.SCRAPPER -> studentRepository.getStudentsScrapper(email, password, endpoint, symbol) + Sdk.Mode.HYBRID -> studentRepository.getStudentsHybrid(email, password, endpoint, symbol) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt index ccbe4bbb..9e43bf77 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt @@ -1,12 +1,15 @@ package io.github.wulkanowy.ui.modules.login.form +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -75,34 +78,44 @@ class LoginFormPresenter @Inject constructor( if (!validateCredentials(email, password, host)) return - disposable.add(rxSingle { studentRepository.getStudentsScrapper(email, password, host, symbol) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.apply { + flowWithResource { studentRepository.getStudentsScrapper(email, password, host, symbol) }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Login started") hideSoftKeyboard() showProgress(true) showContent(false) } - Timber.i("Login started") - } - .doFinally { - view?.apply { - showProgress(false) - showContent(true) + Status.SUCCESS -> { + Timber.i("Login result: Success") + analytics.logEvent( + "registration_form", + "success" to true, + "students" to it.data!!.size, + "scrapperBaseUrl" to host, + "error" to "No error" + ) + view?.notifyParentAccountLogged(it.data, Triple(email, password, host)) + } + Status.ERROR -> { + Timber.i("Login result: An exception occurred") + analytics.logEvent( + "registration_form", + "success" to false, + "students" to -1, + "scrapperBaseUrl" to host, + "error" to it.error!!.message.ifNullOrBlank { "No message" }) + loginErrorHandler.dispatch(it.error) + lastError = it.error + view?.showContact(true) } } - .subscribe({ - Timber.i("Login result: Success") - analytics.logEvent("registration_form", "success" to true, "students" to it.size, "scrapperBaseUrl" to host, "error" to "No error") - view?.notifyParentAccountLogged(it, Triple(email, password, host)) - }, { - Timber.i("Login result: An exception occurred") - analytics.logEvent("registration_form", "success" to false, "students" to -1, "scrapperBaseUrl" to host, "error" to it.message.ifNullOrBlank { "No message" }) - loginErrorHandler.dispatch(it) - lastError = it - view?.showContact(true) - })) + }.afterLoading { + view?.apply { + showProgress(false) + showContent(true) + } + }.launch("login") } fun onFaqClick() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt index 84d5af06..0ef183cc 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt @@ -1,12 +1,15 @@ package io.github.wulkanowy.ui.modules.login.recover +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.recover.RecoverRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -56,24 +59,22 @@ class LoginRecoverPresenter @Inject constructor( if (!validateInput(username, host)) return - disposable.add(rxSingle { recoverRepository.getReCaptchaSiteKey(host, symbol.ifBlank { "Default" }) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.run { + flowWithResource { recoverRepository.getReCaptchaSiteKey(host, symbol.ifBlank { "Default" }) }.onEach { + when (it.status) { + Status.LOADING -> view?.run { hideSoftKeyboard() showRecoverForm(false) showProgress(true) showErrorView(false) showCaptcha(false) } + Status.SUCCESS -> view?.loadReCaptcha(siteKey = it.data!!.first, url = it.data.second) + Status.ERROR -> { + Timber.i("Obtain captcha site key result: An exception occurred") + errorHandler.dispatch(it.error!!) + } } - .subscribe({ (resetUrl, siteKey) -> - view?.loadReCaptcha(siteKey, resetUrl) - }) { - Timber.i("Obtain captcha site key result: An exception occurred") - errorHandler.dispatch(it) - }) + }.launch("captcha") } private fun validateInput(username: String, host: String): Boolean { @@ -97,35 +98,28 @@ class LoginRecoverPresenter @Inject constructor( val host = view?.recoverHostValue.orEmpty() val symbol = view?.formHostSymbol.ifNullOrBlank { "Default" } - with(disposable) { - clear() - add(rxSingle { recoverRepository.sendRecoverRequest(host, symbol, username, reCaptchaResponse) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.run { - showProgress(true) - showRecoverForm(false) - showCaptcha(false) - } + flowWithResource { recoverRepository.sendRecoverRequest(host, symbol, username, reCaptchaResponse) }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + showProgress(true) + showRecoverForm(false) + showCaptcha(false) } - .doFinally { - view?.showProgress(false) - } - .subscribe({ - view?.run { - showSuccessView(true) - setSuccessTitle(it.substringBefore(". ")) - setSuccessMessage(it.substringAfter(". ")) - } - + Status.SUCCESS -> view?.run { + showSuccessView(true) + setSuccessTitle(it.data!!.substringBefore(". ")) + setSuccessMessage(it.data.substringAfter(". ")) analytics.logEvent("account_recover", "register" to host, "symbol" to symbol, "success" to true) - }) { + } + Status.ERROR -> { Timber.i("Send recover request result: An exception occurred") - errorHandler.dispatch(it) + errorHandler.dispatch(it.error!!) analytics.logEvent("account_recover", "register" to host, "symbol" to symbol, "success" to false) - }) - } + } + } + }.afterLoading { + view?.showProgress(false) + }.launch("verified") } fun onDetailsClick() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt index 91d3e66c..99ee7d30 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt @@ -1,14 +1,15 @@ package io.github.wulkanowy.ui.modules.login.studentselect +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import java.io.Serializable import javax.inject.Inject @@ -28,7 +29,7 @@ class LoginStudentSelectPresenter @Inject constructor( fun onAttachView(view: LoginStudentSelectView, students: Serializable?) { super.onAttachView(view) - view.run { + with(view) { initView() showContact(false) enableSignIn(false) @@ -73,22 +74,20 @@ class LoginStudentSelectPresenter @Inject constructor( private fun loadData(students: List) { resetSelectedState() this.students = students - disposable.add(rxSingle { studentRepository.getSavedStudents(false) } - .map { savedStudents -> - students.map { student -> - student to savedStudents.any { compareStudents(student, it) } + + flowWithResource { studentRepository.getSavedStudents(false) }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Login student select students load started") + Status.SUCCESS -> view?.updateData(students.map { student -> + student to it.data!!.any { item -> compareStudents(student, item) } + }) + Status.ERROR -> { + errorHandler.dispatch(it.error!!) + lastError = it.error + view?.updateData(students.map { student -> student to false }) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - view?.updateData(it) - }, { - errorHandler.dispatch(it) - lastError = it - view?.updateData(students.map { student -> student to false }) - }) - ) + }.launch() } private fun resetSelectedState() { @@ -97,33 +96,35 @@ class LoginStudentSelectPresenter @Inject constructor( } private fun registerStudents(students: List) { - disposable.add(rxSingle { studentRepository.saveStudents(students) } - .map { students.first().apply { id = it.first() } } - .flatMapCompletable { rxCompletable { studentRepository.switchStudent(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.apply { + flowWithResource { + val savedStudents = studentRepository.saveStudents(students) + val firstRegistered = students.first().apply { id = savedStudents.first() } + studentRepository.switchStudent(firstRegistered) + }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Registration started") showProgress(true) showContent(false) } - Timber.i("Registration started") - } - .subscribe({ - students.forEach { analytics.logEvent("registration_student_select", "success" to true, "scrapperBaseUrl" to it.scrapperBaseUrl, "symbol" to it.symbol, "error" to "No error") } - Timber.i("Registration result: Success") - view?.openMainView() - }, { error -> - students.forEach { analytics.logEvent("registration_student_select", "success" to false, "scrapperBaseUrl" to it.scrapperBaseUrl, "symbol" to it.symbol, "error" to error.message.ifNullOrBlank { "No message" }) } - Timber.i("Registration result: An exception occurred ") - loginErrorHandler.dispatch(error) - lastError = error - view?.apply { - showProgress(false) - showContent(true) - showContact(true) + Status.SUCCESS -> { + Timber.i("Registration result: Success") + view?.openMainView() + logRegisterEvent(students) } - })) + Status.ERROR -> { + Timber.i("Registration result: An exception occurred ") + view?.apply { + showProgress(false) + showContent(true) + showContact(true) + } + lastError = it.error + loginErrorHandler.dispatch(it.error!!) + logRegisterEvent(students, it.error) + } + } + }.launch("register") } fun onDiscordClick() { @@ -133,4 +134,15 @@ class LoginStudentSelectPresenter @Inject constructor( fun onEmailClick() { view?.openEmail(lastError?.message.ifNullOrBlank { "empty" }) } + + private fun logRegisterEvent(students: List, error: Throwable? = null) { + students.forEach { student -> + analytics.logEvent( + "registration_student_select", + "success" to (error != null), + "scrapperBaseUrl" to student.scrapperBaseUrl, + "symbol" to student.symbol, + "error" to (error?.message?.ifBlank { "No message" } ?: "No error")) + } + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt index b7687ed3..c0f5803f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt @@ -1,13 +1,15 @@ package io.github.wulkanowy.ui.modules.login.symbol +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank -import io.reactivex.Single -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import java.io.Serializable import javax.inject.Inject @@ -47,44 +49,55 @@ class LoginSymbolPresenter @Inject constructor( return } - disposable.add( - Single.fromCallable { loginData } - .flatMap { rxSingle { studentRepository.getStudentsScrapper(it.first, it.second, it.third, symbol) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.apply { - hideSoftKeyboard() - showProgress(true) - showContent(false) - } + flowWithResource { studentRepository.getStudentsScrapper(loginData!!.first, loginData!!.second, loginData!!.third, symbol) }.onEach { + when (it.status) { + Status.LOADING -> view?.run { Timber.i("Login with symbol started") + hideSoftKeyboard() + showProgress(true) + showContent(false) } - .doFinally { - view?.apply { - showProgress(false) - showContent(true) - } - } - .subscribe({ - analytics.logEvent("registration_symbol", "success" to true, "students" to it.size, "scrapperBaseUrl" to loginData?.third, "symbol" to symbol, "error" to "No error") - view?.apply { - if (it.isEmpty()) { + Status.SUCCESS -> { + view?.run { + if (it.data!!.isEmpty()) { Timber.i("Login with symbol result: Empty student list") setErrorSymbolIncorrect() view?.showContact(true) } else { Timber.i("Login with symbol result: Success") - notifyParentAccountLogged(it) + notifyParentAccountLogged(it.data) } } - }, { + analytics.logEvent( + "registration_symbol", + "success" to true, + "students" to it.data!!.size, + "scrapperBaseUrl" to loginData?.third, + "symbol" to symbol, + "error" to "No error" + ) + } + Status.ERROR -> { Timber.i("Login with symbol result: An exception occurred") - analytics.logEvent("registration_symbol", "success" to false, "students" to -1, "scrapperBaseUrl" to loginData?.third, "symbol" to symbol, "error" to it.message.ifNullOrBlank { "No message" }) - loginErrorHandler.dispatch(it) - lastError = it + analytics.logEvent( + "registration_symbol", + "success" to false, + "students" to -1, + "scrapperBaseUrl" to loginData?.third, + "symbol" to symbol, + "error" to it.error!!.message.ifNullOrBlank { "No message" } + ) + loginErrorHandler.dispatch(it.error) + lastError = it.error view?.showContact(true) - })) + } + } + }.afterLoading { + view?.apply { + showProgress(false) + showContent(true) + } + }.launch("login") } fun onParentInitSymbolView(loginData: Triple) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt index 1273a54c..64df3db8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt @@ -1,13 +1,15 @@ package io.github.wulkanowy.ui.modules.luckynumber +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -34,47 +36,47 @@ class LuckyNumberPresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean = false) { - Timber.i("Loading lucky number started") - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMapMaybe { rxMaybe { luckyNumberRepository.getLuckyNumber(it, forceRefresh) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + luckyNumberRepository.getLuckyNumber(student, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading lucky number started") + Status.SUCCESS -> { + if (it.data != null) { + Timber.i("Loading lucky number result: Success") + view?.apply { + updateData(it.data) + showContent(true) + showEmpty(false) + showErrorView(false) + } + analytics.logEvent( + "load_item", + "type" to "lucky_number", + "number" to it.data.luckyNumber + ) + } else { + Timber.i("Loading lucky number result: No lucky number found") + view?.run { + showContent(false) + showEmpty(true) + showErrorView(false) + } } } - .subscribe({ - Timber.i("Loading lucky number result: Success") - view?.apply { - updateData(it) - showContent(true) - showEmpty(false) - showErrorView(false) - } - analytics.logEvent( - "load_item", - "type" to "lucky_number", - "number" to it.luckyNumber, - "force_refresh" to forceRefresh - ) - }, { + Status.ERROR -> { Timber.i("Loading lucky number result: An exception occurred") - errorHandler.dispatch(it) - }, { - Timber.i("Loading lucky number result: No lucky number found") - view?.run { - showContent(false) - showEmpty(true) - showErrorView(false) - } - }) - ) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt index bb7ea75b..5bcdc8a1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.luckynumberwidget +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -8,7 +9,9 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getThemeWidgetKey import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach +import timber.log.Timber import javax.inject.Inject class LuckyNumberWidgetConfigurePresenter @Inject constructor( @@ -46,23 +49,25 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor( } private fun loadData() { - disposable.add(rxSingle { studentRepository.getSavedStudents(false) } - .map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } } - .map { (students, currentStudentId) -> - students.map { student -> student to (student.id == currentStudentId) } - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - when { - it.isEmpty() -> view?.openLoginView() - it.size == 1 -> { - selectedStudent = it.single().first - view?.showThemeDialog() + flowWithResource { studentRepository.getSavedStudents(false) }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Lucky number widget configure students data load") + Status.SUCCESS -> { + val widgetId = appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } + when { + it.data!!.isEmpty() -> view?.openLoginView() + it.data.size == 1 -> { + selectedStudent = it.data.single() + view?.showThemeDialog() + } + else -> view?.updateData(it.data.map { student -> + student to (student.id == widgetId) + }) } - else -> view?.updateData(it) } - }, { errorHandler.dispatch(it) })) + Status.ERROR -> errorHandler.dispatch(it.error!!) + } + }.launch() } private fun registerStudent(student: Student?) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index 204fc79a..cf8395f3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -15,6 +15,7 @@ import android.view.View.VISIBLE import android.widget.RemoteViews import dagger.android.AndroidInjection import io.github.wulkanowy.R +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.exceptions.NoCurrentStudentException @@ -24,6 +25,8 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.SchedulersProvider import io.reactivex.Maybe +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.rx2.rxMaybe import kotlinx.coroutines.rx2.rxSingle import timber.log.Timber @@ -157,7 +160,7 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { else -> Maybe.empty() } } - .flatMap { rxMaybe { luckyNumberRepository.getLuckyNumber(it) } } + .flatMap { rxMaybe { luckyNumberRepository.getLuckyNumber(it, false).takeWhile { it.status == Status.LOADING }.first().data } } .subscribeOn(schedulers.backgroundThread) .blockingGet() } catch (e: Exception) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt index 4a9d217b..1e48f71b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt @@ -5,7 +5,6 @@ import android.view.View import android.view.View.INVISIBLE import android.view.View.VISIBLE import io.github.wulkanowy.R -import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED import io.github.wulkanowy.data.repositories.message.MessageFolder.SENT import io.github.wulkanowy.data.repositories.message.MessageFolder.TRASHED @@ -77,10 +76,6 @@ class MessageFragment : BaseFragment(R.layout.fragment_m binding.messageProgress.visibility = if (show) VISIBLE else INVISIBLE } - fun onDeleteMessage(message: Message) { - presenter.onDeleteMessage(message) - } - fun onChildFragmentLoaded() { presenter.onChildViewLoaded() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt index 0f5598b2..ea482c62 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt @@ -1,13 +1,12 @@ package io.github.wulkanowy.ui.modules.message -import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Completable +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import timber.log.Timber -import java.util.concurrent.TimeUnit.MILLISECONDS import javax.inject.Inject class MessagePresenter @Inject constructor( @@ -18,12 +17,12 @@ class MessagePresenter @Inject constructor( override fun onAttachView(view: MessageView) { super.onAttachView(view) - disposable.add(Completable.timer(150, MILLISECONDS, schedulers.mainThread) - .subscribe { - view.initView() - Timber.i("Message view was initialized") - loadData() - }) + launch { + delay(150) + view.initView() + Timber.i("Message view was initialized") + loadData() + } } fun onPageSelected(index: Int) { @@ -46,15 +45,6 @@ class MessagePresenter @Inject constructor( } } - fun onDeleteMessage(message: Message) { - view?.notifyChildMessageDeleted( - when (message.removed) { - true -> 2 - else -> message.folderId - 1 - } - ) - } - fun onSendMessageButtonClicked() { view?.openSendMessage() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt index 575db75b..ec743cd7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt @@ -203,10 +203,6 @@ class MessagePreviewFragment : (activity as MainActivity).popView() } - override fun notifyParentMessageDeleted(message: Message) { - parentFragmentManager.fragments.forEach { if (it is MessageFragment) it.onDeleteMessage(message) } - } - override fun onSaveInstanceState(outState: Bundle) { outState.putSerializable(MESSAGE_ID_KEY, presenter.message) super.onSaveInstanceState(outState) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index b94c4612..4d3b83f1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -2,6 +2,7 @@ package io.github.wulkanowy.ui.modules.message.preview import android.annotation.SuppressLint import android.os.Build +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.MessageAttachment import io.github.wulkanowy.data.repositories.message.MessageRepository @@ -11,8 +12,11 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -54,33 +58,35 @@ class MessagePreviewPresenter @Inject constructor( } private fun loadData(message: Message) { - Timber.i("Loading message ${message.messageId} preview started") - disposable.apply { - clear() - add(rxSingle { studentRepository.getStudentById(message.studentId) } - .flatMap { rxSingle { messageRepository.getMessage(it, message, true) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { view?.showProgress(false) } - .subscribe({ message -> - Timber.i("Loading message ${message.message.messageId} preview result: Success ") - this@MessagePreviewPresenter.message = message.message - this@MessagePreviewPresenter.attachments = message.attachments + flowWithResourceIn { + val student = studentRepository.getStudentById(message.studentId) + messageRepository.getMessage(student, message, true) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading message ${message.messageId} preview started") + Status.SUCCESS -> { + Timber.i("Loading message ${it.data!!.message.messageId} preview result: Success ") + this@MessagePreviewPresenter.message = it.data.message + this@MessagePreviewPresenter.attachments = it.data.attachments view?.apply { - setMessageWithAttachment(message) + setMessageWithAttachment(it.data) initOptions() } analytics.logEvent( "load_item", "type" to "message_preview", - "length" to message.message.content.length + "length" to it.data.message.content.length ) - }) { + } + Status.ERROR -> { Timber.i("Loading message ${message.messageId} preview result: An exception occurred ") retryCallback = { onMessageLoadRetry(message) } - errorHandler.dispatch(it) - }) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.showProgress(false) + }.launch() } fun onReply(): Boolean { @@ -152,34 +158,37 @@ class MessagePreviewPresenter @Inject constructor( } private fun deleteMessage() { - message?.let { message -> - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { messageRepository.deleteMessage(it, message) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { + message ?: return + + view?.run { + showContent(false) + showProgress(true) + showOptions(false) + showErrorView(false) + } + + flowWithResource { + val student = studentRepository.getCurrentStudent() + messageRepository.deleteMessage(student, message!!) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Message ${message?.id} delete started") + Status.SUCCESS -> { + Timber.d("Message ${message?.id} delete success") view?.run { - showContent(false) - showProgress(true) - showOptions(false) - showErrorView(false) - } - } - .doFinally { - view?.showProgress(false) - } - .subscribe({ - view?.run { - notifyParentMessageDeleted(message) showMessage(deleteMessageSuccessString) popView() } - }, { error -> + } + Status.ERROR -> { + Timber.d("Message ${message?.id} delete failed") retryCallback = { onMessageDelete() } - errorHandler.dispatch(error) - }) - ) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.showProgress(false) + }.launch("delete") } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt index 0fdb4bda..fa6d735e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewView.kt @@ -22,8 +22,6 @@ interface MessagePreviewView : BaseView { fun showContent(show: Boolean) - fun notifyParentMessageDeleted(message: Message) - fun showErrorView(show: Boolean) fun setErrorDetails(message: String) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt index 545409c6..c31fd79a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt @@ -1,8 +1,8 @@ package io.github.wulkanowy.ui.modules.message.send +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Recipient -import io.github.wulkanowy.data.db.entities.ReportingUnit import io.github.wulkanowy.data.repositories.message.MessageRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.recipient.RecipientRepository @@ -13,10 +13,10 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.toFormattedString -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -37,7 +37,7 @@ class SendMessagePresenter @Inject constructor( view.initView() Timber.i("Send message view was initialized") loadData(message, reply) - view.apply { + with(view) { message?.let { setSubject(when (reply) { true -> "RE: " @@ -95,90 +95,89 @@ class SendMessagePresenter @Inject constructor( } private fun loadData(message: Message?, reply: Boolean?) { - var reportingUnit: ReportingUnit? = null - var recipientChips: List = emptyList() - var selectedRecipientChips: List = emptyList() + flowWithResource { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + val unit = reportingUnitRepository.getReportingUnit(student, semester.unitId) - Timber.i("Loading recipients started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) }.map { semester -> it to semester } } - .flatMapCompletable { (student, semester) -> - rxMaybe { reportingUnitRepository.getReportingUnit(student, semester.unitId) } - .doOnSuccess { reportingUnit = it } - .flatMap { rxMaybe { recipientRepository.getRecipients(student, 2, it) } } - .doOnSuccess { - Timber.i("Loading recipients result: Success, fetched %d recipients", it.size) - recipientChips = createChips(it) - } - .flatMapCompletable { - if (message == null || reply != true) Completable.complete() - else rxSingle { recipientRepository.getMessageRecipients(student, message) } - .doOnSuccess { - Timber.i("Loaded message recipients to reply result: Success, fetched %d recipients", it.size) - selectedRecipientChips = createChips(it) - } - .ignoreElement() - } - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.run { + Timber.i("Loading recipients started") + val recipients = when { + unit != null -> recipientRepository.getRecipients(student, 2, unit) + else -> listOf() + }.let { createChips(it) } + Timber.i("Loading recipients result: Success, fetched %d recipients", recipients.size) + + Timber.i("Loading message recipients started") + val messageRecipients = when { + message != null && reply == true -> recipientRepository.getMessageRecipients(student, message) + else -> emptyList() + }.let { createChips(it) } + Timber.i("Loaded message recipients to reply result: Success, fetched %d recipients", messageRecipients.size) + + Triple(unit, recipients, messageRecipients) + }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Loading recipients started") showProgress(true) showContent(false) } - } - .doFinally { view?.run { showProgress(false) } } - .subscribe({ - view?.run { - if (reportingUnit !== null) { - reportingUnit?.let { setReportingUnit(it) } - setRecipients(recipientChips) - if (selectedRecipientChips.isNotEmpty()) setSelectedRecipients(selectedRecipientChips) - showContent(true) - } else { - Timber.i("Loading recipients result: Can't find the reporting unit") - view?.showEmpty(true) + Status.SUCCESS -> it.data!!.let { (reportingUnit, recipientChips, selectedRecipientChips) -> + view?.run { + if (reportingUnit != null) { + setReportingUnit(reportingUnit) + setRecipients(recipientChips) + if (selectedRecipientChips.isNotEmpty()) setSelectedRecipients(selectedRecipientChips) + showContent(true) + } else { + Timber.i("Loading recipients result: Can't find the reporting unit") + view?.showEmpty(true) + } } } - }, { - Timber.i("Loading recipients result: An exception occurred") - view?.showContent(true) - errorHandler.dispatch(it) - })) + Status.ERROR -> { + Timber.i("Loading recipients result: An exception occurred") + view?.showContent(true) + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { showProgress(false) } + }.launch() } private fun sendMessage(subject: String, content: String, recipients: List) { - Timber.i("Sending message started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { messageRepository.sendMessage(it, subject, content, recipients) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doOnSubscribe { - view?.run { + flowWithResource { + val student = studentRepository.getCurrentStudent() + messageRepository.sendMessage(student, subject, content, recipients) + }.onEach { + when (it.status) { + Status.LOADING -> view?.run { + Timber.i("Sending message started") showSoftInput(false) showContent(false) showProgress(true) showActionBar(false) } + Status.SUCCESS -> { + Timber.i("Sending message result: Success") + view?.run { + showMessage(messageSuccess) + popView() + } + analytics.logEvent("send_message", "recipients" to recipients.size) + } + Status.ERROR -> { + Timber.i("Sending message result: An exception occurred") + view?.run { + showContent(true) + showProgress(false) + showActionBar(true) + } + errorHandler.dispatch(it.error!!) + } } - .subscribe({ - Timber.i("Sending message result: Success") - analytics.logEvent("send_message", "recipients" to recipients.size) - view?.run { - showMessage(messageSuccess) - popView() - } - }, { - Timber.i("Sending message result: An exception occurred") - view?.run { - showContent(true) - showProgress(false) - showActionBar(true) - } - errorHandler.dispatch(it) - }) - ) + }.launch("send") } private fun createChips(recipients: List): List { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index 0e96836b..3c9f0444 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.message.tab +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.repositories.message.MessageFolder import io.github.wulkanowy.data.repositories.message.MessageRepository @@ -9,13 +10,20 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.toFormattedString -import io.reactivex.subjects.PublishSubject -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.consumeAsFlow +import kotlinx.coroutines.flow.debounce +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.launch import me.xdrop.fuzzywuzzy.FuzzySearch import timber.log.Timber import java.util.Locale -import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.math.pow @@ -36,7 +44,7 @@ class MessageTabPresenter @Inject constructor( private var messages = emptyList() - private val searchQuery = PublishSubject.create() + private val searchChannel = Channel() fun onAttachView(view: MessageTabView, folder: MessageFolder) { super.onAttachView(view) @@ -64,7 +72,7 @@ class MessageTabPresenter @Inject constructor( } fun onDeleteMessage() { - loadData(false) + loadData(true) } fun onParentViewLoadData(forceRefresh: Boolean) { @@ -83,36 +91,37 @@ class MessageTabPresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean) { - Timber.i("Loading $folder message data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) } - .flatMap { rxSingle { messageRepository.getMessages(student, it, folder, forceRefresh) } } - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - showRefresh(false) - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded() + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + messageRepository.getMessages(student, semester, folder, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading $folder message data started") + Status.SUCCESS -> { + Timber.i("Loading $folder message result: Success") + messages = it.data!! + updateData(getFilteredData(lastSearchQuery)) + analytics.logEvent( + "load_data", + "type" to "messages", + "items" to it.data.size, + "folder" to folder.name + ) + } + Status.ERROR -> { + Timber.i("Loading $folder message result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .subscribe({ - Timber.i("Loading $folder message result: Success") - messages = it - view?.updateData(getFilteredData(lastSearchQuery)) - analytics.logEvent( - "load_data", - "type" to "messages", - "items" to it.size, - "folder" to folder.name - ) - }) { - Timber.i("Loading $folder message result: An exception occurred") - errorHandler.dispatch(it) - }) + }.afterLoading { + view?.run { + showRefresh(false) + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded() + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -127,23 +136,25 @@ class MessageTabPresenter @Inject constructor( } fun onSearchQueryTextChange(query: String) { - if (query != searchQuery.toString()) - searchQuery.onNext(query) + launch { + searchChannel.send(query) + } } private fun initializeSearchStream() { - disposable.add(searchQuery - .debounce(250, TimeUnit.MILLISECONDS) - .map { query -> - lastSearchQuery = query - getFilteredData(query) - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - Timber.d("Applying filter. Full list: ${messages.size}, filtered: ${it.size}") - updateData(it) - }) { Timber.e(it) }) + launch { + searchChannel.consumeAsFlow() + .debounce(250) + .map { query -> + lastSearchQuery = query + getFilteredData(query) + } + .catch { Timber.e(it) } + .collect { + Timber.d("Applying filter. Full list: ${messages.size}, filtered: ${it.size}") + updateData(it) + } + } } private fun getFilteredData(query: String): List { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt index b1dea5df..e665a15b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.mobiledevice +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.data.repositories.mobiledevice.MobileDeviceRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -8,7 +9,11 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -48,39 +53,39 @@ class MobileDevicePresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean = false) { - Timber.i("Loading mobile devices data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { mobileDeviceRepository.getDevices(student, semester, forceRefresh) } + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + mobileDeviceRepository.getDevices(student, semester, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading mobile devices data started") + Status.SUCCESS -> { + Timber.i("Loading mobile devices result: Success") + view?.run { + updateData(it.data!!) + showContent(it.data.isNotEmpty()) + showEmpty(it.data.isEmpty()) + showErrorView(false) + } + analytics.logEvent( + "load_data", + "type" to "devices", + "items" to it.data!!.size + ) + } + Status.ERROR -> { + Timber.i("Loading mobile devices result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - }.subscribe({ - Timber.i("Loading mobile devices result: Success") - view?.run { - updateData(it) - showContent(it.isNotEmpty()) - showEmpty(it.isEmpty()) - showErrorView(false) - } - analytics.logEvent( - "load_data", - "type" to "devices", - "items" to it.size, - "force_refresh" to forceRefresh - ) - }) { - Timber.i("Loading mobile devices result: An exception occurred") - errorHandler.dispatch(it) - }) + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -114,33 +119,25 @@ class MobileDevicePresenter @Inject constructor( } fun onUnregisterConfirmed(device: MobileDevice) { - Timber.i("Unregister device started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { mobileDeviceRepository.unregisterDevice(student, semester, device) } - .flatMap { rxSingle { mobileDeviceRepository.getDevices(student, semester, it) } } + flowWithResource { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + mobileDeviceRepository.unregisterDevice(student, semester, device) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Unregister device started") + Status.SUCCESS -> { + Timber.i("Unregister device result: Success") + view?.run { + showProgress(false) + enableSwipe(true) + } + } + Status.ERROR -> { + Timber.i("Unregister device result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ - Timber.i("Unregister device result: Success") - view?.run { - updateData(it) - showContent(it.isNotEmpty()) - showEmpty(it.isEmpty()) - } - }) { - Timber.i("Unregister device result: An exception occurred") - errorHandler.dispatch(it) - } - ) + }.launchIn(this) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt index 1c0506f0..f5fb7db3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.mobiledevice.token +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.mobiledevice.MobileDeviceRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -7,7 +8,9 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -28,28 +31,29 @@ class MobileDeviceTokenPresenter @Inject constructor( } private fun loadData() { - Timber.i("Mobile device registration data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { mobileDeviceRepository.getToken(student, semester) } + flowWithResource { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + mobileDeviceRepository.getToken(student, semester) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Mobile device registration data started") + Status.SUCCESS -> { + Timber.i("Mobile device registration result: Success") + view?.run { + updateData(it.data!!) + showContent() + } + analytics.logEvent("device_register", "symbol" to it.data!!.token.substring(0, 3)) + } + Status.ERROR -> { + Timber.i("Mobile device registration result: An exception occurred") + view?.closeDialog() + errorHandler.dispatch(it.error!!) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { view?.hideLoading() } - .subscribe({ - Timber.i("Mobile device registration result: Success") - view?.run { - updateData(it) - showContent() - } - analytics.logEvent("device_register", "symbol" to it.token.substring(0, 3)) - }) { - Timber.i("Mobile device registration result: An exception occurred") - view?.closeDialog() - errorHandler.dispatch(it) - } - ) + }.afterLoading { + view?.hideLoading() + }.launch() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt index 4009b4f6..8e5661ad 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.note +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.repositories.note.NoteRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -8,7 +9,11 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResource +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -49,38 +54,39 @@ class NotePresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean = false) { - Timber.i("Loading note data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) }.map { semester -> semester to it } } - .flatMap { rxSingle { noteRepository.getNotes(it.second, it.first, forceRefresh) } } - .map { items -> items.sortedByDescending { it.date } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + noteRepository.getNotes(student, semester, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading note data started") + Status.SUCCESS -> { + Timber.i("Loading note result: Success") + view?.apply { + updateData(it.data!!.sortedByDescending { item -> item.date }) + showEmpty(it.data.isEmpty()) + showErrorView(false) + showContent(it.data.isNotEmpty()) + } + analytics.logEvent( + "load_data", + "type" to "note", + "items" to it.data!!.size + ) } - }.subscribe({ - Timber.i("Loading note result: Success") - view?.apply { - updateData(it) - showEmpty(it.isEmpty()) - showErrorView(false) - showContent(it.isNotEmpty()) + Status.ERROR -> { + Timber.i("Loading note result: An exception occurred") + errorHandler.dispatch(it.error!!) } - analytics.logEvent( - "load_data", - "type" to "note", - "items" to it.size, - "force_refresh" to forceRefresh - ) - }, { - Timber.i("Loading note result: An exception occurred") - errorHandler.dispatch(it) - }) - ) + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { @@ -107,14 +113,15 @@ class NotePresenter @Inject constructor( } private fun updateNote(note: Note) { - Timber.i("Attempt to update note ${note.id}") - disposable.add(rxSingle { noteRepository.updateNote(note) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ Timber.i("Update note result: Success") }) - { error -> - Timber.i("Update note result: An exception occurred") - errorHandler.dispatch(error) - }) + flowWithResource { noteRepository.updateNote(note) }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Attempt to update note ${note.id}") + Status.SUCCESS -> Timber.i("Update note result: Success") + Status.ERROR -> { + Timber.i("Update note result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.launchIn(this) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt index 1856803c..324f2c37 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt @@ -4,9 +4,9 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Completable +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import timber.log.Timber -import java.util.concurrent.TimeUnit import javax.inject.Inject class SchoolAndTeachersPresenter @Inject constructor( @@ -17,12 +17,12 @@ class SchoolAndTeachersPresenter @Inject constructor( override fun onAttachView(view: SchoolAndTeachersView) { super.onAttachView(view) - disposable.add(Completable.timer(150, TimeUnit.MILLISECONDS, schedulers.mainThread) - .subscribe { - view.initView() - Timber.i("Message view was initialized") - loadData() - }) + launch { + delay(150) + view.initView() + Timber.i("Message view was initialized") + loadData() + } } fun onPageSelected(index: Int) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt index 334c60a3..9c10c6ed 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.schoolandteachers.school +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.school.SchoolRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -7,8 +8,9 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -64,48 +66,46 @@ class SchoolPresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean = false) { - Timber.i("Loading school info started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMapMaybe { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMapMaybe { - rxMaybe { schoolRepository.getSchoolInfo(student, it, forceRefresh) } - } - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded() - } - }.subscribe({ - Timber.i("Loading teachers result: Success") - view?.run { - address = it.address.ifBlank { null } - contact = it.contact.ifBlank { null } - updateData(it) - showContent(true) - showEmpty(false) - showErrorView(false) - } - analytics.logEvent( - "load_item", - "type" to "school", - "force_refresh" to forceRefresh - ) - }, { - Timber.i("Loading school result: An exception occurred") - errorHandler.dispatch(it) - }, { - Timber.i("Loading school result: No school info found") - view?.run { + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + schoolRepository.getSchoolInfo(student, semester, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading school info started") + Status.SUCCESS -> if (it.data != null) { + Timber.i("Loading teachers result: Success") + view?.run { + address = it.data.address.ifBlank { null } + contact = it.data.contact.ifBlank { null } + updateData(it.data) + showContent(true) + showEmpty(false) + showErrorView(false) + } + analytics.logEvent( + "load_item", + "type" to "school" + ) + } else view?.run { + Timber.i("Loading school result: No school info found") showContent(!isViewEmpty) showEmpty(isViewEmpty) showErrorView(false) } - })) + Status.ERROR -> { + Timber.i("Loading school result: An exception occurred") + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded() + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt index 2ccba71f..886f5c68 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.schoolandteachers.teacher +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.data.repositories.teacher.TeacherRepository @@ -7,7 +8,9 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -39,7 +42,7 @@ class TeacherPresenter @Inject constructor( showErrorView(false) showProgress(true) } - loadData(true) + loadData() } fun onDetailsClick() { @@ -51,41 +54,40 @@ class TeacherPresenter @Inject constructor( } private fun loadData(forceRefresh: Boolean = false) { - Timber.i("Loading teachers data started") - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { teacherRepository.getTeachers(student, semester, forceRefresh) } + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + teacherRepository.getTeachers(student, semester, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading teachers data started") + Status.SUCCESS -> { + Timber.i("Loading teachers result: Success") + view?.run { + updateData(it.data!!.filter { item -> item.name.isNotBlank() }) + showContent(it.data.isNotEmpty()) + showEmpty(it.data.isEmpty()) + showErrorView(false) + } + analytics.logEvent( + "load_data", + "type" to "teachers", + "items" to it.data!!.size + ) + } + Status.ERROR -> { + Timber.i("Loading teachers result: An exception occurred") + errorHandler.dispatch(it.error!!) } } - .map { it.filter { teacher -> teacher.name.isNotBlank() } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - notifyParentDataLoaded() - } - }.subscribe({ - Timber.i("Loading teachers result: Success") - view?.run { - updateData(it) - showContent(it.isNotEmpty()) - showEmpty(it.isEmpty()) - showErrorView(false) - } - analytics.logEvent( - "load_data", - "type" to "teachers", - "items" to it.size, - "force_refresh" to forceRefresh - ) - }) { - Timber.i("Loading teachers result: An exception occurred") - errorHandler.dispatch(it) - }) + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + notifyParentDataLoaded() + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt index bfdd1766..7ffbf41a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt @@ -1,10 +1,13 @@ package io.github.wulkanowy.ui.modules.splash +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach +import timber.log.Timber import javax.inject.Inject class SplashPresenter @Inject constructor( @@ -15,14 +18,15 @@ class SplashPresenter @Inject constructor( override fun onAttachView(view: SplashView) { super.onAttachView(view) - disposable.add(rxSingle { studentRepository.isCurrentStudentSet() } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - view.apply { - if (it) openMainView() + flowWithResource { studentRepository.isCurrentStudentSet() }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Is current user set check started") + Status.SUCCESS -> with(view) { + if (it.data!!) openMainView() else openLoginView() } - }, { errorHandler.dispatch(it) })) + Status.ERROR -> errorHandler.dispatch(it.error!!) + } + }.launch() } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt index da76854a..bc7e2689 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.ui.modules.timetable import android.annotation.SuppressLint +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -10,13 +11,17 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onEach import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.of @@ -112,60 +117,60 @@ class TimetablePresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) - currentDate = baseDate - reloadNavigation() - }) { - Timber.i("Loading semester result: An exception occurred") - }) + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }.launch("holidays") } private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { - Timber.i("Loading timetable data started") currentDate = date - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { timetableRepository.getTimetable(student, semester, currentDate, currentDate, forceRefresh) } - } - } - .map { items -> items.filter { if (prefRepository.showWholeClassPlan == "no") it.isStudentPlan else true } } - .map { items -> items.sortedWith(compareBy({ it.number }, { !it.isStudentPlan })) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + timetableRepository.getTimetable(student, semester, date, date, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading timetable data started") + Status.SUCCESS -> { Timber.i("Loading timetable result: Success") view?.apply { - updateData(it, prefRepository.showWholeClassPlan, prefRepository.showTimetableTimers) - showEmpty(it.isEmpty()) + updateData( + showWholeClassPlanType = prefRepository.showWholeClassPlan, + showTimetableTimers = prefRepository.showTimetableTimers, + data = it.data!! + .filter { item -> if (prefRepository.showWholeClassPlan == "no") item.isStudentPlan else true } + .sortedWith(compareBy({ item -> item.number }, { item -> !item.isStudentPlan })) + ) + showEmpty(it.data.isEmpty()) showErrorView(false) - showContent(it.isNotEmpty()) + showContent(it.data.isNotEmpty()) } analytics.logEvent( "load_data", "type" to "timetable", - "items" to it.size, - "force_refresh" to forceRefresh + "items" to it.data!!.size ) - }) { + } + Status.ERROR -> { Timber.i("Loading timetable result: An exception occurred") - errorHandler.dispatch(it) - }) - } + errorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt index c6a2cf84..0bab7795 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.ui.modules.timetable.completed import android.annotation.SuppressLint +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.repositories.completedlessons.CompletedLessonsRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -8,13 +9,17 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.afterLoading +import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday import io.github.wulkanowy.utils.isHolidays import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onEach import org.threeten.bp.LocalDate import org.threeten.bp.LocalDate.now import org.threeten.bp.LocalDate.ofEpochDay @@ -94,59 +99,54 @@ class CompletedLessonsPresenter @Inject constructor( } private fun setBaseDateOnHolidays() { - disposable.add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { rxSingle { semesterRepository.getCurrentSemester(it) } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) - currentDate = baseDate - reloadNavigation() - }) { - Timber.i("Loading semester result: An exception occurred") - }) + flow { + val student = studentRepository.getCurrentStudent() + emit(semesterRepository.getCurrentSemester(student)) + }.catch { + Timber.i("Loading semester result: An exception occurred") + }.onEach { + baseDate = baseDate.getLastSchoolDayIfHoliday(it.schoolYear) + currentDate = baseDate + reloadNavigation() + }.launch("holidays") } private fun loadData(date: LocalDate, forceRefresh: Boolean = false) { - Timber.i("Loading completed lessons data started") currentDate = date - disposable.apply { - clear() - add(rxSingle { studentRepository.getCurrentStudent() } - .flatMap { student -> - rxSingle { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxSingle { completedLessonsRepository.getCompletedLessons(student, semester, currentDate, currentDate, forceRefresh) } - } - } - .map { items -> items.sortedBy { it.number } } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .doFinally { - view?.run { - hideRefresh() - showProgress(false) - enableSwipe(true) - } - } - .subscribe({ + + flowWithResourceIn { + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student) + completedLessonsRepository.getCompletedLessons(student, semester, date, date, forceRefresh) + }.onEach { + when (it.status) { + Status.LOADING -> Timber.i("Loading completed lessons data started") + Status.SUCCESS -> { Timber.i("Loading completed lessons lessons result: Success") view?.apply { - updateData(it) - showEmpty(it.isEmpty()) + updateData(it.data!!.sortedBy { item -> item.number }) + showEmpty(it.data.isEmpty()) showErrorView(false) - showContent(it.isNotEmpty()) + showContent(it.data.isNotEmpty()) } analytics.logEvent( "load_data", "type" to "completed_lessons", - "items" to it.size, - "force_refresh" to forceRefresh + "items" to it.data!!.size ) - }) { + } + Status.ERROR -> { Timber.i("Loading completed lessons result: An exception occurred") - completedLessonsErrorHandler.dispatch(it) - }) - } + completedLessonsErrorHandler.dispatch(it.error!!) + } + } + }.afterLoading { + view?.run { + hideRefresh() + showProgress(false) + enableSwipe(true) + } + }.launch() } private fun showErrorViewOnError(message: String, error: Throwable) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt index cc2ac4bb..28eef06e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.timetablewidget +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -8,7 +9,9 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getThemeWidgetKey import io.github.wulkanowy.utils.SchedulersProvider -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.flowWithResource +import kotlinx.coroutines.flow.onEach +import timber.log.Timber import javax.inject.Inject class TimetableWidgetConfigurePresenter @Inject constructor( @@ -51,23 +54,25 @@ class TimetableWidgetConfigurePresenter @Inject constructor( } private fun loadData() { - disposable.add(rxSingle { studentRepository.getSavedStudents(false) } - .map { it to appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } } - .map { (students, currentStudentId) -> - students.map { student -> student to (student.id == currentStudentId) } - } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - when { - it.isEmpty() -> view?.openLoginView() - it.size == 1 && !isFromProvider -> { - selectedStudent = it.single().first - view?.showThemeDialog() + flowWithResource { studentRepository.getSavedStudents(false) }.onEach { + when (it.status) { + Status.LOADING -> Timber.d("Timetable widget configure students data load") + Status.SUCCESS -> { + val widgetId = appWidgetId?.let { id -> sharedPref.getLong(getStudentWidgetKey(id), 0) } + when { + it.data!!.isEmpty() -> view?.openLoginView() + it.data.size == 1 && !isFromProvider -> { + selectedStudent = it.data.single() + view?.showThemeDialog() + } + else -> view?.updateData(it.data.map { student -> + student to (student.id == widgetId) + }) } - else -> view?.updateData(it) } - }, { errorHandler.dispatch(it) })) + Status.ERROR -> errorHandler.dispatch(it.error!!) + } + }.launch() } private fun registerStudent(student: Student?) { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt index 6c043e9e..de09968c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt @@ -12,6 +12,7 @@ import android.widget.AdapterView.INVALID_POSITION import android.widget.RemoteViews import android.widget.RemoteViewsService import io.github.wulkanowy.R +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -25,6 +26,8 @@ import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.toFormattedString import io.reactivex.Maybe +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.rx2.rxMaybe import kotlinx.coroutines.rx2.rxSingle import org.threeten.bp.LocalDate @@ -113,7 +116,7 @@ class TimetableWidgetFactory( } .flatMap { student -> rxMaybe { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxMaybe { timetableRepository.getTimetable(student, semester, date, date) } + rxMaybe { timetableRepository.getTimetable(student, semester, date, date, true).takeWhile { it.status == Status.LOADING }.first().data } } } .map { items -> items.sortedWith(compareBy({ it.number }, { !it.isStudentPlan })) } diff --git a/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt b/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt new file mode 100644 index 00000000..6551606b --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt @@ -0,0 +1,93 @@ +package io.github.wulkanowy.utils + +import io.github.wulkanowy.data.Resource +import io.github.wulkanowy.data.Status +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach + +inline fun networkBoundResource( + showSavedOnLoading: Boolean = true, + crossinline query: () -> Flow, + crossinline fetch: suspend (ResultType) -> RequestType, + crossinline saveFetchResult: suspend (old: ResultType, new: RequestType) -> Unit, + crossinline onFetchFailed: (Throwable) -> Unit = { Unit }, + crossinline shouldFetch: (ResultType) -> Boolean = { true }, + crossinline filterResult: (ResultType) -> ResultType = { it } +) = flow { + emit(Resource.loading()) + + val data = query().first() + emitAll(if (shouldFetch(data)) { + if (showSavedOnLoading) emit(Resource.loading(filterResult(data))) + + try { + saveFetchResult(data, fetch(data)) + query().map { Resource.success(filterResult(it)) } + } catch (throwable: Throwable) { + onFetchFailed(throwable) + query().map { Resource.error(throwable, filterResult(it)) } + } + } else { + query().map { Resource.success(filterResult(it)) } + }) +} + +@JvmName("networkBoundResourceWithMap") +inline fun networkBoundResource( + showSavedOnLoading: Boolean = true, + crossinline query: () -> Flow, + crossinline fetch: suspend (ResultType) -> RequestType, + crossinline saveFetchResult: suspend (old: ResultType, new: RequestType) -> Unit, + crossinline onFetchFailed: (Throwable) -> Unit = { Unit }, + crossinline shouldFetch: (ResultType) -> Boolean = { true }, + crossinline mapResult: (ResultType) -> T +) = flow { + emit(Resource.loading()) + + val data = query().first() + emitAll(if (shouldFetch(data)) { + if (showSavedOnLoading) emit(Resource.loading(mapResult(data))) + + try { + saveFetchResult(data, fetch(data)) + query().map { Resource.success(mapResult(it)) } + } catch (throwable: Throwable) { + onFetchFailed(throwable) + query().map { Resource.error(throwable, mapResult(it)) } + } + } else { + query().map { Resource.success(mapResult(it)) } + }) +} + +fun flowWithResource(block: suspend () -> T) = flow { + emit(Resource.loading()) + try { + emit(Resource.success(block())) + } catch (e: Throwable) { + emit(Resource.error(e)) + } +} + +fun flowWithResourceIn(block: suspend () -> Flow>) = flow { + emit(Resource.loading()) + + try { + block().collect { + if (it.status != Status.LOADING) { // LOADING is already emitted + emit(it) + } + } + } catch (e: Throwable) { + emit(Resource.error(e)) + } +} + +fun Flow>.afterLoading(callback: () -> Unit) = onEach { + if (it.status != Status.LOADING) callback() +} diff --git a/app/src/main/res/layout/fragment_license.xml b/app/src/main/res/layout/fragment_license.xml index 80ecd6b1..f4105355 100644 --- a/app/src/main/res/layout/fragment_license.xml +++ b/app/src/main/res/layout/fragment_license.xml @@ -1,5 +1,6 @@ @@ -14,5 +15,6 @@ + android:layout_height="match_parent" + tools:listitem="@layout/item_license" /> diff --git a/app/src/test/java/io/github/wulkanowy/MainCoroutineRule.kt b/app/src/test/java/io/github/wulkanowy/MainCoroutineRule.kt new file mode 100644 index 00000000..10724868 --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/MainCoroutineRule.kt @@ -0,0 +1,26 @@ +package io.github.wulkanowy + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.rules.TestWatcher +import org.junit.runner.Description + +@OptIn(ExperimentalCoroutinesApi::class) +class MainCoroutineRule( + private val testDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() +) : TestWatcher() { + + override fun starting(description: Description?) { + super.starting(description) + Dispatchers.setMain(testDispatcher) + } + + override fun finished(description: Description?) { + super.finished(description) + Dispatchers.resetMain() + testDispatcher.cleanupTestCoroutines() + } +} diff --git a/app/src/test/java/io/github/wulkanowy/TestDispatchersProvider.kt b/app/src/test/java/io/github/wulkanowy/TestDispatchersProvider.kt new file mode 100644 index 00000000..e60b1d7a --- /dev/null +++ b/app/src/test/java/io/github/wulkanowy/TestDispatchersProvider.kt @@ -0,0 +1,11 @@ +package io.github.wulkanowy + +import io.github.wulkanowy.utils.DispatchersProvider +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +class TestDispatchersProvider : DispatchersProvider() { + + override val backgroundThread: CoroutineDispatcher + get() = Dispatchers.Unconfined +} diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt index 977e8205..3c01a94c 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/message/MessageRepositoryTest.kt @@ -1,6 +1,6 @@ package io.github.wulkanowy.data.repositories.message -import androidx.room.EmptyResultSetException +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.MessageWithAttachment import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.getMessageEntity @@ -10,6 +10,8 @@ import io.mockk.coEvery import io.mockk.coVerify import io.mockk.impl.annotations.MockK import io.mockk.just +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.toList import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before @@ -39,10 +41,10 @@ class MessageRepositoryTest { @Test fun `throw error when message is not in the db`() { val testMessage = getMessageEntity(1, "", false) - coEvery { local.getMessageWithAttachment(student, testMessage) } throws EmptyResultSetException("No message in database") + coEvery { local.getMessageWithAttachment(student, testMessage) } throws NullPointerException("No message in database") - val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } - assertEquals(EmptyResultSetException::class.java, message.exceptionOrNull()?.javaClass) + val message = runCatching { runBlocking { repo.getMessage(student, testMessage).toList()[1] } } + assertEquals(NullPointerException::class.java, message.exceptionOrNull()?.javaClass) } @Test @@ -50,11 +52,12 @@ class MessageRepositoryTest { val testMessage = getMessageEntity(123, "Test", false) val messageWithAttachment = MessageWithAttachment(testMessage, emptyList()) - coEvery { local.getMessageWithAttachment(student, testMessage) } returns messageWithAttachment + coEvery { local.getMessageWithAttachment(student, testMessage) } returns flowOf(messageWithAttachment) - val message = runBlocking { repo.getMessage(student, testMessage) } + val message = runBlocking { repo.getMessage(student, testMessage).toList() } - assertEquals("Test", message.message.content) + assertEquals(Status.SUCCESS, message[1].status) + assertEquals("Test", message[1].data!!.message.content) } @Test @@ -65,14 +68,15 @@ class MessageRepositoryTest { val mWa = MessageWithAttachment(testMessage, emptyList()) val mWaWithContent = MessageWithAttachment(testMessageWithContent, emptyList()) - coEvery { local.getMessageWithAttachment(student, testMessage) } returnsMany listOf(mWa, mWaWithContent) - coEvery { remote.getMessagesContentDetails(student, testMessageWithContent) } returns ("Test" to emptyList()) + coEvery { local.getMessageWithAttachment(student, testMessage) } returnsMany listOf(flowOf(mWa), flowOf(mWaWithContent)) + coEvery { remote.getMessagesContentDetails(student, any(), any()) } returns ("Test" to emptyList()) coEvery { local.updateMessages(any()) } just Runs coEvery { local.saveMessageAttachments(any()) } just Runs - val message = runBlocking { repo.getMessage(student, testMessage) } + val message = runBlocking { repo.getMessage(student, testMessage).toList() } - assertEquals("Test", message.message.content) + assertEquals(Status.SUCCESS, message[2].status) + assertEquals("Test", message[2].data!!.message.content) coVerify { local.updateMessages(listOf(testMessageWithContent)) } } @@ -83,7 +87,7 @@ class MessageRepositoryTest { coEvery { local.getMessageWithAttachment(student, testMessage) } throws UnknownHostException() - val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } + val message = runCatching { runBlocking { repo.getMessage(student, testMessage).toList()[1] } } assertEquals(UnknownHostException::class.java, message.exceptionOrNull()?.javaClass) } @@ -94,7 +98,7 @@ class MessageRepositoryTest { coEvery { local.getMessageWithAttachment(student, testMessage) } throws UnknownHostException() - val message = runCatching { runBlocking { repo.getMessage(student, testMessage) } } + val message = runCatching { runBlocking { repo.getMessage(student, testMessage).toList()[1] } } assertEquals(UnknownHostException::class.java, message.exceptionOrNull()?.javaClass) } } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt index 665185a6..89f5ba16 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt @@ -9,6 +9,8 @@ import io.mockk.coEvery import io.mockk.coVerify import io.mockk.impl.annotations.MockK import io.mockk.just +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.toList import kotlinx.coroutines.runBlocking import org.junit.Before import org.junit.Test @@ -42,12 +44,12 @@ class MobileDeviceRepositoryTest { getDeviceEntity(2) ) - coEvery { mobileDeviceLocal.getDevices(semester) } returns emptyList() + coEvery { mobileDeviceLocal.getDevices(semester) } returns flowOf(emptyList()) coEvery { mobileDeviceLocal.deleteDevices(emptyList()) } just Runs coEvery { mobileDeviceLocal.saveDevices(devices) } just Runs coEvery { mobileDeviceRemote.getDevices(student, semester) } returns devices - runBlocking { mobileDeviceRepository.getDevices(student, semester) } + runBlocking { mobileDeviceRepository.getDevices(student, semester, true).toList() } coVerify { mobileDeviceLocal.deleteDevices(emptyList()) } coVerify { mobileDeviceLocal.saveDevices(devices) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt index 161ce744..2a203103 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.data.repositories.semester +import io.github.wulkanowy.TestDispatchersProvider import io.github.wulkanowy.createSemesterEntity import io.github.wulkanowy.data.db.entities.Student import io.mockk.MockKAnnotations @@ -32,7 +33,7 @@ class SemesterRepositoryTest { @Before fun initTest() { MockKAnnotations.init(this) - semesterRepository = SemesterRepository(semesterRemote, semesterLocal) + semesterRepository = SemesterRepository(semesterRemote, semesterLocal, TestDispatchersProvider()) every { student.loginMode } returns "SCRAPPER" } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt index 85f85a37..81993081 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.ui.modules.grade import io.github.wulkanowy.createSemesterEntity +import io.github.wulkanowy.data.Resource import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.Student @@ -8,10 +9,13 @@ import io.github.wulkanowy.data.repositories.grade.GradeRepository import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.sdk.Sdk +import io.github.wulkanowy.utils.flowWithResource import io.mockk.MockKAnnotations import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.toList import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before @@ -22,6 +26,8 @@ import org.threeten.bp.LocalDateTime class GradeAverageProviderTest { + private suspend fun Flow>.getResult() = toList()[1].data!! + @MockK lateinit var preferencesRepository: PreferencesRepository @@ -93,14 +99,27 @@ class GradeAverageProviderTest { gradeAverageProvider = GradeAverageProvider(semesterRepository, gradeRepository, preferencesRepository) } + @Test + fun `force calc average on no grades`() { + every { preferencesRepository.gradeAverageForceCalc } returns true + every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS + + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { emptyList() to emptyList() } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { emptyList() to emptyList() } + + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } + + assertEquals(0, items.size) + } + @Test fun `force calc current semester average with default modifiers in scraper mode`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) // from details and after set custom plus/minus } @@ -115,9 +134,9 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(3.5, items.single { it.subject == "Język polski" }.average, .0) // from details and after set custom plus/minus } @@ -132,9 +151,9 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(3.375, items.single { it.subject == "Język polski" }.average, .0) // (from details): 3.375 } @@ -149,9 +168,9 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGradeWithModifier to secondSummariesWithModifier) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGradeWithModifier to secondSummariesWithModifier } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(3.375, items.single { it.subject == "Język polski" }.average, .0) // (from details): 3.375 } @@ -160,9 +179,9 @@ class GradeAverageProviderTest { fun `calc current semester average`() { every { preferencesRepository.gradeAverageForceCalc } returns false every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to secondSummaries } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(2.9, items.single { it.subject == "Matematyka" }.average, .0) // from summary: 2,9 @@ -173,9 +192,9 @@ class GradeAverageProviderTest { fun `force calc current semester average`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ONE_SEMESTER - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to secondSummaries } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(2.5, items.single { it.subject == "Matematyka" }.average, .0) // from details: 2,5 @@ -186,9 +205,9 @@ class GradeAverageProviderTest { fun `force calc full year average when current is first`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to firstSummaries } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[1].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[1].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.5, items.single { it.subject == "Matematyka" }.average, .0) // (from summary): 3,5 @@ -199,16 +218,20 @@ class GradeAverageProviderTest { fun `calc both semesters average`() { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS every { preferencesRepository.gradeAverageForceCalc } returns false - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to listOf( - getSummary(22, "Matematyka", 3.0), - getSummary(22, "Fizyka", 3.5) - )) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( - getSummary(22, "Matematyka", 3.5), - getSummary(22, "Fizyka", 4.0) - )) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + firstGrades to listOf( + getSummary(22, "Matematyka", 3.0), + getSummary(22, "Fizyka", 3.5) + ) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + secondGrades to listOf( + getSummary(22, "Matematyka", 3.5), + getSummary(22, "Fizyka", 4.0) + ) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.25, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries ↑): 3,0 + 3,5 → 3,25 @@ -219,13 +242,15 @@ class GradeAverageProviderTest { fun `force calc full year average`() { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( - getSummary(22, "Matematyka", 1.1), - getSummary(22, "Fizyka", 7.26) - )) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to firstSummaries } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + secondGrades to listOf( + getSummary(22, "Matematyka", 1.1), + getSummary(22, "Fizyka", 7.26) + ) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -237,10 +262,10 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to emptyList()) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to emptyList()) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to emptyList() } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to emptyList() } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -252,10 +277,10 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to emptyList()) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to emptyList()) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to emptyList() } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to emptyList() } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -267,14 +292,18 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns false every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to listOf( - getSummary(22, "Matematyka", 4.0) - )) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to listOf( - getSummary(23, "Matematyka", 3.0) - )) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + firstGrades to listOf( + getSummary(22, "Matematyka", 4.0) + ) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + secondGrades to listOf( + getSummary(23, "Matematyka", 3.0) + ) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.5, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries ↑): 4,0 + 3,0 → 3,5 @@ -286,10 +315,10 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns false every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries.dropLast(1)) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to firstSummaries } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to secondSummaries.dropLast(1) } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.4, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries): 3,9 + 2,9 → 3,4 @@ -301,10 +330,10 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns false every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries.dropLast(1)) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to firstSummaries.dropLast(1) } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to secondSummaries } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.4, items.single { it.subject == "Matematyka" }.average, .0) // (from summaries): 3,9 + 2,9 → 3,4 @@ -316,10 +345,10 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (firstGrades to firstSummaries.dropLast(1)) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (secondGrades to secondSummaries) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { firstGrades to firstSummaries.dropLast(1) } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { secondGrades to secondSummaries } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(2, items.size) assertEquals(3.0, items.single { it.subject == "Matematyka" }.average, .0) // (from details): 3,5 + 2,5 → 3,0 @@ -331,23 +360,27 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( - getGrade(22, "Fizyka", 5.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 5.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( - getGrade(23, "Fizyka", 5.0, weight = 1.0), - getGrade(23, "Fizyka", 5.0, weight = 2.0), - getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + listOf( + getGrade(22, "Fizyka", 5.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 5.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0) + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + listOf( + getGrade(23, "Fizyka", 5.0, weight = 1.0), + getGrade(23, "Fizyka", 5.0, weight = 2.0), + getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(5.2296, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,732 → 5.229636363636364 } @@ -357,23 +390,27 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageForceCalc } returns true every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( - getGrade(22, "Fizyka", 5.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 5.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( - getGrade(23, "Fizyka", 5.0, weight = 1.0), - getGrade(23, "Fizyka", 5.0, weight = 2.0), - getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + listOf( + getGrade(22, "Fizyka", 5.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 5.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0) + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + listOf( + getGrade(23, "Fizyka", 5.0, weight = 1.0), + getGrade(23, "Fizyka", 5.0, weight = 2.0), + getGrade(23, "Fizyka", 4.0, modifier = 0.3, weight = 2.0) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(5.5429, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,732 → .average() } @@ -389,23 +426,27 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( - getGrade(22, "Fizyka", 5.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 5.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( - getGrade(23, "Fizyka", 5.0, weight = 1.0), - getGrade(23, "Fizyka", 5.0, weight = 2.0), - getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + listOf( + getGrade(22, "Fizyka", 5.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 5.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0) + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + listOf( + getGrade(23, "Fizyka", 5.0, weight = 1.0), + getGrade(23, "Fizyka", 5.0, weight = 2.0), + getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(5.2636, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,8 → 5.26363636 } @@ -421,23 +462,27 @@ class GradeAverageProviderTest { every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.ALL_YEAR coEvery { semesterRepository.getSemesters(student) } returns semesters - coEvery { gradeRepository.getGrades(student, semesters[1]) } returns (listOf( - getGrade(22, "Fizyka", 5.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 5.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 4.0), - getGrade(22, "Fizyka", 6.0, weight = 2.0) - ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0))) - coEvery { gradeRepository.getGrades(student, semesters[2]) } returns (listOf( - getGrade(23, "Fizyka", 5.0, weight = 1.0), - getGrade(23, "Fizyka", 5.0, weight = 2.0), - getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) - ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))) + coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns flowWithResource { + listOf( + getGrade(22, "Fizyka", 5.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 5.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 4.0), + getGrade(22, "Fizyka", 6.0, weight = 2.0) + ) to listOf(getSummary(semesterId = 22, subject = "Fizyka", average = .0)) + } + coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns flowWithResource { + listOf( + getGrade(23, "Fizyka", 5.0, weight = 1.0), + getGrade(23, "Fizyka", 5.0, weight = 2.0), + getGrade(23, "Fizyka", 4.0, modifier = 0.33, weight = 2.0) + ) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0)) + } - val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId) } + val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() } assertEquals(5.5555, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,8 → .average() } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index 6b15fb08..02d7da34 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.login.form +import io.github.wulkanowy.MainCoroutineRule import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -7,19 +8,22 @@ import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.mockk.MockKAnnotations import io.mockk.Runs -import io.mockk.clearAllMocks import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.just import io.mockk.verify -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import org.threeten.bp.LocalDateTime.now +import java.io.IOException class LoginFormPresenterTest { + @get:Rule + val coroutineRule = MainCoroutineRule() + @MockK(relaxed = true) lateinit var loginFormView: LoginFormView @@ -35,7 +39,7 @@ class LoginFormPresenterTest { private lateinit var presenter: LoginFormPresenter @Before - fun initPresenter() { + fun setUp() { MockKAnnotations.init(this) every { loginFormView.initView() } just Runs @@ -52,11 +56,6 @@ class LoginFormPresenterTest { presenter.onAttachView(loginFormView) } - @After - fun tearDown() { - clearAllMocks() - } - @Test fun initViewTest() { verify { loginFormView.initView() } @@ -111,9 +110,9 @@ class LoginFormPresenterTest { verify { loginFormView.hideSoftKeyboard() } verify { loginFormView.showProgress(true) } -// verify { loginFormView.showProgress(false) } -// verify { loginFormView.showContent(false) } -// verify { loginFormView.showContent(true) } + verify { loginFormView.showProgress(false) } + verify { loginFormView.showContent(false) } + verify { loginFormView.showContent(true) } } @Test @@ -151,7 +150,7 @@ class LoginFormPresenterTest { @Test fun loginErrorTest() { - val testException = RuntimeException("test") + val testException = IOException("test") coEvery { repository.getStudentsScrapper(any(), any(), any(), any()) } throws testException every { loginFormView.formUsernameValue } returns "@" every { loginFormView.formPassValue } returns "123456" @@ -162,10 +161,9 @@ class LoginFormPresenterTest { presenter.onSignInClick() verify { loginFormView.hideSoftKeyboard() } - verify { loginFormView.showProgress(true) } -// verify { loginFormView.showProgress(false) } -// verify { loginFormView.showContent(false) } -// verify { loginFormView.showContent(true) } -// verify { errorHandler.dispatch(testException) } + verify { loginFormView.showProgress(false) } + verify { loginFormView.showContent(false) } + verify { loginFormView.showContent(true) } + verify { errorHandler.dispatch(match { it.message == testException.message }) } } } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt index e37642fd..8b252347 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.login.studentselect +import io.github.wulkanowy.MainCoroutineRule import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository @@ -12,15 +13,17 @@ import io.mockk.coEvery import io.mockk.every import io.mockk.impl.annotations.MockK import io.mockk.just -import io.mockk.unmockkAll import io.mockk.verify -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import org.threeten.bp.LocalDateTime.now class LoginStudentSelectPresenterTest { + @get:Rule + val coroutineRule = MainCoroutineRule() + @MockK(relaxed = true) lateinit var errorHandler: LoginErrorHandler @@ -40,8 +43,9 @@ class LoginStudentSelectPresenterTest { private val testException by lazy { RuntimeException("Problem") } @Before - fun initPresenter() { + fun setUp() { MockKAnnotations.init(this) + clearMocks(studentRepository, loginStudentSelectView) every { loginStudentSelectView.initView() } just Runs every { loginStudentSelectView.showContact(any()) } just Runs @@ -53,11 +57,6 @@ class LoginStudentSelectPresenterTest { presenter.onAttachView(loginStudentSelectView, null) } - @After - fun tearDown() { - unmockkAll() - } - @Test fun initViewTest() { verify { loginStudentSelectView.initView() } @@ -73,7 +72,7 @@ class LoginStudentSelectPresenterTest { verify { loginStudentSelectView.showContent(false) } verify { loginStudentSelectView.showProgress(true) } -// verify { loginStudentSelectView.openMainView() } + verify { loginStudentSelectView.openMainView() } } @Test @@ -84,6 +83,6 @@ class LoginStudentSelectPresenterTest { presenter.onSignIn() verify { loginStudentSelectView.showContent(false) } verify { loginStudentSelectView.showProgress(true) } - verify { errorHandler.dispatch(testException) } + verify { errorHandler.dispatch(match { testException.message == it.message }) } } } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt index 9c7d605e..eb4ac638 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt @@ -1,5 +1,6 @@ package io.github.wulkanowy.ui.modules.splash +import io.github.wulkanowy.MainCoroutineRule import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.ErrorHandler @@ -8,10 +9,14 @@ import io.mockk.coEvery import io.mockk.impl.annotations.MockK import io.mockk.verify import org.junit.Before +import org.junit.Rule import org.junit.Test class SplashPresenterTest { + @get:Rule + val coroutineRule = MainCoroutineRule() + @MockK(relaxed = true) lateinit var splashView: SplashView @@ -24,7 +29,7 @@ class SplashPresenterTest { private lateinit var presenter: SplashPresenter @Before - fun initPresenter() { + fun setUp() { MockKAnnotations.init(this) presenter = SplashPresenter(TestSchedulersProvider(), errorHandler, studentRepository) } From f6dce0fbda99ff89cdb37931db8fc101a43d530a Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 19 Jul 2020 12:00:08 +0000 Subject: [PATCH 18/34] Bump kotlinx-coroutines-rx2 from 1.3.7 to 1.3.8 (#901) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index d5031b31..87eaac80 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -129,7 +129,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.7' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7' implementation "androidx.core:core-ktx:1.3.0" From 6c4f27aff519e4ead1bc9c9d3b0e44b182012a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Mon, 27 Jul 2020 11:44:30 +0200 Subject: [PATCH 19/34] Remove threetenabp (#908) --- app/build.gradle | 13 +++--- .../data/db/migrations/Migration13Test.kt | 2 +- .../data/repositories/TestEntityCreator.kt | 4 +- .../attendance/AttendanceLocalTest.kt | 6 +-- .../CompletedLessonsLocalTest.kt | 6 +-- .../data/repositories/exam/ExamLocalTest.kt | 4 +- .../data/repositories/grade/GradeLocalTest.kt | 4 +- .../repositories/grade/GradeRepositoryTest.kt | 4 +- .../grade/TestGradeEntityCreator.kt | 2 +- .../GradeStatisticsLocalTest.kt | 2 +- .../luckynumber/LuckyNumberLocalTest.kt | 4 +- .../recipient/RecipientLocalTest.kt | 2 +- .../timetable/TestTimetableEntityCreator.kt | 4 +- .../timetable/TimetableLocalTest.kt | 4 +- .../timetable/TimetableRepositoryTest.kt | 4 +- .../java/io/github/wulkanowy/WulkanowyApp.kt | 2 - .../io/github/wulkanowy/data/db/Converters.kt | 13 +++--- .../wulkanowy/data/db/dao/AttendanceDao.kt | 2 +- .../data/db/dao/CompletedLessonsDao.kt | 2 +- .../github/wulkanowy/data/db/dao/ExamDao.kt | 2 +- .../wulkanowy/data/db/dao/HomeworkDao.kt | 2 +- .../wulkanowy/data/db/dao/LuckyNumberDao.kt | 2 +- .../wulkanowy/data/db/dao/TimetableDao.kt | 2 +- .../wulkanowy/data/db/entities/Attendance.kt | 2 +- .../data/db/entities/AttendanceSummary.kt | 2 +- .../data/db/entities/CompletedLesson.kt | 2 +- .../github/wulkanowy/data/db/entities/Exam.kt | 2 +- .../wulkanowy/data/db/entities/Grade.kt | 2 +- .../data/db/entities/GradeSummary.kt | 2 +- .../wulkanowy/data/db/entities/Homework.kt | 2 +- .../wulkanowy/data/db/entities/LuckyNumber.kt | 2 +- .../wulkanowy/data/db/entities/Message.kt | 2 +- .../data/db/entities/MobileDevice.kt | 2 +- .../github/wulkanowy/data/db/entities/Note.kt | 2 +- .../wulkanowy/data/db/entities/Semester.kt | 2 +- .../wulkanowy/data/db/entities/Student.kt | 2 +- .../wulkanowy/data/db/entities/Timetable.kt | 4 +- .../data/db/migrations/Migration5.kt | 4 +- .../attendance/AttendanceLocal.kt | 2 +- .../attendance/AttendanceRemote.kt | 6 +-- .../attendance/AttendanceRepository.kt | 2 +- .../completedlessons/CompletedLessonsLocal.kt | 2 +- .../CompletedLessonsRemote.kt | 2 +- .../CompletedLessonsRepository.kt | 2 +- .../data/repositories/exam/ExamLocal.kt | 2 +- .../data/repositories/exam/ExamRemote.kt | 2 +- .../data/repositories/exam/ExamRepository.kt | 2 +- .../repositories/grade/GradeRepository.kt | 2 +- .../repositories/homework/HomeworkLocal.kt | 2 +- .../repositories/homework/HomeworkRemote.kt | 2 +- .../homework/HomeworkRepository.kt | 2 +- .../luckynumber/LuckyNumberLocal.kt | 2 +- .../luckynumber/LuckyNumberRemote.kt | 2 +- .../luckynumber/LuckyNumberRepository.kt | 2 +- .../repositories/message/MessageRemote.kt | 2 +- .../repositories/student/StudentRemote.kt | 2 +- .../repositories/timetable/TimetableLocal.kt | 2 +- .../repositories/timetable/TimetableRemote.kt | 2 +- .../timetable/TimetableRepository.kt | 2 +- .../io/github/wulkanowy/di/BindingModule.kt | 2 +- .../wulkanowy/services/ServicesModule.kt | 2 +- .../TimetableNotificationSchedulerHelper.kt | 4 +- .../wulkanowy/services/sync/SyncManager.kt | 2 +- .../services/sync/works/AttendanceWork.kt | 2 +- .../sync/works/CompletedLessonWork.kt | 2 +- .../wulkanowy/services/sync/works/ExamWork.kt | 4 +- .../services/sync/works/HomeworkWork.kt | 2 +- .../services/sync/works/TimetableWork.kt | 4 +- .../modules/attendance/AttendanceFragment.kt | 4 +- .../modules/attendance/AttendancePresenter.kt | 6 +-- .../ui/modules/attendance/AttendanceView.kt | 2 +- .../summary/AttendanceSummaryAdapter.kt | 2 +- .../summary/AttendanceSummaryPresenter.kt | 2 +- .../wulkanowy/ui/modules/exam/ExamAdapter.kt | 2 +- .../ui/modules/exam/ExamPresenter.kt | 6 +-- .../ui/modules/homework/HomeworkAdapter.kt | 2 +- .../ui/modules/homework/HomeworkPresenter.kt | 4 +- .../homework/details/HomeworkDetailsDialog.kt | 1 - .../message/preview/MessagePreviewFragment.kt | 1 - .../wulkanowy/ui/modules/note/NoteAdapter.kt | 2 +- .../wulkanowy/ui/modules/note/NoteDialog.kt | 4 +- .../ui/modules/settings/SettingsPresenter.kt | 2 +- .../ui/modules/timetable/TimetableAdapter.kt | 2 +- .../ui/modules/timetable/TimetableDialog.kt | 2 +- .../ui/modules/timetable/TimetableFragment.kt | 2 +- .../modules/timetable/TimetablePresenter.kt | 8 ++-- .../ui/modules/timetable/TimetableView.kt | 2 +- .../completed/CompletedLessonsFragment.kt | 2 +- .../completed/CompletedLessonsPresenter.kt | 6 +-- .../completed/CompletedLessonsView.kt | 2 +- .../timetablewidget/TimetableWidgetFactory.kt | 2 +- .../TimetableWidgetProvider.kt | 4 +- .../wulkanowy/utils/SchooldaysRangeLimiter.kt | 4 +- .../wulkanowy/utils/SemesterExtension.kt | 2 +- .../github/wulkanowy/utils/TimeExtension.kt | 44 +++++++++---------- .../wulkanowy/utils/TimetableExtension.kt | 8 ++-- .../io/github/wulkanowy/TestEnityCreator.kt | 6 +-- .../attendance/AttendanceRemoteTest.kt | 4 +- .../CompletedLessonsRemoteTest.kt | 4 +- .../data/repositories/exam/ExamRemoteTest.kt | 4 +- .../luckynumber/LuckyNumberRemoteTest.kt | 2 +- .../MobileDeviceRepositoryTest.kt | 2 +- .../semester/SemesterRepositoryTest.kt | 2 +- .../timetable/TimetableRemoteTest.kt | 6 +-- .../modules/grade/GradeAverageProviderTest.kt | 6 +-- .../login/form/LoginFormPresenterTest.kt | 2 +- .../LoginStudentSelectPresenterTest.kt | 2 +- .../wulkanowy/utils/GradeExtensionTest.kt | 2 +- .../wulkanowy/utils/TimeExtensionTest.kt | 6 +-- .../wulkanowy/utils/TimetableExtensionTest.kt | 2 +- 110 files changed, 186 insertions(+), 190 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 87eaac80..0bec0db7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -86,6 +86,7 @@ android { } compileOptions { + coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } @@ -125,16 +126,18 @@ configurations.all { } dependencies { - implementation "io.github.wulkanowy:sdk:61250d3" + implementation "io.github.wulkanowy:sdk:02486b9" + + coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.8' - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8' implementation "androidx.core:core-ktx:1.3.0" implementation "androidx.activity:activity-ktx:1.1.0" - implementation "androidx.appcompat:appcompat:1.2.0-rc01" + implementation "androidx.appcompat:appcompat:1.2.0-rc02" implementation "androidx.appcompat:appcompat-resources:1.1.0" implementation "androidx.fragment:fragment-ktx:1.2.5" implementation "androidx.annotation:annotation:1.1.0" @@ -175,7 +178,6 @@ dependencies { implementation "io.reactivex.rxjava2:rxjava:2.2.19" implementation "com.google.code.gson:gson:2.8.6" - implementation "com.jakewharton.threetenabp:threetenabp:1.2.4" implementation "com.jakewharton.timber:timber:4.7.1" implementation "at.favre.lib:slf4j-timber:1.0.1" implementation "fr.bipi.treessence:treessence:0.3.2" @@ -199,8 +201,7 @@ dependencies { testImplementation "junit:junit:4.13" testImplementation "io.mockk:mockk:$mockk" - testImplementation "org.threeten:threetenbp:1.4.4" - testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.7' + testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.8' androidTestImplementation "androidx.test:core:1.2.0" androidTestImplementation "androidx.test:runner:1.2.0" diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt index 05a8a5cf..15e99f5f 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/db/migrations/Migration13Test.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test -import org.threeten.bp.LocalDate.of +import java.time.LocalDate.of import kotlin.test.assertFalse import kotlin.test.assertTrue diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt index f7aa51e4..61286fdc 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/TestEntityCreator.kt @@ -2,8 +2,8 @@ package io.github.wulkanowy.data.repositories import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDateTime +import java.time.LocalDate.now +import java.time.LocalDateTime fun getStudent(): Student { return Student( diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt index 4080b831..fa128986 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocalTest.kt @@ -12,9 +12,9 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.of +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.of import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt index f8ff9213..ca7d0b1b 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocalTest.kt @@ -12,9 +12,9 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.of +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.of import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt index e595d77c..14b29c9f 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/exam/ExamLocalTest.kt @@ -12,8 +12,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.of +import java.time.LocalDate.now +import java.time.LocalDate.of import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt index 6a01b09c..946ebf8e 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeLocalTest.kt @@ -11,8 +11,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now +import java.time.LocalDate +import java.time.LocalDate.now import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt index 5a884530..2efcca48 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/GradeRepositoryTest.kt @@ -22,8 +22,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate.of -import org.threeten.bp.LocalDateTime +import java.time.LocalDate.of +import java.time.LocalDateTime import kotlin.test.assertEquals import kotlin.test.assertFalse import kotlin.test.assertTrue diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt index 9146934b..629c2432 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/grade/TestGradeEntityCreator.kt @@ -1,6 +1,6 @@ package io.github.wulkanowy.data.repositories.grade -import org.threeten.bp.LocalDate +import java.time.LocalDate import io.github.wulkanowy.sdk.pojo.Grade as GradeRemote import io.github.wulkanowy.data.db.entities.Grade as GradeLocal diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt index ff654158..197d2d0e 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/gradestatistics/GradeStatisticsLocalTest.kt @@ -13,7 +13,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt index dfd97394..67c612dd 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocalTest.kt @@ -12,8 +12,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDate +import java.time.LocalDateTime.now import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt index 9ba8a9fb..61eb9a1b 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/recipient/RecipientLocalTest.kt @@ -12,7 +12,7 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDateTime +import java.time.LocalDateTime import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt index aa35fe79..dddf6464 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TestTimetableEntityCreator.kt @@ -1,7 +1,7 @@ package io.github.wulkanowy.data.repositories.timetable -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime +import java.time.LocalDateTime.now import io.github.wulkanowy.data.db.entities.Timetable as TimetableLocal import io.github.wulkanowy.sdk.pojo.Timetable as TimetableRemote diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt index 77d7188c..e793212e 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocalTest.kt @@ -11,8 +11,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime.of +import java.time.LocalDate +import java.time.LocalDateTime.of import kotlin.test.assertEquals @RunWith(AndroidJUnit4::class) diff --git a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt index fa62849a..1bd3c467 100644 --- a/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt +++ b/app/src/androidTest/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepositoryTest.kt @@ -20,8 +20,8 @@ import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime.of +import java.time.LocalDate +import java.time.LocalDateTime.of import kotlin.test.assertEquals @SdkSuppress(minSdkVersion = P) diff --git a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt index 0e094f69..c7c5a6fd 100644 --- a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt +++ b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt @@ -6,7 +6,6 @@ import android.util.Log.INFO import android.util.Log.VERBOSE import androidx.multidex.MultiDex import androidx.work.Configuration -import com.jakewharton.threetenabp.AndroidThreeTen import com.yariksoffice.lingver.Lingver import dagger.android.AndroidInjector import dagger.android.support.DaggerApplication @@ -43,7 +42,6 @@ class WulkanowyApp : DaggerApplication(), Configuration.Provider { override fun onCreate() { super.onCreate() - AndroidThreeTen.init(this) RxJavaPlugins.setErrorHandler(::onError) Lingver.init(this) themeManager.applyDefaultTheme() diff --git a/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt b/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt index 294f73d3..b21c4834 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/Converters.kt @@ -3,19 +3,18 @@ package io.github.wulkanowy.data.db import androidx.room.TypeConverter import com.google.gson.Gson import com.google.gson.reflect.TypeToken -import org.threeten.bp.DateTimeUtils -import org.threeten.bp.Instant -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime -import org.threeten.bp.Month -import org.threeten.bp.ZoneOffset +import java.time.Instant +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.Month +import java.time.ZoneOffset import java.util.Date class Converters { @TypeConverter fun timestampToDate(value: Long?): LocalDate? = value?.run { - DateTimeUtils.toInstant(Date(value)).atZone(ZoneOffset.UTC).toLocalDate() + Date(value).toInstant().atZone(ZoneOffset.UTC).toLocalDate() } @TypeConverter diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt index 96079547..8ef3fd44 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/AttendanceDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Attendance import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt index 4a827b4f..8c03609d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/CompletedLessonsDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.CompletedLesson import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt index e3119d9b..311eeb9c 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/ExamDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Exam import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt index 5d417b04..2092de49 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/HomeworkDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Homework import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt index 55a005ff..e3fdf01b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.LuckyNumber import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt index a099dd80..5e6eec66 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/TimetableDao.kt @@ -4,7 +4,7 @@ import androidx.room.Dao import androidx.room.Query import io.github.wulkanowy.data.db.entities.Timetable import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Singleton @Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Attendance.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Attendance.kt index aa8da8db..f141d5d5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Attendance.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Attendance.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "Attendance") data class Attendance( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/AttendanceSummary.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/AttendanceSummary.kt index d2e1f174..7d628eba 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/AttendanceSummary.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/AttendanceSummary.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.Month import java.io.Serializable +import java.time.Month @Entity(tableName = "AttendanceSummary") data class AttendanceSummary( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/CompletedLesson.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/CompletedLesson.kt index 775f3f55..e305d467 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/CompletedLesson.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/CompletedLesson.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "CompletedLesson") data class CompletedLesson( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Exam.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Exam.kt index 9ae795e7..50ed343a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Exam.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Exam.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "Exams") data class Exam( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt index 3f69c61b..a0f1c3a6 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Grade.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "Grades") data class Grade( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/GradeSummary.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/GradeSummary.kt index dd3126d4..fb7b60bb 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/GradeSummary.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/GradeSummary.kt @@ -3,7 +3,7 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDateTime +import java.time.LocalDateTime @Entity(tableName = "GradesSummary") data class GradeSummary( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Homework.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Homework.kt index cd7d153e..5b21445b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Homework.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Homework.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "Homework") data class Homework( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/LuckyNumber.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/LuckyNumber.kt index 5b9130f5..7c24c8f5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/LuckyNumber.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/LuckyNumber.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "LuckyNumbers") data class LuckyNumber ( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Message.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Message.kt index 05829841..77c8d060 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Message.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Message.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDateTime import java.io.Serializable +import java.time.LocalDateTime @Entity(tableName = "Messages") data class Message( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/MobileDevice.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/MobileDevice.kt index f67ed599..9d8f1162 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/MobileDevice.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/MobileDevice.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDateTime import java.io.Serializable +import java.time.LocalDateTime @Entity(tableName = "MobileDevices") data class MobileDevice( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Note.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Note.kt index 6f707a66..cfd54962 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Note.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Note.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate import java.io.Serializable +import java.time.LocalDate @Entity(tableName = "Notes") data class Note( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Semester.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Semester.kt index 0641e0b6..28fb2b9a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Semester.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Semester.kt @@ -4,7 +4,7 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate +import java.time.LocalDate @Entity(tableName = "Semesters", indices = [Index(value = ["student_id", "diary_id", "semester_id"], unique = true)]) data class Semester( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Student.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Student.kt index 905979f9..1edb81d5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Student.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Student.kt @@ -4,8 +4,8 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey -import org.threeten.bp.LocalDateTime import java.io.Serializable +import java.time.LocalDateTime @Entity(tableName = "Students", indices = [Index(value = ["email", "symbol", "student_id", "school_id", "class_id"], unique = true)]) data class Student( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/entities/Timetable.kt b/app/src/main/java/io/github/wulkanowy/data/db/entities/Timetable.kt index cad3b7c6..1bf159ef 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/entities/Timetable.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/entities/Timetable.kt @@ -3,9 +3,9 @@ package io.github.wulkanowy.data.db.entities import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime import java.io.Serializable +import java.time.LocalDate +import java.time.LocalDateTime @Entity(tableName = "Timetable") data class Timetable( diff --git a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration5.kt b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration5.kt index fe0dec48..dbcd916b 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration5.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/migrations/Migration5.kt @@ -2,8 +2,8 @@ package io.github.wulkanowy.data.db.migrations import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase -import org.threeten.bp.LocalDateTime.now -import org.threeten.bp.ZoneOffset +import java.time.LocalDateTime.now +import java.time.ZoneOffset class Migration5 : Migration(4, 5) { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt index 1e56d872..9aaa5230 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.AttendanceDao import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.data.db.entities.Semester import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt index 1f794f5a..870690ec 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemote.kt @@ -6,9 +6,9 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Absent import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalTime +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.LocalTime import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt index cf4edb6a..60f864f2 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRepository.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt index f68e13cb..51a1bdbf 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.CompletedLessonsDao import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.data.db.entities.Semester import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt index b3d78605..d15a2762 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemote.kt @@ -5,7 +5,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt index 7303575e..61268a66 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRepository.kt @@ -6,7 +6,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt index 2b32f527..acc55b5e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.ExamDao import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Semester import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt index 0668b5c1..ac4aa93d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRemote.kt @@ -5,7 +5,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt index 15297417..e7f115ac 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/exam/ExamRepository.kt @@ -6,7 +6,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt index 935cbedd..c42bb00d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/grade/GradeRepository.kt @@ -9,7 +9,7 @@ import io.github.wulkanowy.utils.uniqueSubtract import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map -import org.threeten.bp.LocalDateTime +import java.time.LocalDateTime import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt index 5373e1b1..f2cbb803 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.HomeworkDao import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Semester import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt index 9e99843d..32109877 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRemote.kt @@ -5,7 +5,7 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt index 5b6aeed4..54397ea0 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/homework/HomeworkRepository.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt index ecc784c4..0c3156d1 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.LuckyNumberDao import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt index e93a6c04..2872957d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemote.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt index 3553a461..ef0ced3a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.networkBoundResource import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt index 26ef3d9e..4dd52ee5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/message/MessageRemote.kt @@ -9,7 +9,7 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.sdk.pojo.Folder import io.github.wulkanowy.sdk.pojo.SentMessage import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime.now import javax.inject.Inject import javax.inject.Singleton import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt index 74d66fed..0a23eb30 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentRemote.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.data.repositories.student import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime.now import javax.inject.Inject import javax.inject.Singleton import io.github.wulkanowy.sdk.pojo.Student as SdkStudent diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt index 91a4b261..df4bfb20 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableLocal.kt @@ -4,7 +4,7 @@ import io.github.wulkanowy.data.db.dao.TimetableDao import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Timetable import kotlinx.coroutines.flow.Flow -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt index 71db8854..eef8729e 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemote.kt @@ -5,7 +5,7 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.utils.init -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt index 54ddf10b..ee2734aa 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/timetable/TimetableRepository.kt @@ -8,7 +8,7 @@ import io.github.wulkanowy.utils.networkBoundResource import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.uniqueSubtract import kotlinx.coroutines.flow.map -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt b/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt index 1b462964..246e8c70 100644 --- a/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt +++ b/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.di import dagger.Module import dagger.android.ContributesAndroidInjector import io.github.wulkanowy.di.scopes.PerActivity -import io.github.wulkanowy.ui.base.ErrorDialog import io.github.wulkanowy.services.alarm.TimetableNotificationReceiver +import io.github.wulkanowy.ui.base.ErrorDialog import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.ui.modules.login.LoginModule import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetConfigureActivity diff --git a/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt b/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt index b87f0e68..facba9cb 100644 --- a/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt +++ b/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt @@ -17,8 +17,8 @@ import io.github.wulkanowy.services.sync.channels.LuckyNumberChannel import io.github.wulkanowy.services.sync.channels.NewGradesChannel import io.github.wulkanowy.services.sync.channels.NewMessagesChannel import io.github.wulkanowy.services.sync.channels.NewNotesChannel -import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel import io.github.wulkanowy.services.sync.channels.PushChannel +import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel import io.github.wulkanowy.services.sync.works.AttendanceSummaryWork import io.github.wulkanowy.services.sync.works.AttendanceWork import io.github.wulkanowy.services.sync.works.CompletedLessonWork diff --git a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt index 54b245dd..9922a275 100644 --- a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt +++ b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt @@ -26,9 +26,9 @@ import io.github.wulkanowy.services.alarm.TimetableNotificationReceiver.Companio import io.github.wulkanowy.services.alarm.TimetableNotificationReceiver.Companion.STUDENT_NAME import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.toTimestamp -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalDateTime.now import timber.log.Timber +import java.time.LocalDateTime +import java.time.LocalDateTime.now import javax.inject.Inject class TimetableNotificationSchedulerHelper @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt index 965ed0ad..c94f8145 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt @@ -23,8 +23,8 @@ import io.github.wulkanowy.services.sync.channels.Channel import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.isHolidays import io.reactivex.Observable -import org.threeten.bp.LocalDate.now import timber.log.Timber +import java.time.LocalDate.now import java.util.concurrent.TimeUnit.MINUTES import javax.inject.Inject import javax.inject.Singleton diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt index 23cb1acd..543ec5f3 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject class AttendanceWork @Inject constructor(private val attendanceRepository: AttendanceRepository) : Work { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt index 347d8bbb..4612ebb4 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject class CompletedLessonWork @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt index a8c7fdb3..11922671 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt @@ -3,11 +3,11 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.exam.ExamRepository -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject class ExamWork @Inject constructor(private val examRepository: ExamRepository) : Work { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt index 8b69b7b6..315c2caf 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt @@ -7,7 +7,7 @@ import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt index f7701856..0832eb4d 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt @@ -3,11 +3,11 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.timetable.TimetableRepository -import io.github.wulkanowy.utils.sunday import io.github.wulkanowy.utils.monday +import io.github.wulkanowy.utils.sunday import io.reactivex.Completable import kotlinx.coroutines.rx2.rxCompletable -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now import javax.inject.Inject class TimetableWork @Inject constructor(private val timetableRepository: TimetableRepository) : Work { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt index 6599243d..51f9cb7b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt @@ -19,13 +19,13 @@ import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.databinding.DialogExcuseBinding import io.github.wulkanowy.databinding.FragmentAttendanceBinding import io.github.wulkanowy.ui.base.BaseFragment -import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.ui.modules.attendance.summary.AttendanceSummaryFragment import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView +import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.SchooldaysRangeLimiter import io.github.wulkanowy.utils.dpToPx -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject class AttendanceFragment : BaseFragment(R.layout.fragment_attendance), AttendanceView, MainView.MainChildView, diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index 0645c7a4..c5002c8b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -23,10 +23,10 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.ofEpochDay import javax.inject.Inject class AttendancePresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceView.kt index 484070a2..d54fb8bf 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceView.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.ui.modules.attendance import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.ui.base.BaseView -import org.threeten.bp.LocalDate +import java.time.LocalDate interface AttendanceView : BaseView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryAdapter.kt index 236c3da1..4250a910 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryAdapter.kt @@ -9,7 +9,7 @@ import io.github.wulkanowy.databinding.ItemAttendanceSummaryBinding import io.github.wulkanowy.databinding.ScrollableHeaderAttendanceSummaryBinding import io.github.wulkanowy.utils.calculatePercentage import io.github.wulkanowy.utils.getFormattedName -import org.threeten.bp.Month +import java.time.Month import java.util.Locale import javax.inject.Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt index 5d16b314..b5d8538f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt @@ -13,8 +13,8 @@ import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach -import org.threeten.bp.Month import timber.log.Timber +import java.time.Month import javax.inject.Inject class AttendanceSummaryPresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamAdapter.kt index 85061997..53558739 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamAdapter.kt @@ -9,7 +9,7 @@ import io.github.wulkanowy.databinding.HeaderExamBinding import io.github.wulkanowy.databinding.ItemExamBinding import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.weekDayName -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject class ExamAdapter @Inject constructor() : RecyclerView.Adapter() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt index 21f7ae6e..a5c6e851 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt @@ -20,10 +20,10 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.ofEpochDay import javax.inject.Inject class ExamPresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkAdapter.kt index a87ad18e..8ae06aeb 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkAdapter.kt @@ -10,7 +10,7 @@ import io.github.wulkanowy.databinding.HeaderHomeworkBinding import io.github.wulkanowy.databinding.ItemHomeworkBinding import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.weekDayName -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject class HomeworkAdapter @Inject constructor() : RecyclerView.Adapter() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index 612da749..cf38d9c2 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -20,9 +20,9 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.ofEpochDay import javax.inject.Inject class HomeworkPresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt index eb85066a..82938a47 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt @@ -12,7 +12,6 @@ import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.databinding.DialogHomeworkBinding import io.github.wulkanowy.ui.base.BaseDialogFragment -import io.github.wulkanowy.ui.modules.homework.HomeworkFragment import io.github.wulkanowy.utils.openInternetBrowser import javax.inject.Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt index ec743cd7..e218d759 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt @@ -23,7 +23,6 @@ import io.github.wulkanowy.databinding.FragmentMessagePreviewBinding import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView -import io.github.wulkanowy.ui.modules.message.MessageFragment import io.github.wulkanowy.ui.modules.message.send.SendMessageActivity import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.shareText diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteAdapter.kt index 2ffcad94..6c7cd3f4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteAdapter.kt @@ -6,11 +6,11 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.content.ContextCompat -import io.github.wulkanowy.sdk.scrapper.notes.Note.CategoryType import androidx.recyclerview.widget.RecyclerView import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.databinding.ItemNoteBinding +import io.github.wulkanowy.sdk.scrapper.notes.Note.CategoryType import io.github.wulkanowy.utils.getThemeAttrColor import io.github.wulkanowy.utils.toFormattedString import javax.inject.Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteDialog.kt index 6d1b181a..b175934f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteDialog.kt @@ -10,10 +10,10 @@ import androidx.fragment.app.DialogFragment import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.databinding.DialogNoteBinding -import io.github.wulkanowy.utils.getThemeAttrColor -import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.sdk.scrapper.notes.Note.CategoryType +import io.github.wulkanowy.utils.getThemeAttrColor import io.github.wulkanowy.utils.lifecycleAwareVariable +import io.github.wulkanowy.utils.toFormattedString class NoteDialog : DialogFragment() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt index bccb6f0b..ea2b2122 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt @@ -12,8 +12,8 @@ import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.isHolidays -import org.threeten.bp.LocalDate.now import timber.log.Timber +import java.time.LocalDate.now import javax.inject.Inject class SettingsPresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableAdapter.kt index 85ded202..d87f0620 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableAdapter.kt @@ -17,8 +17,8 @@ import io.github.wulkanowy.utils.isShowTimeUntil import io.github.wulkanowy.utils.left import io.github.wulkanowy.utils.toFormattedString import io.github.wulkanowy.utils.until -import org.threeten.bp.LocalDateTime import timber.log.Timber +import java.time.LocalDateTime import java.util.Timer import javax.inject.Inject import kotlin.concurrent.timer diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableDialog.kt index 8efecf07..f7d5b1ed 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableDialog.kt @@ -15,7 +15,7 @@ import io.github.wulkanowy.databinding.DialogTimetableBinding import io.github.wulkanowy.utils.getThemeAttrColor import io.github.wulkanowy.utils.lifecycleAwareVariable import io.github.wulkanowy.utils.toFormattedString -import org.threeten.bp.LocalDateTime +import java.time.LocalDateTime class TimetableDialog : DialogFragment() { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt index 2f01511a..c2be76ea 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt @@ -19,7 +19,7 @@ import io.github.wulkanowy.ui.modules.timetable.completed.CompletedLessonsFragme import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.SchooldaysRangeLimiter import io.github.wulkanowy.utils.dpToPx -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject class TimetableFragment : BaseFragment(R.layout.fragment_timetable), diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt index bc7e2689..2e232381 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt @@ -22,11 +22,11 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.of -import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.of +import java.time.LocalDate.ofEpochDay import javax.inject.Inject class TimetablePresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableView.kt index 1efa320f..fe34f1ee 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableView.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.ui.modules.timetable import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.ui.base.BaseView -import org.threeten.bp.LocalDate +import java.time.LocalDate interface TimetableView : BaseView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt index 2efd30a3..5a41f96c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt @@ -17,7 +17,7 @@ import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.SchooldaysRangeLimiter import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.getCompatDrawable -import org.threeten.bp.LocalDate +import java.time.LocalDate import javax.inject.Inject class CompletedLessonsFragment : diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt index 0bab7795..eedd4c25 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt @@ -20,10 +20,10 @@ import io.github.wulkanowy.utils.toFormattedString import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.onEach -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.ofEpochDay import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.now +import java.time.LocalDate.ofEpochDay import javax.inject.Inject class CompletedLessonsPresenter @Inject constructor( diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsView.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsView.kt index 170e1969..7e92cc63 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsView.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsView.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.ui.modules.timetable.completed import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.ui.base.BaseView -import org.threeten.bp.LocalDate +import java.time.LocalDate interface CompletedLessonsView : BaseView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt index de09968c..1ab6757a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt @@ -30,8 +30,8 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.rx2.rxMaybe import kotlinx.coroutines.rx2.rxSingle -import org.threeten.bp.LocalDate import timber.log.Timber +import java.time.LocalDate class TimetableWidgetFactory( private val timetableRepository: TimetableRepository, diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 8beef08a..4c4cc868 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -33,9 +33,9 @@ import io.github.wulkanowy.utils.toFormattedString import io.reactivex.Maybe import kotlinx.coroutines.rx2.rxMaybe import kotlinx.coroutines.rx2.rxSingle -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.now import timber.log.Timber +import java.time.LocalDate +import java.time.LocalDate.now import javax.inject.Inject class TimetableWidgetProvider : BroadcastReceiver() { diff --git a/app/src/main/java/io/github/wulkanowy/utils/SchooldaysRangeLimiter.kt b/app/src/main/java/io/github/wulkanowy/utils/SchooldaysRangeLimiter.kt index 46a707ab..e7c51745 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/SchooldaysRangeLimiter.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/SchooldaysRangeLimiter.kt @@ -3,8 +3,8 @@ package io.github.wulkanowy.utils import android.os.Parcel import android.os.Parcelable import com.wdullaer.materialdatetimepicker.date.DateRangeLimiter -import org.threeten.bp.DayOfWeek -import org.threeten.bp.LocalDate +import java.time.DayOfWeek +import java.time.LocalDate import java.util.Calendar @Suppress("UNUSED_PARAMETER") diff --git a/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt index b3c479c3..1fa3128a 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/SemesterExtension.kt @@ -1,7 +1,7 @@ package io.github.wulkanowy.utils import io.github.wulkanowy.data.db.entities.Semester -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now inline val Semester.isCurrent: Boolean get() = now() in start..end diff --git a/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt index 802b2ee0..d1aba160 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/TimeExtension.kt @@ -1,21 +1,23 @@ package io.github.wulkanowy.utils -import org.threeten.bp.DayOfWeek.FRIDAY -import org.threeten.bp.DayOfWeek.MONDAY -import org.threeten.bp.DayOfWeek.SATURDAY -import org.threeten.bp.DayOfWeek.SUNDAY -import org.threeten.bp.Instant.ofEpochMilli -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalDateTime.ofInstant -import org.threeten.bp.Month -import org.threeten.bp.ZoneId -import org.threeten.bp.ZoneOffset -import org.threeten.bp.format.DateTimeFormatter.ofPattern -import org.threeten.bp.format.TextStyle.FULL_STANDALONE -import org.threeten.bp.temporal.TemporalAdjusters.firstInMonth -import org.threeten.bp.temporal.TemporalAdjusters.next -import org.threeten.bp.temporal.TemporalAdjusters.previous +import android.annotation.SuppressLint +import java.time.DayOfWeek.FRIDAY +import java.time.DayOfWeek.MONDAY +import java.time.DayOfWeek.SATURDAY +import java.time.DayOfWeek.SUNDAY +import java.time.Instant.ofEpochMilli +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.LocalDateTime.ofInstant +import java.time.Month +import java.time.ZoneId +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter.ofPattern +import java.time.format.TextStyle.FULL_STANDALONE +import java.time.format.TextStyle.* +import java.time.temporal.TemporalAdjusters.firstInMonth +import java.time.temporal.TemporalAdjusters.next +import java.time.temporal.TemporalAdjusters.previous import java.util.Locale private const val DATE_PATTERN = "dd.MM.yyyy" @@ -24,17 +26,15 @@ fun String.toLocalDate(format: String = DATE_PATTERN): LocalDate = LocalDate.par fun LocalDateTime.toTimestamp() = atZone(ZoneId.systemDefault()).withZoneSameInstant(ZoneOffset.UTC).toInstant().toEpochMilli() -fun Long.toLocalDateTime() = ofInstant(ofEpochMilli(this), ZoneId.systemDefault()) +fun Long.toLocalDateTime(): LocalDateTime = ofInstant(ofEpochMilli(this), ZoneId.systemDefault()) fun LocalDate.toFormattedString(format: String = DATE_PATTERN): String = format(ofPattern(format)) fun LocalDateTime.toFormattedString(format: String = DATE_PATTERN): String = format(ofPattern(format)) -/** - * https://github.com/ThreeTen/threetenbp/issues/55 - */ +@SuppressLint("DefaultLocale") fun Month.getFormattedName(): String { - return getDisplayName(FULL_STANDALONE, Locale.getDefault()) + return getDisplayName(FULL, Locale.getDefault()) .let { when (it) { "stycznia" -> "Styczeń" @@ -51,7 +51,7 @@ fun Month.getFormattedName(): String { "grudnia" -> "Grudzień" else -> it } - } + }.capitalize() } inline val LocalDate.nextSchoolDay: LocalDate diff --git a/app/src/main/java/io/github/wulkanowy/utils/TimetableExtension.kt b/app/src/main/java/io/github/wulkanowy/utils/TimetableExtension.kt index ccb2afeb..f3591306 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/TimetableExtension.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/TimetableExtension.kt @@ -1,10 +1,10 @@ package io.github.wulkanowy.utils import io.github.wulkanowy.data.db.entities.Timetable -import org.threeten.bp.Duration -import org.threeten.bp.Duration.between -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalDateTime.now +import java.time.Duration +import java.time.Duration.between +import java.time.LocalDateTime +import java.time.LocalDateTime.now fun Timetable.isShowTimeUntil(previousLessonEnd: LocalDateTime?) = when { !isStudentPlan -> false diff --git a/app/src/test/java/io/github/wulkanowy/TestEnityCreator.kt b/app/src/test/java/io/github/wulkanowy/TestEnityCreator.kt index f7d29220..09486ed9 100644 --- a/app/src/test/java/io/github/wulkanowy/TestEnityCreator.kt +++ b/app/src/test/java/io/github/wulkanowy/TestEnityCreator.kt @@ -5,9 +5,9 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.sdk.Sdk -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDateTime -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDate +import java.time.LocalDateTime +import java.time.LocalDateTime.now fun createSemesterEntity(diaryId: Int, semesterId: Int, start: LocalDate, end: LocalDate, semesterName: Int = 1): Semester { return Semester( diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt index a4ca16b6..2400c6c4 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/attendance/AttendanceRemoteTest.kt @@ -14,8 +14,8 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.of +import java.time.LocalDate +import java.time.LocalDate.of class AttendanceRemoteTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt index 31b2af5b..7be3f84f 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/completedlessons/CompletedLessonsRemoteTest.kt @@ -14,8 +14,8 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.of +import java.time.LocalDate +import java.time.LocalDate.of class CompletedLessonsRemoteTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt index 868f6025..23bf0297 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/exam/ExamRemoteTest.kt @@ -14,8 +14,8 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.of +import java.time.LocalDate +import java.time.LocalDate.of class ExamRemoteTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt index 84761ada..1a8157f6 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRemoteTest.kt @@ -11,7 +11,7 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate +import java.time.LocalDate class LuckyNumberRemoteTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt index 89f5ba16..286cf5e9 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/mobiledevice/MobileDeviceRepositoryTest.kt @@ -14,7 +14,7 @@ import kotlinx.coroutines.flow.toList import kotlinx.coroutines.runBlocking import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDateTime.of +import java.time.LocalDateTime.of class MobileDeviceRepositoryTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt index 2a203103..866c2af4 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/semester/SemesterRepositoryTest.kt @@ -15,7 +15,7 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertNotEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate.now +import java.time.LocalDate.now class SemesterRepositoryTest { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt index a88c87bd..c948ba31 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/timetable/TimetableRemoteTest.kt @@ -13,9 +13,9 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate -import org.threeten.bp.LocalDate.of -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDate +import java.time.LocalDate.of +import java.time.LocalDateTime.now class TimetableRemoteTest { diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt index 81993081..33af3f78 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/grade/GradeAverageProviderTest.kt @@ -20,9 +20,9 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate.now -import org.threeten.bp.LocalDate.of -import org.threeten.bp.LocalDateTime +import java.time.LocalDate.now +import java.time.LocalDate.of +import java.time.LocalDateTime class GradeAverageProviderTest { diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index 02d7da34..32c6a74e 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -16,7 +16,7 @@ import io.mockk.verify import org.junit.Before import org.junit.Rule import org.junit.Test -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime.now import java.io.IOException class LoginFormPresenterTest { diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt index 8b252347..8176fc2e 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt @@ -17,7 +17,7 @@ import io.mockk.verify import org.junit.Before import org.junit.Rule import org.junit.Test -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime.now class LoginStudentSelectPresenterTest { diff --git a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt index 6cc37e11..e1693d52 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/GradeExtensionTest.kt @@ -8,7 +8,7 @@ import io.mockk.impl.annotations.MockK import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test -import org.threeten.bp.LocalDate +import java.time.LocalDate class GradeExtensionTest { diff --git a/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt index 72d08c41..9709ded3 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/TimeExtensionTest.kt @@ -4,9 +4,9 @@ import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -import org.threeten.bp.LocalDate.of -import org.threeten.bp.LocalDateTime -import org.threeten.bp.Month.JANUARY +import java.time.LocalDate.of +import java.time.LocalDateTime +import java.time.Month.JANUARY import java.util.Locale class TimeExtensionTest { diff --git a/app/src/test/java/io/github/wulkanowy/utils/TimetableExtensionTest.kt b/app/src/test/java/io/github/wulkanowy/utils/TimetableExtensionTest.kt index 33a79850..eac84759 100644 --- a/app/src/test/java/io/github/wulkanowy/utils/TimetableExtensionTest.kt +++ b/app/src/test/java/io/github/wulkanowy/utils/TimetableExtensionTest.kt @@ -6,7 +6,7 @@ import org.junit.Assert.assertFalse import org.junit.Assert.assertNotEquals import org.junit.Assert.assertTrue import org.junit.Test -import org.threeten.bp.LocalDateTime.now +import java.time.LocalDateTime.now class TimetableExtensionTest { From 6a1a34757988e7580518ad649802d686c1864b87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Mon, 27 Jul 2020 13:20:45 +0200 Subject: [PATCH 20/34] Migrate workers and app widgets to coroutines (#907) --- app/build.gradle | 7 +- app/proguard-rules.pro | 7 -- .../java/io/github/wulkanowy/WulkanowyApp.kt | 12 --- .../java/io/github/wulkanowy/di/AppModule.kt | 5 -- .../alarm/TimetableNotificationReceiver.kt | 26 +++---- .../wulkanowy/services/sync/SyncManager.kt | 9 ++- .../wulkanowy/services/sync/SyncWorker.kt | 75 +++++++++---------- .../sync/works/AttendanceSummaryWork.kt | 8 +- .../services/sync/works/AttendanceWork.kt | 7 +- .../sync/works/CompletedLessonWork.kt | 7 +- .../wulkanowy/services/sync/works/ExamWork.kt | 7 +- .../sync/works/GradeStatisticsWork.kt | 15 ++-- .../services/sync/works/GradeWork.kt | 33 ++++---- .../services/sync/works/HomeworkWork.kt | 7 +- .../services/sync/works/LuckyNumberWork.kt | 18 ++--- .../services/sync/works/MessageWork.kt | 18 ++--- .../wulkanowy/services/sync/works/NoteWork.kt | 18 ++--- .../services/sync/works/RecipientWork.kt | 18 ++--- .../services/sync/works/TeacherWork.kt | 7 +- .../services/sync/works/TimetableWork.kt | 11 +-- .../wulkanowy/services/sync/works/Work.kt | 12 +-- .../widgets/TimetableWidgetService.kt | 8 +- .../github/wulkanowy/ui/base/BasePresenter.kt | 9 +-- .../ui/modules/about/AboutPresenter.kt | 4 +- .../about/contributor/ContributorPresenter.kt | 4 +- .../modules/about/license/LicensePresenter.kt | 4 +- .../about/logviewer/LogViewerPresenter.kt | 4 +- .../ui/modules/account/AccountPresenter.kt | 4 +- .../modules/attendance/AttendancePresenter.kt | 4 +- .../summary/AttendanceSummaryPresenter.kt | 4 +- .../ui/modules/exam/ExamPresenter.kt | 4 +- .../ui/modules/grade/GradePresenter.kt | 4 +- .../grade/details/GradeDetailsPresenter.kt | 4 +- .../statistics/GradeStatisticsPresenter.kt | 4 +- .../grade/summary/GradeSummaryPresenter.kt | 4 +- .../ui/modules/homework/HomeworkPresenter.kt | 4 +- .../details/HomeworkDetailsPresenter.kt | 4 +- .../ui/modules/login/LoginPresenter.kt | 4 +- .../login/advanced/LoginAdvancedPresenter.kt | 4 +- .../modules/login/form/LoginFormPresenter.kt | 4 +- .../login/recover/LoginRecoverPresenter.kt | 4 +- .../LoginStudentSelectPresenter.kt | 4 +- .../login/symbol/LoginSymbolPresenter.kt | 4 +- .../luckynumber/LuckyNumberPresenter.kt | 4 +- .../LuckyNumberWidgetConfigurePresenter.kt | 4 +- .../LuckyNumberWidgetProvider.kt | 50 +++++-------- .../ui/modules/main/MainPresenter.kt | 4 +- .../ui/modules/message/MessagePresenter.kt | 4 +- .../preview/MessagePreviewPresenter.kt | 4 +- .../message/send/SendMessagePresenter.kt | 4 +- .../message/tab/MessageTabPresenter.kt | 4 +- .../mobiledevice/MobileDevicePresenter.kt | 4 +- .../token/MobileDeviceTokenPresenter.kt | 4 +- .../ui/modules/more/MorePresenter.kt | 4 +- .../ui/modules/note/NotePresenter.kt | 4 +- .../SchoolAndTeachersPresenter.kt | 4 +- .../school/SchoolPresenter.kt | 4 +- .../teacher/TeacherPresenter.kt | 4 +- .../ui/modules/settings/SettingsPresenter.kt | 50 ++++++------- .../ui/modules/splash/SplashPresenter.kt | 4 +- .../modules/timetable/TimetablePresenter.kt | 4 +- .../completed/CompletedLessonsPresenter.kt | 4 +- .../TimetableWidgetConfigurePresenter.kt | 4 +- .../timetablewidget/TimetableWidgetFactory.kt | 49 +++++------- .../TimetableWidgetProvider.kt | 59 ++++++--------- .../io/github/wulkanowy/utils/FlowUtils.kt | 6 ++ .../wulkanowy/utils/SchedulersProvider.kt | 14 ---- .../wulkanowy/TestSchedulersProvider.kt | 15 ---- .../ui/modules/login/LoginPresenterTest.kt | 3 +- .../login/form/LoginFormPresenterTest.kt | 3 +- .../LoginStudentSelectPresenterTest.kt | 3 +- .../ui/modules/main/MainPresenterTest.kt | 3 +- .../ui/modules/splash/SplashPresenterTest.kt | 3 +- 73 files changed, 269 insertions(+), 485 deletions(-) delete mode 100644 app/src/main/java/io/github/wulkanowy/utils/SchedulersProvider.kt delete mode 100644 app/src/test/java/io/github/wulkanowy/TestSchedulersProvider.kt diff --git a/app/build.gradle b/app/build.gradle index 0bec0db7..fe316e86 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -132,7 +132,6 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' - implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-rx2:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8' implementation "androidx.core:core-ktx:1.3.0" @@ -155,10 +154,9 @@ dependencies { implementation "me.zhanghai.android.materialprogressbar:library:1.6.1" implementation "androidx.work:work-runtime-ktx:$work_manager" - implementation "androidx.work:work-rxjava2:$work_manager" implementation "androidx.work:work-gcm:$work_manager" - implementation 'com.github.PaulinaSadowska:RxWorkManagerObservers:1.0.0' + implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0" implementation "androidx.room:room-runtime:$room" implementation "androidx.room:room-ktx:$room" @@ -174,9 +172,6 @@ dependencies { implementation "com.ncapdevi:frag-nav:3.3.0" implementation "com.github.YarikSOffice:lingver:1.2.2" - implementation "io.reactivex.rxjava2:rxandroid:2.1.1" - implementation "io.reactivex.rxjava2:rxjava:2.2.19" - implementation "com.google.code.gson:gson:2.8.6" implementation "com.jakewharton.timber:timber:4.7.1" implementation "at.favre.lib:slf4j-timber:1.0.1" diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index 1a8b8c32..7c796257 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -30,13 +30,6 @@ -dontwarn javax.annotation.** -#Config for ReactiveNetwork --dontwarn com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork --dontwarn io.reactivex.functions.Function --dontwarn rx.internal.util.** --dontwarn sun.misc.Unsafe - - #Config for MPAndroidChart -keep class com.github.mikephil.charting.** { *; } diff --git a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt index c7c5a6fd..9b1944c3 100644 --- a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt +++ b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt @@ -18,10 +18,7 @@ import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.CrashlyticsExceptionTree import io.github.wulkanowy.utils.CrashlyticsTree import io.github.wulkanowy.utils.DebugLogTree -import io.reactivex.exceptions.UndeliverableException -import io.reactivex.plugins.RxJavaPlugins import timber.log.Timber -import java.io.IOException import javax.inject.Inject class WulkanowyApp : DaggerApplication(), Configuration.Provider { @@ -42,7 +39,6 @@ class WulkanowyApp : DaggerApplication(), Configuration.Provider { override fun onCreate() { super.onCreate() - RxJavaPlugins.setErrorHandler(::onError) Lingver.init(this) themeManager.applyDefaultTheme() @@ -66,14 +62,6 @@ class WulkanowyApp : DaggerApplication(), Configuration.Provider { registerActivityLifecycleCallbacks(ActivityLifecycleLogger()) } - private fun onError(error: Throwable) { - //RxJava's too deep stack traces may cause SOE on older android devices - val cause = error.cause - if (error is UndeliverableException && cause is IOException || cause is InterruptedException || cause is StackOverflowError) { - Timber.e(cause, "An undeliverable error occurred") - } else throw error - } - override fun applicationInjector(): AndroidInjector { return DaggerAppComponent.factory().create(this) } diff --git a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt index 29b66a4e..d791f0a3 100644 --- a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt +++ b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt @@ -8,7 +8,6 @@ import dagger.Provides import io.github.wulkanowy.WulkanowyApp import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.DispatchersProvider -import io.github.wulkanowy.utils.SchedulersProvider import javax.inject.Singleton @Module @@ -18,10 +17,6 @@ internal class AppModule { @Provides fun provideContext(app: WulkanowyApp): Context = app - @Singleton - @Provides - fun provideSchedulersProvider() = SchedulersProvider() - @Singleton @Provides fun provideDispatchersProvider() = DispatchersProvider() diff --git a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt index 283f0a98..1e68685f 100644 --- a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt +++ b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt @@ -12,14 +12,17 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import dagger.android.AndroidInjection import io.github.wulkanowy.R +import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView -import io.github.wulkanowy.utils.SchedulersProvider +import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.toLocalDateTime -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject @@ -28,9 +31,6 @@ class TimetableNotificationReceiver : BroadcastReceiver() { @Inject lateinit var studentRepository: StudentRepository - @Inject - lateinit var schedulers: SchedulersProvider - companion object { const val NOTIFICATION_TYPE_CURRENT = 1 const val NOTIFICATION_TYPE_UPCOMING = 2 @@ -54,14 +54,14 @@ class TimetableNotificationReceiver : BroadcastReceiver() { Timber.d("Receiving intent... ${intent.toUri(0)}") AndroidInjection.inject(this, context) - rxSingle { studentRepository.getCurrentStudent(false) } - .subscribeOn(schedulers.backgroundThread) - .observeOn(schedulers.mainThread) - .subscribe({ - val studentId = intent.getIntExtra(STUDENT_ID, 0) - if (it.studentId == studentId) prepareNotification(context, intent) - else Timber.d("Notification studentId($studentId) differs from current(${it.studentId})") - }, { Timber.e(it) }) + flowWithResource { + val student = studentRepository.getCurrentStudent(false) + val studentId = intent.getIntExtra(STUDENT_ID, 0) + if (student.studentId == studentId) prepareNotification(context, intent) + else Timber.d("Notification studentId($studentId) differs from current(${student.studentId})") + }.onEach { + if (it.status == Status.ERROR) Timber.e(it.error!!) + }.launchIn(GlobalScope) } private fun prepareNotification(context: Context, intent: Intent) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt index c94f8145..1d005ae8 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncManager.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.services.sync import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.O import androidx.core.app.NotificationManagerCompat +import androidx.lifecycle.asFlow import androidx.work.BackoffPolicy.EXPONENTIAL import androidx.work.Constraints import androidx.work.Data @@ -15,14 +16,13 @@ import androidx.work.OneTimeWorkRequestBuilder import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkInfo import androidx.work.WorkManager -import com.paulinasadowska.rxworkmanagerobservers.extensions.getWorkInfoByIdObservable import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.SharedPrefProvider.Companion.APP_VERSION_CODE_KEY import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.services.sync.channels.Channel import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.isHolidays -import io.reactivex.Observable +import kotlinx.coroutines.flow.Flow import timber.log.Timber import java.time.LocalDate.now import java.util.concurrent.TimeUnit.MINUTES @@ -67,7 +67,7 @@ class SyncManager @Inject constructor( } } - fun startOneTimeSyncWorker(): Observable { + fun startOneTimeSyncWorker(): Flow { val work = OneTimeWorkRequestBuilder() .setInputData( Data.Builder() @@ -77,7 +77,8 @@ class SyncManager @Inject constructor( .build() workManager.enqueueUniqueWork("${SyncWorker::class.java.simpleName}_one_time", ExistingWorkPolicy.REPLACE, work) - return workManager.getWorkInfoByIdObservable(work.id) + + return workManager.getWorkInfoByIdLiveData(work.id).asFlow() } fun stopSyncWorker() { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt index fc02ca75..75711f0e 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt @@ -5,9 +5,9 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.BigTextStyle import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT import androidx.core.app.NotificationManagerCompat +import androidx.work.CoroutineWorker import androidx.work.Data import androidx.work.ListenableWorker -import androidx.work.RxWorker import androidx.work.WorkerParameters import com.squareup.inject.assisted.Assisted import com.squareup.inject.assisted.AssistedInject @@ -20,10 +20,7 @@ import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException import io.github.wulkanowy.services.sync.channels.DebugChannel import io.github.wulkanowy.services.sync.works.Work import io.github.wulkanowy.utils.getCompatColor -import io.reactivex.Completable -import io.reactivex.Single -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.coroutineScope import timber.log.Timber import kotlin.random.Random @@ -35,45 +32,43 @@ class SyncWorker @AssistedInject constructor( private val works: Set<@JvmSuppressWildcards Work>, private val preferencesRepository: PreferencesRepository, private val notificationManager: NotificationManagerCompat -) : RxWorker(appContext, workerParameters) { +) : CoroutineWorker(appContext, workerParameters) { - override fun createWork(): Single { + override suspend fun doWork() = coroutineScope { Timber.i("SyncWorker is starting") - return rxSingle { studentRepository.isCurrentStudentSet() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getCurrentStudent() } } - .flatMapCompletable { student -> - rxSingle { semesterRepository.getCurrentSemester(student, true) } - .flatMapCompletable { semester -> - Completable.mergeDelayError(works.map { work -> - work.create(student, semester) - .onErrorResumeNext { - if (it is FeatureDisabledException || it is FeatureNotAvailableException) Completable.complete() - else Completable.error(it) - } - .doOnSubscribe { Timber.i("${work::class.java.simpleName} is starting") } - .doOnError { Timber.i("${work::class.java.simpleName} result: An exception occurred") } - .doOnComplete { Timber.i("${work::class.java.simpleName} result: Success") } - }) - } + + if (!studentRepository.isCurrentStudentSet()) return@coroutineScope Result.failure() + + val student = studentRepository.getCurrentStudent() + val semester = semesterRepository.getCurrentSemester(student, true) + + val exceptions = works.mapNotNull { work -> + try { + Timber.i("${work::class.java.simpleName} is starting") + work.doWork(student, semester) + Timber.i("${work::class.java.simpleName} result: Success") + null + } catch (e: Throwable) { + Timber.w("${work::class.java.simpleName} result: An exception ${e.message} occurred") + if (e is FeatureDisabledException || e is FeatureNotAvailableException) null + else e } - .toSingleDefault(Result.success()) - .onErrorReturn { - Timber.e(it, "There was an error during synchronization") - when { - inputData.getBoolean("one_time", false) -> { - Result.failure(Data.Builder() - .putString("error", it.toString()) - .build() - ) - } - else -> Result.retry() - } - } - .doOnSuccess { - if (preferencesRepository.isDebugNotificationEnable) notify(it) - Timber.i("SyncWorker result: $it") + } + val result = when { + exceptions.isNotEmpty() && inputData.getBoolean("one_time", false) -> { + Result.failure(Data.Builder() + .putString("error", exceptions.toString()) + .build() + ) } + exceptions.isNotEmpty() -> Result.retry() + else -> Result.success() + } + + if (preferencesRepository.isDebugNotificationEnable) notify(result) + Timber.i("SyncWorker result: $result") + + result } private fun notify(result: Result) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt index 3d118e6f..9e4ab902 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceSummaryWork.kt @@ -3,16 +3,14 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.attendancesummary.AttendanceSummaryRepository -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import javax.inject.Inject class AttendanceSummaryWork @Inject constructor( private val attendanceSummaryRepository: AttendanceSummaryRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + attendanceSummaryRepository.getAttendanceSummary(student, semester, -1, true).waitForResult() } } - diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt index 543ec5f3..ecf0be01 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/AttendanceWork.kt @@ -5,14 +5,13 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.attendance.AttendanceRepository import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject class AttendanceWork @Inject constructor(private val attendanceRepository: AttendanceRepository) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + attendanceRepository.getAttendance(student, semester, now().monday, now().sunday, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt index 4612ebb4..212ea632 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/CompletedLessonWork.kt @@ -5,8 +5,7 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.completedlessons.CompletedLessonsRepository import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject @@ -14,7 +13,7 @@ class CompletedLessonWork @Inject constructor( private val completedLessonsRepository: CompletedLessonsRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + completedLessonsRepository.getCompletedLessons(student, semester, now().monday, now().sunday, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt index 11922671..f7d8db0a 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/ExamWork.kt @@ -5,14 +5,13 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.exam.ExamRepository import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject class ExamWork @Inject constructor(private val examRepository: ExamRepository) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { examRepository.getExams(student, semester, now().monday, now().sunday, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + examRepository.getExams(student, semester, now().monday, now().sunday, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt index cbfa2d9a..3d61a423 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeStatisticsWork.kt @@ -3,21 +3,18 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.gradestatistics.GradeStatisticsRepository -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import javax.inject.Inject class GradeStatisticsWork @Inject constructor( private val gradeStatisticsRepository: GradeStatisticsRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { - with(gradeStatisticsRepository) { - getGradesStatistics(student, semester, "Wszystkie", isSemester = true, forceRefresh = true).waitForResult() - getGradesStatistics(student, semester, "Wszystkie", isSemester = false, forceRefresh = true).waitForResult() - getGradesPointsStatistics(student, semester, "Wszystkie", forceRefresh = true).waitForResult() - } + override suspend fun doWork(student: Student, semester: Semester) { + with(gradeStatisticsRepository) { + getGradesStatistics(student, semester, "Wszystkie", isSemester = true, forceRefresh = true).waitForResult() + getGradesStatistics(student, semester, "Wszystkie", isSemester = false, forceRefresh = true).waitForResult() + getGradesPointsStatistics(student, semester, "Wszystkie", forceRefresh = true).waitForResult() } } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt index 74180993..9d4df201 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt @@ -18,10 +18,8 @@ import io.github.wulkanowy.services.sync.channels.NewGradesChannel import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor -import io.reactivex.Completable +import io.github.wulkanowy.utils.waitForResult import kotlinx.coroutines.flow.first -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -32,18 +30,23 @@ class GradeWork @Inject constructor( private val preferencesRepository: PreferencesRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() } - .concatWith(Completable.concatArray(rxSingle { gradeRepository.getNotNotifiedGrades(semester).first() }.flatMapCompletable { - if (it.isNotEmpty()) notifyDetails(it) - rxCompletable { gradeRepository.updateGrades(it.onEach { grade -> grade.isNotified = true }) } - }, rxSingle { gradeRepository.getNotNotifiedPredictedGrades(semester).first() }.flatMapCompletable { - if (it.isNotEmpty()) notifyPredicted(it) - rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isPredictedGradeNotified = true }) } - }, rxSingle { gradeRepository.getNotNotifiedFinalGrades(semester).first() }.flatMapCompletable { - if (it.isNotEmpty()) notifyFinal(it) - rxCompletable { gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isFinalGradeNotified = true }) } - })) + override suspend fun doWork(student: Student, semester: Semester) { + gradeRepository.getGrades(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() + + gradeRepository.getNotNotifiedGrades(semester).first().let { + if (it.isNotEmpty()) notifyDetails(it) + gradeRepository.updateGrades(it.onEach { grade -> grade.isNotified = true }) + } + + gradeRepository.getNotNotifiedPredictedGrades(semester).first().let { + if (it.isNotEmpty()) notifyPredicted(it) + gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isPredictedGradeNotified = true }) + } + + gradeRepository.getNotNotifiedFinalGrades(semester).first().let { + if (it.isNotEmpty()) notifyFinal(it) + gradeRepository.updateGradesSummary(it.onEach { grade -> grade.isFinalGradeNotified = true }) + } } private fun getNotificationBuilder(): NotificationCompat.Builder { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt index 315c2caf..a07f1107 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/HomeworkWork.kt @@ -5,14 +5,13 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.homework.HomeworkRepository import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject class HomeworkWork @Inject constructor(private val homeworkRepository: HomeworkRepository) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + homeworkRepository.getHomework(student, semester, now().monday, now().sunday, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index 9bf3de0c..0024d903 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -17,10 +17,8 @@ import io.github.wulkanowy.services.sync.channels.LuckyNumberChannel import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor -import io.reactivex.Completable +import io.github.wulkanowy.utils.waitForResult import kotlinx.coroutines.flow.first -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxMaybe import javax.inject.Inject import kotlin.random.Random @@ -31,13 +29,13 @@ class LuckyNumberWork @Inject constructor( private val preferencesRepository: PreferencesRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxMaybe { luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable).waitForResult() } - .flatMap { rxMaybe { luckyNumberRepository.getNotNotifiedLuckyNumber(student).first() } } - .flatMapCompletable { - notify(it) - rxCompletable { luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) } - } + override suspend fun doWork(student: Student, semester: Semester) { + luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable).waitForResult() + + luckyNumberRepository.getNotNotifiedLuckyNumber(student).first()?.let { + notify(it) + luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) + } } private fun notify(luckyNumber: LuckyNumber) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt index 28f6e307..ba026a0e 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt @@ -18,10 +18,8 @@ import io.github.wulkanowy.services.sync.channels.NewMessagesChannel import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor -import io.reactivex.Completable +import io.github.wulkanowy.utils.waitForResult import kotlinx.coroutines.flow.first -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -32,13 +30,13 @@ class MessageWork @Inject constructor( private val preferencesRepository: PreferencesRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxSingle { messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable).waitForResult() } - .flatMap { rxSingle { messageRepository.getNotNotifiedMessages(student).first() } } - .flatMapCompletable { - if (it.isNotEmpty()) notify(it) - rxCompletable { messageRepository.updateMessages(it.onEach { message -> message.isNotified = true }) } - } + override suspend fun doWork(student: Student, semester: Semester) { + messageRepository.getMessages(student, semester, RECEIVED, true, preferencesRepository.isNotificationsEnable).waitForResult() + + messageRepository.getNotNotifiedMessages(student).first().let { + if (it.isNotEmpty()) notify(it) + messageRepository.updateMessages(it.onEach { message -> message.isNotified = true }) + } } private fun notify(messages: List) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt index 029c9f98..2cd9d549 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt @@ -17,10 +17,8 @@ import io.github.wulkanowy.services.sync.channels.NewNotesChannel import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor -import io.reactivex.Completable +import io.github.wulkanowy.utils.waitForResult import kotlinx.coroutines.flow.first -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject import kotlin.random.Random @@ -31,13 +29,13 @@ class NoteWork @Inject constructor( private val preferencesRepository: PreferencesRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxSingle { noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() } - .flatMap { rxSingle { noteRepository.getNotNotifiedNotes(student).first() } } - .flatMapCompletable { - if (it.isNotEmpty()) notify(it) - rxCompletable { noteRepository.updateNotes(it.onEach { note -> note.isNotified = true }) } - } + override suspend fun doWork(student: Student, semester: Semester) { + noteRepository.getNotes(student, semester, true, preferencesRepository.isNotificationsEnable).waitForResult() + + noteRepository.getNotNotifiedNotes(student).first().let { + if (it.isNotEmpty()) notify(it) + noteRepository.updateNotes(it.onEach { note -> note.isNotified = true }) + } } private fun notify(notes: List) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt index 2a53e51b..c433c0ac 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/RecipientWork.kt @@ -4,9 +4,6 @@ import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.recipient.RecipientRepository import io.github.wulkanowy.data.repositories.reportingunit.ReportingUnitRepository -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable -import kotlinx.coroutines.rx2.rxSingle import javax.inject.Inject class RecipientWork @Inject constructor( @@ -14,14 +11,13 @@ class RecipientWork @Inject constructor( private val recipientRepository: RecipientRepository ) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxSingle { reportingUnitRepository.refreshReportingUnits(student) } - .flatMap { rxSingle { reportingUnitRepository.getReportingUnits(student) } } - .flatMapCompletable { units -> - Completable.mergeDelayError(units.map { - rxCompletable { recipientRepository.refreshRecipients(student, 2, it) } - }) + override suspend fun doWork(student: Student, semester: Semester) { + reportingUnitRepository.refreshReportingUnits(student) + + reportingUnitRepository.getReportingUnits(student).let { units -> + units.map { + recipientRepository.refreshRecipients(student, 2, it) } + } } } - diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt index 9954db31..a9abdaa7 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TeacherWork.kt @@ -3,13 +3,12 @@ package io.github.wulkanowy.services.sync.works import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.teacher.TeacherRepository -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import javax.inject.Inject class TeacherWork @Inject constructor(private val teacherRepository: TeacherRepository) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { teacherRepository.getTeachers(student, semester, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + teacherRepository.getTeachers(student, semester, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt index 0832eb4d..3b8c6f5d 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/TimetableWork.kt @@ -5,14 +5,15 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.timetable.TimetableRepository import io.github.wulkanowy.utils.monday import io.github.wulkanowy.utils.sunday -import io.reactivex.Completable -import kotlinx.coroutines.rx2.rxCompletable +import io.github.wulkanowy.utils.waitForResult import java.time.LocalDate.now import javax.inject.Inject -class TimetableWork @Inject constructor(private val timetableRepository: TimetableRepository) : Work { +class TimetableWork @Inject constructor( + private val timetableRepository: TimetableRepository +) : Work { - override fun create(student: Student, semester: Semester): Completable { - return rxCompletable { timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true).waitForResult() } + override suspend fun doWork(student: Student, semester: Semester) { + timetableRepository.getTimetable(student, semester, now().monday, now().sunday, true).waitForResult() } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt index 8dc0b98d..c41f41ce 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/Work.kt @@ -1,19 +1,9 @@ package io.github.wulkanowy.services.sync.works -import io.github.wulkanowy.data.Resource -import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student -import io.reactivex.Completable -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.takeWhile interface Work { - fun create(student: Student, semester: Semester): Completable - - suspend fun Flow>.waitForResult() = takeWhile { - it.status == Status.LOADING - }.collect() + suspend fun doWork(student: Student, semester: Semester) } diff --git a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt index e5ccf963..a3b9da95 100644 --- a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt +++ b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt @@ -9,7 +9,7 @@ import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.data.repositories.timetable.TimetableRepository import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetFactory -import io.github.wulkanowy.utils.SchedulersProvider +import timber.log.Timber import javax.inject.Inject class TimetableWidgetService : RemoteViewsService() { @@ -29,11 +29,9 @@ class TimetableWidgetService : RemoteViewsService() { @Inject lateinit var sharedPref: SharedPrefProvider - @Inject - lateinit var schedulers: SchedulersProvider - override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory { AndroidInjection.inject(this) - return TimetableWidgetFactory(timetableRepo, studentRepo, semesterRepo, prefRepository, sharedPref, schedulers, applicationContext, intent) + Timber.d("TimetableWidgetFactory created") + return TimetableWidgetFactory(timetableRepo, studentRepo, semesterRepo, prefRepository, sharedPref, applicationContext, intent) } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt index b0fc0f6b..aec52425 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BasePresenter.kt @@ -2,9 +2,7 @@ package io.github.wulkanowy.ui.base import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource -import io.reactivex.disposables.CompositeDisposable import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -16,8 +14,7 @@ import kotlin.coroutines.CoroutineContext open class BasePresenter( protected val errorHandler: ErrorHandler, - protected val studentRepository: StudentRepository, - protected val schedulers: SchedulersProvider + protected val studentRepository: StudentRepository ) : CoroutineScope { private var job: Job = Job() @@ -27,9 +24,6 @@ open class BasePresenter( override val coroutineContext: CoroutineContext get() = Dispatchers.Main + job - @Deprecated("Use flow instead :)") - val disposable = CompositeDisposable() - var view: T? = null open fun onAttachView(view: T) { @@ -83,7 +77,6 @@ open class BasePresenter( open fun onDetachView() { view = null - disposable.clear() job.cancel() errorHandler.clear() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutPresenter.kt index ee892adf..24e59d36 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutPresenter.kt @@ -5,17 +5,15 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import timber.log.Timber import javax.inject.Inject class AboutPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val appInfo: AppInfo, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: AboutView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt index c2238de7..c1fe1e11 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorPresenter.kt @@ -6,17 +6,15 @@ import io.github.wulkanowy.data.repositories.appcreator.AppCreatorRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import javax.inject.Inject class ContributorPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val appCreatorRepository: AppCreatorRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: ContributorView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicensePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicensePresenter.kt index ec1ad8a4..18d257ee 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicensePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicensePresenter.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.DispatchersProvider -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach @@ -15,11 +14,10 @@ import timber.log.Timber import javax.inject.Inject class LicensePresenter @Inject constructor( - schedulers: SchedulersProvider, private val dispatchers: DispatchersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: LicenseView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt index 50df763a..85eae8e6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerPresenter.kt @@ -5,18 +5,16 @@ import io.github.wulkanowy.data.repositories.logger.LoggerRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject class LogViewerPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val loggerRepository: LoggerRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: LogViewerView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt index 40239417..8751e523 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountPresenter.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.sync.SyncManager import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach @@ -14,11 +13,10 @@ import timber.log.Timber import javax.inject.Inject class AccountPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val syncManager: SyncManager -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: AccountView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt index c5002c8b..158f08ba 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendancePresenter.kt @@ -10,7 +10,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.flowWithResourceIn @@ -30,14 +29,13 @@ import java.time.LocalDate.ofEpochDay import javax.inject.Inject class AttendancePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val attendanceRepository: AttendanceRepository, private val semesterRepository: SemesterRepository, private val prefRepository: PreferencesRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var baseDate: LocalDate = now().previousOrSameSchoolDay diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt index b5d8538f..e5dce9ac 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryPresenter.kt @@ -9,7 +9,6 @@ import io.github.wulkanowy.data.repositories.subject.SubjectRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -18,14 +17,13 @@ import java.time.Month import javax.inject.Inject class AttendanceSummaryPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val attendanceSummaryRepository: AttendanceSummaryRepository, private val subjectRepository: SubjectRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var subjects = emptyList() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt index a5c6e851..f63316a8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamPresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday @@ -27,13 +26,12 @@ import java.time.LocalDate.ofEpochDay import javax.inject.Inject class ExamPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val examRepository: ExamRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var baseDate: LocalDate = now().nextOrSameSchoolDay diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt index 9dc39d85..d4c9f210 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradePresenter.kt @@ -7,7 +7,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.getCurrentOrLast import kotlinx.coroutines.delay @@ -16,12 +15,11 @@ import timber.log.Timber import javax.inject.Inject class GradePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { var selectedIndex = 0 private set diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt index 5845d32c..a3651793 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsPresenter.kt @@ -11,7 +11,6 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.flowWithResourceIn @@ -21,7 +20,6 @@ import timber.log.Timber import javax.inject.Inject class GradeDetailsPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val gradeRepository: GradeRepository, @@ -29,7 +27,7 @@ class GradeDetailsPresenter @Inject constructor( private val preferencesRepository: PreferencesRepository, private val averageProvider: GradeAverageProvider, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var newGradesAmount: Int = 0 diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt index eb6ae843..112f4c58 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsPresenter.kt @@ -10,7 +10,6 @@ import io.github.wulkanowy.data.repositories.subject.SubjectRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -18,7 +17,6 @@ import timber.log.Timber import javax.inject.Inject class GradeStatisticsPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val gradeStatisticsRepository: GradeStatisticsRepository, @@ -26,7 +24,7 @@ class GradeStatisticsPresenter @Inject constructor( private val semesterRepository: SemesterRepository, private val preferencesRepository: PreferencesRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var subjects = emptyList() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt index 96908c3c..9484d8b7 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryPresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.grade.GradeAverageProvider import io.github.wulkanowy.ui.modules.grade.GradeDetailsWithAverage import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -16,12 +15,11 @@ import timber.log.Timber import javax.inject.Inject class GradeSummaryPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val averageProvider: GradeAverageProvider, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt index cf38d9c2..8af0d83f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkPresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday @@ -26,13 +25,12 @@ import java.time.LocalDate.ofEpochDay import javax.inject.Inject class HomeworkPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val homeworkRepository: HomeworkRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var baseDate: LocalDate = LocalDate.now().nextOrSameSchoolDay diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt index 2ca7a1f8..a53d38e8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsPresenter.kt @@ -7,19 +7,17 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject class HomeworkDetailsPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val homeworkRepository: HomeworkRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: HomeworkDetailsView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginPresenter.kt index 16342de4..8ff7e6fe 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginPresenter.kt @@ -4,15 +4,13 @@ import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import timber.log.Timber import javax.inject.Inject class LoginPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: LoginView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt index 8f187ac8..f5c30c51 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedPresenter.kt @@ -7,7 +7,6 @@ import io.github.wulkanowy.sdk.Sdk import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank @@ -16,11 +15,10 @@ import timber.log.Timber import javax.inject.Inject class LoginAdvancedPresenter @Inject constructor( - schedulers: SchedulersProvider, studentRepository: StudentRepository, private val loginErrorHandler: LoginErrorHandler, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { +) : BasePresenter(loginErrorHandler, studentRepository) { override fun onAttachView(view: LoginAdvancedView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt index 9e43bf77..b921140c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenter.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank @@ -14,11 +13,10 @@ import timber.log.Timber import javax.inject.Inject class LoginFormPresenter @Inject constructor( - schedulers: SchedulersProvider, studentRepository: StudentRepository, private val loginErrorHandler: LoginErrorHandler, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { +) : BasePresenter(loginErrorHandler, studentRepository) { private var lastError: Throwable? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt index 0ef183cc..1d509e1b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverPresenter.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.repositories.recover.RecoverRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank @@ -14,12 +13,11 @@ import timber.log.Timber import javax.inject.Inject class LoginRecoverPresenter @Inject constructor( - schedulers: SchedulersProvider, studentRepository: StudentRepository, private val loginErrorHandler: RecoverErrorHandler, private val analytics: FirebaseAnalyticsHelper, private val recoverRepository: RecoverRepository -) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { +) : BasePresenter(loginErrorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt index 99ee7d30..76e0693f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenter.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank import kotlinx.coroutines.flow.onEach @@ -15,11 +14,10 @@ import java.io.Serializable import javax.inject.Inject class LoginStudentSelectPresenter @Inject constructor( - schedulers: SchedulersProvider, studentRepository: StudentRepository, private val loginErrorHandler: LoginErrorHandler, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { +) : BasePresenter(loginErrorHandler, studentRepository) { private var lastError: Throwable? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt index c0f5803f..a2cda4fc 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolPresenter.kt @@ -5,7 +5,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.modules.login.LoginErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.ifNullOrBlank @@ -16,10 +15,9 @@ import javax.inject.Inject class LoginSymbolPresenter @Inject constructor( studentRepository: StudentRepository, - schedulers: SchedulersProvider, private val loginErrorHandler: LoginErrorHandler, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(loginErrorHandler, studentRepository, schedulers) { +) : BasePresenter(loginErrorHandler, studentRepository) { private var lastError: Throwable? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt index 64df3db8..90aea441 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberPresenter.kt @@ -6,7 +6,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -14,12 +13,11 @@ import timber.log.Timber import javax.inject.Inject class LuckyNumberPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val luckyNumberRepository: LuckyNumberRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt index 5bcdc8a1..32196ee2 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt @@ -8,18 +8,16 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider.Companion.getThemeWidgetKey -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject class LuckyNumberWidgetConfigurePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val sharedPref: SharedPrefProvider -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var appWidgetId: Int? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index cf8395f3..3daaaaf6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -15,20 +15,14 @@ import android.view.View.VISIBLE import android.widget.RemoteViews import dagger.android.AndroidInjection import io.github.wulkanowy.R -import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider -import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.github.wulkanowy.data.repositories.luckynumber.LuckyNumberRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView -import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Maybe -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.takeWhile -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import io.github.wulkanowy.utils.toFirstResult +import kotlinx.coroutines.runBlocking import timber.log.Timber import javax.inject.Inject @@ -40,12 +34,6 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { @Inject lateinit var luckyNumberRepository: LuckyNumberRepository - @Inject - lateinit var schedulers: SchedulersProvider - - @Inject - lateinit var appWidgetManager: AppWidgetManager - @Inject lateinit var sharedPref: SharedPrefProvider @@ -142,27 +130,23 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { return n - 1 } - private fun getLuckyNumber(studentId: Long, appWidgetId: Int): LuckyNumber? { - return try { - rxSingle { studentRepository.isStudentSaved() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getSavedStudents() } } - .flatMap { students -> - val student = students.singleOrNull { student -> student.id == studentId } - when { - student != null -> Maybe.just(student) - studentId != 0L -> { - rxSingle { studentRepository.isCurrentStudentSet() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getCurrentStudent(false) } } - .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } - } - else -> Maybe.empty() + private fun getLuckyNumber(studentId: Long, appWidgetId: Int) = runBlocking { + try { + val students = studentRepository.getSavedStudents() + val student = students.singleOrNull { student -> student.id == studentId } + val currentStudent = when { + student != null -> student + studentId != 0L && studentRepository.isCurrentStudentSet() -> { + studentRepository.getCurrentStudent(false).also { + sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } } - .flatMap { rxMaybe { luckyNumberRepository.getLuckyNumber(it, false).takeWhile { it.status == Status.LOADING }.first().data } } - .subscribeOn(schedulers.backgroundThread) - .blockingGet() + else -> null + } + + currentStudent?.let { + luckyNumberRepository.getLuckyNumber(it, false).toFirstResult().data + } } catch (e: Exception) { if (e.cause !is NoCurrentStudentException) { Timber.e(e, "An error has occurred in lucky number provider") diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt index 233d4491..8d5c9d67 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainPresenter.kt @@ -9,18 +9,16 @@ import io.github.wulkanowy.ui.modules.main.MainView.Section.GRADE import io.github.wulkanowy.ui.modules.main.MainView.Section.MESSAGE import io.github.wulkanowy.ui.modules.main.MainView.Section.SCHOOL import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import timber.log.Timber import javax.inject.Inject class MainPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val prefRepository: PreferencesRepository, private val syncManager: SyncManager, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { fun onAttachView(view: MainView, initMenu: MainView.Section?) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt index ea482c62..0fb454b3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessagePresenter.kt @@ -3,17 +3,15 @@ package io.github.wulkanowy.ui.modules.message import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import kotlinx.coroutines.delay import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject class MessagePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: MessageView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt index 4d3b83f1..39437fe4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewPresenter.kt @@ -11,7 +11,6 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.flowWithResourceIn @@ -21,13 +20,12 @@ import timber.log.Timber import javax.inject.Inject class MessagePreviewPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val messageRepository: MessageRepository, private val analytics: FirebaseAnalyticsHelper, private var appInfo: AppInfo -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { var message: Message? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt index c31fd79a..f877c809 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessagePresenter.kt @@ -12,7 +12,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.toFormattedString @@ -21,7 +20,6 @@ import timber.log.Timber import javax.inject.Inject class SendMessagePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, @@ -30,7 +28,7 @@ class SendMessagePresenter @Inject constructor( private val recipientRepository: RecipientRepository, private val preferencesRepository: PreferencesRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { fun onAttachView(view: SendMessageView, message: Message?, reply: Boolean?) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt index 3c9f0444..0d5bb2a6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/tab/MessageTabPresenter.kt @@ -9,7 +9,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.toFormattedString @@ -28,13 +27,12 @@ import javax.inject.Inject import kotlin.math.pow class MessageTabPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val messageRepository: MessageRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { lateinit var folder: MessageFolder diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt index e665a15b..6314c2c3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDevicePresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.flowWithResourceIn @@ -18,13 +17,12 @@ import timber.log.Timber import javax.inject.Inject class MobileDevicePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, private val mobileDeviceRepository: MobileDeviceRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt index f5fb7db3..f6d0de99 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenPresenter.kt @@ -7,7 +7,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach @@ -15,13 +14,12 @@ import timber.log.Timber import javax.inject.Inject class MobileDeviceTokenPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, private val mobileDeviceRepository: MobileDeviceRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: MobileDeviceTokenVIew) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MorePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MorePresenter.kt index 593645c1..6d80f418 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MorePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MorePresenter.kt @@ -3,15 +3,13 @@ package io.github.wulkanowy.ui.modules.more import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import timber.log.Timber import javax.inject.Inject class MorePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: MoreView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt index 8e5661ad..db3b495a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NotePresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResource import io.github.wulkanowy.utils.flowWithResourceIn @@ -18,13 +17,12 @@ import timber.log.Timber import javax.inject.Inject class NotePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val noteRepository: NoteRepository, private val semesterRepository: SemesterRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt index 324f2c37..305f4e7d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersPresenter.kt @@ -3,17 +3,15 @@ package io.github.wulkanowy.ui.modules.schoolandteachers import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import kotlinx.coroutines.delay import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject class SchoolAndTeachersPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: SchoolAndTeachersView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt index 9c10c6ed..4cda5d33 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolPresenter.kt @@ -7,7 +7,6 @@ import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -15,13 +14,12 @@ import timber.log.Timber import javax.inject.Inject class SchoolPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, private val schoolRepository: SchoolRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var address: String? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt index 886f5c68..ff02116b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherPresenter.kt @@ -7,7 +7,6 @@ import io.github.wulkanowy.data.repositories.teacher.TeacherRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import kotlinx.coroutines.flow.onEach @@ -15,13 +14,12 @@ import timber.log.Timber import javax.inject.Inject class TeacherPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val semesterRepository: SemesterRepository, private val teacherRepository: TeacherRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private lateinit var lastError: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt index ea2b2122..f9e6731f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsPresenter.kt @@ -10,14 +10,14 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.isHolidays +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.onEach import timber.log.Timber import java.time.LocalDate.now import javax.inject.Inject class SettingsPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val preferencesRepository: PreferencesRepository, @@ -26,7 +26,7 @@ class SettingsPresenter @Inject constructor( private val syncManager: SyncManager, private val chuckerCollector: ChuckerCollector, private val appInfo: AppInfo -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: SettingsView) { super.onAttachView(view) @@ -64,31 +64,27 @@ class SettingsPresenter @Inject constructor( fun onForceSyncDialogSubmit() { view?.run { - val successString = syncSuccessString - val failedString = syncFailedString - disposable.add(syncManager.startOneTimeSyncWorker() - .doOnSubscribe { - setSyncInProgress(true) - Timber.i("Setting sync now started") - analytics.logEvent("sync_now", "status" to "started") - } - .doFinally { setSyncInProgress(false) } - .subscribe({ workInfo -> - when (workInfo.state) { - WorkInfo.State.SUCCEEDED -> { - showMessage(successString) - analytics.logEvent("sync_now", "status" to "success") - } - WorkInfo.State.FAILED -> { - showError(failedString, Throwable(workInfo.outputData.getString("error"))) - analytics.logEvent("sync_now", "status" to "failed") - } - else -> Timber.d("Sync now state: ${workInfo.state}") + syncManager.startOneTimeSyncWorker().onEach { workInfo -> + when (workInfo.state) { + WorkInfo.State.ENQUEUED -> { + setSyncInProgress(true) + Timber.i("Setting sync now started") + analytics.logEvent("sync_now", "status" to "started") } - }, { - Timber.e(it, "Sync now failed") - }) - ) + WorkInfo.State.SUCCEEDED -> { + showMessage(syncSuccessString) + analytics.logEvent("sync_now", "status" to "success") + } + WorkInfo.State.FAILED -> { + showError(syncFailedString, Throwable(workInfo.outputData.getString("error"))) + analytics.logEvent("sync_now", "status" to "failed") + } + else -> Timber.d("Sync now state: ${workInfo.state}") + } + if (workInfo.state.isFinished) setSyncInProgress(false) + }.catch { + Timber.e(it, "Sync now failed") + }.launch("sync") } } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt index 7ffbf41a..d16ca4a8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashPresenter.kt @@ -4,17 +4,15 @@ import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject class SplashPresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { override fun onAttachView(view: SplashView) { super.onAttachView(view) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt index 2e232381..e410eeb9 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetablePresenter.kt @@ -10,7 +10,6 @@ import io.github.wulkanowy.data.repositories.timetable.TimetableRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday @@ -30,14 +29,13 @@ import java.time.LocalDate.ofEpochDay import javax.inject.Inject class TimetablePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val timetableRepository: TimetableRepository, private val semesterRepository: SemesterRepository, private val prefRepository: PreferencesRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var baseDate: LocalDate = now().nextOrSameSchoolDay diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt index eedd4c25..f1c2cedc 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsPresenter.kt @@ -8,7 +8,6 @@ import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.afterLoading import io.github.wulkanowy.utils.flowWithResourceIn import io.github.wulkanowy.utils.getLastSchoolDayIfHoliday @@ -27,13 +26,12 @@ import java.time.LocalDate.ofEpochDay import javax.inject.Inject class CompletedLessonsPresenter @Inject constructor( - schedulers: SchedulersProvider, studentRepository: StudentRepository, private val completedLessonsErrorHandler: CompletedLessonsErrorHandler, private val semesterRepository: SemesterRepository, private val completedLessonsRepository: CompletedLessonsRepository, private val analytics: FirebaseAnalyticsHelper -) : BasePresenter(completedLessonsErrorHandler, studentRepository, schedulers) { +) : BasePresenter(completedLessonsErrorHandler, studentRepository) { private var baseDate: LocalDate = now().nextOrSameSchoolDay diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt index 28eef06e..3bac1dac 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigurePresenter.kt @@ -8,18 +8,16 @@ import io.github.wulkanowy.ui.base.BasePresenter import io.github.wulkanowy.ui.base.ErrorHandler import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getThemeWidgetKey -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.flowWithResource import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject class TimetableWidgetConfigurePresenter @Inject constructor( - schedulers: SchedulersProvider, errorHandler: ErrorHandler, studentRepository: StudentRepository, private val sharedPref: SharedPrefProvider -) : BasePresenter(errorHandler, studentRepository, schedulers) { +) : BasePresenter(errorHandler, studentRepository) { private var appWidgetId: Int? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt index 1ab6757a..d238b240 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt @@ -12,7 +12,6 @@ import android.widget.AdapterView.INVALID_POSITION import android.widget.RemoteViews import android.widget.RemoteViewsService import io.github.wulkanowy.R -import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -22,14 +21,10 @@ import io.github.wulkanowy.data.repositories.timetable.TimetableRepository import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getCurrentThemeWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getDateWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCompatColor +import io.github.wulkanowy.utils.toFirstResult import io.github.wulkanowy.utils.toFormattedString -import io.reactivex.Maybe -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.flow.takeWhile -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.runBlocking import timber.log.Timber import java.time.LocalDate @@ -39,7 +34,6 @@ class TimetableWidgetFactory( private val semesterRepository: SemesterRepository, private val prefRepository: PreferencesRepository, private val sharedPref: SharedPrefProvider, - private val schedulers: SchedulersProvider, private val context: Context, private val intent: Intent? ) : RemoteViewsService.RemoteViewsFactory { @@ -74,7 +68,7 @@ class TimetableWidgetFactory( val studentId = sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0) updateTheme(appWidgetId) - updateLessons(date, studentId) + lessons = getLessons(date, studentId) } } @@ -103,30 +97,23 @@ class TimetableWidgetFactory( } } - private fun updateLessons(date: LocalDate, studentId: Long) { - lessons = try { - rxSingle { studentRepository.isStudentSaved() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getSavedStudents() } } - .flatMap { - val student = it.singleOrNull { student -> student.id == studentId } + private fun getLessons(date: LocalDate, studentId: Long) = try { + runBlocking { + if (!studentRepository.isStudentSaved()) return@runBlocking emptyList() - if (student != null) Maybe.just(student) - else Maybe.empty() - } - .flatMap { student -> - rxMaybe { semesterRepository.getCurrentSemester(student) }.flatMap { semester -> - rxMaybe { timetableRepository.getTimetable(student, semester, date, date, true).takeWhile { it.status == Status.LOADING }.first().data } - } - } - .map { items -> items.sortedWith(compareBy({ it.number }, { !it.isStudentPlan })) } - .map { lessons -> lessons.filter { if (prefRepository.showWholeClassPlan == "no") it.isStudentPlan else true } } - .subscribeOn(schedulers.backgroundThread) - .blockingGet(emptyList()) - } catch (e: Exception) { - Timber.e(e, "An error has occurred in timetable widget factory") - emptyList() + val students = studentRepository.getSavedStudents() + val student = students.singleOrNull { student -> student.id == studentId } + ?: return@runBlocking emptyList() + + val semester = semesterRepository.getCurrentSemester(student) + timetableRepository.getTimetable(student, semester, date, date, false) + .toFirstResult().data.orEmpty() + .sortedWith(compareBy({ it.number }, { !it.isStudentPlan })) + .filter { if (prefRepository.showWholeClassPlan == "no") it.isStudentPlan else true } } + } catch (e: Exception) { + Timber.e(e, "An error has occurred in timetable widget factory") + emptyList() } @SuppressLint("DefaultLocale") diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 4c4cc868..426f6cdd 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -25,14 +25,12 @@ import io.github.wulkanowy.services.widgets.TimetableWidgetService import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.FirebaseAnalyticsHelper -import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.nextOrSameSchoolDay import io.github.wulkanowy.utils.nextSchoolDay import io.github.wulkanowy.utils.previousSchoolDay import io.github.wulkanowy.utils.toFormattedString -import io.reactivex.Maybe -import kotlinx.coroutines.rx2.rxMaybe -import kotlinx.coroutines.rx2.rxSingle +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import timber.log.Timber import java.time.LocalDate import java.time.LocalDate.now @@ -49,9 +47,6 @@ class TimetableWidgetProvider : BroadcastReceiver() { @Inject lateinit var sharedPref: SharedPrefProvider - @Inject - lateinit var schedulers: SchedulersProvider - @Inject lateinit var analytics: FirebaseAnalyticsHelper @@ -80,13 +75,15 @@ class TimetableWidgetProvider : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { AndroidInjection.inject(this, context) - when (intent.action) { - ACTION_APPWIDGET_UPDATE -> onUpdate(context, intent) - ACTION_APPWIDGET_DELETED -> onDelete(intent) + GlobalScope.launch { + when (intent.action) { + ACTION_APPWIDGET_UPDATE -> onUpdate(context, intent) + ACTION_APPWIDGET_DELETED -> onDelete(intent) + } } } - private fun onUpdate(context: Context, intent: Intent) { + private suspend fun onUpdate(context: Context, intent: Intent) { if (intent.getStringExtra(EXTRA_BUTTON_TYPE) === null) { intent.getIntArrayExtra(EXTRA_APPWIDGET_IDS)?.forEach { appWidgetId -> val student = getStudent(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId) @@ -170,8 +167,9 @@ class TimetableWidgetProvider : BroadcastReceiver() { } with(appWidgetManager) { - notifyAppWidgetViewDataChanged(appWidgetId, R.id.timetableWidgetList) updateAppWidget(appWidgetId, remoteView) + notifyAppWidgetViewDataChanged(appWidgetId, R.id.timetableWidgetList) + Timber.d("TimetableWidgetProvider updated") } } @@ -184,31 +182,22 @@ class TimetableWidgetProvider : BroadcastReceiver() { }, FLAG_UPDATE_CURRENT) } - private fun getStudent(studentId: Long, appWidgetId: Int): Student? { - return try { - rxSingle { studentRepository.isStudentSaved() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getSavedStudents(false) } } - .flatMap { students -> - val student = students.singleOrNull { student -> student.id == studentId } - when { - student != null -> Maybe.just(student) - studentId != 0L -> { - rxSingle { studentRepository.isCurrentStudentSet() } - .filter { true } - .flatMap { rxMaybe { studentRepository.getCurrentStudent(false) } } - .doOnSuccess { sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } - } - else -> Maybe.empty() - } + private suspend fun getStudent(studentId: Long, appWidgetId: Int) = try { + val students = studentRepository.getSavedStudents(false) + val student = students.singleOrNull { student -> student.id == studentId } + when { + student != null -> student + studentId != 0L && studentRepository.isCurrentStudentSet() -> { + studentRepository.getCurrentStudent(false).also { + sharedPref.putLong(getStudentWidgetKey(appWidgetId), it.id) } - .subscribeOn(schedulers.backgroundThread) - .blockingGet() - } catch (e: Exception) { - if (e.cause !is NoCurrentStudentException) { - Timber.e(e, "An error has occurred in timetable widget provider") } - null + else -> null } + } catch (e: Exception) { + if (e.cause !is NoCurrentStudentException) { + Timber.e(e, "An error has occurred in timetable widget provider") + } + null } } diff --git a/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt b/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt index 6551606b..736a4270 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/FlowUtils.kt @@ -5,10 +5,12 @@ import io.github.wulkanowy.data.Status import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.emitAll +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.takeWhile inline fun networkBoundResource( showSavedOnLoading: Boolean = true, @@ -91,3 +93,7 @@ fun flowWithResourceIn(block: suspend () -> Flow>) = flow { fun Flow>.afterLoading(callback: () -> Unit) = onEach { if (it.status != Status.LOADING) callback() } + +suspend fun Flow>.toFirstResult() = filter { it.status != Status.LOADING }.first() + +suspend fun Flow>.waitForResult() = takeWhile { it.status == Status.LOADING }.collect() diff --git a/app/src/main/java/io/github/wulkanowy/utils/SchedulersProvider.kt b/app/src/main/java/io/github/wulkanowy/utils/SchedulersProvider.kt deleted file mode 100644 index e426d4d5..00000000 --- a/app/src/main/java/io/github/wulkanowy/utils/SchedulersProvider.kt +++ /dev/null @@ -1,14 +0,0 @@ -package io.github.wulkanowy.utils - -import io.reactivex.Scheduler -import io.reactivex.android.schedulers.AndroidSchedulers -import io.reactivex.schedulers.Schedulers - -open class SchedulersProvider { - - open val mainThread: Scheduler - get() = AndroidSchedulers.mainThread() - - open val backgroundThread: Scheduler - get() = Schedulers.io() -} diff --git a/app/src/test/java/io/github/wulkanowy/TestSchedulersProvider.kt b/app/src/test/java/io/github/wulkanowy/TestSchedulersProvider.kt deleted file mode 100644 index 8c52d7b3..00000000 --- a/app/src/test/java/io/github/wulkanowy/TestSchedulersProvider.kt +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.wulkanowy - -import io.github.wulkanowy.utils.SchedulersProvider -import io.reactivex.Scheduler -import io.reactivex.schedulers.Schedulers - -class TestSchedulersProvider : SchedulersProvider() { - - override val backgroundThread: Scheduler - get() = Schedulers.trampoline() - - override val mainThread: Scheduler - get() = Schedulers.trampoline() -} - diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt index 921b32ed..4f32cf7e 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/LoginPresenterTest.kt @@ -1,6 +1,5 @@ package io.github.wulkanowy.ui.modules.login -import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.student.StudentRepository import io.mockk.MockKAnnotations import io.mockk.clearMocks @@ -29,7 +28,7 @@ class LoginPresenterTest { MockKAnnotations.init(this) clearMocks(loginView) - presenter = LoginPresenter(TestSchedulersProvider(), errorHandler, studentRepository) + presenter = LoginPresenter(errorHandler, studentRepository) presenter.onAttachView(loginView) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt index 32c6a74e..d6f58ef8 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/form/LoginFormPresenterTest.kt @@ -1,7 +1,6 @@ package io.github.wulkanowy.ui.modules.login.form import io.github.wulkanowy.MainCoroutineRule -import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.login.LoginErrorHandler @@ -52,7 +51,7 @@ class LoginFormPresenterTest { every { loginFormView.setErrorPassRequired(any()) } just Runs every { loginFormView.setErrorUsernameRequired() } just Runs - presenter = LoginFormPresenter(TestSchedulersProvider(), repository, errorHandler, analytics) + presenter = LoginFormPresenter(repository, errorHandler, analytics) presenter.onAttachView(loginFormView) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt index 8176fc2e..46ae8a61 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectPresenterTest.kt @@ -1,7 +1,6 @@ package io.github.wulkanowy.ui.modules.login.studentselect import io.github.wulkanowy.MainCoroutineRule -import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.modules.login.LoginErrorHandler @@ -53,7 +52,7 @@ class LoginStudentSelectPresenterTest { every { loginStudentSelectView.showProgress(any()) } just Runs every { loginStudentSelectView.showContent(any()) } just Runs - presenter = LoginStudentSelectPresenter(TestSchedulersProvider(), studentRepository, errorHandler, analytics) + presenter = LoginStudentSelectPresenter(studentRepository, errorHandler, analytics) presenter.onAttachView(loginStudentSelectView, null) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt index 5b0408c3..fc2832b2 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/main/MainPresenterTest.kt @@ -1,6 +1,5 @@ package io.github.wulkanowy.ui.modules.main -import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.services.sync.SyncManager @@ -48,7 +47,7 @@ class MainPresenterTest { every { mainView.startMenuIndex } returns 1 every { mainView.startMenuMoreIndex } returns 1 every { mainView.initView() } just Runs - presenter = MainPresenter(TestSchedulersProvider(), errorHandler, studentRepository, prefRepository, syncManager, analytics) + presenter = MainPresenter(errorHandler, studentRepository, prefRepository, syncManager, analytics) presenter.onAttachView(mainView, null) } diff --git a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt index eb4ac638..a81f1abd 100644 --- a/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt +++ b/app/src/test/java/io/github/wulkanowy/ui/modules/splash/SplashPresenterTest.kt @@ -1,7 +1,6 @@ package io.github.wulkanowy.ui.modules.splash import io.github.wulkanowy.MainCoroutineRule -import io.github.wulkanowy.TestSchedulersProvider import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.ui.base.ErrorHandler import io.mockk.MockKAnnotations @@ -31,7 +30,7 @@ class SplashPresenterTest { @Before fun setUp() { MockKAnnotations.init(this) - presenter = SplashPresenter(TestSchedulersProvider(), errorHandler, studentRepository) + presenter = SplashPresenter(errorHandler, studentRepository) } @Test From 3ba16f2903dd376ac2eea670b6792387ed5f1296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sun, 2 Aug 2020 18:29:41 +0200 Subject: [PATCH 21/34] Migrate to dagger hilt (#909) --- app/build.gradle | 19 ++- .../java/io/github/wulkanowy/WulkanowyApp.kt | 16 +- .../github/wulkanowy/data/RepositoryModule.kt | 16 +- .../repositories/logger/LoggerRepository.kt | 3 +- .../preferences/PreferencesRepository.kt | 3 +- .../data/repositories/student/StudentLocal.kt | 3 +- .../io/github/wulkanowy/di/AppComponent.kt | 22 --- .../java/io/github/wulkanowy/di/AppModule.kt | 16 +- .../io/github/wulkanowy/di/BindingModule.kt | 55 ------- .../github/wulkanowy/di/scopes/PerActivity.kt | 7 - .../wulkanowy/di/scopes/PerChildFragment.kt | 7 - .../github/wulkanowy/di/scopes/PerFragment.kt | 7 - .../services/HiltBroadcastReceiver.kt | 9 ++ .../wulkanowy/services/ServicesModule.kt | 19 +-- .../alarm/TimetableNotificationReceiver.kt | 9 +- .../TimetableNotificationSchedulerHelper.kt | 3 +- .../wulkanowy/services/sync/SyncWorker.kt | 13 +- .../services/sync/SyncWorkerFactory.kt | 20 --- .../services/sync/channels/DebugChannel.kt | 3 +- .../sync/channels/LuckyNumberChannel.kt | 3 +- .../sync/channels/NewGradesChannel.kt | 3 +- .../sync/channels/NewMessagesChannel.kt | 3 +- .../services/sync/channels/NewNotesChannel.kt | 3 +- .../services/sync/channels/PushChannel.kt | 3 +- .../sync/channels/UpcomingLessonsChannel.kt | 3 +- .../services/sync/works/GradeWork.kt | 3 +- .../services/sync/works/LuckyNumberWork.kt | 3 +- .../services/sync/works/MessageWork.kt | 3 +- .../wulkanowy/services/sync/works/NoteWork.kt | 3 +- .../widgets/TimetableWidgetService.kt | 4 +- .../github/wulkanowy/ui/base/BaseActivity.kt | 13 +- .../wulkanowy/ui/base/BaseDialogFragment.kt | 4 +- .../github/wulkanowy/ui/base/BaseFragment.kt | 4 +- .../github/wulkanowy/ui/base/ErrorDialog.kt | 2 + .../ui/modules/about/AboutFragment.kt | 2 + .../about/contributor/ContributorFragment.kt | 2 + .../modules/about/license/LicenseFragment.kt | 8 +- .../ui/modules/about/license/LicenseModule.kt | 15 -- .../about/logviewer/LogViewerFragment.kt | 2 + .../ui/modules/account/AccountDialog.kt | 2 + .../modules/attendance/AttendanceFragment.kt | 2 + .../summary/AttendanceSummaryFragment.kt | 2 + .../wulkanowy/ui/modules/exam/ExamFragment.kt | 2 + .../ui/modules/grade/GradeFragment.kt | 8 +- .../wulkanowy/ui/modules/grade/GradeModule.kt | 35 ----- .../grade/details/GradeDetailsFragment.kt | 2 + .../statistics/GradeStatisticsFragment.kt | 2 + .../grade/summary/GradeSummaryFragment.kt | 2 + .../ui/modules/homework/HomeworkFragment.kt | 2 + .../homework/details/HomeworkDetailsDialog.kt | 2 + .../ui/modules/login/LoginActivity.kt | 5 +- .../wulkanowy/ui/modules/login/LoginModule.kt | 45 ------ .../login/advanced/LoginAdvancedFragment.kt | 2 + .../modules/login/form/LoginFormFragment.kt | 2 + .../login/recover/LoginRecoverFragment.kt | 2 + .../LoginStudentSelectFragment.kt | 2 + .../login/symbol/LoginSymbolFragment.kt | 2 + .../luckynumber/LuckyNumberFragment.kt | 2 + .../LuckyNumberWidgetConfigureActivity.kt | 2 + .../LuckyNumberWidgetProvider.kt | 9 +- .../wulkanowy/ui/modules/main/MainActivity.kt | 15 +- .../wulkanowy/ui/modules/main/MainModule.kt | 139 ------------------ .../ui/modules/message/MessageFragment.kt | 5 +- .../ui/modules/message/MessageModule.kt | 25 ---- .../message/preview/MessagePreviewFragment.kt | 2 + .../message/send/SendMessageActivity.kt | 4 +- .../modules/message/tab/MessageTabFragment.kt | 2 + .../mobiledevice/MobileDeviceFragment.kt | 2 + .../token/MobileDeviceTokenDialog.kt | 2 + .../wulkanowy/ui/modules/more/MoreFragment.kt | 2 + .../wulkanowy/ui/modules/note/NoteFragment.kt | 2 + .../SchoolAndTeachersFragment.kt | 5 +- .../SchoolAndTeachersModule.kt | 30 ---- .../school/SchoolFragment.kt | 2 + .../teacher/TeacherFragment.kt | 2 + .../ui/modules/settings/SettingsFragment.kt | 4 +- .../ui/modules/splash/SplashActivity.kt | 2 + .../ui/modules/timetable/TimetableFragment.kt | 2 + .../completed/CompletedLessonsFragment.kt | 2 + .../TimetableWidgetConfigureActivity.kt | 4 +- .../TimetableWidgetProvider.kt | 9 +- .../java/io/github/wulkanowy/utils/AppInfo.kt | 3 +- .../utils/FirebaseAnalyticsHelper.kt | 5 +- build.gradle | 10 +- 84 files changed, 208 insertions(+), 542 deletions(-) delete mode 100644 app/src/main/java/io/github/wulkanowy/di/AppComponent.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/di/BindingModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/di/scopes/PerActivity.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/di/scopes/PerChildFragment.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/di/scopes/PerFragment.kt create mode 100644 app/src/main/java/io/github/wulkanowy/services/HiltBroadcastReceiver.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/services/sync/SyncWorkerFactory.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/main/MainModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageModule.kt delete mode 100644 app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersModule.kt diff --git a/app/build.gradle b/app/build.gradle index fe316e86..e0a7551c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-kapt' +apply plugin: 'dagger.hilt.android.plugin' apply plugin: 'com.google.firebase.crashlytics' apply plugin: 'com.github.triplet.play' apply plugin: 'com.mikepenz.aboutlibraries.plugin' @@ -28,7 +29,7 @@ android { ] javaCompileOptions { annotationProcessorOptions { - arguments = [ + arguments += [ "room.schemaLocation": "$projectDir/schemas".toString(), "room.incremental" : "true" ] @@ -114,9 +115,8 @@ play { } ext { - work_manager = "2.3.4" + work_manager = "2.4.0" room = "2.2.5" - dagger = "2.28.3" chucker = "3.2.0" mockk = "1.10.0" } @@ -128,13 +128,13 @@ configurations.all { dependencies { implementation "io.github.wulkanowy:sdk:02486b9" - coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9' + coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8' - implementation "androidx.core:core-ktx:1.3.0" + implementation "androidx.core:core-ktx:1.3.1" implementation "androidx.activity:activity-ktx:1.1.0" implementation "androidx.appcompat:appcompat:1.2.0-rc02" implementation "androidx.appcompat:appcompat-resources:1.1.0" @@ -162,11 +162,10 @@ dependencies { implementation "androidx.room:room-ktx:$room" kapt "androidx.room:room-compiler:$room" - implementation "com.google.dagger:dagger-android-support:$dagger" - kapt "com.google.dagger:dagger-compiler:$dagger" - kapt "com.google.dagger:dagger-android-processor:$dagger" - implementation "com.squareup.inject:assisted-inject-annotations-dagger2:0.5.2" - kapt "com.squareup.inject:assisted-inject-processor-dagger2:0.5.2" + implementation "com.google.dagger:hilt-android:$hilt_version" + kapt "com.google.dagger:hilt-android-compiler:$hilt_version" + implementation 'androidx.hilt:hilt-work:1.0.0-alpha02' + kapt 'androidx.hilt:hilt-compiler:1.0.0-alpha02' implementation "com.aurelhubert:ahbottomnavigation:2.3.4" implementation "com.ncapdevi:frag-nav:3.3.0" diff --git a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt index 9b1944c3..64995858 100644 --- a/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt +++ b/app/src/main/java/io/github/wulkanowy/WulkanowyApp.kt @@ -1,17 +1,16 @@ package io.github.wulkanowy +import android.app.Application import android.content.Context import android.util.Log.DEBUG import android.util.Log.INFO import android.util.Log.VERBOSE +import androidx.hilt.work.HiltWorkerFactory import androidx.multidex.MultiDex import androidx.work.Configuration import com.yariksoffice.lingver.Lingver -import dagger.android.AndroidInjector -import dagger.android.support.DaggerApplication +import dagger.hilt.android.HiltAndroidApp import fr.bipi.tressence.file.FileLoggerTree -import io.github.wulkanowy.di.DaggerAppComponent -import io.github.wulkanowy.services.sync.SyncWorkerFactory import io.github.wulkanowy.ui.base.ThemeManager import io.github.wulkanowy.utils.ActivityLifecycleLogger import io.github.wulkanowy.utils.AppInfo @@ -21,10 +20,11 @@ import io.github.wulkanowy.utils.DebugLogTree import timber.log.Timber import javax.inject.Inject -class WulkanowyApp : DaggerApplication(), Configuration.Provider { +@HiltAndroidApp +class WulkanowyApp : Application(), Configuration.Provider { @Inject - lateinit var workerFactory: SyncWorkerFactory + lateinit var workerFactory: HiltWorkerFactory @Inject lateinit var themeManager: ThemeManager @@ -62,10 +62,6 @@ class WulkanowyApp : DaggerApplication(), Configuration.Provider { registerActivityLifecycleCallbacks(ActivityLifecycleLogger()) } - override fun applicationInjector(): AndroidInjector { - return DaggerAppComponent.factory().create(this) - } - override fun getWorkManagerConfiguration() = Configuration.Builder() .setWorkerFactory(workerFactory) .setMinimumLoggingLevel(if (appInfo.isDebug) VERBOSE else INFO) diff --git a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt index 6ece2d97..3370d0ac 100644 --- a/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt +++ b/app/src/main/java/io/github/wulkanowy/data/RepositoryModule.kt @@ -10,6 +10,9 @@ import com.chuckerteam.chucker.api.ChuckerInterceptor import com.chuckerteam.chucker.api.RetentionManager import dagger.Module import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.data.db.AppDatabase import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -18,11 +21,12 @@ import timber.log.Timber import javax.inject.Singleton @Module +@InstallIn(ApplicationComponent::class) internal class RepositoryModule { @Singleton @Provides - fun provideSdk(chuckerCollector: ChuckerCollector, context: Context): Sdk { + fun provideSdk(chuckerCollector: ChuckerCollector, @ApplicationContext context: Context): Sdk { return Sdk().apply { androidVersion = android.os.Build.VERSION.RELEASE buildTag = android.os.Build.MODEL @@ -35,7 +39,7 @@ internal class RepositoryModule { @Singleton @Provides - fun provideChuckerCollector(context: Context, prefRepository: PreferencesRepository): ChuckerCollector { + fun provideChuckerCollector(@ApplicationContext context: Context, prefRepository: PreferencesRepository): ChuckerCollector { return ChuckerCollector( context = context, showNotification = prefRepository.isDebugNotificationEnable, @@ -45,19 +49,19 @@ internal class RepositoryModule { @Singleton @Provides - fun provideDatabase(context: Context, sharedPrefProvider: SharedPrefProvider) = AppDatabase.newInstance(context, sharedPrefProvider) + fun provideDatabase(@ApplicationContext context: Context, sharedPrefProvider: SharedPrefProvider) = AppDatabase.newInstance(context, sharedPrefProvider) @Singleton @Provides - fun provideResources(context: Context): Resources = context.resources + fun provideResources(@ApplicationContext context: Context): Resources = context.resources @Singleton @Provides - fun provideAssets(context: Context): AssetManager = context.assets + fun provideAssets(@ApplicationContext context: Context): AssetManager = context.assets @Singleton @Provides - fun provideSharedPref(context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + fun provideSharedPref(@ApplicationContext context: Context): SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) @Singleton @Provides diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt index e50955e2..85168fee 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/logger/LoggerRepository.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.data.repositories.logger import android.content.Context +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.utils.DispatchersProvider import kotlinx.coroutines.withContext import java.io.File @@ -8,7 +9,7 @@ import java.io.FileNotFoundException import javax.inject.Inject class LoggerRepository @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val dispatchers: DispatchersProvider ) { diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt index 7715cde0..1d5e57f8 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/preferences/PreferencesRepository.kt @@ -2,6 +2,7 @@ package io.github.wulkanowy.data.repositories.preferences import android.content.Context import android.content.SharedPreferences +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.ui.modules.grade.GradeAverageMode import javax.inject.Inject @@ -10,7 +11,7 @@ import javax.inject.Singleton @Singleton class PreferencesRepository @Inject constructor( private val sharedPref: SharedPreferences, - val context: Context + @ApplicationContext val context: Context ) { val startMenuIndex: Int get() = getString(R.string.pref_key_start_menu, R.string.pref_default_startup).toInt() diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt index 3a964aae..5aff8f86 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/student/StudentLocal.kt @@ -1,6 +1,7 @@ package io.github.wulkanowy.data.repositories.student import android.content.Context +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.data.db.dao.StudentDao import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.sdk.Sdk @@ -15,7 +16,7 @@ import javax.inject.Singleton class StudentLocal @Inject constructor( private val studentDb: StudentDao, private val dispatchers: DispatchersProvider, - private val context: Context + @ApplicationContext private val context: Context ) { suspend fun saveStudents(students: List) = withContext(dispatchers.backgroundThread) { diff --git a/app/src/main/java/io/github/wulkanowy/di/AppComponent.kt b/app/src/main/java/io/github/wulkanowy/di/AppComponent.kt deleted file mode 100644 index c0329f7d..00000000 --- a/app/src/main/java/io/github/wulkanowy/di/AppComponent.kt +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.wulkanowy.di - -import dagger.Component -import dagger.android.AndroidInjector -import dagger.android.support.AndroidSupportInjectionModule -import io.github.wulkanowy.WulkanowyApp -import io.github.wulkanowy.data.RepositoryModule -import io.github.wulkanowy.services.ServicesModule -import javax.inject.Singleton - -@Singleton -@Component(modules = [ - AndroidSupportInjectionModule::class, - AppModule::class, - RepositoryModule::class, - ServicesModule::class, - BindingModule::class]) -interface AppComponent : AndroidInjector { - - @Component.Factory - interface Factory : AndroidInjector.Factory -} diff --git a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt index d791f0a3..9133c34e 100644 --- a/app/src/main/java/io/github/wulkanowy/di/AppModule.kt +++ b/app/src/main/java/io/github/wulkanowy/di/AppModule.kt @@ -5,29 +5,23 @@ import android.content.Context import com.yariksoffice.lingver.Lingver import dagger.Module import dagger.Provides -import io.github.wulkanowy.WulkanowyApp -import io.github.wulkanowy.utils.AppInfo +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.utils.DispatchersProvider import javax.inject.Singleton @Module +@InstallIn(ApplicationComponent::class) internal class AppModule { - @Singleton - @Provides - fun provideContext(app: WulkanowyApp): Context = app - @Singleton @Provides fun provideDispatchersProvider() = DispatchersProvider() @Singleton @Provides - fun provideAppWidgetManager(context: Context): AppWidgetManager = AppWidgetManager.getInstance(context) - - @Singleton - @Provides - fun provideAppInfo() = AppInfo() + fun provideAppWidgetManager(@ApplicationContext context: Context): AppWidgetManager = AppWidgetManager.getInstance(context) @Singleton @Provides diff --git a/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt b/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt deleted file mode 100644 index 246e8c70..00000000 --- a/app/src/main/java/io/github/wulkanowy/di/BindingModule.kt +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.wulkanowy.di - -import dagger.Module -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.di.scopes.PerActivity -import io.github.wulkanowy.services.alarm.TimetableNotificationReceiver -import io.github.wulkanowy.ui.base.ErrorDialog -import io.github.wulkanowy.ui.modules.login.LoginActivity -import io.github.wulkanowy.ui.modules.login.LoginModule -import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetConfigureActivity -import io.github.wulkanowy.ui.modules.luckynumberwidget.LuckyNumberWidgetProvider -import io.github.wulkanowy.ui.modules.main.MainActivity -import io.github.wulkanowy.ui.modules.main.MainModule -import io.github.wulkanowy.ui.modules.message.send.SendMessageActivity -import io.github.wulkanowy.ui.modules.splash.SplashActivity -import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetConfigureActivity -import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider - -@Suppress("unused") -@Module -internal abstract class BindingModule { - - @ContributesAndroidInjector - abstract fun bindErrorDialog(): ErrorDialog - - @PerActivity - @ContributesAndroidInjector - abstract fun bindSplashActivity(): SplashActivity - - @PerActivity - @ContributesAndroidInjector(modules = [LoginModule::class]) - abstract fun bindLoginActivity(): LoginActivity - - @PerActivity - @ContributesAndroidInjector(modules = [MainModule::class]) - abstract fun bindMainActivity(): MainActivity - - @ContributesAndroidInjector - abstract fun bindMessageSendActivity(): SendMessageActivity - - @ContributesAndroidInjector - abstract fun bindTimetableWidgetAccountActivity(): TimetableWidgetConfigureActivity - - @ContributesAndroidInjector - abstract fun bindTimetableWidgetProvider(): TimetableWidgetProvider - - @ContributesAndroidInjector - abstract fun bindLuckyNumberWidgetAccountActivity(): LuckyNumberWidgetConfigureActivity - - @ContributesAndroidInjector - abstract fun bindLuckyNumberWidgetProvider(): LuckyNumberWidgetProvider - - @ContributesAndroidInjector - abstract fun bindTimetableNotificationReceiver(): TimetableNotificationReceiver -} diff --git a/app/src/main/java/io/github/wulkanowy/di/scopes/PerActivity.kt b/app/src/main/java/io/github/wulkanowy/di/scopes/PerActivity.kt deleted file mode 100644 index c1b4352a..00000000 --- a/app/src/main/java/io/github/wulkanowy/di/scopes/PerActivity.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.wulkanowy.di.scopes - -import javax.inject.Scope - -@Scope -@Retention(AnnotationRetention.RUNTIME) -annotation class PerActivity diff --git a/app/src/main/java/io/github/wulkanowy/di/scopes/PerChildFragment.kt b/app/src/main/java/io/github/wulkanowy/di/scopes/PerChildFragment.kt deleted file mode 100644 index 08884dac..00000000 --- a/app/src/main/java/io/github/wulkanowy/di/scopes/PerChildFragment.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.wulkanowy.di.scopes - -import javax.inject.Scope - -@Scope -@Retention(AnnotationRetention.RUNTIME) -annotation class PerChildFragment diff --git a/app/src/main/java/io/github/wulkanowy/di/scopes/PerFragment.kt b/app/src/main/java/io/github/wulkanowy/di/scopes/PerFragment.kt deleted file mode 100644 index 1203d7d8..00000000 --- a/app/src/main/java/io/github/wulkanowy/di/scopes/PerFragment.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.wulkanowy.di.scopes - -import javax.inject.Scope - -@Scope -@Retention(AnnotationRetention.RUNTIME) -annotation class PerFragment diff --git a/app/src/main/java/io/github/wulkanowy/services/HiltBroadcastReceiver.kt b/app/src/main/java/io/github/wulkanowy/services/HiltBroadcastReceiver.kt new file mode 100644 index 00000000..1e795d43 --- /dev/null +++ b/app/src/main/java/io/github/wulkanowy/services/HiltBroadcastReceiver.kt @@ -0,0 +1,9 @@ +package io.github.wulkanowy.services + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +abstract class HiltBroadcastReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) {} +} diff --git a/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt b/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt index facba9cb..ac5a84e8 100644 --- a/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt +++ b/app/src/main/java/io/github/wulkanowy/services/ServicesModule.kt @@ -5,11 +5,12 @@ import android.content.Context import androidx.core.app.NotificationManagerCompat import androidx.core.content.getSystemService import androidx.work.WorkManager -import com.squareup.inject.assisted.dagger2.AssistedModule import dagger.Binds import dagger.Module import dagger.Provides -import dagger.android.ContributesAndroidInjector +import dagger.hilt.InstallIn +import dagger.hilt.android.components.ApplicationComponent +import dagger.hilt.android.qualifiers.ApplicationContext import dagger.multibindings.IntoSet import io.github.wulkanowy.services.sync.channels.Channel import io.github.wulkanowy.services.sync.channels.DebugChannel @@ -33,31 +34,27 @@ import io.github.wulkanowy.services.sync.works.RecipientWork import io.github.wulkanowy.services.sync.works.TeacherWork import io.github.wulkanowy.services.sync.works.TimetableWork import io.github.wulkanowy.services.sync.works.Work -import io.github.wulkanowy.services.widgets.TimetableWidgetService import javax.inject.Singleton @Suppress("unused") -@AssistedModule -@Module(includes = [AssistedInject_ServicesModule::class]) +@Module +@InstallIn(ApplicationComponent::class) abstract class ServicesModule { companion object { @Provides - fun provideWorkManager(context: Context) = WorkManager.getInstance(context) + fun provideWorkManager(@ApplicationContext context: Context) = WorkManager.getInstance(context) @Singleton @Provides - fun provideNotificationManager(context: Context) = NotificationManagerCompat.from(context) + fun provideNotificationManager(@ApplicationContext context: Context) = NotificationManagerCompat.from(context) @Singleton @Provides - fun provideAlarmManager(context: Context): AlarmManager = context.getSystemService()!! + fun provideAlarmManager(@ApplicationContext context: Context): AlarmManager = context.getSystemService()!! } - @ContributesAndroidInjector - abstract fun bindTimetableWidgetService(): TimetableWidgetService - @Binds @IntoSet abstract fun provideGradeWork(work: GradeWork): Work diff --git a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt index 1e68685f..592d0919 100644 --- a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt +++ b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationReceiver.kt @@ -3,17 +3,17 @@ package io.github.wulkanowy.services.alarm import android.annotation.SuppressLint import android.app.PendingIntent import android.app.PendingIntent.FLAG_UPDATE_CURRENT -import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build import android.os.Build.VERSION_CODES.N import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat -import dagger.android.AndroidInjection +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.Status import io.github.wulkanowy.data.repositories.student.StudentRepository +import io.github.wulkanowy.services.HiltBroadcastReceiver import io.github.wulkanowy.services.sync.channels.UpcomingLessonsChannel.Companion.CHANNEL_ID import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView @@ -26,7 +26,8 @@ import kotlinx.coroutines.flow.onEach import timber.log.Timber import javax.inject.Inject -class TimetableNotificationReceiver : BroadcastReceiver() { +@AndroidEntryPoint +class TimetableNotificationReceiver : HiltBroadcastReceiver() { @Inject lateinit var studentRepository: StudentRepository @@ -51,8 +52,8 @@ class TimetableNotificationReceiver : BroadcastReceiver() { @SuppressLint("CheckResult") override fun onReceive(context: Context, intent: Intent) { + super.onReceive(context, intent) Timber.d("Receiving intent... ${intent.toUri(0)}") - AndroidInjection.inject(this, context) flowWithResource { val student = studentRepository.getCurrentStudent(false) diff --git a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt index 9922a275..8f8782a2 100644 --- a/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt +++ b/app/src/main/java/io/github/wulkanowy/services/alarm/TimetableNotificationSchedulerHelper.kt @@ -8,6 +8,7 @@ import android.content.Context import android.content.Intent import androidx.core.app.AlarmManagerCompat import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository @@ -32,7 +33,7 @@ import java.time.LocalDateTime.now import javax.inject.Inject class TimetableNotificationSchedulerHelper @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val alarmManager: AlarmManager, private val preferencesRepository: PreferencesRepository ) { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt index 75711f0e..2ba0435b 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorker.kt @@ -5,12 +5,11 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.BigTextStyle import androidx.core.app.NotificationCompat.PRIORITY_DEFAULT import androidx.core.app.NotificationManagerCompat +import androidx.hilt.Assisted +import androidx.hilt.work.WorkerInject import androidx.work.CoroutineWorker import androidx.work.Data -import androidx.work.ListenableWorker import androidx.work.WorkerParameters -import com.squareup.inject.assisted.Assisted -import com.squareup.inject.assisted.AssistedInject import io.github.wulkanowy.R import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -24,7 +23,7 @@ import kotlinx.coroutines.coroutineScope import timber.log.Timber import kotlin.random.Random -class SyncWorker @AssistedInject constructor( +class SyncWorker @WorkerInject constructor( @Assisted appContext: Context, @Assisted workerParameters: WorkerParameters, private val studentRepository: StudentRepository, @@ -81,10 +80,4 @@ class SyncWorker @AssistedInject constructor( .setPriority(PRIORITY_DEFAULT) .build()) } - - @AssistedInject.Factory - interface Factory { - - fun create(appContext: Context, workerParameters: WorkerParameters): ListenableWorker - } } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorkerFactory.kt b/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorkerFactory.kt deleted file mode 100644 index aadfc27f..00000000 --- a/app/src/main/java/io/github/wulkanowy/services/sync/SyncWorkerFactory.kt +++ /dev/null @@ -1,20 +0,0 @@ -package io.github.wulkanowy.services.sync - -import android.content.Context -import androidx.work.ListenableWorker -import androidx.work.WorkerFactory -import androidx.work.WorkerParameters -import timber.log.Timber -import javax.inject.Inject - -class SyncWorkerFactory @Inject constructor(private val syncWorkerFactory: SyncWorker.Factory) : WorkerFactory() { - - override fun createWorker(appContext: Context, workerClassName: String, workerParameters: WorkerParameters): ListenableWorker? { - return if (workerClassName == SyncWorker::class.java.name) { - syncWorkerFactory.create(appContext, workerParameters) - } else { - Timber.e(IllegalArgumentException("Unknown worker class name: $workerClassName")) - null - } - } -} diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt index a6735055..2c756519 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/DebugChannel.kt @@ -6,6 +6,7 @@ import android.app.NotificationChannel import android.app.NotificationManager.IMPORTANCE_DEFAULT import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject @@ -13,7 +14,7 @@ import javax.inject.Inject @TargetApi(26) class DebugChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context, + @ApplicationContext private val context: Context, private val appInfo: AppInfo ) : Channel { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/LuckyNumberChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/LuckyNumberChannel.kt index ae228edd..8025bd76 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/LuckyNumberChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/LuckyNumberChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class LuckyNumberChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewGradesChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewGradesChannel.kt index 7d6f275d..5d5727d3 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewGradesChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewGradesChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class NewGradesChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewMessagesChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewMessagesChannel.kt index 49f39c6b..962fbfa2 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewMessagesChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewMessagesChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class NewMessagesChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewNotesChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewNotesChannel.kt index d80d43dc..c6e79b39 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewNotesChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/NewNotesChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class NewNotesChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/PushChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/PushChannel.kt index a0e24ab4..1d7376b9 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/PushChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/PushChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class PushChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/channels/UpcomingLessonsChannel.kt b/app/src/main/java/io/github/wulkanowy/services/sync/channels/UpcomingLessonsChannel.kt index a292c8b5..cd25eea6 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/channels/UpcomingLessonsChannel.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/channels/UpcomingLessonsChannel.kt @@ -6,13 +6,14 @@ import android.app.NotificationChannel import android.app.NotificationManager.IMPORTANCE_DEFAULT import android.content.Context import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import javax.inject.Inject @TargetApi(26) class UpcomingLessonsChannel @Inject constructor( private val notificationManager: NotificationManagerCompat, - private val context: Context + @ApplicationContext private val context: Context ) : Channel { companion object { diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt index 9d4df201..00bce109 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/GradeWork.kt @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.DEFAULT_ALL import androidx.core.app.NotificationCompat.PRIORITY_HIGH import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.GradeSummary @@ -24,7 +25,7 @@ import javax.inject.Inject import kotlin.random.Random class GradeWork @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val notificationManager: NotificationManagerCompat, private val gradeRepository: GradeRepository, private val preferencesRepository: PreferencesRepository diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index 0024d903..9cb765ec 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.DEFAULT_ALL import androidx.core.app.NotificationCompat.PRIORITY_HIGH import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Semester @@ -23,7 +24,7 @@ import javax.inject.Inject import kotlin.random.Random class LuckyNumberWork @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val notificationManager: NotificationManagerCompat, private val luckyNumberRepository: LuckyNumberRepository, private val preferencesRepository: PreferencesRepository diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt index ba026a0e..7b32f13b 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/MessageWork.kt @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.DEFAULT_ALL import androidx.core.app.NotificationCompat.PRIORITY_HIGH import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Semester @@ -24,7 +25,7 @@ import javax.inject.Inject import kotlin.random.Random class MessageWork @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val notificationManager: NotificationManagerCompat, private val messageRepository: MessageRepository, private val preferencesRepository: PreferencesRepository diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt index 2cd9d549..84b80679 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/NoteWork.kt @@ -7,6 +7,7 @@ import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat.DEFAULT_ALL import androidx.core.app.NotificationCompat.PRIORITY_HIGH import androidx.core.app.NotificationManagerCompat +import dagger.hilt.android.qualifiers.ApplicationContext import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Semester @@ -23,7 +24,7 @@ import javax.inject.Inject import kotlin.random.Random class NoteWork @Inject constructor( - private val context: Context, + @ApplicationContext private val context: Context, private val notificationManager: NotificationManagerCompat, private val noteRepository: NoteRepository, private val preferencesRepository: PreferencesRepository diff --git a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt index a3b9da95..42c6d3bf 100644 --- a/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt +++ b/app/src/main/java/io/github/wulkanowy/services/widgets/TimetableWidgetService.kt @@ -2,7 +2,7 @@ package io.github.wulkanowy.services.widgets import android.content.Intent import android.widget.RemoteViewsService -import dagger.android.AndroidInjection +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository @@ -12,6 +12,7 @@ import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetFactory import timber.log.Timber import javax.inject.Inject +@AndroidEntryPoint class TimetableWidgetService : RemoteViewsService() { @Inject @@ -30,7 +31,6 @@ class TimetableWidgetService : RemoteViewsService() { lateinit var sharedPref: SharedPrefProvider override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory { - AndroidInjection.inject(this) Timber.d("TimetableWidgetFactory created") return TimetableWidgetFactory(timetableRepo, studentRepo, semesterRepo, prefRepository, sharedPref, applicationContext, intent) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BaseActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BaseActivity.kt index f20fb22f..86b6701b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BaseActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BaseActivity.kt @@ -14,9 +14,6 @@ import androidx.appcompat.app.AppCompatDelegate import androidx.viewbinding.ViewBinding import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar.LENGTH_LONG -import dagger.android.AndroidInjection -import dagger.android.DispatchingAndroidInjector -import dagger.android.HasAndroidInjector import io.github.wulkanowy.R import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.utils.FragmentLifecycleLogger @@ -25,13 +22,10 @@ import io.github.wulkanowy.utils.lifecycleAwareVariable import javax.inject.Inject abstract class BaseActivity, VB : ViewBinding> : - AppCompatActivity(), BaseView, HasAndroidInjector { + AppCompatActivity(), BaseView { protected var binding: VB by lifecycleAwareVariable() - @Inject - lateinit var androidInjector: DispatchingAndroidInjector - @Inject lateinit var fragmentLifecycleLogger: FragmentLifecycleLogger @@ -42,8 +36,7 @@ abstract class BaseActivity, VB : ViewBinding> : abstract var presenter: T - public override fun onCreate(savedInstanceState: Bundle?) { - AndroidInjection.inject(this) + override fun onCreate(savedInstanceState: Bundle?) { themeManager.applyActivityTheme(this) super.onCreate(savedInstanceState) supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleLogger, true) @@ -91,6 +84,4 @@ abstract class BaseActivity, VB : ViewBinding> : invalidateOptionsMenu() presenter.onDetachView() } - - override fun androidInjector() = androidInjector } diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BaseDialogFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BaseDialogFragment.kt index 0279dccf..18cd58b4 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BaseDialogFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BaseDialogFragment.kt @@ -1,11 +1,11 @@ package io.github.wulkanowy.ui.base import android.widget.Toast +import androidx.fragment.app.DialogFragment import androidx.viewbinding.ViewBinding -import dagger.android.support.DaggerAppCompatDialogFragment import io.github.wulkanowy.utils.lifecycleAwareVariable -abstract class BaseDialogFragment : DaggerAppCompatDialogFragment(), BaseView { +abstract class BaseDialogFragment : DialogFragment(), BaseView { protected var binding: VB by lifecycleAwareVariable() diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragment.kt index 83f78765..c6a2e1d1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragment.kt @@ -2,14 +2,14 @@ package io.github.wulkanowy.ui.base import android.view.View import androidx.annotation.LayoutRes +import androidx.fragment.app.Fragment import androidx.viewbinding.ViewBinding import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar.LENGTH_LONG -import dagger.android.support.DaggerFragment import io.github.wulkanowy.R import io.github.wulkanowy.utils.lifecycleAwareVariable -abstract class BaseFragment(@LayoutRes layoutId: Int) : DaggerFragment(layoutId), +abstract class BaseFragment(@LayoutRes layoutId: Int) : Fragment(layoutId), BaseView { protected var binding: VB by lifecycleAwareVariable() diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt index 627e0a5f..8d6739a1 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/ErrorDialog.kt @@ -11,6 +11,7 @@ import android.widget.Toast import android.widget.Toast.LENGTH_LONG import androidx.appcompat.app.AlertDialog import androidx.core.content.getSystemService +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.DialogErrorBinding import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException @@ -28,6 +29,7 @@ import java.net.SocketTimeoutException import java.net.UnknownHostException import javax.inject.Inject +@AndroidEntryPoint class ErrorDialog : BaseDialogFragment() { private lateinit var error: Throwable diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt index d85d01e9..5ec1a66e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/AboutFragment.kt @@ -4,6 +4,7 @@ import android.graphics.drawable.Drawable import android.os.Bundle import android.view.View import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentAboutBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -19,6 +20,7 @@ import io.github.wulkanowy.utils.openEmailClient import io.github.wulkanowy.utils.openInternetBrowser import javax.inject.Inject +@AndroidEntryPoint class AboutFragment : BaseFragment(R.layout.fragment_about), AboutView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorFragment.kt index 42eebd34..5d7e076c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/contributor/ContributorFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.pojos.Contributor import io.github.wulkanowy.databinding.FragmentContributorBinding @@ -14,6 +15,7 @@ import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.openInternetBrowser import javax.inject.Inject +@AndroidEntryPoint class ContributorFragment : BaseFragment(R.layout.fragment_contributor), ContributorView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseFragment.kt index f6c3b569..cdd29b41 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseFragment.kt @@ -9,13 +9,14 @@ import androidx.core.text.parseAsHtml import androidx.recyclerview.widget.LinearLayoutManager import com.mikepenz.aboutlibraries.Libs import com.mikepenz.aboutlibraries.entity.Library -import dagger.Lazy +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentLicenseBinding import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.main.MainView import javax.inject.Inject +@AndroidEntryPoint class LicenseFragment : BaseFragment(R.layout.fragment_license), LicenseView, MainView.TitledView { @@ -25,14 +26,13 @@ class LicenseFragment : BaseFragment(R.layout.fragment_l @Inject lateinit var licenseAdapter: LicenseAdapter - @Inject - lateinit var libs: Lazy + private val libs by lazy { Libs(requireContext()) } override val titleStringId get() = R.string.license_title override val appLibraries: ArrayList? get() = context?.let { - libs.get().prepareLibraries(it, emptyArray(), emptyArray(), autoDetect = true, checkCachedDetection = true, sort = true) + libs.prepareLibraries(it, emptyArray(), emptyArray(), autoDetect = true, checkCachedDetection = true, sort = true) } companion object { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseModule.kt deleted file mode 100644 index 0ffc3d90..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/license/LicenseModule.kt +++ /dev/null @@ -1,15 +0,0 @@ -package io.github.wulkanowy.ui.modules.about.license - -import android.content.Context -import com.mikepenz.aboutlibraries.Libs -import dagger.Module -import dagger.Provides -import io.github.wulkanowy.di.scopes.PerFragment - -@Module -class LicenseModule { - - @PerFragment - @Provides - fun provideLibs(context: Context) = Libs(context) -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerFragment.kt index 08f91aff..cf40975e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/about/logviewer/LogViewerFragment.kt @@ -14,6 +14,7 @@ import android.view.MenuItem import android.view.View import androidx.core.content.FileProvider import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.BuildConfig.APPLICATION_ID import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentLogviewerBinding @@ -22,6 +23,7 @@ import io.github.wulkanowy.ui.modules.main.MainView import java.io.File import javax.inject.Inject +@AndroidEntryPoint class LogViewerFragment : BaseFragment(R.layout.fragment_logviewer), LogViewerView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountDialog.kt index 32061374..1fa87268 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/account/AccountDialog.kt @@ -8,12 +8,14 @@ import android.widget.Toast import android.widget.Toast.LENGTH_LONG import androidx.appcompat.app.AlertDialog import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.DialogAccountBinding import io.github.wulkanowy.ui.base.BaseDialogFragment import io.github.wulkanowy.ui.modules.login.LoginActivity import javax.inject.Inject +@AndroidEntryPoint class AccountDialog : BaseDialogFragment(), AccountView { @Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt index 51f9cb7b..c7caef06 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/AttendanceFragment.kt @@ -14,6 +14,7 @@ import androidx.appcompat.app.AlertDialog import androidx.appcompat.view.ActionMode import androidx.recyclerview.widget.LinearLayoutManager import com.wdullaer.materialdatetimepicker.date.DatePickerDialog +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Attendance import io.github.wulkanowy.databinding.DialogExcuseBinding @@ -28,6 +29,7 @@ import io.github.wulkanowy.utils.dpToPx import java.time.LocalDate import javax.inject.Inject +@AndroidEntryPoint class AttendanceFragment : BaseFragment(R.layout.fragment_attendance), AttendanceView, MainView.MainChildView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryFragment.kt index 1b9fe08e..2f862237 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/attendance/summary/AttendanceSummaryFragment.kt @@ -8,6 +8,7 @@ import android.view.View.VISIBLE import android.widget.ArrayAdapter import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.AttendanceSummary import io.github.wulkanowy.databinding.FragmentAttendanceSummaryBinding @@ -17,6 +18,7 @@ import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.setOnItemSelectedListener import javax.inject.Inject +@AndroidEntryPoint class AttendanceSummaryFragment : BaseFragment(R.layout.fragment_attendance_summary), AttendanceSummaryView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamFragment.kt index eeab30c1..000da00f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/exam/ExamFragment.kt @@ -6,6 +6,7 @@ import android.view.View.GONE import android.view.View.INVISIBLE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.databinding.FragmentExamBinding @@ -16,6 +17,7 @@ import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.dpToPx import javax.inject.Inject +@AndroidEntryPoint class ExamFragment : BaseFragment(R.layout.fragment_exam), ExamView, MainView.MainChildView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt index 59b83c4b..0678e13e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt @@ -8,6 +8,7 @@ import android.view.View import android.view.View.INVISIBLE import android.view.View.VISIBLE import androidx.appcompat.app.AlertDialog +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentGradeBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -20,13 +21,14 @@ import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.setOnSelectPageListener import javax.inject.Inject -class GradeFragment : BaseFragment(R.layout.fragment_grade), GradeView, MainView.MainChildView, MainView.TitledView { +@AndroidEntryPoint +class GradeFragment : BaseFragment(R.layout.fragment_grade), GradeView, + MainView.MainChildView, MainView.TitledView { @Inject lateinit var presenter: GradePresenter - @Inject - lateinit var pagerAdapter: BaseFragmentPagerAdapter + private val pagerAdapter by lazy { BaseFragmentPagerAdapter(childFragmentManager) } private var semesterSwitchMenu: MenuItem? = null diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeModule.kt deleted file mode 100644 index 342f356c..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeModule.kt +++ /dev/null @@ -1,35 +0,0 @@ -package io.github.wulkanowy.ui.modules.grade - -import dagger.Module -import dagger.Provides -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.di.scopes.PerChildFragment -import io.github.wulkanowy.di.scopes.PerFragment -import io.github.wulkanowy.ui.base.BaseFragmentPagerAdapter -import io.github.wulkanowy.ui.modules.grade.details.GradeDetailsFragment -import io.github.wulkanowy.ui.modules.grade.statistics.GradeStatisticsFragment -import io.github.wulkanowy.ui.modules.grade.summary.GradeSummaryFragment - -@Suppress("unused") -@Module -abstract class GradeModule { - - companion object { - - @PerFragment - @Provides - fun provideGradeAdapter(fragment: GradeFragment) = BaseFragmentPagerAdapter(fragment.childFragmentManager) - } - - @PerChildFragment - @ContributesAndroidInjector - abstract fun bindGradeDetailsFragment(): GradeDetailsFragment - - @PerChildFragment - @ContributesAndroidInjector - abstract fun binGradeSummaryFragment(): GradeSummaryFragment - - @PerChildFragment - @ContributesAndroidInjector - abstract fun binGradeStatisticsFragment(): GradeStatisticsFragment -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt index ac50b9f5..ef9a932e 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/details/GradeDetailsFragment.kt @@ -9,6 +9,7 @@ import android.view.View.GONE import android.view.View.INVISIBLE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.databinding.FragmentGradeDetailsBinding @@ -18,6 +19,7 @@ import io.github.wulkanowy.ui.modules.grade.GradeView import io.github.wulkanowy.ui.modules.main.MainActivity import javax.inject.Inject +@AndroidEntryPoint class GradeDetailsFragment : BaseFragment(R.layout.fragment_grade_details), GradeDetailsView, GradeView.GradeChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt index 3a8f4007..75050249 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/statistics/GradeStatisticsFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.widget.ArrayAdapter import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.pojos.GradeStatisticsItem import io.github.wulkanowy.databinding.FragmentGradeStatisticsBinding @@ -15,6 +16,7 @@ import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.setOnItemSelectedListener import javax.inject.Inject +@AndroidEntryPoint class GradeStatisticsFragment : BaseFragment(R.layout.fragment_grade_statistics), GradeStatisticsView, GradeView.GradeChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryFragment.kt index d169f7c6..4d12dcd5 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/summary/GradeSummaryFragment.kt @@ -6,6 +6,7 @@ import android.view.View.GONE import android.view.View.INVISIBLE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.databinding.FragmentGradeSummaryBinding @@ -14,6 +15,7 @@ import io.github.wulkanowy.ui.modules.grade.GradeFragment import io.github.wulkanowy.ui.modules.grade.GradeView import javax.inject.Inject +@AndroidEntryPoint class GradeSummaryFragment : BaseFragment(R.layout.fragment_grade_summary), GradeSummaryView, GradeView.GradeChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt index ee2751d7..e1d7ac52 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/HomeworkFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.databinding.FragmentHomeworkBinding @@ -16,6 +17,7 @@ import io.github.wulkanowy.ui.widgets.DividerItemDecoration import io.github.wulkanowy.utils.dpToPx import javax.inject.Inject +@AndroidEntryPoint class HomeworkFragment : BaseFragment(R.layout.fragment_homework), HomeworkView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt index 82938a47..78abfffd 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/homework/details/HomeworkDetailsDialog.kt @@ -8,6 +8,7 @@ import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.databinding.DialogHomeworkBinding @@ -15,6 +16,7 @@ import io.github.wulkanowy.ui.base.BaseDialogFragment import io.github.wulkanowy.utils.openInternetBrowser import javax.inject.Inject +@AndroidEntryPoint class HomeworkDetailsDialog : BaseDialogFragment(), HomeworkDetailsView { @Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt index ffb36fe6..c568b33c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.Intent import android.os.Bundle import android.view.MenuItem +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.ActivityLoginBinding import io.github.wulkanowy.ui.base.BaseActivity @@ -16,13 +17,13 @@ import io.github.wulkanowy.ui.modules.login.symbol.LoginSymbolFragment import io.github.wulkanowy.utils.setOnSelectPageListener import javax.inject.Inject +@AndroidEntryPoint class LoginActivity : BaseActivity(), LoginView { @Inject override lateinit var presenter: LoginPresenter - @Inject - lateinit var loginAdapter: BaseFragmentPagerAdapter + private val loginAdapter = BaseFragmentPagerAdapter(supportFragmentManager) companion object { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginModule.kt deleted file mode 100644 index 26a243dc..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginModule.kt +++ /dev/null @@ -1,45 +0,0 @@ -package io.github.wulkanowy.ui.modules.login - -import dagger.Module -import dagger.Provides -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.di.scopes.PerActivity -import io.github.wulkanowy.di.scopes.PerFragment -import io.github.wulkanowy.ui.base.BaseFragmentPagerAdapter -import io.github.wulkanowy.ui.modules.login.advanced.LoginAdvancedFragment -import io.github.wulkanowy.ui.modules.login.form.LoginFormFragment -import io.github.wulkanowy.ui.modules.login.recover.LoginRecoverFragment -import io.github.wulkanowy.ui.modules.login.studentselect.LoginStudentSelectFragment -import io.github.wulkanowy.ui.modules.login.symbol.LoginSymbolFragment - -@Suppress("unused") -@Module -internal abstract class LoginModule { - - companion object { - - @PerActivity - @Provides - fun provideLoginAdapter(activity: LoginActivity) = BaseFragmentPagerAdapter(activity.supportFragmentManager) - } - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLoginFormFragment(): LoginFormFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLoginAdvancedFragment(): LoginAdvancedFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLoginSymbolFragment(): LoginSymbolFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLoginSelectStudentFragment(): LoginStudentSelectFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLoginRecoverFragment(): LoginRecoverFragment -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedFragment.kt index 3b6a985b..38951791 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/advanced/LoginAdvancedFragment.kt @@ -6,6 +6,7 @@ import android.view.View.GONE import android.view.View.VISIBLE import android.widget.ArrayAdapter import androidx.core.widget.doOnTextChanged +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.FragmentLoginAdvancedBinding @@ -18,6 +19,7 @@ import io.github.wulkanowy.utils.setOnEditorDoneSignIn import io.github.wulkanowy.utils.showSoftInput import javax.inject.Inject +@AndroidEntryPoint class LoginAdvancedFragment : BaseFragment(R.layout.fragment_login_advanced), LoginAdvancedView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt index a2a083c0..2dd5abc3 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/form/LoginFormFragment.kt @@ -6,6 +6,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.core.widget.doOnTextChanged +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.FragmentLoginFormBinding @@ -19,6 +20,7 @@ import io.github.wulkanowy.utils.setOnEditorDoneSignIn import io.github.wulkanowy.utils.showSoftInput import javax.inject.Inject +@AndroidEntryPoint class LoginFormFragment : BaseFragment(R.layout.fragment_login_form), LoginFormView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverFragment.kt index e27c845a..f0c9652f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/recover/LoginRecoverFragment.kt @@ -10,6 +10,7 @@ import android.webkit.JavascriptInterface import android.webkit.WebView import android.webkit.WebViewClient import androidx.core.widget.doOnTextChanged +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentLoginRecoverBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -19,6 +20,7 @@ import io.github.wulkanowy.utils.hideSoftInput import io.github.wulkanowy.utils.showSoftInput import javax.inject.Inject +@AndroidEntryPoint class LoginRecoverFragment : BaseFragment(R.layout.fragment_login_recover), LoginRecoverView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectFragment.kt index b0df5199..d2884c21 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/studentselect/LoginStudentSelectFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.FragmentLoginStudentSelectBinding @@ -16,6 +17,7 @@ import io.github.wulkanowy.utils.openInternetBrowser import java.io.Serializable import javax.inject.Inject +@AndroidEntryPoint class LoginStudentSelectFragment : BaseFragment(R.layout.fragment_login_student_select), LoginStudentSelectView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolFragment.kt index befbffd5..005fcb7f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/symbol/LoginSymbolFragment.kt @@ -8,6 +8,7 @@ import android.view.inputmethod.EditorInfo.IME_ACTION_DONE import android.view.inputmethod.EditorInfo.IME_NULL import android.widget.ArrayAdapter import androidx.core.widget.doOnTextChanged +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.FragmentLoginSymbolBinding @@ -20,6 +21,7 @@ import io.github.wulkanowy.utils.openInternetBrowser import io.github.wulkanowy.utils.showSoftInput import javax.inject.Inject +@AndroidEntryPoint class LoginSymbolFragment : BaseFragment(R.layout.fragment_login_symbol), LoginSymbolView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberFragment.kt index 0775ce18..0f1d836d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumber/LuckyNumberFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.View import android.view.View.GONE import android.view.View.VISIBLE +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.databinding.FragmentLuckyNumberBinding @@ -11,6 +12,7 @@ import io.github.wulkanowy.ui.base.BaseFragment import io.github.wulkanowy.ui.modules.main.MainView import javax.inject.Inject +@AndroidEntryPoint class LuckyNumberFragment : BaseFragment(R.layout.fragment_lucky_number), LuckyNumberView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt index 961ac633..692615d9 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt @@ -9,6 +9,7 @@ import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.ActivityWidgetConfigureBinding @@ -18,6 +19,7 @@ import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject +@AndroidEntryPoint class LuckyNumberWidgetConfigureActivity : BaseActivity(), LuckyNumberWidgetConfigureView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index 3daaaaf6..4d42b355 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -7,13 +7,12 @@ import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH import android.appwidget.AppWidgetProvider import android.content.Context -import android.content.Intent import android.content.res.Configuration import android.os.Bundle import android.view.View.GONE import android.view.View.VISIBLE import android.widget.RemoteViews -import dagger.android.AndroidInjection +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.exceptions.NoCurrentStudentException @@ -26,6 +25,7 @@ import kotlinx.coroutines.runBlocking import timber.log.Timber import javax.inject.Inject +@AndroidEntryPoint class LuckyNumberWidgetProvider : AppWidgetProvider() { @Inject @@ -48,11 +48,6 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { fun getWidthWidgetKey(appWidgetId: Int) = "lucky_number_widget_width_$appWidgetId" } - override fun onReceive(context: Context, intent: Intent) { - AndroidInjection.inject(this, context) - super.onReceive(context, intent) - } - override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray?) { super.onUpdate(context, appWidgetManager, appWidgetIds) appWidgetIds?.forEach { appWidgetId -> diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt index f5b7c47c..03560889 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainActivity.kt @@ -17,7 +17,7 @@ import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem import com.google.android.material.elevation.ElevationOverlayProvider import com.ncapdevi.fragnav.FragNavController import com.ncapdevi.fragnav.FragNavController.Companion.HIDE -import dagger.Lazy +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.ActivityMainBinding import io.github.wulkanowy.ui.base.BaseActivity @@ -39,19 +39,18 @@ import io.github.wulkanowy.utils.setOnViewChangeListener import timber.log.Timber import javax.inject.Inject +@AndroidEntryPoint class MainActivity : BaseActivity(), MainView { @Inject override lateinit var presenter: MainPresenter - @Inject - lateinit var navController: FragNavController - @Inject lateinit var analytics: FirebaseAnalyticsHelper - @Inject - lateinit var overlayProvider: Lazy + private val overlayProvider by lazy { ElevationOverlayProvider(this) } + + private val navController = FragNavController(supportFragmentManager, R.id.mainFragmentContainer) companion object { const val EXTRA_START_MENU = "extraStartMenu" @@ -106,7 +105,7 @@ class MainActivity : BaseActivity(), MainVie override fun initView() { with(binding.mainToolbar) { if (SDK_INT >= LOLLIPOP) stateListAnimator = null - setBackgroundColor(overlayProvider.get().compositeOverlayWithThemeSurfaceColorIfNeeded(dpToPx(4f))) + setBackgroundColor(overlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(dpToPx(4f))) } with(binding.mainBottomNav) { @@ -119,7 +118,7 @@ class MainActivity : BaseActivity(), MainVie )) accentColor = getThemeAttrColor(R.attr.colorPrimary) inactiveColor = getThemeAttrColor(R.attr.colorOnSurface, 153) - defaultBackgroundColor = overlayProvider.get().compositeOverlayWithThemeSurfaceColorIfNeeded(dpToPx(8f)) + defaultBackgroundColor = overlayProvider.compositeOverlayWithThemeSurfaceColorIfNeeded(dpToPx(8f)) titleState = ALWAYS_SHOW currentItem = startMenuIndex isBehaviorTranslationEnabled = false diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainModule.kt deleted file mode 100644 index 03f6ac38..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/main/MainModule.kt +++ /dev/null @@ -1,139 +0,0 @@ -package io.github.wulkanowy.ui.modules.main - -import com.google.android.material.elevation.ElevationOverlayProvider -import com.ncapdevi.fragnav.FragNavController -import dagger.Module -import dagger.Provides -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.R -import io.github.wulkanowy.di.scopes.PerFragment -import io.github.wulkanowy.ui.modules.about.AboutFragment -import io.github.wulkanowy.ui.modules.about.contributor.ContributorFragment -import io.github.wulkanowy.ui.modules.about.license.LicenseFragment -import io.github.wulkanowy.ui.modules.about.license.LicenseModule -import io.github.wulkanowy.ui.modules.about.logviewer.LogViewerFragment -import io.github.wulkanowy.ui.modules.account.AccountDialog -import io.github.wulkanowy.ui.modules.attendance.AttendanceFragment -import io.github.wulkanowy.ui.modules.attendance.summary.AttendanceSummaryFragment -import io.github.wulkanowy.ui.modules.exam.ExamFragment -import io.github.wulkanowy.ui.modules.grade.GradeFragment -import io.github.wulkanowy.ui.modules.grade.GradeModule -import io.github.wulkanowy.ui.modules.homework.HomeworkFragment -import io.github.wulkanowy.ui.modules.homework.details.HomeworkDetailsDialog -import io.github.wulkanowy.ui.modules.luckynumber.LuckyNumberFragment -import io.github.wulkanowy.ui.modules.message.MessageFragment -import io.github.wulkanowy.ui.modules.message.MessageModule -import io.github.wulkanowy.ui.modules.message.preview.MessagePreviewFragment -import io.github.wulkanowy.ui.modules.mobiledevice.MobileDeviceFragment -import io.github.wulkanowy.ui.modules.mobiledevice.token.MobileDeviceTokenDialog -import io.github.wulkanowy.ui.modules.more.MoreFragment -import io.github.wulkanowy.ui.modules.note.NoteFragment -import io.github.wulkanowy.ui.modules.schoolandteachers.SchoolAndTeachersFragment -import io.github.wulkanowy.ui.modules.schoolandteachers.SchoolAndTeachersModule -import io.github.wulkanowy.ui.modules.settings.SettingsFragment -import io.github.wulkanowy.ui.modules.timetable.TimetableFragment -import io.github.wulkanowy.ui.modules.timetable.completed.CompletedLessonsFragment - -@Suppress("unused") -@Module -abstract class MainModule { - - companion object { - - @Provides - fun provideFragNavController(activity: MainActivity) = - FragNavController(activity.supportFragmentManager, R.id.mainFragmentContainer) - - //In activities must be injected as Lazy - @Provides - fun provideElevationOverlayProvider(activity: MainActivity) = ElevationOverlayProvider(activity) - } - - @PerFragment - @ContributesAndroidInjector - abstract fun bindAttendanceFragment(): AttendanceFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindAttendanceSummaryFragment(): AttendanceSummaryFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindExamFragment(): ExamFragment - - @PerFragment - @ContributesAndroidInjector(modules = [GradeModule::class]) - abstract fun bindGradeFragment(): GradeFragment - - @PerFragment - @ContributesAndroidInjector(modules = [MessageModule::class]) - abstract fun bindMessagesFragment(): MessageFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindMessagePreviewFragment(): MessagePreviewFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindMoreFragment(): MoreFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindTimetableFragment(): TimetableFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindAboutFragment(): AboutFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindSettingsFragment(): SettingsFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindNoteFragment(): NoteFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindHomeworkFragment(): HomeworkFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindHomeworkDetailsDialog(): HomeworkDetailsDialog - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLuckyNumberFragment(): LuckyNumberFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindCompletedLessonsFragment(): CompletedLessonsFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindAccountDialog(): AccountDialog - - @PerFragment - @ContributesAndroidInjector - abstract fun bindMobileDevices(): MobileDeviceFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindMobileDeviceDialog(): MobileDeviceTokenDialog - - @PerFragment - @ContributesAndroidInjector(modules = [LicenseModule::class]) - abstract fun bindLicenseFragment(): LicenseFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindLogViewerFragment(): LogViewerFragment - - @PerFragment - @ContributesAndroidInjector - abstract fun bindContributorFragment(): ContributorFragment - - @PerFragment - @ContributesAndroidInjector(modules = [SchoolAndTeachersModule::class]) - abstract fun bindSchoolAndTeachersFragment(): SchoolAndTeachersFragment -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt index 1e48f71b..16e0184a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.View import android.view.View.INVISIBLE import android.view.View.VISIBLE +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.repositories.message.MessageFolder.RECEIVED import io.github.wulkanowy.data.repositories.message.MessageFolder.SENT @@ -18,14 +19,14 @@ import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.setOnSelectPageListener import javax.inject.Inject +@AndroidEntryPoint class MessageFragment : BaseFragment(R.layout.fragment_message), MessageView, MainView.TitledView { @Inject lateinit var presenter: MessagePresenter - @Inject - lateinit var pagerAdapter: BaseFragmentPagerAdapter + private val pagerAdapter by lazy { BaseFragmentPagerAdapter(childFragmentManager) } companion object { fun newInstance() = MessageFragment() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageModule.kt deleted file mode 100644 index bf795020..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageModule.kt +++ /dev/null @@ -1,25 +0,0 @@ -package io.github.wulkanowy.ui.modules.message - -import dagger.Module -import dagger.Provides -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.di.scopes.PerChildFragment -import io.github.wulkanowy.di.scopes.PerFragment -import io.github.wulkanowy.ui.base.BaseFragmentPagerAdapter -import io.github.wulkanowy.ui.modules.message.tab.MessageTabFragment - -@Suppress("unused") -@Module -abstract class MessageModule { - - companion object { - - @PerFragment - @Provides - fun provideMessageAdapter(fragment: MessageFragment) = BaseFragmentPagerAdapter(fragment.childFragmentManager) - } - - @PerChildFragment - @ContributesAndroidInjector - abstract fun bindMessageTabFragment(): MessageTabFragment -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt index e218d759..740f4927 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/preview/MessagePreviewFragment.kt @@ -16,6 +16,7 @@ import android.webkit.WebViewClient import androidx.annotation.RequiresApi import androidx.core.content.getSystemService import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.MessageWithAttachment @@ -28,6 +29,7 @@ import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.shareText import javax.inject.Inject +@AndroidEntryPoint class MessagePreviewFragment : BaseFragment(R.layout.fragment_message_preview), MessagePreviewView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessageActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessageActivity.kt index 7b750343..2267279c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessageActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/send/SendMessageActivity.kt @@ -11,6 +11,7 @@ import android.view.View.GONE import android.view.View.VISIBLE import android.widget.Toast import android.widget.Toast.LENGTH_LONG +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.ReportingUnit @@ -21,6 +22,7 @@ import io.github.wulkanowy.utils.hideSoftInput import io.github.wulkanowy.utils.showSoftInput import javax.inject.Inject +@AndroidEntryPoint class SendMessageActivity : BaseActivity(), SendMessageView { @Inject @@ -62,7 +64,7 @@ class SendMessageActivity : BaseActivity(R.layout.fragment_message_tab), MessageTabView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt index 6a3e5a44..8065e9b6 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/MobileDeviceFragment.kt @@ -7,6 +7,7 @@ import android.view.View.VISIBLE import androidx.core.view.postDelayed import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.snackbar.Snackbar +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.MobileDevice import io.github.wulkanowy.databinding.FragmentMobileDeviceBinding @@ -17,6 +18,7 @@ import io.github.wulkanowy.ui.modules.mobiledevice.token.MobileDeviceTokenDialog import io.github.wulkanowy.ui.widgets.DividerItemDecoration import javax.inject.Inject +@AndroidEntryPoint class MobileDeviceFragment : BaseFragment(R.layout.fragment_mobile_device), MobileDeviceView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenDialog.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenDialog.kt index 9ac6049e..48150d8d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenDialog.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/mobiledevice/token/MobileDeviceTokenDialog.kt @@ -12,12 +12,14 @@ import android.view.View.VISIBLE import android.view.ViewGroup import android.widget.Toast import androidx.core.content.getSystemService +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.pojos.MobileDeviceToken import io.github.wulkanowy.databinding.DialogMobileDeviceBinding import io.github.wulkanowy.ui.base.BaseDialogFragment import javax.inject.Inject +@AndroidEntryPoint class MobileDeviceTokenDialog : BaseDialogFragment(), MobileDeviceTokenVIew { @Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreFragment.kt index a79087de..1bdcc26f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/more/MoreFragment.kt @@ -4,6 +4,7 @@ import android.graphics.drawable.Drawable import android.os.Bundle import android.view.View import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentMoreBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -20,6 +21,7 @@ import io.github.wulkanowy.ui.modules.settings.SettingsFragment import io.github.wulkanowy.utils.getCompatDrawable import javax.inject.Inject +@AndroidEntryPoint class MoreFragment : BaseFragment(R.layout.fragment_more), MoreView, MainView.TitledView, MainView.MainChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteFragment.kt index 47338165..079285a8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/note/NoteFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.databinding.FragmentNoteBinding @@ -14,6 +15,7 @@ import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.ui.widgets.DividerItemDecoration import javax.inject.Inject +@AndroidEntryPoint class NoteFragment : BaseFragment(R.layout.fragment_note), NoteView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersFragment.kt index 0245aa07..c1c56961 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.View import android.view.View.INVISIBLE import android.view.View.VISIBLE +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.databinding.FragmentSchoolandteachersBinding import io.github.wulkanowy.ui.base.BaseFragment @@ -15,6 +16,7 @@ import io.github.wulkanowy.utils.dpToPx import io.github.wulkanowy.utils.setOnSelectPageListener import javax.inject.Inject +@AndroidEntryPoint class SchoolAndTeachersFragment : BaseFragment(R.layout.fragment_schoolandteachers), SchoolAndTeachersView, MainView.TitledView { @@ -22,8 +24,7 @@ class SchoolAndTeachersFragment : @Inject lateinit var presenter: SchoolAndTeachersPresenter - @Inject - lateinit var pagerAdapter: BaseFragmentPagerAdapter + private val pagerAdapter by lazy { BaseFragmentPagerAdapter(childFragmentManager) } companion object { fun newInstance() = SchoolAndTeachersFragment() diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersModule.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersModule.kt deleted file mode 100644 index 2087b0b4..00000000 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/SchoolAndTeachersModule.kt +++ /dev/null @@ -1,30 +0,0 @@ -package io.github.wulkanowy.ui.modules.schoolandteachers - -import dagger.Module -import dagger.Provides -import dagger.android.ContributesAndroidInjector -import io.github.wulkanowy.di.scopes.PerChildFragment -import io.github.wulkanowy.di.scopes.PerFragment -import io.github.wulkanowy.ui.base.BaseFragmentPagerAdapter -import io.github.wulkanowy.ui.modules.schoolandteachers.school.SchoolFragment -import io.github.wulkanowy.ui.modules.schoolandteachers.teacher.TeacherFragment - -@Suppress("unused") -@Module -abstract class SchoolAndTeachersModule { - - companion object { - - @PerFragment - @Provides - fun provideSchoolAndTeachersAdapter(fragment: SchoolAndTeachersFragment) = BaseFragmentPagerAdapter(fragment.childFragmentManager) - } - - @PerChildFragment - @ContributesAndroidInjector - abstract fun provideSchoolFragment(): SchoolFragment - - @PerChildFragment - @ContributesAndroidInjector - abstract fun provideTeacherFragment(): TeacherFragment -} diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolFragment.kt index 722999f8..d3f11007 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/school/SchoolFragment.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.View import android.view.View.GONE import android.view.View.VISIBLE +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.School import io.github.wulkanowy.databinding.FragmentSchoolBinding @@ -15,6 +16,7 @@ import io.github.wulkanowy.utils.openDialer import io.github.wulkanowy.utils.openNavigation import javax.inject.Inject +@AndroidEntryPoint class SchoolFragment : BaseFragment(R.layout.fragment_school), SchoolView, MainView.TitledView, SchoolAndTeachersChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherFragment.kt index aa50339c..5914945b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/schoolandteachers/teacher/TeacherFragment.kt @@ -5,6 +5,7 @@ import android.view.View import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Teacher import io.github.wulkanowy.databinding.FragmentTeacherBinding @@ -15,6 +16,7 @@ import io.github.wulkanowy.ui.modules.schoolandteachers.SchoolAndTeachersFragmen import io.github.wulkanowy.ui.widgets.DividerItemDecoration import javax.inject.Inject +@AndroidEntryPoint class TeacherFragment : BaseFragment(R.layout.fragment_teacher), TeacherView, MainView.TitledView, SchoolAndTeachersChildView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt index 248417fd..a8a15cec 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/settings/SettingsFragment.kt @@ -9,7 +9,7 @@ import androidx.preference.PreferenceFragmentCompat import com.thelittlefireman.appkillermanager.AppKillerManager import com.thelittlefireman.appkillermanager.exceptions.NoActionFoundException import com.yariksoffice.lingver.Lingver -import dagger.android.support.AndroidSupportInjection +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.base.ErrorDialog @@ -18,6 +18,7 @@ import io.github.wulkanowy.utils.AppInfo import io.github.wulkanowy.utils.openInternetBrowser import javax.inject.Inject +@AndroidEntryPoint class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener, MainView.TitledView, SettingsView { @@ -42,7 +43,6 @@ class SettingsFragment : PreferenceFragmentCompat(), override val syncFailedString get() = getString(R.string.pref_services_message_sync_failed) override fun onAttach(context: Context) { - AndroidSupportInjection.inject(this) super.onAttach(context) } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt index 23a43750..7fc20d23 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/splash/SplashActivity.kt @@ -4,11 +4,13 @@ import android.os.Bundle import android.widget.Toast import android.widget.Toast.LENGTH_LONG import androidx.viewbinding.ViewBinding +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.ui.modules.main.MainActivity import javax.inject.Inject +@AndroidEntryPoint class SplashActivity : BaseActivity(), SplashView { @Inject diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt index c2be76ea..2c5e136d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/TimetableFragment.kt @@ -9,6 +9,7 @@ import android.view.View.GONE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager import com.wdullaer.materialdatetimepicker.date.DatePickerDialog +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Timetable import io.github.wulkanowy.databinding.FragmentTimetableBinding @@ -22,6 +23,7 @@ import io.github.wulkanowy.utils.dpToPx import java.time.LocalDate import javax.inject.Inject +@AndroidEntryPoint class TimetableFragment : BaseFragment(R.layout.fragment_timetable), TimetableView, MainView.MainChildView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt index 5a41f96c..5b77fc1f 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetable/completed/CompletedLessonsFragment.kt @@ -7,6 +7,7 @@ import android.view.View.INVISIBLE import android.view.View.VISIBLE import androidx.recyclerview.widget.LinearLayoutManager import com.wdullaer.materialdatetimepicker.date.DatePickerDialog +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.CompletedLesson import io.github.wulkanowy.databinding.FragmentTimetableCompletedBinding @@ -20,6 +21,7 @@ import io.github.wulkanowy.utils.getCompatDrawable import java.time.LocalDate import javax.inject.Inject +@AndroidEntryPoint class CompletedLessonsFragment : BaseFragment(R.layout.fragment_timetable_completed), CompletedLessonsView, MainView.TitledView { diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt index 3dc7a3a8..23d1f27a 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt @@ -10,6 +10,7 @@ import android.widget.Toast import android.widget.Toast.LENGTH_LONG import androidx.appcompat.app.AlertDialog import androidx.recyclerview.widget.LinearLayoutManager +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.databinding.ActivityWidgetConfigureBinding @@ -20,6 +21,7 @@ import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Co import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject +@AndroidEntryPoint class TimetableWidgetConfigureActivity : BaseActivity(), TimetableWidgetConfigureView { @@ -35,7 +37,7 @@ class TimetableWidgetConfigureActivity : private var dialog: AlertDialog? = null - override fun onCreate(savedInstanceState: Bundle?) { + override public fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setResult(RESULT_CANCELED) setContentView(ActivityWidgetConfigureBinding.inflate(layoutInflater).apply { binding = this }.root) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 426f6cdd..8b3709a8 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -8,19 +8,19 @@ import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_DELETED import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS -import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.res.Configuration import android.widget.RemoteViews -import dagger.android.AndroidInjection +import dagger.hilt.android.AndroidEntryPoint import io.github.wulkanowy.R import io.github.wulkanowy.data.db.SharedPrefProvider import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.exceptions.NoCurrentStudentException import io.github.wulkanowy.data.repositories.student.StudentRepository +import io.github.wulkanowy.services.HiltBroadcastReceiver import io.github.wulkanowy.services.widgets.TimetableWidgetService import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView @@ -36,7 +36,8 @@ import java.time.LocalDate import java.time.LocalDate.now import javax.inject.Inject -class TimetableWidgetProvider : BroadcastReceiver() { +@AndroidEntryPoint +class TimetableWidgetProvider : HiltBroadcastReceiver() { @Inject lateinit var appWidgetManager: AppWidgetManager @@ -74,7 +75,7 @@ class TimetableWidgetProvider : BroadcastReceiver() { } override fun onReceive(context: Context, intent: Intent) { - AndroidInjection.inject(this, context) + super.onReceive(context, intent) GlobalScope.launch { when (intent.action) { ACTION_APPWIDGET_UPDATE -> onUpdate(context, intent) diff --git a/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt index 6912baa3..1098a9a0 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt @@ -7,10 +7,11 @@ import android.os.Build.VERSION.SDK_INT import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.BuildConfig.VERSION_CODE import io.github.wulkanowy.BuildConfig.VERSION_NAME +import javax.inject.Inject import javax.inject.Singleton @Singleton -open class AppInfo { +open class AppInfo @Inject constructor() { open val isDebug get() = DEBUG diff --git a/app/src/play/java/io/github/wulkanowy/utils/FirebaseAnalyticsHelper.kt b/app/src/play/java/io/github/wulkanowy/utils/FirebaseAnalyticsHelper.kt index b0b2fda4..f7e4a0fc 100644 --- a/app/src/play/java/io/github/wulkanowy/utils/FirebaseAnalyticsHelper.kt +++ b/app/src/play/java/io/github/wulkanowy/utils/FirebaseAnalyticsHelper.kt @@ -4,11 +4,14 @@ import android.app.Activity import android.content.Context import android.os.Bundle import com.google.firebase.analytics.FirebaseAnalytics +import dagger.hilt.android.qualifiers.ApplicationContext import javax.inject.Inject import javax.inject.Singleton @Singleton -class FirebaseAnalyticsHelper @Inject constructor(private val context: Context) { +class FirebaseAnalyticsHelper @Inject constructor( + @ApplicationContext private val context: Context +) { private val analytics by lazy { FirebaseAnalytics.getInstance(context) } diff --git a/build.gradle b/build.gradle index 164e258a..cc66780f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,9 @@ buildscript { - ext.kotlin_version = '1.3.72' - ext.about_libraries = '8.3.0' + ext { + kotlin_version = '1.3.72' + about_libraries = '8.3.0' + hilt_version = "2.28.3-alpha" + } repositories { mavenCentral() google() @@ -10,12 +13,13 @@ buildscript { dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.android.tools.build:gradle:4.0.1' + classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" classpath 'com.google.gms:google-services:4.3.3' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.2.0' classpath "com.github.triplet.gradle:play-publisher:2.7.5" classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.0" classpath "gradle.plugin.com.star-zero.gradle:githook:1.2.0" - classpath "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:${about_libraries}" + classpath "com.mikepenz.aboutlibraries.plugin:aboutlibraries-plugin:$about_libraries" } } From 8922d7d48d20324527cfa40e1720283de71eb52d Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 2 Aug 2020 16:30:06 +0000 Subject: [PATCH 22/34] Bump core-ktx from 1.3.0 to 1.3.1 (#911) From 062985c5a0f4fc2515283f7bd60265cd1daf51a9 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 2 Aug 2020 16:30:31 +0000 Subject: [PATCH 23/34] Bump firebase-messaging from 20.2.3 to 20.2.4 (#912) --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index e0a7551c..3d078c81 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -184,7 +184,7 @@ dependencies { playImplementation 'com.google.firebase:firebase-analytics:17.4.4' playImplementation 'com.google.firebase:firebase-inappmessaging-display-ktx:19.1.0' playImplementation "com.google.firebase:firebase-inappmessaging-ktx:19.1.0" - playImplementation 'com.google.firebase:firebase-messaging:20.2.3' + playImplementation 'com.google.firebase:firebase-messaging:20.2.4' playImplementation 'com.google.firebase:firebase-crashlytics:17.1.1' playImplementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava' From ca7d9773421e9254a5fae4c2f9a1b4b5bdde81f0 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 2 Aug 2020 18:41:22 +0200 Subject: [PATCH 24/34] Bump work_manager from 2.3.4 to 2.4.0 (#910) Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> From d5187d1808fe15a2592a556a0434f6b7a7617462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Wed, 26 Aug 2020 10:25:01 +0200 Subject: [PATCH 25/34] Save semesters with students during registration (#915) --- .idea/codeStyles/Project.xml | 11 ++++- app/build.gradle | 7 ++- .../wulkanowy/data/db/dao/SemesterDao.kt | 5 ++ .../wulkanowy/data/db/dao/StudentDao.kt | 6 +++ .../wulkanowy/data/db/entities/Semester.kt | 3 +- .../data/db/entities/StudentWithSemesters.kt | 13 +++++ .../wulkanowy/data/mappers/SemesterMapper.kt | 19 ++++++++ .../wulkanowy/data/mappers/StudentMapper.kt | 34 ++++++++++++++ .../repositories/grade/GradeRepository.kt | 2 +- .../repositories/semester/SemesterLocal.kt | 2 +- .../data/repositories/student/StudentLocal.kt | 4 +- .../repositories/student/StudentRemote.kt | 44 ++++------------- .../repositories/student/StudentRepository.kt | 20 ++++---- .../ui/modules/account/AccountAdapter.kt | 17 ++++--- .../ui/modules/account/AccountPresenter.kt | 14 +++--- .../ui/modules/login/LoginActivity.kt | 14 +++--- .../ui/modules/login/LoginPresenter.kt | 12 ++--- .../wulkanowy/ui/modules/login/LoginView.kt | 4 +- .../login/advanced/LoginAdvancedFragment.kt | 6 +-- .../login/advanced/LoginAdvancedPresenter.kt | 4 +- .../login/advanced/LoginAdvancedView.kt | 4 +- .../modules/login/form/LoginFormFragment.kt | 6 +-- .../ui/modules/login/form/LoginFormView.kt | 4 +- .../LoginStudentSelectAdapter.kt | 15 +++--- .../LoginStudentSelectFragment.kt | 8 ++-- .../LoginStudentSelectPresenter.kt | 47 ++++++++++--------- .../studentselect/LoginStudentSelectView.kt | 4 +- .../login/symbol/LoginSymbolFragment.kt | 6 +-- .../modules/login/symbol/LoginSymbolView.kt | 4 +- .../LuckyNumberWidgetConfigurePresenter.kt | 6 +-- .../LuckyNumberWidgetProvider.kt | 2 +- .../message/tab/MessageTabPresenter.kt | 2 +- .../wulkanowy/ui/modules/note/NoteAdapter.kt | 8 ++-- .../wulkanowy/ui/modules/note/NoteDialog.kt | 8 ++-- .../TimetableWidgetConfigurePresenter.kt | 6 +-- .../timetablewidget/TimetableWidgetFactory.kt | 2 +- .../TimetableWidgetProvider.kt | 2 +- .../wulkanowy/utils/SemesterExtension.kt | 2 +- .../repositories/student/StudentRemoteTest.kt | 8 +++- .../login/form/LoginFormPresenterTest.kt | 3 +- .../LoginStudentSelectPresenterTest.kt | 11 +++-- build.gradle | 2 +- 42 files changed, 239 insertions(+), 162 deletions(-) create mode 100644 app/src/main/java/io/github/wulkanowy/data/db/entities/StudentWithSemesters.kt create mode 100644 app/src/main/java/io/github/wulkanowy/data/mappers/SemesterMapper.kt create mode 100644 app/src/main/java/io/github/wulkanowy/data/mappers/StudentMapper.kt diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index d66c3361..cb2f4119 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -4,7 +4,16 @@ +