Skip to content

Instantly share code, notes, and snippets.

@staakk
staakk / RxBackpressure.kt
Last active June 17, 2018 21:23
Processing only the latest emitted element.
import io.reactivex.BackpressureStrategy
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import java.util.concurrent.TimeUnit
fun main(args: Array<String>) {
val subject = PublishSubject.create<Int>().toSerialized()
val producer = Producer(subject)
val consumer = Consumer(subject)
@staakk
staakk / ViewModelFactory
Created November 23, 2017 13:47
Factory which allows ViewModels to be injected by Dagger2
@Singleton
public class ViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@staakk
staakk / NetModule.java
Created November 23, 2017 13:44
Dagger2 module for Retrofit with authorization, and Java 8 DateTime mappers
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.threeten.bp.LocalDateTime;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import java.io.*
class FileDownloader(
val onProgressUpdate: ((fileSize: Long, downloadedSize: Long) -> Unit)?,
val onDownloadComplete: () -> Unit) {
var bufferSize = 1024 * 8
var singleReadSize = 1024 * 4
var progressNotificationDelay = 1000
@staakk
staakk / BackgroundDrawable.java
Created September 14, 2017 12:45
[Android] Custom drawable for displaying solid color with border
import android.graphics.Canvas;
import android.graphics.Color;
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.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;