Skip to content

Instantly share code, notes, and snippets.

View SylvainHocq's full-sized avatar

Sylvain HOCQ SylvainHocq

View GitHub Profile
@SylvainHocq
SylvainHocq / OkHttp3Stack.java
Created September 25, 2016 10:20 — forked from alashow/OkHttp3Stack.java
An OkHttp backed HttpStack for Volley (okhttp3 version)
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Circle Internet Financial
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
inline fun <A, B, R> ifNotNull(a: A?, b: B?, code: (A, B) -> R) {
if (a != null && b != null) {
code(a, b)
}
}
inline fun <A, B, C, R> ifNotNull(a: A?, b: B?, c: C?, code: (A, B, C) -> R) {
if (a != null && b != null && c != null) {
code(a, b, c)
}
fun <TSource> Observable<TSource>.pairWithPrevious()
: Observable<Pair<TSource?, TSource?>> {
return this
.map {
Pair(null as TSource?, it)
}
.scan { acc: Pair<TSource?, TSource?>, current: Pair<TSource?, TSource?> ->
Pair(acc.second, current.second)
}
}
private fun composeTransformer(vararg transformers: Transformer<Any, Any>): Transformer<Any, Any> {
return Transformer { t ->
var resultObservable = t
transformers.forEach {
resultObservable = resultObservable.compose(it)
}
resultObservable
}
}
fun getReadLinesObservable(ctx: Context, fileName: String): Observable<String> {
return Observable
.create { emitter:Emitter<String> ->
val input: BufferedReader
try {
val open = ctx.assets.open(fileName)
input = BufferedReader(InputStreamReader(open))
var line = input.readLine()
while (line != null) {
emitter.onNext(line)