Is anyone using compose resources in SwiftUI? Appa...
# multiplatform
m
Is anyone using compose resources in SwiftUI? Apparently you need to call a suspend fun to resolve them outside of Compose code. We want to be able to reuse our ViewModels in both CMP and Swiftui (when we don't want to use CMP on iOS), and right now we expose a lot of resources through the ViewState and resolve them in the View. Due to the suspend fun, I wonder how to do that on SwiftUI in a clean way. Perhaps it would be best to ditch Compose resources and use Moko resources instead? 🤔
👀 1
m
Came up with something like this for iOS last week
Copy code
@OptIn(ExperimentalResourceApi::class)
object SharedStrings {

    private val stringResources: Map<String, StringResource> = Res.allStringResources
    private val scope = CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>)
    private val loadedStrings = mutableMapOf<String, String>()

    fun get(key: String): String {
        return loadedStrings[key] ?: scope.async {
            val resource = stringResources[key]
            if (resource != null) {
                val localizedString = getString(resource)
                loadedStrings[key] = localizedString
                localizedString
            } else {
                key
            }
        }.let { runBlocking { it.await() } }
    }
}
but I ended up not using it 😄
❤️ 1
👀 2
1
🙌 1
m
Interesting approach, thanks for sharing! I ended up implementing Moko resources and I'm very happy with the result, it even fixed our issue of not being able to bundle the resources in the XCFramework package.
m
Moko resources has 144 open issues on github which made my manager anxious about using it
1
m
Fair 😅 I tested it and everything went smoothly