anyone using any kotlin integer type to control Sw...
# ios
l
anyone using any kotlin integer type to control SwiftUI ?
Copy code
.animation(_:, value: <a kotlin integer>)
The animation does not get executed, and I have to wrap it around a Swift Int, like so:
Copy code
.animation(_:, value: Int(<a kotlin integer>))
a
In SwiftUI that would be an Int32 value but the animation function is looking for a property wrapper such as a state value.
l
I think I can parse your message, but I don’t understand yet why. What does swift int construtor do here that another int type can’t satisfy?
a
Perhaps this would help more...are you using a view model library for multiplatform such as KMMViewModel?
l
No, I am not
a
Ok, in iOS that animation function is going to want a @State property or @StateObject / @ObservedObject to detect the change so it knows when to re-render. Without using a framework, you will have to have some means of making that connection. We currently use KMMViewModel / KMPNativeCoroutines to generate wrappers for our viewmodel properties such that they are usable in Swift and Android for state management. For your code, you might try switching the property to a normal
@State var myInt: Int = 0
in Swift and adding some simple logic to change it when you click a button to see how the animation works in Native Swift. Then you will need to figure out how you want to make that property powered by your Kotlin code. There are several ways to do it, but the common pattern is to convert a Kotlin MutableStateFlow<Int> and wrap that with an iOS native ObservedObject or Publisher to update your property from Kotlin. Awesome libraries/frameworks like Decompose and KMMViewModel ultimately just level this up to make it more natural while Kotlin Multiplatform matures.
l
Ah that makes sense. For future reference, the integer I was trying to bind to is a property of a class that is already annotated with
@StateObject
. I didnt know that I couldn't refer to a property of a
@StateObject
directly.
a
To be honest, I'm not 100% certain what SwiftUI is supposed to do versus what it does. Experience so far tells me it is much less mature than apple would have us believe. I just learned yesterday that on tvos, .delay(duration:) on animations didn't work at all in the simulator on an x86 mac, but did work on the actual device. Animations were sluggish and then I was able to add the magical .drawingGroup() modifier to render via the GPU and it worked on both. I've been finding a lot of little quirks like that and I wish I felt 100% confident explaining their behavior. In addition, using timing with animation is complicated because there is no consistent time in which it can be guaranteed swift realized it needed to redraw after a property changes, so I'm having to be very careful where I put certain logic on the flow of an animation (fade out, load something, fade in for example).
111 Views