Skip to content

Commit

Permalink
move out progress listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
makeevrserg committed Sep 24, 2024
1 parent 3b47e2a commit bd66358
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.flipperdevices.core.progress

fun interface DetailedProgressListener {
interface Detail

suspend fun onProgress(current: Float, detail: Detail)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.flipperdevices.core.progress

import com.flipperdevices.core.buildkonfig.BuildKonfig
import kotlin.math.min

private const val MAX_PERCENT = 1.0f
private const val MIN_PERCENT = 0f

class DetailedProgressWrapperTracker(
private val progressListener: DetailedProgressListener,
private val min: Float = MIN_PERCENT,
private val max: Float = MAX_PERCENT
) : DetailedProgressListener {
override suspend fun onProgress(current: Float, detail: DetailedProgressListener.Detail) {
val diff = max - min
if (diff <= 0) { // This means that our min and max are originally incorrect
return
}

val currentPercent = min + current * diff

progressListener.onProgress(
min(
min(currentPercent, max),
MAX_PERCENT
),
detail
)
}

suspend fun report(current: Long, max: Long, detail: DetailedProgressListener.Detail) {
if (current > max) {
onProgress(MAX_PERCENT, detail)
if (BuildKonfig.IS_LOG_ENABLED) {
error("Current larger then max (current: $current, max: $max)")
}
return
}
if (max == 0L) {
onProgress(MAX_PERCENT, detail)
if (BuildKonfig.IS_LOG_ENABLED) {
error("Max is zero")
}
return
}

val percent = current.toDouble() / max
onProgress(percent.toFloat(), detail)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.flipperdevices.core.progress

fun interface ProgressListener {
interface Detail

suspend fun onProgress(current: Float)
}

fun interface DetailedProgressListener {
interface Detail

suspend fun onProgress(current: Float, detail: Detail)
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,3 @@ class ProgressWrapperTracker(
onProgress(percent.toFloat())
}
}

class DetailedProgressWrapperTracker(
private val progressListener: DetailedProgressListener,
private val min: Float = MIN_PERCENT,
private val max: Float = MAX_PERCENT
) : DetailedProgressListener {
override suspend fun onProgress(current: Float, detail: DetailedProgressListener.Detail) {
val diff = max - min
if (diff <= 0) { // This means that our min and max are originally incorrect
return
}

val currentPercent = min + current * diff

progressListener.onProgress(min(min(currentPercent, max), MAX_PERCENT), detail)
}

suspend fun report(current: Long, max: Long, detail: DetailedProgressListener.Detail) {
if (current > max) {
onProgress(MAX_PERCENT, detail)
if (BuildKonfig.IS_LOG_ENABLED) {
error("Current larger then max (current: $current, max: $max)")
}
return
}
if (max == 0L) {
onProgress(MAX_PERCENT, detail)
if (BuildKonfig.IS_LOG_ENABLED) {
error("Max is zero")
}
return
}

val percent = current.toDouble() / max
onProgress(percent.toFloat(), detail)
}
}

0 comments on commit bd66358

Please sign in to comment.