Skip to content

Instantly share code, notes, and snippets.

View kiwiandroiddev's full-sized avatar

Matt Clarke kiwiandroiddev

  • Auckland, New Zealand
View GitHub Profile
@kiwiandroiddev
kiwiandroiddev / FooterAdapterWrapper.kt
Created June 13, 2017 04:44
Wraps any RecyclerView Adapter to add a toggleable footer item
import android.content.Context
import android.support.annotation.LayoutRes
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* Wraps any RecyclerView Adapter, adding support for a dynamically toggleable footer layout.
* It is itself a RecyclerView Adapter (i.e. it's a Decorator), allowing composability.
@kiwiandroiddev
kiwiandroiddev / activity_main.xml
Created July 4, 2017 05:51
Main activity layout for Distraction-free reading sample app Part 1
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
@kiwiandroiddev
kiwiandroiddev / MainActivity.java
Created July 4, 2017 05:53
First cut of Main activity code for Distraction-free reading sample app Part 1
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupToolbar();
}
@kiwiandroiddev
kiwiandroiddev / asyncCombineLatestRxJava2.kt
Created May 28, 2018 03:03
Async combine latest operator implementation for RxJava2 in Kotlin
fun <T1, T2, T3, R> asyncCombineLatest(o1: Observable<T1>,
o2: Observable<T2>,
o3: Observable<T3>,
asyncCombineFunc: (T1, T2, T3) -> Observable<R>): Observable<R> =
Observable.combineLatest(o1, o2, o3,
Function3<T1, T2, T3, Observable<R>> { t1, t2, t3 -> asyncCombineFunc(t1, t2, t3) }
).flatMap { it }
fun <T1, T2, R> asyncZip(source1: Single<T1>,
source2: Single<T2>,
zipper: (T1, T2) -> Single<R>): Single<R> =
Single.zip(
source1, source2,
io.reactivex.functions.BiFunction<T1, T2, Single<R>> { t1, t2 -> zipper(t1, t2) }
).flatMap { it }
@kiwiandroiddev
kiwiandroiddev / RxWebSocketAdapter.kt
Created October 12, 2018 06:52
Converts OkHttp's callback interface for WebSockets into an RxJava Observable of events
import io.reactivex.Observable
import okhttp3.*
import okio.ByteString
fun OkHttpClient.newWebSocketObservable(serverUrl: String): Observable<WebSocketEvent> {
val request = Request.Builder().url(serverUrl).build()
return newWebSocketObservable(request)
}
fun OkHttpClient.newWebSocketObservable(request: Request): Observable<WebSocketEvent> {
@kiwiandroiddev
kiwiandroiddev / rxThrottleMatchingItems.kt
Last active October 19, 2018 07:44
Selectively throttles items in an observable stream
fun <T> Observable<T>.throttleMatchingItems(
windowDuration: Long,
unit: TimeUnit,
predicate: (T) -> Boolean
): Observable<T> {
return this.compose { stream ->
val throttledItems = stream
.filter(predicate)
.throttleFirst(windowDuration, unit)
val allOtherItems = stream.filter { !predicate(it) }
@kiwiandroiddev
kiwiandroiddev / rxFilterWithBiPredicate.kt
Created February 4, 2019 06:15
Allows a stream to be filtered with a predicate that evaluates the latest and previous items.
/**
* Filters items emitted by an observable by only emitting those that satisfy the specified
* predicate based on the latest and previous items emitted.
* Note that this will always filter out the first item of the stream since in this case there
* is as of yet no previous item to compare it to.
*
* @param bipredicate
* a function that evaluates the current and previous item emitted by the source ObservableSource, returning {@code true}
* if it passes the filter
* @return an Observable that emits only those items emitted by the source ObservableSource that the filter
@kiwiandroiddev
kiwiandroiddev / StringFindAllInstances.kt
Created July 11, 2019 00:16
Kotlin extension function on String to find all instances of given substring within. Results are in the form of a list of index pairs.
/**
* Find all instances of given substring in this string.
* Results are in the form of a list of index pairs (start and end index of that particular match).
*/
fun String.findAllInstancesOf(
subString: String,
startIndex: Int = 0
): List<Pair<Int, Int>> {
val index = this.indexOf(subString, startIndex)
if (index == -1) return emptyList()
@kiwiandroiddev
kiwiandroiddev / CartesianProduct.kt
Created August 23, 2019 01:58
Kotlin function to return the cartesian product of two collections
/**
* E.g.
* cartesianProduct(listOf(1, 2, 3), listOf(true, false)) returns
* [(1, true), (1, false), (2, true), (2, false), (3, true), (3, false)]
*/
fun <T, U> cartesianProduct(c1: Collection<T>, c2: Collection<U>): List<Pair<T, U>> {
return c1.flatMap { lhsElem -> c2.map { rhsElem -> lhsElem to rhsElem } }
}