Michal Klimczak
10/17/2024, 10:55 AMfun initializeKoin(forcedLogoutHandler: ForcedLogooutHandler, ...)
But I will have a few of those and would like to leverage koin's modules and declare them in swift code. Think of sth like
kotlin
fun <T : Any> Module.registerSingleton(clazz: KClass<T>, definition: () -> T) {
single(kClass = clazz, qualifier = null, createdAtStart = false, definition = { definition() })
}
swift
let iosModule = DiHelper().createKoinModule { module in
module.registerSingleton(clazz: ForcedLogoutNavigator.self) { ForcedLogoutNavigator() as ForcedLogoutHandler }
}
Problem is, I don't think swift types are meant to be translatable to KClass in any way. At least I haven't found a way to do that. Reified type doesn't work either.
How do you handle that kind of architecture pickle?Michal Klimczak
10/17/2024, 12:11 PM@Suppress("UNCHECKED_CAST")
@OptIn(BetaInteropApi::class)
fun <T : Any> Module.single(clazz: ObjCProtocol, definition: () -> T) {
val kClazz = getOriginalKotlinClass(clazz) as KClass<T>
single(kClass = kClazz, qualifier = null, createdAtStart = false, definition = { definition() })
}
Not that ObjCProtocol and ObjCClass here are not interchangeable and using one where another should be used will result in a very nasty EXC_BAD_ACC crashAlexandru Caraus
10/17/2024, 1:35 PMAlexandru Caraus
10/17/2024, 1:39 PMMichal Klimczak
10/17/2024, 1:41 PMAlexandru Caraus
10/17/2024, 1:43 PMAlexandru Caraus
10/17/2024, 1:44 PMMichal Klimczak
10/17/2024, 2:08 PMAlexandru Caraus
10/17/2024, 2:18 PMimport platform.UIKit.UIViewController
actual class NavigationController(private val rootViewController: UIViewController) {
actual fun navigateTo(destination: String) {
// Use UIKit's UINavigationController or UIViewController to handle navigation
// Example:
val viewController = getViewControllerForDestination(destination)
rootViewController.navigationController?.pushViewController(viewController, animated = true)
}
private fun getViewControllerForDestination(destination: String): UIViewController {
// Map destination string to corresponding UIViewController
// Return the corresponding view controller instance
}
}
Alexandru Caraus
10/17/2024, 2:19 PMAlexandru Caraus
10/17/2024, 2:25 PMMichal Klimczak
10/17/2024, 2:27 PMAlexandru Caraus
10/17/2024, 2:37 PM