Skip to content

Commit 421ca3d

Browse files
Prepare exercise#4
1 parent 8eb10b3 commit 421ca3d

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase12/CalculationInSeveralCoroutinesViewModel.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
33
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
44
import kotlinx.coroutines.CoroutineDispatcher
55
import kotlinx.coroutines.Dispatchers
6+
import java.math.BigInteger
7+
import kotlin.system.measureTimeMillis
68

79
class CalculationInSeveralCoroutinesViewModel(
810
private val factorialCalculator: FactorialCalculator = FactorialCalculator(),
@@ -13,6 +15,26 @@ class CalculationInSeveralCoroutinesViewModel(
1315
factorialOf: Int,
1416
numberOfCoroutines: Int
1517
) {
18+
uiState.value = UiState.Loading
1619

20+
var factorialResult = BigInteger.ZERO
21+
val computationDuration = measureTimeMillis {
22+
factorialResult =
23+
factorialCalculator.calculateFactorial(
24+
factorialOf,
25+
numberOfCoroutines
26+
)
27+
}
28+
29+
var resultString = ""
30+
val stringConversionDuration = measureTimeMillis {
31+
resultString = convertToString(factorialResult)
32+
}
33+
34+
uiState.value =
35+
UiState.Success(resultString, computationDuration, stringConversionDuration)
1736
}
37+
38+
// TODO: execute on background thread
39+
private fun convertToString(number: BigInteger): String = number.toString()
1840
}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase12
22

3-
import com.lukaslechner.coroutineusecasesonandroid.utils.addCoroutineDebugInfo
4-
import kotlinx.coroutines.*
5-
import timber.log.Timber
3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.Dispatchers
65
import java.math.BigInteger
76

87
class FactorialCalculator(
98
private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
109
) {
1110

12-
suspend fun calculateFactorial(
11+
fun calculateFactorial(
1312
factorialOf: Int,
1413
numberOfCoroutines: Int
1514
): BigInteger {
16-
return withContext(defaultDispatcher) {
17-
val subRanges = createSubRangeList(factorialOf, numberOfCoroutines)
18-
subRanges.map { subRange ->
19-
async {
20-
calculateFactorialOfSubRange(subRange)
21-
}
22-
}.awaitAll()
23-
.fold(BigInteger.ONE, { acc, element ->
24-
ensureActive()
25-
acc.multiply(element)
26-
})
27-
}
15+
16+
// TODO: create sub range list *on background thread*
17+
val subRanges = createSubRangeList(factorialOf, numberOfCoroutines)
18+
19+
20+
// TODO: calculate factorial of each subrange in separate coroutine
21+
// use calculateFactorialOfSubRange(subRange) therefore
22+
23+
24+
// TODO: create factorial result by multiplying all sub-results and return this
25+
// result
26+
27+
return BigInteger.ZERO
2828
}
2929

30-
suspend fun calculateFactorialOfSubRange(
30+
// TODO: execute on background thread
31+
fun calculateFactorialOfSubRange(
3132
subRange: SubRange
3233
): BigInteger {
33-
return withContext(defaultDispatcher) {
34-
Timber.d(addCoroutineDebugInfo("Calculate factorial of $subRange"))
35-
var factorial = BigInteger.ONE
36-
for (i in subRange.start..subRange.end) {
37-
ensureActive()
38-
factorial = factorial.multiply(BigInteger.valueOf(i.toLong()))
39-
}
40-
factorial
34+
var factorial = BigInteger.ONE
35+
for (i in subRange.start..subRange.end) {
36+
factorial = factorial.multiply(BigInteger.valueOf(i.toLong()))
4137
}
38+
return factorial
4239
}
4340

4441
fun createSubRangeList(
@@ -63,4 +60,5 @@ class FactorialCalculator(
6360
}
6461
}
6562

63+
6664
data class SubRange(val start: Int, val end: Int)

0 commit comments

Comments
 (0)