mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-01-19 00:16:48 -06:00
Fix last sync date to save only successful sync (#1595)
This commit is contained in:
parent
58ea2c530e
commit
09a134d442
@ -20,7 +20,7 @@ class AppCreatorRepository @Inject constructor(
|
|||||||
|
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
suspend fun getAppCreators() = withContext(dispatchers.backgroundThread) {
|
suspend fun getAppCreators() = withContext(dispatchers.io) {
|
||||||
val inputStream = context.assets.open("contributors.json").buffered()
|
val inputStream = context.assets.open("contributors.json").buffered()
|
||||||
json.decodeFromStream<List<Contributor>>(inputStream)
|
json.decodeFromStream<List<Contributor>>(inputStream)
|
||||||
}
|
}
|
||||||
|
@ -15,24 +15,23 @@ class LoggerRepository @Inject constructor(
|
|||||||
|
|
||||||
suspend fun getLastLogLines() = getLastModified().readText().split("\n")
|
suspend fun getLastLogLines() = getLastModified().readText().split("\n")
|
||||||
|
|
||||||
suspend fun getLogFiles() = withContext(dispatchers.backgroundThread) {
|
suspend fun getLogFiles() = withContext(dispatchers.io) {
|
||||||
File(context.filesDir.absolutePath).listFiles(File::isFile)?.filter {
|
File(context.filesDir.absolutePath).listFiles(File::isFile)
|
||||||
it.name.endsWith(".log")
|
?.filter { it.name.endsWith(".log") }!!
|
||||||
}!!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getLastModified(): File {
|
private suspend fun getLastModified() = withContext(dispatchers.io) {
|
||||||
return withContext(dispatchers.backgroundThread) {
|
var lastModifiedTime = Long.MIN_VALUE
|
||||||
var lastModifiedTime = Long.MIN_VALUE
|
var chosenFile: File? = null
|
||||||
var chosenFile: File? = null
|
|
||||||
File(context.filesDir.absolutePath).listFiles(File::isFile)?.forEach { file ->
|
File(context.filesDir.absolutePath).listFiles(File::isFile)
|
||||||
|
?.forEach { file ->
|
||||||
if (file.lastModified() > lastModifiedTime) {
|
if (file.lastModified() > lastModifiedTime) {
|
||||||
lastModifiedTime = file.lastModified()
|
lastModifiedTime = file.lastModified()
|
||||||
chosenFile = file
|
chosenFile = file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (chosenFile == null) throw FileNotFoundException("Log file not found")
|
|
||||||
chosenFile!!
|
chosenFile ?: throw FileNotFoundException("Log file not found")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ class SemesterRepository @Inject constructor(
|
|||||||
student: Student,
|
student: Student,
|
||||||
forceRefresh: Boolean = false,
|
forceRefresh: Boolean = false,
|
||||||
refreshOnNoCurrent: Boolean = false
|
refreshOnNoCurrent: Boolean = false
|
||||||
) = withContext(dispatchers.backgroundThread) {
|
) = withContext(dispatchers.io) {
|
||||||
val semesters = semesterDb.loadAll(student.studentId, student.classId)
|
val semesters = semesterDb.loadAll(student.studentId, student.classId)
|
||||||
|
|
||||||
if (isShouldFetch(student, semesters, forceRefresh, refreshOnNoCurrent)) {
|
if (isShouldFetch(student, semesters, forceRefresh, refreshOnNoCurrent)) {
|
||||||
@ -64,7 +64,7 @@ class SemesterRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false) =
|
suspend fun getCurrentSemester(student: Student, forceRefresh: Boolean = false) =
|
||||||
withContext(dispatchers.backgroundThread) {
|
withContext(dispatchers.io) {
|
||||||
getSemesters(student, forceRefresh).getCurrentOrLast()
|
getSemesters(student, forceRefresh).getCurrentOrLast()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ class StudentRepository @Inject constructor(
|
|||||||
.map {
|
.map {
|
||||||
it.apply {
|
it.apply {
|
||||||
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
||||||
student.password = withContext(dispatchers.backgroundThread) {
|
student.password = withContext(dispatchers.io) {
|
||||||
decrypt(student.password)
|
decrypt(student.password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ class StudentRepository @Inject constructor(
|
|||||||
val student = studentDb.loadById(id) ?: throw NoCurrentStudentException()
|
val student = studentDb.loadById(id) ?: throw NoCurrentStudentException()
|
||||||
|
|
||||||
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
||||||
student.password = withContext(dispatchers.backgroundThread) {
|
student.password = withContext(dispatchers.io) {
|
||||||
decrypt(student.password)
|
decrypt(student.password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ class StudentRepository @Inject constructor(
|
|||||||
val student = studentDb.loadCurrent() ?: throw NoCurrentStudentException()
|
val student = studentDb.loadCurrent() ?: throw NoCurrentStudentException()
|
||||||
|
|
||||||
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
if (decryptPass && Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API) {
|
||||||
student.password = withContext(dispatchers.backgroundThread) {
|
student.password = withContext(dispatchers.io) {
|
||||||
decrypt(student.password)
|
decrypt(student.password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +101,7 @@ class StudentRepository @Inject constructor(
|
|||||||
.map {
|
.map {
|
||||||
it.apply {
|
it.apply {
|
||||||
if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) {
|
if (Sdk.Mode.valueOf(it.loginMode) != Sdk.Mode.API) {
|
||||||
password = withContext(dispatchers.backgroundThread) {
|
password = withContext(dispatchers.io) {
|
||||||
encrypt(password, context)
|
encrypt(password, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ class TimetableNotificationSchedulerHelper @Inject constructor(
|
|||||||
|
|
||||||
suspend fun cancelScheduled(lessons: List<Timetable>, student: Student) {
|
suspend fun cancelScheduled(lessons: List<Timetable>, student: Student) {
|
||||||
val studentId = student.studentId
|
val studentId = student.studentId
|
||||||
withContext(dispatchersProvider.backgroundThread) {
|
withContext(dispatchersProvider.io) {
|
||||||
lessons.sortedBy { it.start }.forEachIndexed { index, lesson ->
|
lessons.sortedBy { it.start }.forEachIndexed { index, lesson ->
|
||||||
val upcomingTime = getUpcomingLessonTime(index, lessons, lesson)
|
val upcomingTime = getUpcomingLessonTime(index, lessons, lesson)
|
||||||
cancelScheduledTo(
|
cancelScheduledTo(
|
||||||
@ -91,7 +91,7 @@ class TimetableNotificationSchedulerHelper @Inject constructor(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
withContext(dispatchersProvider.backgroundThread) {
|
withContext(dispatchersProvider.io) {
|
||||||
lessons.groupBy { it.date }
|
lessons.groupBy { it.date }
|
||||||
.map { it.value.sortedBy { lesson -> lesson.start } }
|
.map { it.value.sortedBy { lesson -> lesson.start } }
|
||||||
.map { it.filter { lesson -> lesson.isStudentPlan } }
|
.map { it.filter { lesson -> lesson.isStudentPlan } }
|
||||||
|
@ -19,11 +19,11 @@ import io.github.wulkanowy.sdk.exception.FeatureNotAvailableException
|
|||||||
import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException
|
import io.github.wulkanowy.sdk.scrapper.exception.FeatureDisabledException
|
||||||
import io.github.wulkanowy.services.sync.channels.DebugChannel
|
import io.github.wulkanowy.services.sync.channels.DebugChannel
|
||||||
import io.github.wulkanowy.services.sync.works.Work
|
import io.github.wulkanowy.services.sync.works.Work
|
||||||
|
import io.github.wulkanowy.utils.DispatchersProvider
|
||||||
import io.github.wulkanowy.utils.getCompatColor
|
import io.github.wulkanowy.utils.getCompatColor
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.withContext
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.ZoneId
|
|
||||||
import kotlin.random.Random
|
import kotlin.random.Random
|
||||||
|
|
||||||
@HiltWorker
|
@HiltWorker
|
||||||
@ -34,13 +34,14 @@ class SyncWorker @AssistedInject constructor(
|
|||||||
private val semesterRepository: SemesterRepository,
|
private val semesterRepository: SemesterRepository,
|
||||||
private val works: Set<@JvmSuppressWildcards Work>,
|
private val works: Set<@JvmSuppressWildcards Work>,
|
||||||
private val preferencesRepository: PreferencesRepository,
|
private val preferencesRepository: PreferencesRepository,
|
||||||
private val notificationManager: NotificationManagerCompat
|
private val notificationManager: NotificationManagerCompat,
|
||||||
|
private val dispatchersProvider: DispatchersProvider
|
||||||
) : CoroutineWorker(appContext, workerParameters) {
|
) : CoroutineWorker(appContext, workerParameters) {
|
||||||
|
|
||||||
override suspend fun doWork() = coroutineScope {
|
override suspend fun doWork() = withContext(dispatchersProvider.io) {
|
||||||
Timber.i("SyncWorker is starting")
|
Timber.i("SyncWorker is starting")
|
||||||
|
|
||||||
if (!studentRepository.isCurrentStudentSet()) return@coroutineScope Result.failure()
|
if (!studentRepository.isCurrentStudentSet()) return@withContext Result.failure()
|
||||||
|
|
||||||
val student = studentRepository.getCurrentStudent()
|
val student = studentRepository.getCurrentStudent()
|
||||||
val semester = semesterRepository.getCurrentSemester(student, true)
|
val semester = semesterRepository.getCurrentSemester(student, true)
|
||||||
@ -50,12 +51,12 @@ class SyncWorker @AssistedInject constructor(
|
|||||||
Timber.i("${work::class.java.simpleName} is starting")
|
Timber.i("${work::class.java.simpleName} is starting")
|
||||||
work.doWork(student, semester)
|
work.doWork(student, semester)
|
||||||
Timber.i("${work::class.java.simpleName} result: Success")
|
Timber.i("${work::class.java.simpleName} result: Success")
|
||||||
preferencesRepository.lasSyncDate = LocalDateTime.now(ZoneId.systemDefault())
|
|
||||||
null
|
null
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
Timber.w("${work::class.java.simpleName} result: An exception ${e.message} occurred")
|
Timber.w("${work::class.java.simpleName} result: An exception ${e.message} occurred")
|
||||||
if (e is FeatureDisabledException || e is FeatureNotAvailableException) null
|
if (e is FeatureDisabledException || e is FeatureNotAvailableException) {
|
||||||
else {
|
null
|
||||||
|
} else {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
e
|
e
|
||||||
}
|
}
|
||||||
@ -70,13 +71,16 @@ class SyncWorker @AssistedInject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
exceptions.isNotEmpty() -> Result.retry()
|
exceptions.isNotEmpty() -> Result.retry()
|
||||||
else -> Result.success()
|
else -> {
|
||||||
|
preferencesRepository.lasSyncDate = LocalDateTime.now()
|
||||||
|
Result.success()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (preferencesRepository.isDebugNotificationEnable) notify(result)
|
if (preferencesRepository.isDebugNotificationEnable) notify(result)
|
||||||
Timber.i("SyncWorker result: $result")
|
Timber.i("SyncWorker result: $result")
|
||||||
|
|
||||||
result
|
return@withContext result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun notify(result: Result) {
|
private fun notify(result: Result) {
|
||||||
|
@ -31,7 +31,7 @@ class LicensePresenter @Inject constructor(
|
|||||||
|
|
||||||
private fun loadData() {
|
private fun loadData() {
|
||||||
flowWithResource {
|
flowWithResource {
|
||||||
withContext(dispatchers.backgroundThread) {
|
withContext(dispatchers.io) {
|
||||||
view?.appLibraries.orEmpty()
|
view?.appLibraries.orEmpty()
|
||||||
}
|
}
|
||||||
}.onEach {
|
}.onEach {
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
package io.github.wulkanowy.utils
|
package io.github.wulkanowy.utils
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
open class DispatchersProvider {
|
open class DispatchersProvider {
|
||||||
|
|
||||||
open val backgroundThread: CoroutineDispatcher
|
open val io get() = Dispatchers.IO
|
||||||
get() = Dispatchers.IO
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package io.github.wulkanowy
|
package io.github.wulkanowy
|
||||||
|
|
||||||
import io.github.wulkanowy.utils.DispatchersProvider
|
import io.github.wulkanowy.utils.DispatchersProvider
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
class TestDispatchersProvider : DispatchersProvider() {
|
class TestDispatchersProvider : DispatchersProvider() {
|
||||||
|
|
||||||
override val backgroundThread: CoroutineDispatcher
|
override val io get() = Dispatchers.Unconfined
|
||||||
get() = Dispatchers.Unconfined
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user