Skip to content

Instantly share code, notes, and snippets.

View ZacSweers's full-sized avatar

Zac Sweers ZacSweers

View GitHub Profile
@ZacSweers
ZacSweers / RxponetialBackoff
Created February 23, 2016 01:03 — forked from sddamico/LICENSE
Exponential Backoff Transformer
/**
* @param interval The base interval to start backing off from. The function is: attemptNum^2 * intervalTime
* @param units The units for interval
* @param retryAttempts The max number of attempts to retry this task or -1 to try MAX_INT times,
*/
public static <T> Observable.Transformer<T, T> backoff(final long interval, final TimeUnit units, final int retryAttempts) {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(final Observable<T> observable) {
return observable.retryWhen(
public class MenuPreference extends ListPreference {
private View anchor;
public MenuPreference(Context context) {
super(context);
}
public MenuPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@ZacSweers
ZacSweers / ReusableInjection.kt
Last active October 2, 2017 14:26
Reusable injections
class Parent {
// Somewhere in here, a lot of external service modules are included and contribute their `Service` impls to this multibinding
@Inject lateinit var services: Map<String, Provider<Service>>
fun showChild() {
addChildController(serviceKey = "barServiceKey") // Key into the services map
}
}
@ZacSweers
ZacSweers / Sample.kt
Created November 19, 2017 02:05
Stars in notifications
fun notification() {
val channelId = "stars"
val notificationManager = activity.getSystemService<NotificationManager>()
if (VERSION.SDK_INT >= VERSION_CODES.O) {
val channels = notificationManager.notificationChannels
if (channels.none { it.id == channelId }) {
NotificationChannel(
channelId, "stars", NotificationManager.IMPORTANCE_HIGH)
.apply {
description = "Demo"
val asyncMainThreadScheduler = AndroidSchedulers.from(Looper.getMainLooper(), true)
RxAndroidPlugins.setInitMainThreadSchedulerHandler { asyncMainThreadScheduler }
// Or if the default scheduler is already initialiazed
RxAndroidPlugins.setMainThreadSchedulerHandler { asyncMainThreadScheduler }
Scheduler asyncMainThreadScheduler = AndroidSchedulers.from(Looper.getMainLooper(), true);
RxAndroidPlugins.setInitMainThreadSchedulerHandler(callable -> asyncMainThreadScheduler);
// Or if the default scheduler is already initialiazed
RxAndroidPlugins.setMainThreadSchedulerHandler(scheduler -> asyncMainThreadScheduler);
@ZacSweers
ZacSweers / RxJava.md
Last active September 3, 2018 18:52 — forked from cesarferreira/RxJava.md
Party tricks with RxJava, RxAndroid & Retrolambda

View Click

Instead of the verbose setOnClickListener:

RxView.clicks(submitButton).subscribe(o -> log("submit button clicked!"));

Filter even numbers

Observable
    .just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
@ZacSweers
ZacSweers / GuardTest.kt
Last active September 14, 2018 23:05
Demo of how the Nothing type in Kotlin can allow a Swift-style guard function
import com.google.common.truth.Truth.assertThat
import org.junit.Test
inline fun <T> guard(receiver: T?, block: () -> Nothing): T {
if (receiver == null) {
block()
}
return receiver
}
import android.support.v4.view.animation.PathInterpolatorCompat;
import android.view.animation.Interpolator;
/**
* Cheatsheet: http://easings.net/
*/
public class EasingsConstants {
public static final Interpolator easeInSine = PathInterpolatorCompat.create(0.47f, 0f, 0.745f, 0.715f);
public static final Interpolator easeOutSine = PathInterpolatorCompat.create(0.39f, 0.575f, 0.565f, 1f);
public static final Interpolator easeInOutSine = PathInterpolatorCompat.create(0.445f, 0.05f, 0.55f, 0.95f);
> Task :compileJava
warning: [options] bootstrap class path not set in conjunction with -source 1.6
warning: [options] source value 1.6 is obsolete and will be removed in a future release
warning: [options] target value 1.6 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
/Users/zsweers/dev/java/contributing/RxJava/src/main/java/io/reactivex/Single.java:135: error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
return RxJavaPlugins.onAssembly(new SingleAmb<T>(null, sources));
^
(see http://t.uber.com/nullaway )
/Users/zsweers/dev/java/contributing/RxJava/src/main/java/io/reactivex/Single.java:163: error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required