Skip to content

Instantly share code, notes, and snippets.

@polson
polson / Oauth1SigningInterceptor.kt
Last active January 30, 2023 13:36 — forked from JakeWharton/Oauth1SigningInterceptor.java
An OkHttp interceptor written in Kotlin that does OAuth 1.0a signing
/*
* Copyright (C) 2015 Jake Wharton
* Modified work Copyright 2019 Phil Olson
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@polson
polson / SmoothListAdapter.kt
Last active December 6, 2020 07:23
A ListAdapter that pre-caches its views on creation
class SmoothListAdapter(val context: Context) : ListAdapter<ListItem, ListItemViewHolder>(MyDiffCallback()) {
data class ListItem(val id: String, val text: String)
class ListItemViewHolder(view: View) : ViewHolder(view) {
fun populateFrom(listItem: ListItem) {
//TODO: populate your view
}
}
@polson
polson / UIJobScheduler.kt
Last active April 28, 2020 23:27
A job scheduler to process UI jobs without skipping frames
object UIJobScheduler {
private const val MAX_JOB_TIME_MS: Float = 4f
private var elapsed = 0L
private val jobQueue = ArrayDeque<() -> Unit>()
private val isOverMaxTime get() = elapsed > MAX_JOB_TIME_MS * 1_000_000
private val handler = Handler()
fun submitJob(job: () -> Unit) {
jobQueue.add(job)
@polson
polson / MyView.kt
Last active May 27, 2019 06:15
Gist demonstrating how to get an ViewModel from your custom view
class MyView(context: Context) : FrameLayout(context) {
private val viewModel by lazy { setupViewModel() }
private val activity by lazy { scanForActivity(context) }
private fun scanForActivity(context: Context?): FragmentActivity = when (context) {
is FragmentActivity -> context
is ContextWrapper -> scanForActivity(context.baseContext)
else -> throw IllegalArgumentException("Context must be a FragmentActivity!")
}
@Suppress("UNCHECKED_CAST")
private inline fun <reified T : ViewModel> createViewModel(key: String, crossinline initializer: () -> T): T {
val factory = object : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T = initializer() as T
}
return ViewModelProviders.of(activity, factory).get(key, T::class.java)
}
@polson
polson / ActivityFromContext.kt
Created May 22, 2019 17:18
Method to get an activity instance from a context
private fun scanForActivity(context: Context?): FragmentActivity? = when (context) {
is FragmentActivity -> context
is ContextWrapper -> scanForActivity(context.baseContext)
else -> throw IllegalArgumentException("Context must be a FragmentActivity!")
}
@polson
polson / recycler_view_with_header.xml
Last active July 11, 2019 21:04
Example of how to add a header to a RecyclerView in XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
/**
* Provides a single instance of a class requiring a context. Can be inherited by a companion
* object to easily make it a singleton
*/
abstract class SingletonWithContext<T> {
@Volatile
private var instance: T? = null
fun getInstance(context: Context): T =
instance ?: synchronized(this) {
class NormalActivity : AppCompatActivity(), NormalDialogFragment.NormalDialogListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_normal)
val button = findViewById<Button>(R.id.normalButton)
button.setOnClickListener { showNormalDialog() }
}
private fun showNormalDialog() =
class BetterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener { showBetterDialog() }
val normalButton = findViewById<Button>(R.id.normalButton)
normalButton.setOnClickListener { launchNormalActivity() }