Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Achtien nachtien

View GitHub Profile
@rishabhkohli
rishabhkohli / ViewLifecycleAware.kt
Last active Jan 13, 2021 — forked from jamiesanson/ViewLifecycleLazy.kt
A Kotlin delegated property implementation which automatically clears itself at appropriate times in the View Lifecycle of a Fragment.
View ViewLifecycleAware.kt
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
fun <T> Fragment.viewLifecycleAware(initialise: () -> T): ReadOnlyProperty<Fragment, T> =
object : ReadOnlyProperty<Fragment, T>, DefaultLifecycleObserver {
private var binding: T? = null
@jamiesanson
jamiesanson / ViewLifecycleLazy.kt
Last active Jan 20, 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 {
@ologe
ologe / PreferenceExtensions.kt
Last active Jan 12, 2021
Android shared preference observer using kotlin coroutines (1.3.0)
View PreferenceExtensions.kt
inline fun <reified T> SharedPreferences.observeKey(key: String, default: T): Flow<T> {
val flow = MutableStateFlow(getItem(key, default))
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, k ->
if (key == k) {
flow.value = getItem(key, default)!!
}
}
registerOnSharedPreferenceChangeListener(listener)
@davidair
davidair / AsyncActivity.kt
Created Nov 9, 2018
A base class activity that leverages Kotlin coroutines and allows streamlining activity invocation
View AsyncActivity.kt
/**
* Copyright 2017 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@joyrexus
joyrexus / README.md
Last active Jan 26, 2021
collapsible markdown
View README.md

collapsible markdown?

CLICK ME

yes, even hidden code blocks!

print("hello world!")
@Darep
Darep / no-comments.js
Created May 30, 2016
Remove all comments from GitHub Pull Request
View no-comments.js
// Sometimes it's necessary to do a bit of clean-up
Array.prototype.forEach.call(document.querySelectorAll('.js-comment-delete button'), function(el, i) {
el.removeAttribute('data-confirm');
el.click();
});