1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-18 21:36:49 -06:00

Fix last sync date to save only successful sync (#1595)

This commit is contained in:
Rafał Borcz 2021-10-21 10:47:37 +02:00 committed by GitHub
parent 58ea2c530e
commit 09a134d442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 37 additions and 38 deletions

View File

@ -20,7 +20,7 @@ class AppCreatorRepository @Inject constructor(
@OptIn(ExperimentalSerializationApi::class)
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun getAppCreators() = withContext(dispatchers.backgroundThread) {
suspend fun getAppCreators() = withContext(dispatchers.io) {
val inputStream = context.assets.open("contributors.json").buffered()
json.decodeFromStream<List<Contributor>>(inputStream)
}

View File

@ -15,24 +15,23 @@ class LoggerRepository @Inject constructor(
suspend fun getLastLogLines() = getLastModified().readText().split("\n")
suspend fun getLogFiles() = withContext(dispatchers.backgroundThread) {
File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter {
it.name.endsWith(".log")
}!!
suspend fun getLogFiles() = withContext(dispatchers.io) {
File(context.filesDir.absolutePath).listFiles(File::isFile)
?.filter { it.name.endsWith(".log") }!!
}
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 ->
private suspend fun getLastModified() = withContext(dispatchers.io) {
var lastModifiedTime = Long.MIN_VALUE
var chosenFile: File? = null
File(context.filesDir.absolutePath).listFiles(File::isFile)
?.forEach { file ->
if (file.lastModified() > lastModifiedTime) {
lastModifiedTime = file.lastModified()
chosenFile = file
}
}
if (chosenFile == null) throw FileNotFoundException("Log file not found")
chosenFile!!
}
chosenFile ?: throw FileNotFoundException("Log file not found")
}
}

View File

@ -26,7 +26,7 @@ class SemesterRepository @Inject constructor(
student: Student,
forceRefresh: Boolean = false,
refreshOnNoCurrent: Boolean = false
) = withContext(dispatchers.backgroundThread) {
) = withContext(dispatchers.io) {
val semesters = semesterDb.loadAll(student.studentId, student.classId)
if (isShouldFetch(student, semesters, forceRefresh, refreshOnNoCurrent)) {
@ -64,7 +64,7 @@ class SemesterRepository @Inject constructor(
}
suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false) =
withContext(dispatchers.backgroundThread) {
withContext(dispatchers.io) {
getSemesters(student, forceRefresh).getCurrentOrLast()
}
}

View File

@ -66,7 +66,7 @@ class StudentRepository @Inject constructor(
.map {
it.apply {
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
student.password = withContext(dispatchers.backgroundThread) {
student.password = withContext(dispatchers.io) {
decrypt(student.password)
}
}
@ -77,7 +77,7 @@ class StudentRepository @Inject constructor(
val student = studentDb.loadById(id) ?: throw NoCurrentStudentException()
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
student.password = withContext(dispatchers.backgroundThread) {
student.password = withContext(dispatchers.io) {
decrypt(student.password)
}
}
@ -88,7 +88,7 @@ class StudentRepository @Inject constructor(
val student = studentDb.loadCurrent() ?: throw NoCurrentStudentException()
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
student.password = withContext(dispatchers.backgroundThread) {
student.password = withContext(dispatchers.io) {
decrypt(student.password)
}
}
@ -101,7 +101,7 @@ class StudentRepository @Inject constructor(
.map {
it.apply {
if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) {
password = withContext(dispatchers.backgroundThread) {
password = withContext(dispatchers.io) {
encrypt(password, context)
}
}

View File

@ -54,7 +54,7 @@ class TimetableNotificationSchedulerHelper @Inject constructor(
suspend fun cancelScheduled(lessons: List<Timetable>, student: Student) {
val studentId = student.studentId
withContext(dispatchersProvider.backgroundThread) {
withContext(dispatchersProvider.io) {
lessons.sortedBy { it.start }.forEachIndexed { index, lesson ->
val upcomingTime = getUpcomingLessonTime(index, lessons, lesson)
cancelScheduledTo(
@ -91,7 +91,7 @@ class TimetableNotificationSchedulerHelper @Inject constructor(
return
}
withContext(dispatchersProvider.backgroundThread) {
withContext(dispatchersProvider.io) {
lessons.groupBy { it.date }
.map { it.value.sortedBy { lesson -> lesson.start } }
.map { it.filter { lesson -> lesson.isStudentPlan } }

View File

@ -19,11 +19,11 @@ 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.DispatchersProvider
import io.github.wulkanowy.utils.getCompatColor
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.time.LocalDateTime
import java.time.ZoneId
import kotlin.random.Random
@HiltWorker
@ -34,13 +34,14 @@ class SyncWorker @AssistedInject constructor(
private val semesterRepository: SemesterRepository,
private val works: Set<@JvmSuppressWildcards Work>,
private val preferencesRepository: PreferencesRepository,
private val notificationManager: NotificationManagerCompat
private val notificationManager: NotificationManagerCompat,
private val dispatchersProvider: DispatchersProvider
) : CoroutineWorker(appContext, workerParameters) {
override suspend fun doWork() = coroutineScope {
override suspend fun doWork() = withContext(dispatchersProvider.io) {
Timber.i("SyncWorker is starting")
if (!studentRepository.isCurrentStudentSet()) return@coroutineScope Result.failure()
if (!studentRepository.isCurrentStudentSet()) return@withContext Result.failure()
val student = studentRepository.getCurrentStudent()
val semester = semesterRepository.getCurrentSemester(student, true)
@ -50,12 +51,12 @@ class SyncWorker @AssistedInject constructor(
Timber.i("${work::class.java.simpleName} is starting")
work.doWork(student, semester)
Timber.i("${work::class.java.simpleName} result: Success")
preferencesRepository.lasSyncDate = LocalDateTime.now(ZoneId.systemDefault())
null
} catch (e: Throwable) {
Timber.w("${work::class.java.simpleName} result: An exception ${e.message} occurred")
if (e is FeatureDisabledException || e is FeatureNotAvailableException) null
else {
if (e is FeatureDisabledException || e is FeatureNotAvailableException) {
null
} else {
Timber.e(e)
e
}
@ -70,13 +71,16 @@ class SyncWorker @AssistedInject constructor(
)
}
exceptions.isNotEmpty() -> Result.retry()
else -> Result.success()
else -> {
preferencesRepository.lasSyncDate = LocalDateTime.now()
Result.success()
}
}
if (preferencesRepository.isDebugNotificationEnable) notify(result)
Timber.i("SyncWorker result: $result")
result
return@withContext result
}
private fun notify(result: Result) {

View File

@ -31,7 +31,7 @@ class LicensePresenter @Inject constructor(
private fun loadData() {
flowWithResource {
withContext(dispatchers.backgroundThread) {
withContext(dispatchers.io) {
view?.appLibraries.orEmpty()
}
}.onEach {

View File

@ -1,10 +1,8 @@
package io.github.wulkanowy.utils
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
open class DispatchersProvider {
open val backgroundThread: CoroutineDispatcher
get() = Dispatchers.IO
open val io get() = Dispatchers.IO
}

View File

@ -1,11 +1,9 @@
package io.github.wulkanowy
import io.github.wulkanowy.utils.DispatchersProvider
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
class TestDispatchersProvider : DispatchersProvider() {
override val backgroundThread: CoroutineDispatcher
get() = Dispatchers.Unconfined
override val io get() = Dispatchers.Unconfined
}