Skip to content

Instantly share code, notes, and snippets.

View Moes81's full-sized avatar

Andreas Spindler Moes81

  • MaibornWolff GmbH
  • Munich
View GitHub Profile
@Moes81
Moes81 / ReferenceDelegates.kt
Last active December 29, 2021 16:23
Some useful delegates
package delegates
import java.lang.ref.SoftReference
import java.lang.ref.WeakReference
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
// Idea taken from: https://medium.com/@pgopinath/idiomatic-weakreference-implementation-by-delegate-in-kotlin-65ec60287de6
fun <T>weakReference(tIn: T? = null): ReadWriteProperty<Any?, T?> {
@Moes81
Moes81 / FlowTestObserver.kt
Created August 3, 2021 14:44
A Kotlin Flow test observer that enables easy testing of flow emitted values in unit tests
/**
* Creates a [FlowTestObserver] instance and starts collecting the values of the [Flow] immediately.
* You must call [FlowTestObserver.finish] after all the values have been emitted to the flow in order to continue.
*/
fun <T> Flow<T>.test(scope: CoroutineScope): FlowTestObserver<T> {
return FlowTestObserver(scope, this)
}
/**
* A test class that collects the values of the provided `flow` in its own coroutine. The collected values are
@Moes81
Moes81 / ViewBindingDelegates.kt
Last active January 21, 2021 08:07
Android ViewBinding delegate function to minimize boiler plate code when working with view bindings.
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
@Moes81
Moes81 / BlockSnapHelper.kt
Last active January 7, 2021 10:31
A custom SnapHelper implementation for the RecyclerView that snaps to a predefined block of items.
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import android.view.animation.Interpolator
import android.widget.Scroller
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.*
import kotlin.math.abs
import kotlin.math.max
@Moes81
Moes81 / coroutine_broadcastchannel_playground.kt
Last active April 30, 2020 06:32
coroutine BroadcastChannel playground for scratch file
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
runBlocking {
val sendChannel = BroadcastChannel<Int>(4)
println("Starting")
@Moes81
Moes81 / coroutine_channel_playground.kt
Last active April 30, 2020 06:29
Coroutine Channel playground for scratch file
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
runBlocking {
val sendChannel = Channel<Int>(4)