Maybe I will write article about this
internal fun <T, R> Flow<T>.retryMap(
map: suspend (T) -> R,
predicate: suspend FlowCollector<R>.(value: T, result: R, attempt: Int) -> Boolean
): Flow<R> {
var retryCount = 1
dependencies { | |
implementation "dev.burnoo:cokoin:0.3.2" | |
// REMOVE "org.koin:koin-androidx-compose:X.Y.Z" if you were using it | |
} |
val appModule = module { | |
single { AppData() } | |
} | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
Koin(appDeclaration = { modules(appModule) }) { | |
val appData = get<AppData>() |
@Composable | |
fun Hello() { | |
val hello = get<Hello>() | |
Text(hello.text) | |
} | |
val previewModule = module { | |
factory { Hello(text = "preview hello text") } | |
} |
data class A(val value: String) | |
val scopedModule = module { | |
scope<ScopeTypeA> { | |
scoped { A("scopeA") } | |
} | |
scope<ScopeTypeB> { | |
scoped { A("scopeB") } | |
} | |
} |
@Composable | |
fun App() { | |
val globalData = get<AppData>() // uses Global KoinContext | |
Koin(koinApplication) { | |
val localData = get<AppData>() // uses Local KoinContext (koinApplication) | |
} | |
} |
val module = module { | |
viewModel { AppViewModel() } | |
} | |
Koin(appDeclaration = { modules(module) }) { | |
val viewModel = getViewModel<AppViewModel>() | |
Text(viewModel.text) | |
} |
@Composable | |
fun Screen1(navController: NavController) { | |
val rootViewModel = getViewModel<RootViewModel>( | |
viewModelStoreOwner = navController.getBackStackEntry("root") | |
) | |
} |
private class MutableStateAdapter<T>( | |
private val state: State<T>, | |
private val mutate: (T) -> Unit | |
) : MutableState<T> { | |
override var value: T | |
get() = state.value | |
set(value) { | |
mutate(value) | |
} |