Skip to content

Instantly share code, notes, and snippets.

View Aidanvii7's full-sized avatar
👾

Aidan McWilliams Aidanvii7

👾
View GitHub Profile
@Aidanvii7
Aidanvii7 / google_sign_in.dart
Last active July 25, 2019 12:41
Flutter Google sign in
Future<FirebaseUser> login() async {
final googleUser = await _googleSignIn.signIn();
final googleAuth = await googleUser.authentication;
final authCredential = GoogleAuthProvider.getCredential(
idToken: googleAuth.idToken,
accessToken: googleAuth.accessToken
);
return await _firebaseAuth.signInWithCredential(authCredential);
}
@Aidanvii7
Aidanvii7 / answer_builder.dart
Last active July 29, 2019 19:37
A fluent answer builder for Mockito for 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
@Aidanvii7
Aidanvii7 / async_utils.dart
Last active August 3, 2019 15:24
Example of how to use Dart's Completer
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 / MutableNode.kt
Created October 15, 2019 01:13
Binary Tree
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 / SpinnerBindingAdapters.kt
Last active March 7, 2020 11:36
Binding adapter for Android Spinner view when using https://github.com/Aidanvii7/Toolbox
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
fun main() {
FakeAppFeatures {
if (SomeFeature.enabled) {
println("some feature is enabled!")
}
}
}
sealed class Feature
object SomeFeature : Feature()
@Aidanvii7
Aidanvii7 / BundleExtensions.kt
Last active October 1, 2021 09:56
A Kotlin way to use Android Bundles. Offers type safety unlike android ktx's version of bundleOf
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) {
@Aidanvii7
Aidanvii7 / ContactViewModel.kt
Last active October 17, 2020 21:01
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.
@OptIn(ExperimentalCoroutinesApi::class)
class ContactViewModel() : ViewModel() {
val firstName = MutableStateFlow("")
val lastName = MutableStateFlow("")
val fullName by DerivedStateFlow {
"${+::firstName} ${+::lastName}"
}
}
@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 / FlowToState.kt
Last active July 12, 2021 20:35
Transforming Flow types to State in a lifecycle aware way
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