Skip to content

Instantly share code, notes, and snippets.


Jamie Sanson jamiesanson

View GitHub Profile

Keybase proof

I hereby claim:

  • I am jamiesanson on github.
  • I am jamiesanson ( on keybase.
  • I have a public key ASCJYZckhawpHlJ9PCpvFro0NqeHdopuwDh-hk1suNZ80Ao

To claim this, I am signing this object:

jamiesanson / ViewLifecycleLazy.kt
Last active Sep 10, 2022
A Kotlin lazy implementation which automatically clears itself at appropriate times in the View Lifecycle, with a Fragment example
View ViewLifecycleLazy.kt
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleLazy(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
jamiesanson / ViewLifecycleBinding.kt
Last active Jan 2, 2022
Kotlin Property Delegate for Fragment view lifecycle binding
View ViewLifecycleBinding.kt
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 / LifecycleCameraView.kt
Created Jun 21, 2018
Lifecycle-aware CameraView implementation
View LifecycleCameraView.kt
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 / StringResolving.kt
Last active Jul 24, 2018
Resolving resources in a weird, Kotlin-y way
View StringResolving.kt
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 / RetentionDelegate.kt
Last active Aug 12, 2019
Retention via ViewModel delegation
View RetentionDelegate.kt
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelStoreOwner
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 / LifecycleDelegate.kt
Last active Feb 19, 2018
Kotlin Lifecycle-aware fields
View LifecycleDelegate.kt
import android.arch.lifecycle.*
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 / LiveDataExtensions.kt
Created Feb 1, 2018
LiveData NonNull Observers
View LiveDataExtensions.kt
* 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) {
jamiesanson /
Created Nov 14, 2017
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
View Run this to install the right dependencies
pip install textract
# If this doesnt work, or fails at some point do this instead
pip install --no-deps textract