This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class CooldownStateFlow<T>( | |
initialValue: T, | |
private val coroutineScope: CoroutineScope, | |
dispatcher: CoroutineDispatcher, | |
private val cooldownMillis: Long = 50L, | |
) { | |
private val singleThreadDispatcher = dispatcher.limitedParallelism(1) | |
private val _events = | |
MutableSharedFlow<T>().also { | |
coroutineScope.launch(singleThreadDispatcher) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun onRefresh() { | |
viewModelScope.launch { | |
isRefreshingState.update { true } | |
delay(2000) // simulate loading | |
isRefreshingState.update { false } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HiltViewModel | |
class PullToRefreshViewModel @Inject constructor() : ViewModel() { | |
val isRefreshingState = MutableStateFlow(false) | |
fun onRefresh() { | |
viewModelScope.launch { | |
isRefreshingState.update { true } | |
isRefreshingState.update { false } | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
private fun MainScreen( | |
viewModel: PullToRefreshViewModel = hiltViewModel() | |
) { | |
Box( | |
modifier = Modifier | |
.fillMaxWidth() | |
.background(Color.Blue) | |
) { | |
val isRefreshing: Boolean by viewModel.isRefreshingState.collectAsStateWithLifecycle() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FirstScreen( | |
navigate: () -> Unit, | |
firstContainer: FirstContainer = rememberFirstContainer(), | |
firstViewModel: FirstViewModel = viewModel(factory = firstContainer.viewModelFactory) | |
) { // content here.. } | |
@Composable | |
fun rememberFirstContainer(): FirstContainer { | |
return remember { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FirstScreen( | |
navigate: () -> Unit, | |
firstContainer: FirstContainer = FirstContainer().also { | |
DaggerFirstComponent.builder().build().inject(it) | |
}, | |
firstViewModel: FirstViewModel = viewModel(factory = firstContainer.viewModelFactory) | |
) { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component( | |
modules = [FirstModule::class, VmModule::class] // dagger modules here if needed | |
) | |
interface FirstComponent { | |
fun inject(firstContainer: FirstContainer) // inject the container instead of the activity/fragment | |
@Component.Builder | |
interface Builder { | |
fun build(): FirstComponent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stable | |
class FirstContainer { | |
@Inject lateinit var firstScreenTracker: FirstScreenTracker | |
@Inject lateinit var viewModelFactory: ViewModelFactory | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
@Inject lateinit var firstScreenTracker: FirstScreenTracker | |
@Inject lateinit var viewModelFactory: ViewModelFactory | |
override fun onCreate(savedInstanceState: Bundle?) { | |
DaggerFirstComponent.builder().build().inject(this) // build dagger component and inject | |
super.onCreate(savedInstanceState) | |
setContent { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun FirstScreen( | |
navigate: () -> Unit, | |
firstScreenTracker: FirstScreenTracker, | |
viewModelFactory: ViewModelFactory, | |
firstViewModel: FirstViewModel = viewModel(factory = viewModelFactory) | |
) { | |
// content... | |
} |
NewerOlder