Skip to content

Instantly share code, notes, and snippets.

View fergusonm's full-sized avatar

Michael Ferguson fergusonm

View GitHub Profile
@fergusonm
fergusonm / gist:92ef15e8611211bfd08121d106c1e626
Created January 27, 2021 00:58
Demonstration of launchWhenResumed event loss
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
@fergusonm
fergusonm / gist:ca387b98cd17eba3073df24529c46a85
Last active March 9, 2021 19:20
Conflated channel crashes
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
@fergusonm
fergusonm / gist:88a728eb543c7f6727a7cc473671befc
Last active October 28, 2023 20:01
launchWhenX dropping events
class MainFragment : Fragment(R.layout.main_fragment) {
companion object {
fun newInstance() = MainFragment()
}
private lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
@fergusonm
fergusonm / collectIn.kt
Created March 29, 2021 14:48
Compact flowWithLifecycle
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)
}
@fergusonm
fergusonm / flowWithLifecycle
Created October 21, 2021 21:02
Flow with lifecycle example
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)
@fergusonm
fergusonm / gist:b8cb267a1319b25e11c0d73613f956c4
Created October 21, 2021 21:06
Flow with lifecycle extension functions
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,
viewModel.events
.observeWithLifecycle(fragment = this, minActiveState = Lifecycle.State.RESUMED) {
// do things
}
viewModel.events
.observeWithLifecycle(lifecycleOwner = viewLifecycleOwner, minActiveState = Lifecycle.State.RESUMED) {
// do things
}
@fergusonm
fergusonm / gist:5ef6d0793d9419b2a3f6ff9d048a871e
Created December 22, 2021 15:26
View model UI stateflow
class MyViewMode: ViewModel() {
data class ViewState(
val someUIProperty: String = "",
val someOtherUIProperty: Int = 1,
)
private val _viewState = MutableStateFlow<ViewState>(ViewState())
val viewState = _viewState.asStateFlow()
}
@fergusonm
fergusonm / gist:8d202e5e8ba57d85fe4c77c708600e91
Last active December 22, 2021 15:56
View observing view state updates
// In your view/fragment
viewLifecycleOwner.lifecycleScope.launch {
viewModel.viewState
.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.collect {
// do something with the UI updates
}
}
// In your view model
private val _eventChannel = Channel<Event>(Channel.BUFFERED)
val events = _eventChannel.receiveAsFlow()