Skip to content

Instantly share code, notes, and snippets.

Avatar
👾

Aidan McWilliams Aidanvii7

👾
View GitHub Profile
@Aidanvii7
Aidanvii7 / NavControllerComposeUtils.kt
Last active Mar 16, 2022
Compose Navigation with Parcelable arguments
View NavControllerComposeUtils.kt
@file:Suppress("UnnecessaryVariable", "PackageDirectoryMismatch")
package androidx.navigation
import android.os.Bundle
import android.os.Parcelable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
fun NavController.navigate(
@Aidanvii7
Aidanvii7 / FlowToState.kt
Last active Jul 12, 2021
Transforming Flow types to State in a lifecycle aware way
View FlowToState.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 GuardUtils.kt
@file:OptIn(ExperimentalTypeInference::class)
@file:Suppress("unused")
import kotlin.experimental.ExperimentalTypeInference
inline fun <reified R> R.unless(@BuilderInference block: Guarded<R>.() -> Any?): R =
try {
val result = Guarded<R>().block()
result as? R ?: this
} catch (e: Guarded.BreakGuardWithResult) {
@Aidanvii7
Aidanvii7 / ContactViewModel.kt
Last active Oct 17, 2020
A `StateFlow` builder that can subscribe to other StateFlows that are read within the builder block. This is intended for deriving state from one or more other states.
View ContactViewModel.kt
@OptIn(ExperimentalCoroutinesApi::class)
class ContactViewModel() : ViewModel() {
val firstName = MutableStateFlow("")
val lastName = MutableStateFlow("")
val fullName by DerivedStateFlow {
"${+::firstName} ${+::lastName}"
}
}
@Aidanvii7
Aidanvii7 / BundleExtensions.kt
Last active Oct 1, 2021
A Kotlin way to use Android Bundles. Offers type safety unlike android ktx's version of bundleOf
View BundleExtensions.kt
import android.os.Bundle
import android.os.Parcelable
import android.util.Size
import java.io.Serializable
typealias BundleContext = Bundle.() -> Unit
/**
* ```
* fun example(someParcelable: Parcelable, someOtherBundle: Bundle) {
View AppFeaturesExtension.kt
fun main() {
FakeAppFeatures {
if (SomeFeature.enabled) {
println("some feature is enabled!")
}
}
}
sealed class Feature
object SomeFeature : Feature()
@Aidanvii7
Aidanvii7 / SpinnerBindingAdapters.kt
Last active Mar 7, 2020
Binding adapter for Android Spinner view when using https://github.com/Aidanvii7/Toolbox
View SpinnerBindingAdapters.kt
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import androidx.databinding.BindingAdapter
import androidx.databinding.InverseBindingAdapter
import androidx.databinding.InverseBindingListener
import com.aidanvii.toolbox.databinding.getTrackedValue
import com.aidanvii.toolbox.databinding.trackValue
View MutableNode.kt
import java.util.ArrayDeque
fun mutableNode(
value: Int,
left: MutableNode? = null,
right: MutableNode? = null
) = MutableNode(value, left, right)
data class MutableNode(
val value: Int,
@Aidanvii7
Aidanvii7 / async_utils.dart
Last active Aug 3, 2019
Example of how to use Dart's Completer
View async_utils.dart
typedef CompleterBlock<T> = void Function(Completer<T> completer);
Future<T> completer<T>(final CompleterBlock<T> block) {
assert(block != null);
final completer = Completer<T>();
block(completer);
return completer.future;
}
@Aidanvii7
Aidanvii7 / answer_builder.dart
Last active Jul 29, 2019
A fluent answer builder for Mockito for Dart
View answer_builder.dart
import 'dart:collection';
import 'package:mockito/mockito.dart';
typedef ComputedAnswer<T> = T Function();
class AnswerBuilder<T> implements _AnswerBuilderNode {
@override
final AnswerBuilder<T> _parent;
@override