Skip to content

Instantly share code, notes, and snippets.

Avatar
📈

Jamie Sanson jamiesanson

📈
View GitHub Profile
View 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 Jun 15, 2021
A Kotlin lazy implementation which automatically clears itself at appropriate times in the View Lifecycle, with a Fragment example
View ViewLifecycleLazy.kt
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 Sep 1, 2021
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
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
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
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 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 Feb 19, 2018
Kotlin Lifecycle-aware fields
View LifecycleDelegate.kt
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 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) {
onItem(t)
}
})
}
@jamiesanson
jamiesanson / LSTM_chopra.py
Created Nov 14, 2017
LSTM text-gen implementation adapted from Keras Example
View LSTM_chopra.py
'''
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