Skip to content

Instantly share code, notes, and snippets.

View fullkomnun's full-sized avatar

Or Noyman fullkomnun

View GitHub Profile
@fullkomnun
fullkomnun / ColorBarDrawable.java
Last active July 21, 2017 16:37
ColorBarDrawable
package ornoyman.com.colorbardrawable;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
@fullkomnun
fullkomnun / AnimatableInsetDrawable.java
Last active July 21, 2017 16:37
StarwarzCollapsingToolbarLayout.java
package android.graphics.drawable;
/*
* Copyright (C) 2008 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
@fullkomnun
fullkomnun / RecyclerViewUtils.kt
Last active August 19, 2017 18:08
A util for updating RecyclerView while making sure LayoutManager's state is properly maintained through update operation, avoiding some 'Inconsistency Detected' errors
@file:JvmName("RecyclerViewUtils")
package com.ornoyman.core.ui.recyclerview
import android.support.v7.widget.RecyclerView
import io.reactivex.functions.Action
/**
* Allows safely updating the backing data and notifying recycler view.
* If adapter is currently in lockdown state, will post update action to avoid illegal state error.
@fullkomnun
fullkomnun / RecursiveFlatMap.kt
Last active March 23, 2018 01:18
Implementations of flatMap that runs repeatedly as a feedback-loop when number of iterations is unknown ahead of time(e.g. pagination). First implementation is "recursive"(through deferred execution) while second one is not. The sample code tries to fetch at least 100 integers starting from 1 while each request returns the next n integers when n…
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import java.util.*
fun <T> flatMapUntil_deferred_rec(seedState: T, flat: (T) -> Single<T>, until: (T) -> Boolean): Observable<T> {
fun flatMapUntil_rec(state: T): Observable<T> =
if (until(state)) Observable.empty()
else flat(state).flatMapObservable { next -> Observable.just(next).concatWith(flatMapUntil_deferred_rec(next)) }
return flatMapUntil_rec(seedState).replay().refCount()
@fullkomnun
fullkomnun / ContextUtil.kt
Last active December 3, 2017 07:32
Get activity from context - iterative vs tail recursion
private fun Context?.findActivity_iter(): Activity? {
var context = this
while (context is ContextWrapper) {
if (context is Activity) {
return context
}
context = context.baseContext
}
return null
}
@fullkomnun
fullkomnun / FallthroughRecursiveWhen.kt
Last active December 3, 2017 16:04
switch case fallthrough-like recursive flow
fun verify(driver: TestDriver) {
// a switch case fallthrough-like recursive flow
tailrec fun verify(verification: Verification) {
when (verification) {
NO_VERIFICATION -> return // THE END
VERIFY_PHASE_1 -> verifyPhase1(driver)
VERIFY_PHASE_2 -> verifyPhase2(driver)
VERIFY_PHASE_3 -> verifyPhase3(driver)
VERIFY_PHASE_4 -> verifyPhase4(driver)
}
private fun prefetchData(): Completable =
Completable.mergeArray(
async1().concatWith(async2()).concatWith(async3().mergeWith(async4())),
async1().concatWith(async5()),
async6().concatWith(async7()))
fun async1(): Completable { TODO("not implemented") }
fun async2(): Completable { TODO("not implemented") }
fun async3(): Completable { TODO("not implemented") }
fun async4(): Completable { TODO("not implemented") }
@fullkomnun
fullkomnun / DebugCallAdapterFactory.kt
Last active August 28, 2018 15:31
A retrofit CallAdapterFactory that attaches the original service method name to the corresponding request by using it's tag property. For debugging purposes only.
class DebugCallAdapterFactory(private val callAdapterFactories: List<CallAdapter.Factory>) : CallAdapter.Factory() {
override fun get(returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit): CallAdapter<out Any, out Any>? =
callAdapterFactories.asSequence()
.map { it.get(returnType, annotations, retrofit) }
.filterNotNull().firstOrNull()?.let { WrappingCallAdapter(it, retrofit) }
}
private class WrappingCallAdapter<R, T>(private val adapter: CallAdapter<R, T>,
private val retrofit: Retrofit) : CallAdapter<R, T> by adapter {
@fullkomnun
fullkomnun / RxErrorHandlingCallAdapterFactory.kt
Created February 6, 2018 10:17
Wraps retrofit reactive calls so that retrofit errors can be traced back to client code that invoked the operation
class RxErrorHandlingCallAdapterFactory private constructor(private val original: RxJava2CallAdapterFactory) : CallAdapter.Factory() {
override fun get(returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit): CallAdapter<*, *>? =
original.get(returnType, annotations, retrofit)?.let {
RxCallAdapterWrapper(it as CallAdapter<Any, Any>)
}
private class RxCallAdapterWrapper(private val wrapped: CallAdapter<Any, Any>) : CallAdapter<Any, Any> by wrapped {
override fun adapt(call: Call<Any>): Any {
@fullkomnun
fullkomnun / Kovfefe.kt
Last active April 14, 2018 11:27
Kotlin data types simple generator - currently generates with default values naively. "Despite having to generate data types kovfefe".
inline fun <reified T : Any> generate(): T = T::class.generate()
fun <T : Any> KClass<T>.generate(): T =
when {
isData -> generateDataClass()
isTopLevelAutoValue -> generateTopLevelAutoValue()
isNestedAutoValue -> generateNestedAutoValue()
java.isEnum -> java.enumConstants.first()
else -> doGenerate() as T
}