Migrate repositories from rxjava to coroutines (#885)
This commit is contained in:
@ -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 {
|
||||
|
@ -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<Attendance> {
|
||||
|
||||
@Query("SELECT * FROM Attendance WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
|
||||
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Attendance>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Attendance>
|
||||
}
|
||||
|
@ -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<AttendanceSummary> {
|
||||
|
||||
@Query("SELECT * FROM AttendanceSummary WHERE diary_id = :diaryId AND student_id = :studentId AND subject_id = :subjectId")
|
||||
fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): Maybe<List<AttendanceSummary>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int, subjectId: Int): List<AttendanceSummary>
|
||||
}
|
||||
|
@ -7,11 +7,11 @@ import androidx.room.Update
|
||||
interface BaseDao<T> {
|
||||
|
||||
@Insert
|
||||
fun insertAll(items: List<T>): List<Long>
|
||||
suspend fun insertAll(items: List<T>): List<Long>
|
||||
|
||||
@Update
|
||||
fun updateAll(items: List<T>)
|
||||
suspend fun updateAll(items: List<T>)
|
||||
|
||||
@Delete
|
||||
fun deleteAll(items: List<T>)
|
||||
suspend fun deleteAll(items: List<T>)
|
||||
}
|
||||
|
@ -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<CompletedLesson> {
|
||||
|
||||
@Query("SELECT * FROM CompletedLesson WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
|
||||
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<CompletedLesson>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<CompletedLesson>
|
||||
}
|
||||
|
@ -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<Exam> {
|
||||
|
||||
@Query("SELECT * FROM Exams WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
|
||||
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Exam>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Exam>
|
||||
}
|
||||
|
@ -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<Grade> {
|
||||
|
||||
@Query("SELECT * FROM Grades WHERE semester_id = :semesterId AND student_id = :studentId")
|
||||
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<Grade>>
|
||||
|
||||
suspend fun loadAll(semesterId: Int, studentId: Int): List<Grade>
|
||||
}
|
||||
|
@ -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<GradePointsStatistics> {
|
||||
|
||||
@Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName")
|
||||
fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): Maybe<List<GradePointsStatistics>>
|
||||
suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String): List<GradePointsStatistics>
|
||||
|
||||
@Query("SELECT * FROM GradesPointsStatistics WHERE student_id = :studentId AND semester_id = :semesterId")
|
||||
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<GradePointsStatistics>>
|
||||
suspend fun loadAll(semesterId: Int, studentId: Int): List<GradePointsStatistics>
|
||||
}
|
||||
|
@ -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<GradeStatistics> {
|
||||
|
||||
@Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND subject = :subjectName AND is_semester = :isSemester")
|
||||
fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): Maybe<List<GradeStatistics>>
|
||||
suspend fun loadSubject(semesterId: Int, studentId: Int, subjectName: String, isSemester: Boolean): List<GradeStatistics>
|
||||
|
||||
@Query("SELECT * FROM GradesStatistics WHERE student_id = :studentId AND semester_id = :semesterId AND is_semester = :isSemester")
|
||||
fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): Maybe<List<GradeStatistics>>
|
||||
suspend fun loadAll(semesterId: Int, studentId: Int, isSemester: Boolean): List<GradeStatistics>
|
||||
}
|
||||
|
@ -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<GradeSummary> {
|
||||
|
||||
@Query("SELECT * FROM GradesSummary WHERE student_id = :studentId AND semester_id = :semesterId")
|
||||
fun loadAll(semesterId: Int, studentId: Int): Maybe<List<GradeSummary>>
|
||||
suspend fun loadAll(semesterId: Int, studentId: Int): List<GradeSummary>
|
||||
}
|
||||
|
@ -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<Homework> {
|
||||
|
||||
@Query("SELECT * FROM Homework WHERE semester_id = :semesterId AND student_id = :studentId AND date >= :from AND date <= :end")
|
||||
fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Homework>>
|
||||
suspend fun loadAll(semesterId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Homework>
|
||||
}
|
||||
|
@ -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<LuckyNumber> {
|
||||
|
||||
@Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date")
|
||||
fun load(studentId: Int, date: LocalDate): Maybe<LuckyNumber>
|
||||
suspend fun load(studentId: Int, date: LocalDate): LuckyNumber
|
||||
}
|
||||
|
@ -9,5 +9,5 @@ import io.github.wulkanowy.data.db.entities.MessageAttachment
|
||||
interface MessageAttachmentDao : BaseDao<MessageAttachment> {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertAttachments(items: List<MessageAttachment>): List<Long>
|
||||
suspend fun insertAttachments(items: List<MessageAttachment>): List<Long>
|
||||
}
|
||||
|
@ -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<Message> {
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND message_id = :messageId")
|
||||
fun loadMessageWithAttachment(studentId: Int, messageId: Int): Single<MessageWithAttachment>
|
||||
suspend fun loadMessageWithAttachment(studentId: Int, messageId: Int): MessageWithAttachment
|
||||
|
||||
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND folder_id = :folder AND removed = 0 ORDER BY date DESC")
|
||||
fun loadAll(studentId: Int, folder: Int): Maybe<List<Message>>
|
||||
suspend fun loadAll(studentId: Int, folder: Int): List<Message>
|
||||
|
||||
@Query("SELECT * FROM Messages WHERE student_id = :studentId AND removed = 1 ORDER BY date DESC")
|
||||
fun loadDeleted(studentId: Int): Maybe<List<Message>>
|
||||
suspend fun loadDeleted(studentId: Int): List<Message>
|
||||
}
|
||||
|
@ -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<MobileDevice> {
|
||||
|
||||
@Query("SELECT * FROM MobileDevices WHERE student_id = :studentId ORDER BY date DESC")
|
||||
fun loadAll(studentId: Int): Maybe<List<MobileDevice>>
|
||||
suspend fun loadAll(studentId: Int): List<MobileDevice>
|
||||
}
|
||||
|
@ -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<Note> {
|
||||
|
||||
@Query("SELECT * FROM Notes WHERE student_id = :studentId")
|
||||
fun loadAll(studentId: Int): Maybe<List<Note>>
|
||||
suspend fun loadAll(studentId: Int): List<Note>
|
||||
}
|
||||
|
@ -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<Recipient> {
|
||||
|
||||
@Query("SELECT * FROM Recipients WHERE student_id = :studentId AND role = :role AND unit_id = :unitId")
|
||||
fun load(studentId: Int, role: Int, unitId: Int): Maybe<List<Recipient>>
|
||||
suspend fun load(studentId: Int, role: Int, unitId: Int): List<Recipient>
|
||||
}
|
||||
|
@ -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<ReportingUnit> {
|
||||
|
||||
@Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId")
|
||||
fun load(studentId: Int): Maybe<List<ReportingUnit>>
|
||||
suspend fun load(studentId: Int): List<ReportingUnit>
|
||||
|
||||
@Query("SELECT * FROM ReportingUnits WHERE student_id = :studentId AND real_id = :unitId")
|
||||
fun loadOne(studentId: Int, unitId: Int): Maybe<ReportingUnit>
|
||||
suspend fun loadOne(studentId: Int, unitId: Int): ReportingUnit?
|
||||
}
|
||||
|
@ -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<School> {
|
||||
|
||||
@Query("SELECT * FROM School WHERE student_id = :studentId AND class_id = :classId")
|
||||
fun load(studentId: Int, classId: Int): Maybe<School>
|
||||
suspend fun load(studentId: Int, classId: Int): School?
|
||||
}
|
||||
|
@ -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<Semester> {
|
||||
|
||||
@Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId")
|
||||
fun loadAll(studentId: Int, classId: Int): Maybe<List<Semester>>
|
||||
suspend fun loadAll(studentId: Int, classId: Int): List<Semester>
|
||||
}
|
||||
|
@ -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<Student>): List<Long>
|
||||
suspend fun insertAll(student: List<Student>): List<Long>
|
||||
|
||||
@Delete
|
||||
fun delete(student: Student)
|
||||
suspend fun delete(student: Student)
|
||||
|
||||
@Query("SELECT * FROM Students WHERE is_current = 1")
|
||||
fun loadCurrent(): Maybe<Student>
|
||||
suspend fun loadCurrent(): Student?
|
||||
|
||||
@Query("SELECT * FROM Students WHERE id = :id")
|
||||
fun loadById(id: Int): Maybe<Student>
|
||||
suspend fun loadById(id: Int): Student?
|
||||
|
||||
@Query("SELECT * FROM Students")
|
||||
fun loadAll(): Maybe<List<Student>>
|
||||
suspend fun loadAll(): List<Student>
|
||||
|
||||
@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()
|
||||
}
|
||||
|
@ -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<Subject> {
|
||||
|
||||
@Query("SELECT * FROM Subjects WHERE diary_id = :diaryId AND student_id = :studentId")
|
||||
fun loadAll(diaryId: Int, studentId: Int): Maybe<List<Subject>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int): List<Subject>
|
||||
}
|
||||
|
@ -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<Teacher> {
|
||||
|
||||
@Query("SELECT * FROM Teachers WHERE student_id = :studentId AND class_id = :classId")
|
||||
fun loadAll(studentId: Int, classId: Int): Maybe<List<Teacher>>
|
||||
suspend fun loadAll(studentId: Int, classId: Int): List<Teacher>
|
||||
}
|
||||
|
@ -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<Timetable> {
|
||||
|
||||
@Query("SELECT * FROM Timetable WHERE diary_id = :diaryId AND student_id = :studentId AND date >= :from AND date <= :end")
|
||||
fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): Maybe<List<Timetable>>
|
||||
suspend fun loadAll(diaryId: Int, studentId: Int, from: LocalDate, end: LocalDate): List<Timetable>
|
||||
}
|
||||
|
@ -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<List<Contributor>> {
|
||||
return Single.fromCallable {
|
||||
class AppCreatorRepository @Inject constructor(
|
||||
private val assets: AssetManager,
|
||||
private val dispatchers: DispatchersProvider
|
||||
) {
|
||||
|
||||
suspend fun getAppCreators(): List<Contributor> {
|
||||
return withContext(dispatchers.backgroundThread) {
|
||||
Gson().fromJson(
|
||||
assets.open("contributors.json").bufferedReader().use { it.readText() },
|
||||
Array<Contributor>::class.java
|
||||
|
@ -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<Attendance>) {
|
||||
suspend fun saveAttendance(attendance: List<Attendance>) {
|
||||
attendanceDb.insertAll(attendance)
|
||||
}
|
||||
|
||||
fun deleteAttendance(attendance: List<Attendance>) {
|
||||
suspend fun deleteAttendance(attendance: List<Attendance>) {
|
||||
attendanceDb.deleteAll(attendance)
|
||||
}
|
||||
|
||||
fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Attendance>> {
|
||||
return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() }
|
||||
suspend fun getAttendance(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Attendance> {
|
||||
return attendanceDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Attendance>> {
|
||||
suspend fun getAttendance(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Attendance> {
|
||||
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<Attendance>, reason: String?): Single<Boolean> {
|
||||
suspend fun excuseAbsence(student: Student, semester: Semester, absenceList: List<Attendance>, reason: String?): Boolean {
|
||||
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear).excuseForAbsence(absenceList.map { attendance ->
|
||||
Absent(
|
||||
date = LocalDateTime.of(attendance.date, LocalTime.of(0, 0)),
|
||||
|
@ -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<List<Attendance>> {
|
||||
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<Attendance> {
|
||||
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<Attendance>, reason: String? = null): Single<Boolean> {
|
||||
suspend fun excuseForAbsence(student: Student, semester: Semester, attendanceList: List<Attendance>, reason: String? = null): Boolean {
|
||||
return remote.excuseAbsence(student, semester, attendanceList, reason)
|
||||
}
|
||||
}
|
||||
|
@ -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<AttendanceSummary>) {
|
||||
suspend fun saveAttendanceSummary(attendance: List<AttendanceSummary>) {
|
||||
attendanceDb.insertAll(attendance)
|
||||
}
|
||||
|
||||
fun deleteAttendanceSummary(attendance: List<AttendanceSummary>) {
|
||||
suspend fun deleteAttendanceSummary(attendance: List<AttendanceSummary>) {
|
||||
attendanceDb.deleteAll(attendance)
|
||||
}
|
||||
|
||||
fun getAttendanceSummary(semester: Semester, subjectId: Int): Maybe<List<AttendanceSummary>> {
|
||||
return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId).filter { it.isNotEmpty() }
|
||||
suspend fun getAttendanceSummary(semester: Semester, subjectId: Int): List<AttendanceSummary> {
|
||||
return attendanceDb.loadAll(semester.diaryId, semester.studentId, subjectId)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<AttendanceSummary>> {
|
||||
suspend fun getAttendanceSummary(student: Student, semester: Semester, subjectId: Int): List<AttendanceSummary> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<AttendanceSummary>> {
|
||||
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<AttendanceSummary> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<CompletedLesson>) {
|
||||
suspend fun saveCompletedLessons(completedLessons: List<CompletedLesson>) {
|
||||
completedLessonsDb.insertAll(completedLessons)
|
||||
}
|
||||
|
||||
fun deleteCompleteLessons(completedLessons: List<CompletedLesson>) {
|
||||
suspend fun deleteCompleteLessons(completedLessons: List<CompletedLesson>) {
|
||||
completedLessonsDb.deleteAll(completedLessons)
|
||||
}
|
||||
|
||||
fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): Maybe<List<CompletedLesson>> {
|
||||
return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end).filter { it.isNotEmpty() }
|
||||
suspend fun getCompletedLessons(semester: Semester, start: LocalDate, end: LocalDate): List<CompletedLesson> {
|
||||
return completedLessonsDb.loadAll(semester.diaryId, semester.studentId, start, end)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<CompletedLesson>> {
|
||||
suspend fun getCompletedLessons(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<CompletedLesson> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<CompletedLesson>> {
|
||||
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<CompletedLesson> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Exam>> {
|
||||
suspend fun getExams(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Exam> {
|
||||
return examDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
|
||||
.filter { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
fun saveExams(exams: List<Exam>) {
|
||||
suspend fun saveExams(exams: List<Exam>) {
|
||||
examDb.insertAll(exams)
|
||||
}
|
||||
|
||||
fun deleteExams(exams: List<Exam>) {
|
||||
suspend fun deleteExams(exams: List<Exam>) {
|
||||
examDb.deleteAll(exams)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Exam>> {
|
||||
suspend fun getExams(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Exam> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Exam>> {
|
||||
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<Exam> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
@ -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<Grade>) {
|
||||
suspend fun saveGrades(grades: List<Grade>) {
|
||||
gradeDb.insertAll(grades)
|
||||
}
|
||||
|
||||
fun deleteGrades(grades: List<Grade>) {
|
||||
suspend fun deleteGrades(grades: List<Grade>) {
|
||||
gradeDb.deleteAll(grades)
|
||||
}
|
||||
|
||||
fun updateGrades(grades: List<Grade>) {
|
||||
suspend fun updateGrades(grades: List<Grade>) {
|
||||
gradeDb.updateAll(grades)
|
||||
}
|
||||
|
||||
fun updateGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
suspend fun updateGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
gradeSummaryDb.updateAll(gradesSummary)
|
||||
}
|
||||
|
||||
fun getGradesDetails(semester: Semester): Maybe<List<Grade>> {
|
||||
return gradeDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getGradesDetails(semester: Semester): List<Grade> {
|
||||
return gradeDb.loadAll(semester.semesterId, semester.studentId)
|
||||
}
|
||||
|
||||
fun saveGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
suspend fun saveGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
gradeSummaryDb.insertAll(gradesSummary)
|
||||
}
|
||||
|
||||
fun deleteGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
suspend fun deleteGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
gradeSummaryDb.deleteAll(gradesSummary)
|
||||
}
|
||||
|
||||
fun getGradesSummary(semester: Semester): Maybe<List<GradeSummary>> {
|
||||
return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getGradesSummary(semester: Semester): List<GradeSummary> {
|
||||
return gradeSummaryDb.loadAll(semester.semesterId, semester.studentId)
|
||||
}
|
||||
}
|
||||
|
@ -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<Pair<List<Grade>, List<GradeSummary>>> {
|
||||
return sdk.init(student).switchDiary(semester.diaryId, semester.schoolYear)
|
||||
suspend fun getGrades(student: Student, semester: Semester): Pair<List<Grade>, List<GradeSummary>> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Pair<List<Grade>, List<GradeSummary>>> {
|
||||
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<Grade>, List<GradeSummary>> {
|
||||
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<List<Grade>> {
|
||||
return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isRead } }.toSingle(emptyList())
|
||||
suspend fun getUnreadGrades(semester: Semester): List<Grade> {
|
||||
return local.getGradesDetails(semester).filter { grade -> !grade.isRead }
|
||||
}
|
||||
|
||||
fun getNotNotifiedGrades(semester: Semester): Single<List<Grade>> {
|
||||
return local.getGradesDetails(semester).map { it.filter { grade -> !grade.isNotified } }.toSingle(emptyList())
|
||||
suspend fun getNotNotifiedGrades(semester: Semester): List<Grade> {
|
||||
return local.getGradesDetails(semester).filter { grade -> !grade.isNotified }
|
||||
}
|
||||
|
||||
fun getNotNotifiedPredictedGrades(semester: Semester): Single<List<GradeSummary>> {
|
||||
return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified } }.toSingle(emptyList())
|
||||
suspend fun getNotNotifiedPredictedGrades(semester: Semester): List<GradeSummary> {
|
||||
return local.getGradesSummary(semester).filter { gradeSummary -> !gradeSummary.isPredictedGradeNotified }
|
||||
}
|
||||
|
||||
fun getNotNotifiedFinalGrades(semester: Semester): Single<List<GradeSummary>> {
|
||||
return local.getGradesSummary(semester).map { it.filter { gradeSummary -> !gradeSummary.isFinalGradeNotified } }.toSingle(emptyList())
|
||||
suspend fun getNotNotifiedFinalGrades(semester: Semester): List<GradeSummary> {
|
||||
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<Grade>): Completable {
|
||||
return Completable.fromCallable { local.updateGrades(grades) }
|
||||
suspend fun updateGrades(grades: List<Grade>) {
|
||||
return local.updateGrades(grades)
|
||||
}
|
||||
|
||||
fun updateGradesSummary(gradesSummary: List<GradeSummary>): Completable {
|
||||
return Completable.fromCallable { local.updateGradesSummary(gradesSummary) }
|
||||
suspend fun updateGradesSummary(gradesSummary: List<GradeSummary>) {
|
||||
return local.updateGradesSummary(gradesSummary)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<GradeStatistics>> {
|
||||
return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester).filter { it.isNotEmpty() }
|
||||
suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean): List<GradeStatistics> {
|
||||
return gradeStatisticsDb.loadAll(semester.semesterId, semester.studentId, isSemester)
|
||||
}
|
||||
|
||||
fun getGradesPointsStatistics(semester: Semester): Maybe<List<GradePointsStatistics>> {
|
||||
return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getGradesPointsStatistics(semester: Semester): List<GradePointsStatistics> {
|
||||
return gradePointsStatisticsDb.loadAll(semester.semesterId, semester.studentId)
|
||||
}
|
||||
|
||||
fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): Maybe<List<GradeStatistics>> {
|
||||
suspend fun getGradesStatistics(semester: Semester, isSemester: Boolean, subjectName: String): List<GradeStatistics> {
|
||||
return when (subjectName) {
|
||||
"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<List<GradePointsStatistics>> {
|
||||
suspend fun getGradesPointsStatistics(semester: Semester, subjectName: String): List<GradePointsStatistics> {
|
||||
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<GradeStatistics>) {
|
||||
suspend fun saveGradesStatistics(gradesStatistics: List<GradeStatistics>) {
|
||||
gradeStatisticsDb.insertAll(gradesStatistics)
|
||||
}
|
||||
|
||||
fun saveGradesPointsStatistics(gradePointsStatistics: List<GradePointsStatistics>) {
|
||||
suspend fun saveGradesPointsStatistics(gradePointsStatistics: List<GradePointsStatistics>) {
|
||||
gradePointsStatisticsDb.insertAll(gradePointsStatistics)
|
||||
}
|
||||
|
||||
fun deleteGradesStatistics(gradesStatistics: List<GradeStatistics>) {
|
||||
suspend fun deleteGradesStatistics(gradesStatistics: List<GradeStatistics>) {
|
||||
gradeStatisticsDb.deleteAll(gradesStatistics)
|
||||
}
|
||||
|
||||
fun deleteGradesPointsStatistics(gradesPointsStatistics: List<GradePointsStatistics>) {
|
||||
suspend fun deleteGradesPointsStatistics(gradesPointsStatistics: List<GradePointsStatistics>) {
|
||||
gradePointsStatisticsDb.deleteAll(gradesPointsStatistics)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<GradeStatistics>> {
|
||||
suspend fun getGradeStatistics(student: Student, semester: Semester, isSemester: Boolean): List<GradeStatistics> {
|
||||
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<List<GradePointsStatistics>> {
|
||||
suspend fun getGradePointsStatistics(student: Student, semester: Semester): List<GradePointsStatistics> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<GradeStatisticsItem>> {
|
||||
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<GradeStatisticsItem> {
|
||||
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<List<GradeStatisticsItem>> {
|
||||
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<GradeStatistics>.mapToStatisticItems(): List<GradeStatisticsItem> {
|
||||
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<GradePointsStatistics>.mapToStatisticsItem(): List<GradeStatisticsItem> {
|
||||
return map {
|
||||
GradeStatisticsItem(
|
||||
type = ViewType.POINTS,
|
||||
partial = emptyList(),
|
||||
points = it
|
||||
)
|
||||
suspend fun getGradesPointsStatistics(student: Student, semester: Semester, subjectName: String, forceRefresh: Boolean): List<GradeStatisticsItem> {
|
||||
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<GradeStatistics>.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<GradePointsStatistics>.mapToStatisticsItem() = map {
|
||||
GradeStatisticsItem(
|
||||
type = ViewType.POINTS,
|
||||
partial = emptyList(),
|
||||
points = it
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -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<Homework>) {
|
||||
suspend fun saveHomework(homework: List<Homework>) {
|
||||
homeworkDb.insertAll(homework)
|
||||
}
|
||||
|
||||
fun deleteHomework(homework: List<Homework>) {
|
||||
suspend fun deleteHomework(homework: List<Homework>) {
|
||||
homeworkDb.deleteAll(homework)
|
||||
}
|
||||
|
||||
fun updateHomework(homework: List<Homework>) {
|
||||
suspend fun updateHomework(homework: List<Homework>) {
|
||||
homeworkDb.updateAll(homework)
|
||||
}
|
||||
|
||||
fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Homework>> {
|
||||
suspend fun getHomework(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Homework> {
|
||||
return homeworkDb.loadAll(semester.semesterId, semester.studentId, startDate, endDate)
|
||||
.filter { it.isNotEmpty() }
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Homework>> {
|
||||
suspend fun getHomework(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Homework> {
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Homework>> {
|
||||
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<Homework> {
|
||||
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
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
@ -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<List<String>> {
|
||||
return getLastModified()
|
||||
.map { it.readText() }
|
||||
.map { it.split("\n") }
|
||||
suspend fun getLastLogLines(): List<String> {
|
||||
return getLastModified().readText().split("\n")
|
||||
}
|
||||
|
||||
fun getLogFiles(): Single<List<File>> {
|
||||
return Single.fromCallable {
|
||||
suspend fun getLogFiles(): List<File> {
|
||||
return withContext(dispatchers.backgroundThread) {
|
||||
File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter {
|
||||
it.name.endsWith(".log")
|
||||
}
|
||||
}!!
|
||||
}
|
||||
}
|
||||
|
||||
private fun getLastModified(): Single<File> {
|
||||
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!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<LuckyNumber> {
|
||||
suspend fun getLuckyNumber(student: Student, date: LocalDate): LuckyNumber? {
|
||||
return luckyNumberDb.load(student.studentId, date)
|
||||
}
|
||||
}
|
||||
|
@ -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<LuckyNumber> {
|
||||
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(),
|
||||
|
@ -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<LuckyNumber> {
|
||||
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<LuckyNumber> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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<Message>) {
|
||||
suspend fun saveMessages(messages: List<Message>) {
|
||||
messagesDb.insertAll(messages)
|
||||
}
|
||||
|
||||
fun updateMessages(messages: List<Message>) {
|
||||
suspend fun updateMessages(messages: List<Message>) {
|
||||
messagesDb.updateAll(messages)
|
||||
}
|
||||
|
||||
fun deleteMessages(messages: List<Message>) {
|
||||
suspend fun deleteMessages(messages: List<Message>) {
|
||||
messagesDb.deleteAll(messages)
|
||||
}
|
||||
|
||||
fun getMessageWithAttachment(student: Student, message: Message): Single<MessageWithAttachment> {
|
||||
suspend fun getMessageWithAttachment(student: Student, message: Message): MessageWithAttachment {
|
||||
return messagesDb.loadMessageWithAttachment(student.id.toInt(), message.messageId)
|
||||
}
|
||||
|
||||
fun saveMessageAttachments(attachments: List<MessageAttachment>) {
|
||||
suspend fun saveMessageAttachments(attachments: List<MessageAttachment>) {
|
||||
messageAttachmentDao.insertAttachments(attachments)
|
||||
}
|
||||
|
||||
fun getMessages(student: Student, folder: MessageFolder): Maybe<List<Message>> {
|
||||
suspend fun getMessages(student: Student, folder: MessageFolder): List<Message> {
|
||||
return when (folder) {
|
||||
TRASHED -> messagesDb.loadDeleted(student.id.toInt())
|
||||
else -> messagesDb.loadAll(student.id.toInt(), folder.id)
|
||||
}.filter { it.isNotEmpty() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Message>> {
|
||||
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<Message> {
|
||||
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<Pair<String, List<MessageAttachment>>> {
|
||||
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<String, List<MessageAttachment>> {
|
||||
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<Recipient>): Single<SentMessage> {
|
||||
suspend fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): 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<Boolean> {
|
||||
suspend fun deleteMessage(student: Student, message: Message): Boolean {
|
||||
return sdk.init(student).deleteMessages(listOf(message.messageId to message.folderId))
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Message>> {
|
||||
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<Message> {
|
||||
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<MessageWithAttachment> {
|
||||
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<List<Message>> {
|
||||
suspend fun getNotNotifiedMessages(student: Student): List<Message> {
|
||||
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<Message>): Completable {
|
||||
return Completable.fromCallable { local.updateMessages(messages) }
|
||||
suspend fun updateMessages(messages: List<Message>) {
|
||||
return local.updateMessages(messages)
|
||||
}
|
||||
|
||||
fun sendMessage(student: Student, subject: String, content: String, recipients: List<Recipient>): Single<SentMessage> {
|
||||
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<Recipient>): SentMessage {
|
||||
return remote.sendMessage(student, subject, content, recipients)
|
||||
}
|
||||
|
||||
fun deleteMessage(student: Student, message: Message): Single<Boolean> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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<MobileDevice>) {
|
||||
suspend fun saveDevices(devices: List<MobileDevice>) {
|
||||
mobileDb.insertAll(devices)
|
||||
}
|
||||
|
||||
fun deleteDevices(devices: List<MobileDevice>) {
|
||||
suspend fun deleteDevices(devices: List<MobileDevice>) {
|
||||
mobileDb.deleteAll(devices)
|
||||
}
|
||||
|
||||
fun getDevices(semester: Semester): Maybe<List<MobileDevice>> {
|
||||
return mobileDb.loadAll(semester.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getDevices(semester: Semester): List<MobileDevice> {
|
||||
return mobileDb.loadAll(semester.studentId)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<MobileDevice>> {
|
||||
suspend fun getDevices(student: Student, semester: Semester): List<MobileDevice> {
|
||||
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<Boolean> {
|
||||
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<MobileDeviceToken> {
|
||||
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,
|
||||
|
@ -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<List<MobileDevice>> {
|
||||
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<MobileDevice> {
|
||||
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<Boolean> {
|
||||
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<MobileDeviceToken> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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<Note>) {
|
||||
suspend fun saveNotes(notes: List<Note>) {
|
||||
noteDb.insertAll(notes)
|
||||
}
|
||||
|
||||
fun updateNotes(notes: List<Note>) {
|
||||
suspend fun updateNotes(notes: List<Note>) {
|
||||
noteDb.updateAll(notes)
|
||||
}
|
||||
|
||||
fun deleteNotes(notes: List<Note>) {
|
||||
suspend fun deleteNotes(notes: List<Note>) {
|
||||
noteDb.deleteAll(notes)
|
||||
}
|
||||
|
||||
fun getNotes(student: Student): Maybe<List<Note>> {
|
||||
return noteDb.loadAll(student.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getNotes(student: Student): List<Note> {
|
||||
return noteDb.loadAll(student.studentId)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Note>> {
|
||||
suspend fun getNotes(student: Student, semester: Semester): List<Note> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Note>> {
|
||||
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<Note> {
|
||||
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<List<Note>> {
|
||||
return local.getNotes(student).map { it.filter { note -> !note.isNotified } }.toSingle(emptyList())
|
||||
suspend fun getNotNotifiedNotes(student: Student): List<Note> {
|
||||
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<Note>): Completable {
|
||||
return Completable.fromCallable { local.updateNotes(notes) }
|
||||
suspend fun updateNotes(notes: List<Note>) {
|
||||
return local.updateNotes(notes)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Recipient>> {
|
||||
return recipientDb.load(student.studentId, role, unit.realId).filter { it.isNotEmpty() }
|
||||
suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List<Recipient> {
|
||||
return recipientDb.load(student.studentId, role, unit.realId)
|
||||
}
|
||||
|
||||
fun saveRecipients(recipients: List<Recipient>): List<Long> {
|
||||
suspend fun saveRecipients(recipients: List<Recipient>): List<Long> {
|
||||
return recipientDb.insertAll(recipients)
|
||||
}
|
||||
|
||||
fun deleteRecipients(recipients: List<Recipient>) {
|
||||
suspend fun deleteRecipients(recipients: List<Recipient>) {
|
||||
recipientDb.deleteAll(recipients)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Recipient>> {
|
||||
suspend fun getRecipients(student: Student, role: Int, unit: ReportingUnit): List<Recipient> {
|
||||
return sdk.init(student).getRecipients(unit.realId, role)
|
||||
.map { recipients ->
|
||||
recipients.map { it.toRecipient() }
|
||||
}
|
||||
.map { it.toRecipient() }
|
||||
}
|
||||
|
||||
fun getMessageRecipients(student: Student, message: Message): Single<List<Recipient>> {
|
||||
suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
|
||||
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId)
|
||||
.map { recipients ->
|
||||
recipients.map { it.toRecipient() }
|
||||
}
|
||||
.map { it.toRecipient() }
|
||||
}
|
||||
|
||||
private fun SdkRecipient.toRecipient(): Recipient {
|
||||
|
@ -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<List<Recipient>> {
|
||||
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<Recipient> {
|
||||
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<List<Recipient>> {
|
||||
return ReactiveNetwork.checkInternetConnectivity(settings)
|
||||
.flatMap {
|
||||
if (it) remote.getMessageRecipients(student, message)
|
||||
else Single.error(UnknownHostException())
|
||||
}
|
||||
suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
|
||||
return remote.getMessageRecipients(student, message)
|
||||
}
|
||||
}
|
||||
|
@ -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<Pair<String, String>> {
|
||||
suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair<String, String> {
|
||||
return sdk.getPasswordResetCaptchaCode(host, symbol)
|
||||
}
|
||||
|
||||
fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single<String> {
|
||||
suspend fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): String {
|
||||
return sdk.sendPasswordResetRequest(url, symbol, email, reCaptchaResponse)
|
||||
}
|
||||
}
|
||||
|
@ -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<Pair<String, String>> {
|
||||
return ReactiveNetwork.checkInternetConnectivity(settings).flatMap {
|
||||
if (it) remote.getReCaptchaSiteKey(host, symbol)
|
||||
else Single.error(UnknownHostException())
|
||||
}
|
||||
suspend fun getReCaptchaSiteKey(host: String, symbol: String): Pair<String, String> {
|
||||
return remote.getReCaptchaSiteKey(host, symbol)
|
||||
}
|
||||
|
||||
fun sendRecoverRequest(url: String, symbol: String, email: String, reCaptchaResponse: String): Single<String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<ReportingUnit>> {
|
||||
return reportingUnitDb.load(student.studentId).filter { it.isNotEmpty() }
|
||||
suspend fun getReportingUnits(student: Student): List<ReportingUnit> {
|
||||
return reportingUnitDb.load(student.studentId)
|
||||
}
|
||||
|
||||
fun getReportingUnit(student: Student, unitId: Int): Maybe<ReportingUnit> {
|
||||
suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit? {
|
||||
return reportingUnitDb.loadOne(student.studentId, unitId)
|
||||
}
|
||||
|
||||
fun saveReportingUnits(reportingUnits: List<ReportingUnit>): List<Long> {
|
||||
suspend fun saveReportingUnits(reportingUnits: List<ReportingUnit>): List<Long> {
|
||||
return reportingUnitDb.insertAll(reportingUnits)
|
||||
}
|
||||
|
||||
fun deleteReportingUnits(reportingUnits: List<ReportingUnit>) {
|
||||
suspend fun deleteReportingUnits(reportingUnits: List<ReportingUnit>) {
|
||||
reportingUnitDb.deleteAll(reportingUnits)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<ReportingUnit>> {
|
||||
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<ReportingUnit> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<ReportingUnit>> {
|
||||
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<ReportingUnit> {
|
||||
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<ReportingUnit> {
|
||||
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)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<School> {
|
||||
suspend fun getSchool(semester: Semester): School? {
|
||||
return schoolDb.load(semester.studentId, semester.classId)
|
||||
}
|
||||
}
|
||||
|
@ -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<School> {
|
||||
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,
|
||||
|
@ -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<School> {
|
||||
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)!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Semester>) {
|
||||
suspend fun saveSemesters(semesters: List<Semester>) {
|
||||
semesterDb.insertAll(semesters)
|
||||
}
|
||||
|
||||
fun deleteSemesters(semesters: List<Semester>) {
|
||||
suspend fun deleteSemesters(semesters: List<Semester>) {
|
||||
semesterDb.deleteAll(semesters)
|
||||
}
|
||||
|
||||
fun getSemesters(student: Student): Maybe<List<Semester>> {
|
||||
return semesterDb.loadAll(student.studentId, student.classId).filter { it.isNotEmpty() }
|
||||
suspend fun getSemesters(student: Student): List<Semester> {
|
||||
return semesterDb.loadAll(student.studentId, student.classId)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Semester>> {
|
||||
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<Semester> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Semester>> {
|
||||
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<Semester> {
|
||||
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<Semester> {
|
||||
return getSemesters(student, forceRefresh).map { it.getCurrentOrLast() }
|
||||
suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false): Semester {
|
||||
return getSemesters(student, forceRefresh).getCurrentOrLast()
|
||||
}
|
||||
}
|
||||
|
@ -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<Student>): Single<List<Long>> {
|
||||
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<Student>): List<Long> {
|
||||
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<List<Student>> {
|
||||
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<Student> {
|
||||
return studentDb.loadById(id).map {
|
||||
it.apply {
|
||||
if (Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getCurrentStudent(decryptPass: Boolean): Maybe<Student> {
|
||||
return studentDb.loadCurrent().map {
|
||||
suspend fun getStudents(decryptPass: Boolean): List<Student> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Student>> {
|
||||
return sdk.getStudentsFromMobileApi(token, pin, symbol, "").map { mapStudents(it, "", "") }
|
||||
suspend fun getStudentsMobileApi(token: String, pin: String, symbol: String): List<Student> {
|
||||
return mapStudents(sdk.getStudentsFromMobileApi(token, pin, symbol, ""), "", "")
|
||||
}
|
||||
|
||||
fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single<List<Student>> {
|
||||
return sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol).map { mapStudents(it, email, password) }
|
||||
suspend fun getStudentsScrapper(email: String, password: String, scrapperBaseUrl: String, symbol: String): List<Student> {
|
||||
return mapStudents(sdk.getStudentsFromScrapper(email, password, scrapperBaseUrl, symbol), email, password)
|
||||
}
|
||||
|
||||
fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): Single<List<Student>> {
|
||||
return sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol).map { mapStudents(it, email, password) }
|
||||
suspend fun getStudentsHybrid(email: String, password: String, scrapperBaseUrl: String, symbol: String): List<Student> {
|
||||
return mapStudents(sdk.getStudentsHybrid(email, password, scrapperBaseUrl, "", symbol), email, password)
|
||||
}
|
||||
}
|
||||
|
@ -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<Boolean> = local.getStudents(false).isEmpty.map { !it }
|
||||
suspend fun isStudentSaved(): Boolean = local.getStudents(false).isNotEmpty()
|
||||
|
||||
fun isCurrentStudentSet(): Single<Boolean> = local.getCurrentStudent(false).isEmpty.map { !it }
|
||||
suspend fun isCurrentStudentSet(): Boolean = local.getCurrentStudent(false)?.isCurrent ?: false
|
||||
|
||||
fun getStudentsApi(pin: String, symbol: String, token: String): Single<List<Student>> {
|
||||
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<Student> {
|
||||
return remote.getStudentsMobileApi(token, pin, symbol)
|
||||
}
|
||||
|
||||
fun getStudentsScrapper(email: String, password: String, endpoint: String, symbol: String): Single<List<Student>> {
|
||||
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<Student> {
|
||||
return remote.getStudentsScrapper(email, password, endpoint, symbol)
|
||||
}
|
||||
|
||||
fun getStudentsHybrid(email: String, password: String, endpoint: String, symbol: String): Single<List<Student>> {
|
||||
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<Student> {
|
||||
return remote.getStudentsHybrid(email, password, endpoint, symbol)
|
||||
}
|
||||
|
||||
fun getSavedStudents(decryptPass: Boolean = true): Single<List<Student>> {
|
||||
return local.getStudents(decryptPass).toSingle(emptyList())
|
||||
suspend fun getSavedStudents(decryptPass: Boolean = true): List<Student> {
|
||||
return local.getStudents(decryptPass)
|
||||
}
|
||||
|
||||
fun getStudentById(id: Int): Single<Student> {
|
||||
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<Student> {
|
||||
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<Student>): Single<List<Long>> {
|
||||
suspend fun saveStudents(students: List<Student>): List<Long> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Subject>> {
|
||||
suspend fun getSubjects(semester: Semester): List<Subject> {
|
||||
return subjectDao.loadAll(semester.diaryId, semester.studentId)
|
||||
.filter { it.isNotEmpty() }
|
||||
}
|
||||
|
||||
fun saveSubjects(subjects: List<Subject>) {
|
||||
suspend fun saveSubjects(subjects: List<Subject>) {
|
||||
subjectDao.insertAll(subjects)
|
||||
}
|
||||
|
||||
fun deleteSubjects(subjects: List<Subject>) {
|
||||
suspend fun deleteSubjects(subjects: List<Subject>) {
|
||||
subjectDao.deleteAll(subjects)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Subject>> {
|
||||
suspend fun getSubjects(student: Student, semester: Semester): List<Subject> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Subject>> {
|
||||
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<Subject> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Teacher>) {
|
||||
suspend fun saveTeachers(teachers: List<Teacher>) {
|
||||
teacherDb.insertAll(teachers)
|
||||
}
|
||||
|
||||
fun deleteTeachers(teachers: List<Teacher>) {
|
||||
suspend fun deleteTeachers(teachers: List<Teacher>) {
|
||||
teacherDb.deleteAll(teachers)
|
||||
}
|
||||
|
||||
fun getTeachers(semester: Semester): Maybe<List<Teacher>> {
|
||||
return teacherDb.loadAll(semester.studentId, semester.classId).filter { it.isNotEmpty() }
|
||||
suspend fun getTeachers(semester: Semester): List<Teacher> {
|
||||
return teacherDb.loadAll(semester.studentId, semester.classId)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Teacher>> {
|
||||
suspend fun getTeachers(student: Student, semester: Semester): List<Teacher> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Teacher>> {
|
||||
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<Teacher> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Timetable>) {
|
||||
suspend fun saveTimetable(timetables: List<Timetable>) {
|
||||
timetableDb.insertAll(timetables)
|
||||
}
|
||||
|
||||
fun deleteTimetable(timetables: List<Timetable>) {
|
||||
suspend fun deleteTimetable(timetables: List<Timetable>) {
|
||||
timetableDb.deleteAll(timetables)
|
||||
}
|
||||
|
||||
fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): Maybe<List<Timetable>> {
|
||||
return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate).filter { it.isNotEmpty() }
|
||||
suspend fun getTimetable(semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Timetable> {
|
||||
return timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate)
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Timetable>> {
|
||||
suspend fun getTimetable(student: Student, semester: Semester, startDate: LocalDate, endDate: LocalDate): List<Timetable> {
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<List<Timetable>> {
|
||||
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<Timetable> {
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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({
|
||||
|
@ -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<Result> {
|
||||
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)
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 }) }
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
@ -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 }) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 }) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 }) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user