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
package com.example.launchwhendemo.ui.main | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.* | |
import com.example.launchwhendemo.R | |
import kotlinx.coroutines.channels.Channel |
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
package com.example.launchwhendemo.ui.main | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.View | |
import android.widget.Button | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.lifecycle.viewModelScope |
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 MainFragment : Fragment(R.layout.main_fragment) { | |
companion object { | |
fun newInstance() = MainFragment() | |
} | |
private lateinit var viewModel: MainViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { |
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 <T> Flow<T>.collectIn( | |
lifecycleOwner: LifecycleOwner, | |
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | |
action: suspend (T) -> Unit | |
): Job = lifecycleOwner.lifecycleScope.launch { | |
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action) | |
} |
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
viewModel.events | |
.onEach { | |
// can get cancelled when the lifecycle state falls below min | |
} | |
.flowWithLifecycle(lifecycle = viewLifecycleOwner.lifecycle, minActiveState = Lifecycle.State.STARTED) | |
.onEach { | |
// Do things | |
} | |
.launchIn(viewLifecycleOwner.lifecycleScope) |
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
inline fun <reified T> Flow<T>.observeWithLifecycle( | |
lifecycleOwner: LifecycleOwner, | |
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | |
noinline action: suspend (T) -> Unit | |
): Job = lifecycleOwner.lifecycleScope.launch { | |
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState).collect(action) | |
} | |
inline fun <reified T> Flow<T>.observeWithLifecycle( | |
fragment: Fragment, |
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
viewModel.events | |
.observeWithLifecycle(fragment = this, minActiveState = Lifecycle.State.RESUMED) { | |
// do things | |
} | |
viewModel.events | |
.observeWithLifecycle(lifecycleOwner = viewLifecycleOwner, minActiveState = Lifecycle.State.RESUMED) { | |
// do things | |
} |
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 MyViewMode: ViewModel() { | |
data class ViewState( | |
val someUIProperty: String = "", | |
val someOtherUIProperty: Int = 1, | |
) | |
private val _viewState = MutableStateFlow<ViewState>(ViewState()) | |
val viewState = _viewState.asStateFlow() | |
} |
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
// In your view/fragment | |
viewLifecycleOwner.lifecycleScope.launch { | |
viewModel.viewState | |
.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED) | |
.collect { | |
// do something with the UI updates | |
} | |
} |
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
// In your view model | |
private val _eventChannel = Channel<Event>(Channel.BUFFERED) | |
val events = _eventChannel.receiveAsFlow() |