diff --git a/core/src/main/java/in/testpress/database/dao/OfflineExamDao.kt b/core/src/main/java/in/testpress/database/dao/OfflineExamDao.kt index ca88861c2..e86e9a66d 100644 --- a/core/src/main/java/in/testpress/database/dao/OfflineExamDao.kt +++ b/core/src/main/java/in/testpress/database/dao/OfflineExamDao.kt @@ -41,4 +41,7 @@ interface OfflineExamDao: BaseDao { @Query("UPDATE OfflineExam SET downloadComplete = :downloadComplete WHERE id = :examId") suspend fun updateDownloadedState(examId: Long, downloadComplete: Boolean) + + @Query("SELECT * FROM OfflineExam") + suspend fun getAllOfflineExams() : List } \ No newline at end of file diff --git a/core/src/main/java/in/testpress/database/entities/OfflineExam.kt b/core/src/main/java/in/testpress/database/entities/OfflineExam.kt index 989d4e011..e206e8196 100644 --- a/core/src/main/java/in/testpress/database/entities/OfflineExam.kt +++ b/core/src/main/java/in/testpress/database/entities/OfflineExam.kt @@ -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.* @@ -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 + } } \ No newline at end of file diff --git a/course/src/main/java/in/testpress/course/repository/OfflineExamRepository.kt b/course/src/main/java/in/testpress/course/repository/OfflineExamRepository.kt index f3edf6e02..50390a924 100644 --- a/course/src/main/java/in/testpress/course/repository/OfflineExamRepository.kt +++ b/course/src/main/java/in/testpress/course/repository/OfflineExamRepository.kt @@ -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 @@ -212,7 +213,7 @@ class OfflineExamRepository(val context: Context) { } fun getAll():LiveData>{ - return offlineExamDao.getAll() + return offlineExamDao.getAll().map { it.filter { offlineExam -> !offlineExam.isEnded() } } } suspend fun deleteOfflineExam(examId: Long) { @@ -304,7 +305,10 @@ class OfflineExamRepository(val context: Context) { } private suspend fun updateCompletedAttempts(completedOfflineAttempts: List){ - if(completedOfflineAttempts.isEmpty()) return + if(completedOfflineAttempts.isEmpty()){ + deleteEndedOfflineExam() + return + } val totalAttempts = completedOfflineAttempts.size var currentAttemptSize = 0 completedOfflineAttempts.forEach { completedOfflineAttempt -> @@ -344,6 +348,7 @@ class OfflineExamRepository(val context: Context) { currentAttemptSize++ if (totalAttempts == currentAttemptSize){ _offlineAttemptSyncResult.postValue(Resource.success(true)) + deleteEndedOfflineExam() } } } @@ -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)