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 MainScreen( | |
| viewModel: SomeViewModel | |
| ) { | |
| val lifecycle = LocalLifecycleOwner.current | |
| LaunchedEffect(Unit) { | |
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
| viewModel.sharedFlow.collectLatest { | |
| Log.d("MainActivity", it.toString()) | |
| } |
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 MainScreen( | |
| viewModel: SomeViewModel | |
| ) { | |
| val state = viewModel.stateFlow.collectAsStateWithLifecycle() | |
| Log.d("MainActivity", state.value.toString()) | |
| } |
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 MainScreen( | |
| viewModel: SomeViewModel | |
| ) { | |
| val result = viewModel.flow.collectAsStateWithLifecycle(initialValue = 0) | |
| Log.d("MainActivity", result.value.toString()) | |
| } |
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 SomeViewModel constructor( | |
| private val getUserUseCase: GetUserUseCase, | |
| private val dispatcher: CoroutineDispatcher = Dispatchers.IO | |
| ): ViewModel() { | |
| // ... Rest of your code ... | |
| private val _liveData = MutableLiveData<User>() | |
| val liveData: LiveData<User> | |
| get() = _liveData |
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() { | |
| private val viewModel: SomeViewModel by viewModels() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| // ... Rest of your code ... | |
| viewModel.liveData.observe(this) { | |
| // DO SOMETHING |