Skip to content

Instantly share code, notes, and snippets.

View jamiesanson's full-sized avatar

Jamie Sanson jamiesanson

View GitHub Profile
@jamiesanson
jamiesanson / keybase.md
Created October 13, 2021 00:31
keybase.md

Keybase proof

I hereby claim:

  • I am jamiesanson on github.
  • I am jamiesanson (https://keybase.io/jamiesanson) on keybase.
  • I have a public key ASCJYZckhawpHlJ9PCpvFro0NqeHdopuwDh-hk1suNZ80Ao

To claim this, I am signing this object:

@jamiesanson
jamiesanson / ViewLifecycleLazy.kt
Last active May 29, 2024 10:43
A Kotlin lazy implementation which automatically clears itself at appropriate times in the View Lifecycle, with a Fragment example
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleLazy(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
@jamiesanson
jamiesanson / ViewLifecycleBinding.kt
Last active April 18, 2023 11:25
Kotlin Property Delegate for Fragment view lifecycle binding
fun <T> Fragment.viewLifecycle(bindUntilEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY): ReadWriteProperty<Fragment, T> =
object: ReadWriteProperty<Fragment, T>, LifecycleObserver {
// A backing property to hold our value
private var binding: T? = null
private var viewLifecycleOwner: LifecycleOwner? = null
init {
// Observe the View Lifecycle of the Fragment
@jamiesanson
jamiesanson / LifecycleCameraView.kt
Created June 21, 2018 21:46
Lifecycle-aware CameraView implementation
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.OnLifecycleEvent
import android.content.Context
import android.util.AttributeSet
import com.wonderkiln.camerakit.*
/**
* Lifecycle aware view subclass of CameraView to allow automatic starting and stopping on lifecycle
@jamiesanson
jamiesanson / StringResolving.kt
Last active July 24, 2018 03:06
Resolving resources in a weird, Kotlin-y way
var resources: Resources? = null
val something by R.string.abc_capital_off<String>()
private inline operator fun <reified T> Int.invoke(): ReadOnlyProperty<Any, T> {
return when {
String is T -> StringResolvingDelegate(this)
else -> throw IllegalArgumentException("Unexpected resource type")
} as ReadOnlyProperty<Any, T>
}
@jamiesanson
jamiesanson / RetentionDelegate.kt
Last active August 12, 2019 22:12
Retention via ViewModel delegation
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelStoreOwner
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import kotlin.reflect.KProperty
/**
* This class handles retention of any object using the ViewModel framework. An
* example of this method being used can be found in the Loader Manager framework
@jamiesanson
jamiesanson / LifecycleDelegate.kt
Last active February 19, 2018 03:41
Kotlin Lifecycle-aware fields
import android.arch.lifecycle.*
import android.support.v4.app.ActivityCompat
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import kotlin.reflect.KProperty
fun <T> Fragment.selfClearing(valInitializer: (() -> T)? = null) = LifecycleDelegate(this.lifecycle, valInitializer)
fun <T> AppCompatActivity.selfClearing(valInitializer: (() -> T)? = null) = LifecycleDelegate(this.lifecycle, valInitializer)
@jamiesanson
jamiesanson / LiveDataExtensions.kt
Created February 1, 2018 03:59
LiveData NonNull Observers
/**
* Extends LiveData allowing Kotlin DSL for onChanged callback usage
*/
fun <T> LiveData<T>.observeNonNull(lifecycleOwner: LifecycleOwner, onItem: (T) -> Unit) {
this.observe(lifecycleOwner, object : NonNullObserver<T> {
override fun onChangedNonNull(t: T) {
onItem(t)
}
})
}
@jamiesanson
jamiesanson / LSTM_chopra.py
Created November 14, 2017 21:11
LSTM text-gen implementation adapted from Keras Example
'''
Script to generate text from Chopra's writings.
At least 20 epochs are required before the generated text
starts sounding coherent.
It is recommended to run this script on GPU, as recurrent
networks are quite computationally intensive.
If you try this script on new data, make sure your corpus
pip install textract
# If this doesnt work, or fails at some point do this instead
pip install --no-deps textract