Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add downloaded bytes info & get/clear by status #33 #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions ketch/src/main/java/com/ketch/DownloadModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.ketch
* @property headers Optional headers sent when making api call for file download
* @property timeQueued First time in millisecond when download was queued into the database
* @property status Current [Status] of the download
* @property downloaded Downloaded size of file in bytes
* @property total Total size of file in bytes
* @property progress Current download progress in Int between 0 and 100
* @property speedInBytePerMs Current speed of download in bytes per second
Expand All @@ -29,6 +30,7 @@ data class DownloadModel(
val headers: HashMap<String, String>,
val timeQueued: Long,
val status: Status,
val downloaded: Long,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required, you can get the downloaded size by file.size()

val total: Long,
val progress: Int,
val speedInBytePerMs: Float,
Expand Down
18 changes: 18 additions & 0 deletions ketch/src/main/java/com/ketch/Ketch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ class Ketch private constructor(
downloadManager.clearDbAsync(id, deleteFile)
}

/**
* Clear entries from database and delete files with given [status]
*
* @param status Status associated with the download
* @param deleteFile delete the actual file from the system
*/
fun clearDb(status: Status, deleteFile: Boolean = true) {
downloadManager.clearDbAsync(status, deleteFile)
}

/**
* Clear entries from database and delete files with given [tag]
*
Expand Down Expand Up @@ -418,4 +428,12 @@ class Ketch private constructor(
*/
suspend fun getAllDownloads() = downloadManager.getAllDownloads()

/**
* Suspend function to get list of all Downloads with given [status]
*
* @param status Status associated with the download
* @return List of [DownloadModel]
*/
suspend fun getAllDownloads(status: Status) = downloadManager.getAllDownloads(status)

}
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 All @@ -30,6 +31,9 @@ internal interface DownloadDao {
@Query("SELECT * FROM downloads WHERE lastModified <= :timeMillis ORDER BY timeQueued ASC")
fun getEntityTillTimeFlow(timeMillis: Long): Flow<List<DownloadEntity>>

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

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

Expand All @@ -42,6 +46,9 @@ internal interface DownloadDao {
@Query("SELECT * FROM downloads WHERE lastModified <= :timeMillis ORDER BY timeQueued ASC")
suspend fun getEntityTillTime(timeMillis: Long): List<DownloadEntity>

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

@Query("SELECT * FROM downloads WHERE tag = :tag ORDER BY timeQueued ASC")
suspend fun getAllEntityByTag(tag: String): List<DownloadEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,23 @@ internal class DownloadManager(
}
}

fun clearDbAsync(status: Status, deleteFile: Boolean) {
scope.launch {
downloadDao.getAllEntityByStatus(status).forEach {
workManager.cancelUniqueWork(it.id.toString())
val downloadEntity = downloadDao.find(it.id)
val path = downloadEntity?.path
val fileName = downloadEntity?.fileName
if (path != null && fileName != null && deleteFile) {
deleteFileIfExists(path, fileName)
}
downloadDao.remove(it.id)
removeNotification(context, it.id) // In progress notification
removeNotification(context, it.id + 1) // Cancelled, Paused, Failed, Success notification
}
}
}

fun clearAllDbAsync(deleteFile: Boolean) {
scope.launch {
downloadDao.getAllEntity().forEach {
Expand Down Expand Up @@ -478,6 +495,14 @@ internal class DownloadManager(
}
}

fun observeDownloadsByStatus(status: Status): Flow<List<DownloadModel>> {
return downloadDao.getAllEntityByStatusFlow(status).map { entityList ->
entityList.map { entity ->
entity.toDownloadModel()
}
}
}

fun observeAllDownloads(): Flow<List<DownloadModel>> {
return downloadDao.getAllEntityFlow().map { entityList ->
entityList.map { entity ->
Expand All @@ -486,10 +511,15 @@ internal class DownloadManager(
}
}

suspend fun getAllDownloads(status: Status): List<DownloadModel> {
return downloadDao.getAllEntityByStatus(status).map { entity ->
entity.toDownloadModel()
}
}

suspend fun getAllDownloads(): List<DownloadModel> {
return downloadDao.getAllEntity().map { entity ->
entity.toDownloadModel()
}
}

}
1 change: 1 addition & 0 deletions ketch/src/main/java/com/ketch/internal/utils/MapperUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal fun DownloadEntity.toDownloadModel() =
headers = WorkUtil.jsonToHashMap(headersJson),
timeQueued = timeQueued,
status = Status.entries.find { it.name == status } ?: Status.DEFAULT,
downloaded = downloadedBytes,
total = totalBytes,
progress = if (totalBytes.toInt() != 0) ((downloadedBytes * 100) / totalBytes).toInt() else 0,
speedInBytePerMs = speedInBytePerMs,
Expand Down