Skip to content

Instantly share code, notes, and snippets.

View justasm's full-sized avatar

Justas Medeišis justasm

View GitHub Profile
@justasm
justasm / AuthorizationInterceptor.kt
Created February 12, 2021 12:57
OkHttp Interceptor to retry 401 requests after a token refresh
import java.io.IOException
import java.net.HttpURLConnection.HTTP_UNAUTHORIZED
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
class AuthorizationInterceptor(private val delegate: TokenDelegate) : Interceptor {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
@justasm
justasm / MobileDataOnlyInterceptor.kt
Created March 20, 2020 12:47
OkHttp Interceptor that ensures requests happen on mobile data even if WiFi is on
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.content.ContextCompat
@justasm
justasm / AndroidDebounced.kt
Last active August 22, 2018 12:23
Debounced property delegate Android implementation
import android.os.Handler
fun <T> debounced(initialValue: T, debounceMs: Long = 500L): Debounced<T> {
return AndroidDebounced(initialValue, debounceMs)
}
private class AndroidDebounced<T>(initialValue: T, private val debounceMs: Long) : Debounced<T> {
private val handler = Handler()
private var _value: T = initialValue
@justasm
justasm / LegacyLineBreak.kt
Created August 14, 2018 13:42
Soft-hyphen (U+00AD) support for API <21
/*
* Copyright (C) 2006 The Android Open Source Project
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
@justasm
justasm / CenterVerticalDrawableSpan.kt
Created June 19, 2018 00:34
Image / drawable span that is vertically centered relative to its surrounding text
private abstract class CenterVerticalDrawableSpan : ReplacementSpan() {
abstract fun getDrawable(): Drawable?
private var drawableRef: WeakReference<Drawable>? = null
private val cachedDrawable: Drawable?
get() = drawableRef?.get() ?: getDrawable()?.also { drawableRef = WeakReference(it) }
override fun getSize(paint: Paint, text: CharSequence,
@justasm
justasm / LayoutExtensions.kt
Last active November 4, 2017 00:28
Android View Kotlin extension for a one-shot callback after layout
fun View.afterLayout(block: View.() -> Unit) {
val originalVto = viewTreeObserver
originalVto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
if (originalVto.isAlive) originalVto.removeOnPreDrawListener(this)
viewTreeObserver.removeOnPreDrawListener(this)
block(this@afterLayout)
return true
}
})
@justasm
justasm / LazyMutable.kt
Created August 8, 2017 08:39
Kotlin Lazy for mutable values
@file:Suppress("RedundantVisibilityModifier", "NOTHING_TO_INLINE")
import kotlin.reflect.KProperty
public interface LazyMutable<T> {
public var value: T
public fun isInitialized(): Boolean
}
public inline operator fun <T> LazyMutable<T>.getValue(thisRef: Any?, property: KProperty<*>): T {
@justasm
justasm / app_build.gradle
Last active May 15, 2020 17:55
Kotlin Android Extensions with Android library module https://youtrack.jetbrains.com/issue/KT-16934
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.kxt"
minSdkVersion 15
@justasm
justasm / CircleTransformation.java
Created August 15, 2016 19:34
Picasso CircleTransformation
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import com.squareup.picasso.Transformation;
public class CircleTransformation implements Transformation {
@justasm
justasm / VTKLoader.java
Created February 21, 2016 21:17
Legacy VTK file loader. Decodes POLYDATA datasets from binary or ASCII VTK files to vertex, index and normal float buffers.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;