-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b47e2a
commit bd66358
Showing
4 changed files
with
57 additions
and
45 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ogress/src/commonMain/kotlin/com/flipperdevices/core/progress/DetailedProgressListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
50 changes: 50 additions & 0 deletions
50
.../src/commonMain/kotlin/com/flipperdevices/core/progress/DetailedProgressWrapperTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
8 changes: 0 additions & 8 deletions
8
.../core/progress/src/commonMain/kotlin/com/flipperdevices/core/progress/ProgressListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters