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 / 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 }
@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 / 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 / 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 / adb_airplane_mode_on.sh
Created January 4, 2017 00:47
Bash script to enable Airplane mode on the running Android emulator
#!/bin/bash
# adb must be running in root mode to broadcast intents
adb -e root
adb -e shell settings put global airplane_mode_on 1
# radios won't actually turn off until this is broadcasted
adb -e shell am broadcast -a android.intent.action.AIRPLANE_MODE
@kiwiandroiddev
kiwiandroiddev / PredicateTextWatcher.kt
Created October 19, 2016 03:52
Android TextWatcher to restrict user input to match a given predicate
/**
* Add this to (e.g.) an EditText via addTextChangedListener() to only allow user input that
* is accepted by the supplied predicate.
*
* e.g.
* emailEditText.addTextChangedListener(PredicateTextWatcher({ presenter.isValidEmail(it) })
*
* Inspired by original Java code here: http://stackoverflow.com/a/11545229/1405990
*/
class PredicateTextWatcher(val predicate : (String) -> Boolean) : TextWatcher {
@kiwiandroiddev
kiwiandroiddev / RegexMaskTextWatcher.kt
Last active October 1, 2021 09:22
Android TextWatcher to restrict user input to match a given Regular Expression
/**
* Add this to (e.g.) an EditText via addTextChangedListener() to prevent any user input
* that doesn't match its supplied regex.
*
* Inspired by original Java code here: http://stackoverflow.com/a/11545229/1405990
*/
class RegexMaskTextWatcher(regexForInputToMatch : String) : TextWatcher {
private val regex = Pattern.compile(regexForInputToMatch)
private var previousText: String = ""
@kiwiandroiddev
kiwiandroiddev / RxJavaExtensions.kt
Created October 3, 2016 05:20
filterNotNull for RxJava in Kotlin
/**
* Asynchronous equivalent of filterNotNull in Kotlin's stdlin (kotlin.collections)
*/
fun <T> Observable<T?>.filterNotNull() : Observable<T> {
return this.filter { item -> item != null }.map { it!! }
}
@kiwiandroiddev
kiwiandroiddev / BouncyButton.java
Created August 15, 2016 10:32
Custom Android Button that makes use of Facebook's Rebound library to animate changes to its enabled state.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;
import com.facebook.rebound.BaseSpringSystem;
import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.facebook.rebound.SpringUtil;
@kiwiandroiddev
kiwiandroiddev / kill_adb_alias.sh
Last active September 9, 2016 02:22
Bash command to force-kill Genymotion and your ADB server (sometimes necessary after an Android Studio Invalidate caches/restart; Genymotion will restart the problematic adb server unless it is killed first)
function kill_one_ps() {
ps -A | grep $1 | egrep -m1 -o '^\d+' | xargs kill -9
}
function kill_all_ps() {
ps -A | grep $1 | egrep -o '^\d+' | xargs kill -9
}
# aliases
alias kill_adb="kill_all_ps Genymotion ; kill_one_ps 'adb.*server'"