diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt index e3fdf01b..57f3005a 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/LuckyNumberDao.kt @@ -12,5 +12,5 @@ import javax.inject.Singleton interface LuckyNumberDao : BaseDao { @Query("SELECT * FROM LuckyNumbers WHERE student_id = :studentId AND date = :date") - fun load(studentId: Int, date: LocalDate): Flow + fun load(studentId: Int, date: LocalDate): Flow } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt index ef0ced3a..173ce7e4 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/luckynumber/LuckyNumberRepository.kt @@ -3,7 +3,8 @@ package io.github.wulkanowy.data.repositories.luckynumber import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.utils.networkBoundResource -import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.map import java.time.LocalDate.now import javax.inject.Inject import javax.inject.Singleton @@ -28,11 +29,8 @@ class LuckyNumberRepository @Inject constructor( } ) - fun getNotNotifiedLuckyNumber(student: Student): Flow { - return local.getLuckyNumber(student, now()) - } + suspend fun getNotNotifiedLuckyNumber(student: Student) = + local.getLuckyNumber(student, now()).map { if (it?.isNotified == false) it else null }.first() - suspend fun updateLuckyNumber(luckyNumber: LuckyNumber?) { - local.updateLuckyNumber(luckyNumber) - } + suspend fun updateLuckyNumber(luckyNumber: LuckyNumber?) = local.updateLuckyNumber(luckyNumber) } diff --git a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt index 9cb765ec..7b9f5ab3 100644 --- a/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt +++ b/app/src/main/java/io/github/wulkanowy/services/sync/works/LuckyNumberWork.kt @@ -19,7 +19,6 @@ import io.github.wulkanowy.ui.modules.main.MainActivity import io.github.wulkanowy.ui.modules.main.MainView import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.waitForResult -import kotlinx.coroutines.flow.first import javax.inject.Inject import kotlin.random.Random @@ -33,7 +32,7 @@ class LuckyNumberWork @Inject constructor( override suspend fun doWork(student: Student, semester: Semester) { luckyNumberRepository.getLuckyNumber(student, true, preferencesRepository.isNotificationsEnable).waitForResult() - luckyNumberRepository.getNotNotifiedLuckyNumber(student).first()?.let { + luckyNumberRepository.getNotNotifiedLuckyNumber(student)?.let { notify(it) luckyNumberRepository.updateLuckyNumber(it.apply { isNotified = true }) }