Skip to content

Instantly share code, notes, and snippets.

Avatar
👾

Aidan McWilliams Aidanvii7

👾
View GitHub Profile
@Aidanvii7
Aidanvii7 / SharedPreferenceDelegates.kt
Last active Mar 24, 2022
mutableStateOf for SharedPreferences
View SharedPreferenceDelegates.kt
import android.content.SharedPreferences
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.core.content.edit
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun SharedPreferences.mutableStateOf(
key: String,
@Aidanvii7
Aidanvii7 / FlowUtils.kt
Last active Mar 10, 2022
compose utils
View FlowUtils.kt
package com.example
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
@Aidanvii7
Aidanvii7 / RandomNumberServiceExample.kt
Last active Feb 10, 2022
Utility for binding a local android service in compose
View RandomNumberServiceExample.kt
package com.example.app
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import kotlin.random.Random
@Aidanvii7
Aidanvii7 / BindingAdapterUtils.kt
Created Dec 18, 2021
Binding adapter for Spinner
View BindingAdapterUtils.kt
import android.view.View
import androidx.annotation.IdRes
import androidx.databinding.BindingAdapter
import androidx.databinding.adapters.ListenerUtil as FrameworkListenerUtil
/**
* Designed to track objects passed as optional parameters via static [BindingAdapter] methods.
*
* Only one instance per instanceResId can be tracked at a time.
*
@Aidanvii7
Aidanvii7 / ShamefulHack.kt
Last active Dec 8, 2021
'fix' for correct lifecycle behaviour when using StateFlow in databinding expressions, issue here https://issuetracker.google.com/issues/184935697
View ShamefulHack.kt
import androidx.annotation.MainThread
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
/**
* See the [androidx.databinding.ViewDataBindingKtx] class as to why this works (specifically setLifecycleOwner and startCollection)
* [androidx.databinding.ViewDataBindingKtx] has been copied to the gist from the androidx.databinding:databinding-ktx artifact, do not copy into project!
@Aidanvii7
Aidanvii7 / ResultCallAdapterFactory.kt
Created Aug 31, 2021
A CallAdapter for the built in Result type
View ResultCallAdapterFactory.kt
import okhttp3.ResponseBody
import retrofit2.*
import java.io.IOException
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
internal class ResultCallAdapterFactory(
private val errorResponseConverterFactory: Converter.Factory,
private val errorType: Type,
) : CallAdapter.Factory() {
@Aidanvii7
Aidanvii7 / CollectAsStateWhile.kt
Created Aug 19, 2021
Lifecycle bound collectAsState
View CollectAsStateWhile.kt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.Lifecycle.State.STARTED
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.flow.Flow
View Provider.kt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.staticCompositionLocalOf
import kotlin.reflect.KClass
sealed class Providable<T : Any>(
val type: KClass<T>
) {
abstract val factory: () -> T
@Aidanvii7
Aidanvii7 / NavGraph.kt
Last active Oct 17, 2021
Navigate with Parcelable arguments in Jetpack Compose
View NavGraph.kt
composable(route = "screen_1") {
Button(
onClick = {
navController.navigate(
route = "screen_2",
args = parcelableArgs {
arg { MyParcelableArgument() }
arg("named") { MyParcelableArgument() }
},
)
@Aidanvii7
Aidanvii7 / CollectAsStateWithLifecycle.kt
Created Aug 4, 2021
CollectAsState extensions for lifecycle awareness
View CollectAsStateWithLifecycle.kt
@file:Suppress("PackageDirectoryMismatch")
package androidx.compose.runtime
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.Lifecycle.State.STARTED
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.flow.Flow