Skip to content

Instantly share code, notes, and snippets.

View qwert2603's full-sized avatar

Alexander Zhdanov qwert2603

View GitHub Profile
package com.qwert2603.myapplication
import androidx.annotation.CheckResult
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
class Storage {
suspend fun get(): Result<String> = runCatching {
delay(1)
import org.koin.core.Koin
import org.koin.core.context.startKoin
import org.koin.core.module.Module
import org.koin.core.qualifier.Qualifier
import org.koin.core.qualifier.named
import org.koin.dsl.module
import org.koin.dsl.onClose
import org.koin.ext.getFullName
// Based on Koin 3.1.2
inline fun <reified K, reified V> Koin.getMultibinding(): Map<K, V> =
get<Multibinding<K, V>>(qualifier = multibindingQualifier<K, V>()).toMap()
inline fun <reified K, reified V> Module.intoMultibinding(key: K, value: V) {
var multibinding: Multibinding<K, V>? = null
single(
qualifier = named("${K::class.getFullName()}_${V::class.getFullName()}_$key"),
createdAtStart = true,
) {
multibinding = get(multibindingQualifier<K, V>())
multibinding!![key] = value
}.onClose {
multibinding?.remove(key)
fun main() {
val koin = startKoin {
val module1 = module {
declareMultibinding<String, Int>()
intoMultibinding("one", 1)
intoMultibinding("two", 2)
intoMultibinding("three", 3)
declareMultibinding<String, Double>()
intoMultibinding("one", 1.0)
class SomeFragment {
private val vm = SomeViewModel()
fun onCreate() {
val currentValue: Int = vm.flow.value
// vm.flow.setValue(4) // compilation error
}
}
class SomeViewModel : BaseViewModel() {
val flow = createViewModelStateFlow(0)
suspend fun load() {
flow.setValue(1)
delay(1000)
flow.setValue(flow.value + 1)
}
}
protected class ViewModelStateFlowImpl<T>(initial: T) : ViewModelStateFlow<T> {
val wrapped = MutableStateFlow(initial)
override val value: T
get() = wrapped.value
override val replayCache: List<T>
get() = wrapped.replayCache
@InternalCoroutinesApi
@qwert2603
qwert2603 / BaseViewModel.kt
Last active September 7, 2022 11:09
ViewModelStateFlow
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
sealed class ViewModelStateFlow<T>(stateFlow: StateFlow<T>) : StateFlow<T> by stateFlow
private class ViewModelStateFlowImpl<T>(
initial: T,
val wrapped: MutableStateFlow<T> = MutableStateFlow(initial)
) : ViewModelStateFlow<T>(wrapped)
import 'package:flutter/material.dart';
import 'package:circular_reveal_animation/circular_reveal_animation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CRA Demo',