Skip to content

Instantly share code, notes, and snippets.

View lukeduncan-scot's full-sized avatar

L Duncan lukeduncan-scot

  • Highlands, Scotland
  • 06:39 (UTC +01:00)
View GitHub Profile
@lukeduncan-scot
lukeduncan-scot / AndroidManifest.xml
Last active November 21, 2018 23:32
Activity that loads a URL into a WebView
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="***">
// This is required
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@lukeduncan-scot
lukeduncan-scot / Event.kt
Created January 21, 2020 13:59
LiveData Event
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
@lukeduncan-scot
lukeduncan-scot / DataBoundAdapter.kt
Created January 21, 2020 14:00
Data Bound Adapter
abstract class DataBoundAdapter<T, V : ViewDataBinding>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, DataBoundViewHolder<V>>(diffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> {
val binding = createBinding(parent)
return DataBoundViewHolder(binding)
}
protected abstract fun createBinding(parent: ViewGroup): V
@lukeduncan-scot
lukeduncan-scot / ShootproofDownloader.kt
Created March 6, 2020 23:11
Quick Selenium script in Kotlin to download photos from Shootproof albums
package com.beflukey.shootproofdownloader
import org.openqa.selenium.By
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy
import org.openqa.selenium.support.ui.WebDriverWait
import java.io.*
import java.net.URL
import java.time.Duration

Android Audio Flinger

Requirements

  1. Introduction 1.1 Purpose 1.2 Intended Audience 1.3 Intended Use 1.4 Scope
  2. Overall Description

Keybase proof

I hereby claim:

  • I am lduncandroid on github.
  • I am lduncandroid (https://keybase.io/lduncandroid) on keybase.
  • I have a public key ASAOQZsKTkEH14w2y882fSvi50PhMuHeELEbx_-t4tTQdwo

To claim this, I am signing this object:

@lukeduncan-scot
lukeduncan-scot / Caching.kt
Created November 13, 2020 01:06
Implementing caching using Flow from Kotlin
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
// Following the RxJava implementation here: https://blog.mindorks.com/implement-caching-in-android-using-rxjava-operators
data class Data(val source: String)
class MemoryDataSource {
class LRUCache(capacity: Int) {
private val internalCache: LinkedHashMap<Int, Int> =
object : LinkedHashMap<Int, Int>(initialCapacity = capacity, loadFactor = 0.75f, accessOrder = true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, Int>?): Boolean {
return size > capacity
}
}
fun get(key: Int) = internalCache.getOrDefault(key, -1)
@lukeduncan-scot
lukeduncan-scot / iterable_sequence_count_pairs.kts
Last active May 24, 2021 22:23
Counts Pairs in Iterable & Sequence
fun <T> Iterable<T>.countPairs() = groupingBy { it }
.eachCount()
.values
.sumOf { it.div(2) }
fun <T> Sequence<T>.countPairs() = groupBy { it }
.mapValues { it.value.size }
.values
.sumOf {it.div(2)}
@lukeduncan-scot
lukeduncan-scot / ResponseBodyLogger.kt
Last active August 1, 2022 00:44
An OKHttp Interceptor that prints the response of a request to the console.
import com.squareup.okhttp.Interceptor
import com.squareup.okhttp.MediaType
import com.squareup.okhttp.Response
import com.squareup.okhttp.ResponseBody
import okio.BufferedSource
import okio.GzipSource
import okio.Okio
class ResponseBodyLogger : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {