Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize TaskItem
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Jul 2, 2024
1 parent f1596fd commit c79acc5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
16 changes: 6 additions & 10 deletions app/src/main/kotlin/dev/sanmer/mrepo/service/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class DownloadService : LifecycleService() {
.setProgress(100, (progress * 100).toInt(), false)
.build()

notify(item.id, notification)
notify(item.key, notification)
}

private fun onDownloadSucceeded(item: TaskItem) {
Expand All @@ -157,7 +157,7 @@ class DownloadService : LifecycleService() {
.setSilent(true)
.build()

notify(item.id, notification)
notify(item.key, notification)
}

private fun onDownloadFailed(item: TaskItem, message: String?) {
Expand All @@ -167,7 +167,7 @@ class DownloadService : LifecycleService() {
.setContentText(message ?: getString(R.string.unknown_error))
.build()

notify(item.id, notification)
notify(item.key, notification)
}

private fun setForeground() {
Expand Down Expand Up @@ -204,17 +204,15 @@ class DownloadService : LifecycleService() {

@Parcelize
data class TaskItem(
val id: Int = System.currentTimeMillis().toInt(),
val key: String,
val key: Int,
val url: String,
val filename: String,
val title: String?,
val desc: String?,
) : Parcelable {
companion object {
fun empty() = TaskItem(
id = -1,
key = "",
key = -1,
url = "",
filename = "",
title = null,
Expand All @@ -237,7 +235,7 @@ class DownloadService : LifecycleService() {
private val listeners = hashMapOf<TaskItem, IDownloadListener>()
private val progressFlow = MutableStateFlow(TaskItem.empty() to 0f)

fun getProgressByKey(key: String): Flow<Float> {
fun getProgressByKey(key: Int): Flow<Float> {
return progressFlow.filter { (item, _) ->
item.key == key
}.map { (_, progress) ->
Expand Down Expand Up @@ -265,8 +263,6 @@ class DownloadService : LifecycleService() {

listeners[task] = listener
context.startService(intent)
} else {
Timber.w("notGranted: $state")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ModuleViewModel @Inject constructor(
)

val task = DownloadService.TaskItem(
key = item.toString(),
key = item.hashCode(),
url = item.zipUrl,
filename = filename,
title = online.name,
Expand All @@ -142,7 +142,7 @@ class ModuleViewModel @Inject constructor(
}

fun getProgress(item: VersionItem) =
DownloadService.getProgressByKey(item.toString())
DownloadService.getProgressByKey(item.hashCode())

companion object {
fun putModuleId(module: OnlineModule) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class ModulesViewModel @Inject constructor(
)

val task = DownloadService.TaskItem(
key = item.toString(),
key = item.hashCode(),
url = item.zipUrl,
filename = filename,
title = module.name,
Expand Down Expand Up @@ -292,6 +292,6 @@ class ModulesViewModel @Inject constructor(
val change: () -> Unit = {},
val version: VersionItem? = null,
val updatable: Boolean = version?.let { it.versionCode > original.versionCode } == true,
val progress: Flow<Float> = DownloadService.getProgressByKey(version.toString()),
val progress: Flow<Float> = DownloadService.getProgressByKey(version.hashCode()),
)
}

0 comments on commit c79acc5

Please sign in to comment.