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 @@ -5,6 +5,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.ketch.Status
import kotlinx.coroutines.flow.Flow

@Dao
Expand Down Expand Up @@ -43,10 +44,10 @@ internal interface DownloadDao {
fun getAllEntityByTagsFlow(tags: List<String>): Flow<List<DownloadEntity>>

@Query("SELECT * FROM downloads WHERE status = :status ORDER BY timeQueued ASC")
fun getAllEntityByStatusFlow(status: String): Flow<List<DownloadEntity>>
fun getAllEntityByStatusFlow(status: Status): Flow<List<DownloadEntity>>

@Query("SELECT * FROM downloads WHERE status IN (:statuses) ORDER BY timeQueued ASC")
fun getAllEntityByStatusesFlow(statuses: List<String>): Flow<List<DownloadEntity>>
fun getAllEntityByStatusesFlow(statuses: List<Status>): Flow<List<DownloadEntity>>

@Query("SELECT * FROM downloads ORDER BY timeQueued ASC")
suspend fun getAllEntity(): List<DownloadEntity>
Expand All @@ -64,8 +65,8 @@ internal interface DownloadDao {
suspend fun getAllEntityByIds(ids: List<Int>): List<DownloadEntity?>

@Query("SELECT * FROM downloads WHERE status = :status ORDER BY timeQueued ASC")
suspend fun getAllEntityByStatus(status: String): List<DownloadEntity>
suspend fun getAllEntityByStatus(status: Status): List<DownloadEntity>

@Query("SELECT * FROM downloads WHERE status IN (:statuses) ORDER BY timeQueued ASC")
suspend fun getAllEntityByStatuses(statuses: List<String>): List<DownloadEntity>
suspend fun getAllEntityByStatuses(statuses: List<Status>): List<DownloadEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ internal data class DownloadEntity(
var id: Int = 0,
var headersJson: String = "",
var timeQueued: Long = 0,
var status: String = Status.DEFAULT.toString(),
var status: Status = Status.DEFAULT,
var totalBytes: Long = 0,
var downloadedBytes: Long = 0,
var speedInBytePerMs: Float = 0f,
var uuid: String = "",
var lastModified: Long = 0,
var eTag: String = "",
var userAction: String = UserAction.DEFAULT.toString(),
var userAction: UserAction = UserAction.DEFAULT,
var metaData: String = "",
var failureReason: String = ""
)
49 changes: 22 additions & 27 deletions ketch/src/main/java/com/ketch/internal/download/DownloadManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ internal class DownloadManager(

WorkInfo.State.CANCELLED -> {
val downloadEntity = findDownloadEntityFromUUID(workInfo.id)
if (downloadEntity?.userAction == UserAction.PAUSE.toString()) {
if (downloadEntity?.userAction == UserAction.PAUSE) {
logger.log(
msg = "Download Paused. FileName: ${downloadEntity.fileName}, " +
"ID: ${downloadEntity.id}"
)
} else if (downloadEntity?.userAction == UserAction.CANCEL.toString()) {
} else if (downloadEntity?.userAction == UserAction.CANCEL) {
logger.log(
msg = "Download Cancelled. FileName: ${downloadEntity.fileName}, " +
"ID: ${downloadEntity.id}"
Expand Down Expand Up @@ -155,7 +155,7 @@ internal class DownloadManager(
if (downloadDao.find(downloadRequest.id) != null) {

downloadDao.find(downloadRequest.id)?.copy(
userAction = UserAction.START.toString(),
userAction = UserAction.START,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }

Expand All @@ -166,13 +166,13 @@ internal class DownloadManager(

if (downloadEntity != null &&
downloadEntity.uuid != downloadWorkRequest.id.toString() &&
downloadEntity.status != Status.QUEUED.toString() &&
downloadEntity.status != Status.PROGRESS.toString() &&
downloadEntity.status != Status.STARTED.toString()
downloadEntity.status != Status.QUEUED &&
downloadEntity.status != Status.PROGRESS &&
downloadEntity.status != Status.STARTED
) {
downloadDao.find(downloadRequest.id)?.copy(
uuid = downloadWorkRequest.id.toString(),
status = Status.QUEUED.toString(),
status = Status.QUEUED,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }
}
Expand All @@ -186,10 +186,10 @@ internal class DownloadManager(
id = downloadRequest.id,
headersJson = WorkUtil.hashMapToJson(downloadRequest.headers),
timeQueued = System.currentTimeMillis(),
status = Status.QUEUED.toString(),
status = Status.QUEUED,
uuid = downloadWorkRequest.id.toString(),
lastModified = System.currentTimeMillis(),
userAction = UserAction.START.toString(),
userAction = UserAction.START,
metaData = downloadRequest.metaData
)
)
Expand All @@ -204,10 +204,11 @@ internal class DownloadManager(

private suspend fun resume(id: Int) {
val downloadEntity = downloadDao.find(id)
if (downloadEntity != null) {
if (downloadEntity != null && (downloadEntity.status!= Status.PROGRESS &&
downloadEntity.status!= Status.SUCCESS)) {
downloadDao.update(
downloadEntity.copy(
userAction = UserAction.RESUME.toString(),
userAction = UserAction.RESUME,
lastModified = System.currentTimeMillis()
)
)
Expand All @@ -230,16 +231,16 @@ internal class DownloadManager(
if (downloadEntity != null) {
downloadDao.update(
downloadEntity.copy(
userAction = UserAction.CANCEL.toString(),
userAction = UserAction.CANCEL,
lastModified = System.currentTimeMillis()
)
)
if (downloadEntity.status == Status.PAUSED.toString() ||
downloadEntity.status == Status.FAILED.toString()
if (downloadEntity.status == Status.PAUSED ||
downloadEntity.status == Status.FAILED
) { // Edge Case: When user cancel the download in pause or fail (terminating) state as work is already cancelled.

downloadDao.find(downloadEntity.id)?.copy(
status = Status.CANCELLED.toString(),
status = Status.CANCELLED,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }
deleteFileIfExists(downloadEntity.path, downloadEntity.fileName)
Expand All @@ -259,7 +260,7 @@ internal class DownloadManager(
if (downloadEntity != null) {
downloadDao.update(
downloadEntity.copy(
userAction = UserAction.PAUSE.toString(),
userAction = UserAction.PAUSE,
lastModified = System.currentTimeMillis()
)
)
Expand All @@ -272,7 +273,7 @@ internal class DownloadManager(
if (downloadEntity != null) {
downloadDao.update(
downloadEntity.copy(
userAction = UserAction.RETRY.toString(),
userAction = UserAction.RETRY,
lastModified = System.currentTimeMillis()
)
)
Expand Down Expand Up @@ -485,7 +486,7 @@ internal class DownloadManager(
}

fun observeDownloadsByStatus(status: Status): Flow<List<DownloadModel>> {
return downloadDao.getAllEntityByStatusFlow(status.name).distinctUntilChanged().map { entityList ->
return downloadDao.getAllEntityByStatusFlow(status).distinctUntilChanged().map { entityList ->
entityList.map { entity ->
entity.toDownloadModel()
}
Expand All @@ -509,11 +510,7 @@ internal class DownloadManager(
}

fun observeDownloadsByStatuses(statuses: List<Status>): Flow<List<DownloadModel>> {
return downloadDao.getAllEntityByStatusesFlow(
statuses.map {
it.name
}
).distinctUntilChanged().map { entityList ->
return downloadDao.getAllEntityByStatusesFlow(statuses).distinctUntilChanged().map { entityList ->
entityList.map { entity ->
entity.toDownloadModel()
}
Expand All @@ -537,7 +534,7 @@ internal class DownloadManager(
}

suspend fun getDownloadModelByStatus(status: Status): List<DownloadModel> {
return downloadDao.getAllEntityByStatus(status.name).map { entity ->
return downloadDao.getAllEntityByStatus(status).map { entity ->
entity.toDownloadModel()
}
}
Expand All @@ -557,9 +554,7 @@ internal class DownloadManager(

suspend fun getDownloadModelByStatuses(statuses: List<Status>): List<DownloadModel> {
return downloadDao.getAllEntityByStatuses(
statuses.map {
it.name
}
statuses
).map { entity ->
entity.toDownloadModel()
}
Expand Down
3 changes: 1 addition & 2 deletions ketch/src/main/java/com/ketch/internal/utils/MapperUtil.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ketch.internal.utils

import com.ketch.DownloadModel
import com.ketch.Status
import com.ketch.internal.database.DownloadEntity

// Mapper function to convert DownloadEntity to DownloadModel
Expand All @@ -14,7 +13,7 @@ internal fun DownloadEntity.toDownloadModel() =
id = id,
headers = WorkUtil.jsonToHashMap(headersJson),
timeQueued = timeQueued,
status = Status.entries.find { it.name == status } ?: Status.DEFAULT,
status = status,
total = totalBytes,
progress = if (totalBytes.toInt() != 0) ((downloadedBytes * 100) / totalBytes).toInt() else 0,
speedInBytePerMs = speedInBytePerMs,
Expand Down
16 changes: 8 additions & 8 deletions ketch/src/main/java/com/ketch/internal/worker/DownloadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.ketch.Status
import com.ketch.internal.database.DatabaseInstance
import com.ketch.internal.download.DownloadTask
import com.ketch.internal.download.ApiResponseHeaderChecker
import com.ketch.internal.download.DownloadTask
import com.ketch.internal.network.RetrofitInstance
import com.ketch.internal.notification.DownloadNotificationManager
import com.ketch.internal.utils.DownloadConst
Expand Down Expand Up @@ -102,7 +102,7 @@ internal class DownloadWorker(

downloadDao.find(id)?.copy(
totalBytes = length,
status = Status.STARTED.toString(),
status = Status.STARTED,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }

Expand All @@ -127,7 +127,7 @@ internal class DownloadWorker(
downloadDao.find(id)?.copy(
downloadedBytes = downloadedBytes,
speedInBytePerMs = speed,
status = Status.PROGRESS.toString(),
status = Status.PROGRESS,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }

Expand All @@ -154,7 +154,7 @@ internal class DownloadWorker(

downloadDao.find(id)?.copy(
totalBytes = totalLength,
status = Status.SUCCESS.toString(),
status = Status.SUCCESS,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }

Expand All @@ -165,10 +165,10 @@ internal class DownloadWorker(
} catch (e: Exception) {
GlobalScope.launch {
if (e is CancellationException) {
if (downloadDao.find(id)?.userAction == UserAction.PAUSE.toString()) {
if (downloadDao.find(id)?.userAction == UserAction.PAUSE) {

downloadDao.find(id)?.copy(
status = Status.PAUSED.toString(),
status = Status.PAUSED,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }
val downloadEntity = downloadDao.find(id)
Expand All @@ -186,7 +186,7 @@ internal class DownloadWorker(
} else {

downloadDao.find(id)?.copy(
status = Status.CANCELLED.toString(),
status = Status.CANCELLED,
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }
FileUtil.deleteFileIfExists(dirPath, fileName)
Expand All @@ -196,7 +196,7 @@ internal class DownloadWorker(
} else {

downloadDao.find(id)?.copy(
status = Status.FAILED.toString(),
status = Status.FAILED,
failureReason = e.message ?: "",
lastModified = System.currentTimeMillis()
)?.let { downloadDao.update(it) }
Expand Down