Skip to content

Instantly share code, notes, and snippets.

View Aidanvii7's full-sized avatar
👾

Aidan McWilliams Aidanvii7

👾
View GitHub Profile
@Aidanvii7
Aidanvii7 / SparseArrayExtensions.kt
Last active December 17, 2019 14:48
A collection of extension functions for Android's SparseArray
package com.aidanvii.extensions
import android.util.SparseArray
import java.util.*
val SparseArray<*>.max: Int get () = size() - 1;
inline fun <V> SparseArray<V>.forEachValue(action: (V) -> Unit): SparseArray<V> {
for (index in 0..max) {
val key = keyAt(index)
@Aidanvii7
Aidanvii7 / Logger.kt
Last active February 25, 2017 18:11
Simple Log delegation in Kotlin with extension function.
package com.aidanvii.extensions
fun Any.logD(message: String) = Logger.d(javaClass.simpleName, message)
interface LoggerDelegate {
fun d(tag: String, message: String)
}
interface CompositeLoggerDelegate : LoggerDelegate {
fun attachDelegate(delegate: LoggerDelegate)
@Aidanvii7
Aidanvii7 / UserViewModel.kt
Last active May 27, 2017 14:53
MYVMGA: 3
package com.aidanvii7.mvvm
class UserViewModel(val firstName: String, val lastName: String)
fun <T> BaseObservable.bindable(initialValue: T, propertyId: Int): BindableProperty<T> {
return BindableProperty<T>(initialValue, this)
}
class BindableProperty<T>(initialValue: T,
private val observable: BaseObservable) : ObservableProperty<T>(initialValue) {
private var propertyId: Int = 0
override fun beforeChange(property: KProperty<*>, oldValue: T, newValue: T): Boolean {
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
/**
* Implementation of [ViewModelProvider.Factory] that takes care of the slightly awkward generic method [ViewModelProvider.Factory.create]
*
* It removes the need for the client to manually handle the given ViewModel [Class] in an if else clause or switch statement, or perform type casting when returning.
class MyViewModel : ViewModel() {
@get:MainThread
val users: LiveData<List<String>> by unsafeLazy {
MutableLiveData<List<String>>().apply { loadUsersInto(this) }
}
private fun loadUsersInto(liveData: MutableLiveData<List<String>>) {
// do async operation to fetch users and update
}
@Aidanvii7
Aidanvii7 / DataBindingTrackingExample.kt
Last active April 9, 2019 12:07
Data binding tracking
inline fun <V : View, I> V.trackInstance(
newInstance: I?,
@IdRes instanceResId: Int,
onDetached: V.(I) -> Unit = {},
onAttached: V.(I) -> Unit = {}
) {
ListenerUtil.trackListener(this, newInstance, instanceResId).let { oldInstance ->
if (oldInstance !== newInstance) { // referential comparison
oldInstance?.let { onDetached(oldInstance) }
newInstance?.let { onAttached(newInstance) }
@Aidanvii7
Aidanvii7 / counter_store.dart
Created July 7, 2019 16:02
Example of using MobX Dart with Flutter's Counter example, plus some extras
import 'package:mobx/mobx.dart';
part 'counter_store.g.dart';
class CounterStore = _CounterStore with _$CounterStore;
abstract class _CounterStore with Store {
@observable
int counter1 = 0;
@Aidanvii7
Aidanvii7 / consumer_observer.dart
Last active July 9, 2019 16:31
A convenience widget for providing instances of Store, with automatic disposal
import 'package:flutter/widgets.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:provider/provider.dart';
typedef ConsumerObserverBuilder<T> = Widget Function(BuildContext context, T value);
class ConsumerObserver<T> extends StatelessWidget {
final ConsumerObserverBuilder<T> builder;
final Widget prebuiltChild;
@Aidanvii7
Aidanvii7 / transient.dart
Created July 9, 2019 17:03
Simple wrapper class for consuming one time events from a MobX store
class Transient<T> {
T _value;
Transient(T value) {
this._value = value;
}
void use(Function(T value) block) {
assert(block != null);
if (_value != null) {