Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ interface OfflineExamDao: BaseDao<OfflineExam> {

@Query("UPDATE OfflineExam SET downloadComplete = :downloadComplete WHERE id = :examId")
suspend fun updateDownloadedState(examId: Long, downloadComplete: Boolean)

@Query("SELECT * FROM OfflineExam")
suspend fun getAllOfflineExams() : List<OfflineExam>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package `in`.testpress.database.entities

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

Expand Down Expand Up @@ -64,4 +65,9 @@ data class OfflineExam(
}
return 0
}

fun isEnded(): Boolean {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
return !endDate.isNullOrEmpty() && simpleDateFormat.parse(endDate)?.before(Date()) ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import android.util.Log
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
import java.time.ZoneId
Expand Down Expand Up @@ -212,7 +213,7 @@ class OfflineExamRepository(val context: Context) {
}

fun getAll():LiveData<List<OfflineExam>>{
return offlineExamDao.getAll()
return offlineExamDao.getAll().map { it.filter { offlineExam -> !offlineExam.isEnded() } }
}

suspend fun deleteOfflineExam(examId: Long) {
Expand Down Expand Up @@ -304,7 +305,10 @@ class OfflineExamRepository(val context: Context) {
}

private suspend fun updateCompletedAttempts(completedOfflineAttempts: List<OfflineAttempt>){
if(completedOfflineAttempts.isEmpty()) return
if(completedOfflineAttempts.isEmpty()){
deleteEndedOfflineExam()
return
}
val totalAttempts = completedOfflineAttempts.size
var currentAttemptSize = 0
completedOfflineAttempts.forEach { completedOfflineAttempt ->
Expand Down Expand Up @@ -344,6 +348,7 @@ class OfflineExamRepository(val context: Context) {
currentAttemptSize++
if (totalAttempts == currentAttemptSize){
_offlineAttemptSyncResult.postValue(Resource.success(true))
deleteEndedOfflineExam()
}
}
}
Expand All @@ -359,6 +364,15 @@ class OfflineExamRepository(val context: Context) {
}
}

private fun deleteEndedOfflineExam() {
CoroutineScope(Dispatchers.IO).launch {
val endedExam = offlineExamDao.getAllOfflineExams().filter { it.isEnded() }
endedExam.forEach {
deleteOfflineExam(it.id!!)
}
}
}

private fun deleteSyncedAttempt(attemptId: Long) {
CoroutineScope(Dispatchers.IO).launch {
offlineAttemptDao.deleteByAttemptId(attemptId)
Expand Down