Skip to content

Instantly share code, notes, and snippets.

View fergusonm's full-sized avatar

Michael Ferguson fergusonm

View GitHub Profile
@fergusonm
fergusonm / gist:bc12dfe20562389c6064c1efa158ce66
Created September 3, 2019 14:29
Method level logging live template. Logs the method name with all arguments.
Saved as "mlog" with Kotlin statement applicability. (Obviously change to suit your own needs.)
android.util.Log.d("LOGLOGLOG", "$className$ $METHOD_NAME$() called with: " + $args$)
Params:
classname - kotlinClassName()
METHOD_NAME - kotlinFunctionName()
args - groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", functionParameters())
@fergusonm
fergusonm / gist:fb1da641246bd68275597561baa636e7
Last active September 26, 2022 08:01
Single Live Event Stream Sample
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()
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()
@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 -> {}
}
}
@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 / 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: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 / 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 / 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 / gist:2a87652f85e7391b922245e7e1412958
Created January 26, 2021 23:09
Basic one shot event flow
private val eventChannel = Channel<Event>(Channel.BUFFERED)
val eventsFlow = eventChannel.receiveAsFlow()