Mike Welsh
09/14/2023, 3:21 PMTadeas Kriz
09/14/2023, 7:49 PMTadeas Kriz
09/14/2023, 7:50 PMMike Welsh
09/14/2023, 8:02 PMTadeas Kriz
09/14/2023, 8:06 PMMike Welsh
09/14/2023, 8:10 PMTadeas Kriz
09/14/2023, 8:11 PMMike Welsh
09/14/2023, 8:29 PMTadeas Kriz
09/14/2023, 8:30 PMTadeas Kriz
09/14/2023, 8:30 PMTadeas Kriz
09/14/2023, 8:31 PMMike Welsh
09/14/2023, 8:32 PMTadeas Kriz
09/14/2023, 8:32 PMMike Welsh
09/14/2023, 8:41 PM/// Convenience object for accessing common coroutine scopes from iOS.
class CoroutineScopes {
companion object {
val MainScope = kotlinx.coroutines.MainScope()
val DefaultScope = kotlinx.coroutines.CoroutineScope(kotlinx.coroutines.Dispatchers.Default)
}
}
Then we created a test class, specifically because on the Android side we wanted to expose coroutine scopes:
/**
* This class exists for the purpose of testing functionality from KMP in the target platforms
* (such as iOS and Android).
*
* This is intended to be public and published with each release of the library to allow downstream
* clients to test out new functionality and behaviours.
*
* If this gets sufficiently large, move it to it's own module so that it may include content from
* any module.
*/
class TestClass {
companion object {
/**
* Static getter to test static functionality.
*/
fun getInstance(): TestClass {
return TestClass()
}
private val staticMutatingString = "Something to mutate"
private val staticString = "Static String"
}
// Some code removed for readability
/// Function which allows defining the scope to launch the work in.
fun doWorkWithScope(scope: CoroutineScope) {
scope.launch {
printTest("Background!")
}
}
suspend fun printTest(input: String): String {
/// Use a Coroutine to make the input capitalized on a non-main thread.
return withContext(Dispatchers.Default) {
println(input)
input.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
}
}
}
Which we then tested on the iOS side:
func test_doWorkWithScope() {
sut = TestClass.Companion.getInstance(TestClass.Companion.shared)()
sut.doWorkWithScope(scope: CoroutineScopeKt.MainScope())
}
Mike Welsh
09/14/2023, 8:41 PMTadeas Kriz
09/14/2023, 8:44 PMMike Welsh
09/14/2023, 8:57 PM