Skip to content

Instantly share code, notes, and snippets.

Justas Medeišis justasm

Block or report user

Report or block justasm

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@justasm
justasm / AndroidDebounced.kt
Last active Aug 22, 2018
Debounced property delegate Android implementation
View AndroidDebounced.kt
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 Aug 14, 2018
Soft-hyphen (U+00AD) support for API <21
View LegacyLineBreak.kt
/*
* 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 Jun 19, 2018
Image / drawable span that is vertically centered relative to its surrounding text
View CenterVerticalDrawableSpan.kt
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 Nov 4, 2017
Android View Kotlin extension for a one-shot callback after layout
View LayoutExtensions.kt
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 Aug 8, 2017
Kotlin Lazy for mutable values
View LazyMutable.kt
@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
Created Mar 9, 2017
Kotlin Android Extensions with Android library module
View app_build.gradle
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 Aug 15, 2016
Picasso CircleTransformation
View CircleTransformation.java
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 Feb 21, 2016
Legacy VTK file loader. Decodes POLYDATA datasets from binary or ASCII VTK files to vertex, index and normal float buffers.
View VTKLoader.java
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;
@justasm
justasm / NonPersistentCookieJar.java
Last active Apr 21, 2019
OkHttp 3 non-persistent CookieJar with an ACCEPT_ALL policy
View NonPersistentCookieJar.java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
@justasm
justasm / HorSwipeAwareRelativeLayout.java
Last active Aug 29, 2015
Android detecting horizontal swipe gestures. Designed for use in layout parent for top-level back navigation, but easily made more flexible.
View HorSwipeAwareRelativeLayout.java
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.RelativeLayout;
You can’t perform that action at this time.