Skip to content

Commit

Permalink
Fix Koin update to 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DevSrSouza committed Oct 7, 2024
1 parent a4861e4 commit 02dd74e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cafe.adriel.voyager.core.screen.uniqueScreenKey
import cafe.adriel.voyager.navigator.LocalNavigator
import cafe.adriel.voyager.navigator.currentOrThrow
import cafe.adriel.voyager.sample.DetailsContent
import org.koin.androidx.compose.getViewModel
import org.koin.androidx.compose.koinViewModel
import org.koin.core.parameter.parametersOf

data class AndroidDetailsScreen(
Expand All @@ -18,7 +18,7 @@ data class AndroidDetailsScreen(
@Composable
override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val viewModel = getViewModel<AndroidDetailsViewModel> { parametersOf(index) }
val viewModel = koinViewModel<AndroidDetailsViewModel> { parametersOf(index) }

DetailsContent(viewModel, "Item #${viewModel.index}", navigator::pop)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package cafe.adriel.voyager.koin

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import cafe.adriel.voyager.core.model.ScreenModel
import cafe.adriel.voyager.core.model.rememberNavigatorScreenModel
import cafe.adriel.voyager.core.model.rememberScreenModel
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.navigator.Navigator
import org.koin.compose.currentKoinScope
import org.koin.compose.stable.rememberStableParametersDefinition
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.emptyParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

Expand All @@ -19,10 +21,12 @@ public inline fun <reified T : ScreenModel> Screen.koinScreenModel(
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null
): T {
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
val currentParameters by rememberUpdatedState(parameters)
val tag = remember(qualifier, scope) { qualifier?.value }
return rememberScreenModel(tag = tag) {
scope.get(qualifier, st?.parametersDefinition)
scope.get(qualifier) {
currentParameters?.invoke() ?: emptyParametersHolder()
}
}
}

Expand All @@ -32,10 +36,12 @@ public inline fun <reified T : ScreenModel> Navigator.koinNavigatorScreenModel(
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null
): T {
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
val currentParameters by rememberUpdatedState(parameters)
val tag = remember(qualifier, scope) { qualifier?.value }
return rememberNavigatorScreenModel(tag = tag) {
scope.get(qualifier, st?.parametersDefinition)
scope.get(qualifier) {
currentParameters?.invoke() ?: emptyParametersHolder()
}
}
}

Expand Down

0 comments on commit 02dd74e

Please sign in to comment.