Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View ZakTaccardi's full-sized avatar

Zak Taccardi ZakTaccardi

View GitHub Profile
@ZakTaccardi
ZakTaccardi / Scoping.kt
Created March 25, 2020 18:57
scoping of data
typealias Session = String
typealias SessionId = String
// Global Graph
interface GlobalGraph {
val sessionCache: SessionCache
}
// 1 - Global state
// globalGraph.sessionCache.getCurrentSession()
@ZakTaccardi
ZakTaccardi / AddTest.kt
Created October 6, 2019 19:54
Companion Code to - Writing Awesome Tests (Coroutine Edition)
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.withContext
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
// function to test
suspend fun Int.add(valueToAdd: Int): Int = withContext(Dispatchers.Default) {
// pretend this is an expensive operation, so we switch off the main thread
@ZakTaccardi
ZakTaccardi / TestDelegate.kt
Last active October 6, 2019 19:49
Companion Code to - Writing Awesome Tests - https://medium.com/p/b271c7838344
import org.assertj.core.api.Assertions
import org.junit.Before
import org.junit.Test
// function to test
fun Int.add(valueToAdd: Int): Int = this + valueToAdd
class AddTest {
private lateinit var test: TestDelegate
@ZakTaccardi
ZakTaccardi / RxChannelExtensions.kt
Created February 2, 2019 04:45
Some Rx-style operators on ReceiveChannel<T>
@file:JvmName("RxChannelExtensions")
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.GlobalScope
import kotlinx.coroutines.experimental.channels.ReceiveChannel
import kotlinx.coroutines.experimental.channels.consumeEach
import kotlinx.coroutines.experimental.channels.consumes
import kotlinx.coroutines.experimental.channels.consumesAll
import kotlinx.coroutines.experimental.channels.produce
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.sync.Mutex
@ZakTaccardi
ZakTaccardi / ExampleActivity.kt
Last active October 20, 2023 02:27
Example MVI Implementation with Coroutines
import kotlinx.coroutines.experimental.android.Main
import kotlinx.coroutines.experimental.CoroutineScope
class ExampleActivity : Activity(), CoroutineScope by CoroutineScope(Dispatchers.Main) {
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
val ui = Ui(this) // bind views, etc
@ZakTaccardi
ZakTaccardi / WhatIsACoroutine.kt
Created December 10, 2018 20:29
What is a coroutine?
// 1
Thread {
var counter = 0
counter++
counter++
println("$counter") // will always print "2"
}
// 2
// for every Request, a Response is returned
import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
// A Graph is just an interface that exposes your dependencies (like a Dagger 2 @Component).
interface Graph {
val context: Context
@ZakTaccardi
ZakTaccardi / LoginFeature.kt
Last active March 23, 2018 22:18
Reactive Login
import android.app.Activity
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.rxkotlin.subscribeBy
/**
* Stores state related to login.
*/
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.support.annotation.MainThread
import kotlin.reflect.KClass
/** Only emits [T] when [source] is true ]*/
fun <T : Any> LiveData<T>.filterTrue(source: LiveData<Boolean>): LiveData<T> {
return this.combineLatest(source) { emission, isStateInitialized ->
Pair(emission, isStateInitialized)
@ZakTaccardi
ZakTaccardi / RxChannelExtensions.kt
Last active March 12, 2018 16:03
combineLatest in coroutines adventure
suspend fun <A : Any?, B : Any?, R> ReceiveChannel<A>.combineLatest(
otherSource: ReceiveChannel<B>,
context: CoroutineContext = Unconfined,
combineFunction: suspend (A, B) -> R
): ReceiveChannel<R> = produce(context) {
val sourceA: ReceiveChannel<A> = this@combineLatest
val sourceB: ReceiveChannel<B> = otherSource
val latestA = AtomicReference<A>()