@@ -4,10 +4,10 @@ import androidx.annotation.StringRes
4
4
import androidx.compose.runtime.*
5
5
import com.aliucord.manager.installer.steps.StepGroup
6
6
import com.aliucord.manager.installer.steps.StepRunner
7
- import kotlinx.coroutines.Dispatchers
8
- import kotlinx.coroutines.withContext
7
+ import com.aliucord.manager.util.toPrecision
8
+ import kotlinx.coroutines.*
9
9
import org.koin.core.time.measureTimedValue
10
- import kotlin.math.roundToInt
10
+ import kotlin.math.roundToLong
11
11
12
12
/* *
13
13
* A base install process step. Steps are single-use
@@ -44,12 +44,44 @@ abstract class Step {
44
44
var progress by mutableFloatStateOf(- 1f )
45
45
protected set
46
46
47
+ private val durationSecs = mutableFloatStateOf(0f )
48
+ private var startTime: Long? = null
49
+ private var totalTimeMs: Long? = null
50
+
51
+ /* *
52
+ * The total amount of time this step has/was executed for in milliseconds.
53
+ * If this step has not started executing then it will return `0`.
54
+ */
55
+ fun getDuration (): Long {
56
+ // Step hasn't started executing
57
+ val startTime = startTime ? : return 0
58
+
59
+ // Step already finished executing
60
+ totalTimeMs?.let { return it }
61
+
62
+ return System .currentTimeMillis() - startTime
63
+ }
64
+
47
65
/* *
48
- * The total execution time once this step has finished execution.
66
+ * The live execution time of this step in seconds.
67
+ * The value is clamped to a resolution of 10ms updated every 50ms.
49
68
*/
50
- // TODO: make this a live value
51
- var durationMs by mutableIntStateOf(0 )
52
- private set
69
+ @Composable
70
+ fun collectDurationAsState (): State <Float > {
71
+ if (state.isFinished)
72
+ return durationSecs
73
+
74
+ LaunchedEffect (state) {
75
+ while (true ) {
76
+ durationSecs.floatValue = (getDuration() / 1000.0 )
77
+ .toPrecision(2 ).toFloat()
78
+
79
+ delay(50 )
80
+ }
81
+ }
82
+
83
+ return durationSecs
84
+ }
53
85
54
86
/* *
55
87
* Thin wrapper over [execute] but handling errors.
@@ -60,6 +92,7 @@ abstract class Step {
60
92
throw IllegalStateException (" Cannot execute a step that has already started" )
61
93
62
94
state = StepState .Running
95
+ startTime = System .currentTimeMillis()
63
96
64
97
// Execute this steps logic while timing it
65
98
val (error, executionTimeMs) = measureTimedValue {
@@ -78,7 +111,9 @@ abstract class Step {
78
111
}
79
112
}
80
113
81
- durationMs = executionTimeMs.roundToInt()
114
+ totalTimeMs = executionTimeMs.roundToLong()
115
+ durationSecs.floatValue = executionTimeMs.toFloat() / 1000f
116
+
82
117
return error
83
118
}
84
119
}
0 commit comments