Stately has a fairly involved config. iOS and Macos share all of the same code.
To structure the project, there's commonMain
, nativeCommonMain
depends on that, and actually appleMain
which depends on nativeCommonMain
.
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
jvmMain {
dependsOn commonMain
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
nativeCommonMain {
dependsOn commonMain
}
appleMain {
dependsOn nativeCommonMain
}
configure([iosX64Main, iosArm64Main, macosMain, iosArm32Main]) {
dependsOn appleMain
}
That structure is probably deeper than you need, but we needed something for linux and windows that was different. Egor's answer above is easier to follow I think.
We actually define multiplatform atomics in Stately, so you can use them as inspiration or actually just use the library itself.
https://github.com/touchlab/Stately
Common
expect class AtomicInt(initialValue: Int) {
fun get(): Int
fun set(newValue: Int)
fun incrementAndGet(): Int
fun decrementAndGet(): Int
fun addAndGet(delta: Int): Int
fun compareAndSet(expected: Int, new: Int): Boolean
}
JVM
actual typealias AtomicInt = AtomicInteger
Native
actual class AtomicInt actual constructor(initialValue:Int){
private val atom = AtomicInt(initialValue)
actual fun get(): Int = atom.value
actual fun set(newValue: Int) {
atom.value = newValue
}
actual fun incrementAndGet(): Int = atom.addAndGet(1)
actual fun decrementAndGet(): Int = atom.addAndGet(-1)
actual fun addAndGet(delta: Int): Int = atom.addAndGet(delta)
actual fun compareAndSet(expected: Int, new: Int): Boolean = atom.compareAndSet(expected, new)
}