Skip to content

Instantly share code, notes, and snippets.

View fergusonm's full-sized avatar

Michael Ferguson fergusonm

View GitHub Profile
@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: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:2a87652f85e7391b922245e7e1412958
Created January 26, 2021 23:09
Basic one shot event flow
private val eventChannel = Channel<Event>(Channel.BUFFERED)
val eventsFlow = eventChannel.receiveAsFlow()
@fergusonm
fergusonm / Example observers
Last active February 22, 2021 00:05
Fragment flow observer usage
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.eventsFlow
.onEach {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
@fergusonm
fergusonm / FlowObserver.kt
Created January 26, 2021 23:07
Flow observer
class FlowObserver<T> (
lifecycleOwner: LifecycleOwner,
private val flow: Flow<T>,
private val collector: suspend (T) -> Unit
) {
private var job: Job? = null
init {
lifecycleOwner.lifecycle.addObserver(LifecycleEventObserver {
@fergusonm
fergusonm / gist:35a9b2ac4ba885ffc659db492f1f83c2
Created January 26, 2021 23:07
flow observation between start and stop
override fun onStart() {
super.onStart()
job = viewModel.eventsFlow
.onEach {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
}
@fergusonm
fergusonm / gist:29a27494714dd65c1588a593b88a9dcf
Created January 26, 2021 23:06
rxjava observation between start and stop
override fun onStart() {
super.onStart()
disposable = viewModel.eventsFlow
.asObservable() // converting to Rx for the example
.subscribe {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
@fergusonm
fergusonm / gist:32b5be1af5bf6dd1f7f286c2f1081eda
Last active March 15, 2021 19:18
lanchWhenX flow collector
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// get your view model here
lifecycleScope.launchWhenStarted {
viewModel.eventsFlow
.collect {
when (it) {
MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
@fergusonm
fergusonm / launch in example
Last active February 21, 2021 23:55
Fragment, observe using view lifecycle scope
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.eventsFlow
.onEach {
when (it) {
is MainViewModel.Event.NavigateToSettings -> {}
is MainViewModel.Event.ShowSnackBar -> {}
is MainViewModel.Event.ShowToast -> {}
}
}
class MainViewModel : ViewModel() {
sealed class Event {
object NavigateToSettings: Event()
data class ShowSnackBar(val text: String): Event()
data class ShowToast(val text: String): Event()
}
private val eventChannel = Channel<Event>(Channel.BUFFERED)
val eventsFlow = eventChannel.receiveAsFlow()