Skip to content

Instantly share code, notes, and snippets.

View ZakTaccardi's full-sized avatar

Zak Taccardi ZakTaccardi

View GitHub Profile
@ZakTaccardi
ZakTaccardi / LifecycleScopeExtensionsInternal.kt
Created May 18, 2020 15:54
ViewLifecycleScope extensions
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.CoroutineScope
@Suppress("UNCHECKED_CAST")
internal class ViewLifecycleScopeImpl(
private val fragment: Fragment
) : ViewLifecycleScope {
@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 / LibsAccessor.kt
Created August 29, 2022 17:09
Migrate to version catalogs now!
import myapp.dependencyCatalogs.LibsCatalogExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.the
// this file is in `buildSrc` in the root package, so `libs` will point to this function or the generated `libs` from the `.toml` file
/**
* Wrapper with no package name to have projects automatically pick up [LibsCatalogExtension] when this function exists.
*
* The generated version accessors is [org.gradle.accessors.dm.LibrariesForLibs].
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)
public void elementClicked(int position){
if (view != null){
final int word = view.getWordByPosition(int position)
view.doAnimationStuff();
view.launchActivityWithWord(word);
}
}
//because there are no android dependencies here, it becomes easy to implement Mockito style behavioral testing.
@ZakTaccardi
ZakTaccardi / LocalPropertySupport.kt
Created January 3, 2022 16:48
`local.properties` and project level gradle properties support for Configuration Cache
import org.gradle.api.Project
import org.gradle.api.file.ProjectLayout
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.provider.ValueSource
import org.gradle.api.provider.ValueSourceParameters
import org.gradle.kotlin.dsl.of
import java.io.StringReader
import java.util.Properties
@ZakTaccardi
ZakTaccardi / build.gradle
Created August 26, 2015 01:24
Automatic per-variant google_services.json configurations with Gradle
//append code below to existing build.gradle
def appModuleRootFolder = '.'
def srcDir = 'src'
def googleServicesJson = 'google-services.json'
task switchToDebug(type: Copy) {
def buildType = 'debug'
description = 'Switches to DEBUG google-services.json'
from "${srcDir}/${buildType}"
@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
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 / FunctionInterceptionExample.kt
Created December 16, 2020 17:15
Function Interception (composition)
/**
* An abstraction around a 3rd party SDK that does some magic to
* identify if a user is a bot or not.
*
* The real implementation may have direct Android dependencies,
* so being able to swap this out is useful for unit testing
*/
interface VendorSdk {
suspend fun isUserABot(): Boolean
}