Muaz KADAN
09/11/2024, 7:56 PM@Composable
@Preview
fun App() {
KoinApplication(application = {
koinApplication {
modules(databaseModule, viewModelModule)
}
}) {
val dataStore = koinInject<DataStore<Preferences>>()
val isDarkTheme by dataStore.data.map { it[booleanPreferencesKey(Consts.IS_DARK_THEME)] }
.collectAsStateWithLifecycle(initialValue = isSystemInDarkTheme())
TestAppTheme(darkTheme = isDarkTheme == true) {
val navController = rememberNavController()
Scaffold {
NavHost(
navController = navController,
startDestination = Screen.Home
) {
composable<Screen.Home> {
val viewModel: TestViewModel = koinViewModel()
val isDarkThemeHome by viewModel.isDarkTheme.collectAsStateWithLifecycle()
Text(text = "$isDarkThemeHome")
}
}
}
}
}
}
class TestViewModel(
private val dataStore: DataStore<Preferences>
) : ViewModel() {
val isDarkTheme = dataStore.data.map { it[booleanPreferencesKey(Consts.IS_DARK_THEME)] }
.stateIn(viewModelScope, started = SharingStarted.WhileSubscribed(), initialValue = false)
}
I have this small sample of the problem that I'm having
The code above works fine with Compose 1.7.0-beta01 but when I update to 1.7.0-beta02 the iOS app crashes saying
kotlin.IllegalStateException: There are multiple DataStores active for the same file: etc. etc. while the Android app runs without any crashes or problems
I tried to trace the cause of the problem and it turns out that removing the Scaffold from the compose hierarchy interestingly solves the crash on iOS
I'm using Compose 1.7.0-beta02 and Koin-bom 4.0.0-RC2Muaz KADAN
09/11/2024, 8:17 PMclass Person(val name: String)
@Composable
@Preview
fun App() {
KoinApplication(application = {
koinApplication {
modules(
module {
single<Person> { Person(name = "Muaz") }
}
)
}
}) {
val person = koinInject<Person>()
println("Person: ${person.hashCode()}")
TestAppTheme {
Scaffold {
val person2 = koinInject<Person>()
val person3 = koinInject<Person>()
println("Person2: ${person2.hashCode()}")
println("Person3: ${person3.hashCode()}")
}
}
}
}
on Android side the app prints:
Person: 55430932
Person2: 55430932
Person3: 55430932
While on iOS side the app prints:
Person: 107172400
Person: 107186776
Person2: 107186776
Person3: 107186776
Person2: 107186776
Person3: 107186776
iOS app without Scaffold
Person: 85676592
Person2: 85676592
Person3: 85676592
Person: 85687968
Person2: 85687968
Person3: 85687968
Mohit
09/12/2024, 3:23 AM