Skip to content

Instantly share code, notes, and snippets.

@wispborne
wispborne / ViewOnceData.kt
Last active October 3, 2018 08:14
A class that encapsulates data, adding ability to know when the data has already been viewed. Useful for putting one-shot events, like "display dialog", into a State object that can be reapplied to a view.
import java.util.concurrent.atomic.AtomicBoolean
/**
* Wraps some data that should track when it has been viewed and allow future viewers to avoid a second showing.
* Useful for showing messages in the app that auto-hide because this class can exist in the app's state.
*/
data class ViewOnceData<T>(private val data: T) {
val wasViewed = AtomicBoolean(false)
/**
@wispborne
wispborne / ObservableActivityForResult.java
Last active December 16, 2020 19:10
RxJava wrapper for startActivityForResult and onActivityResult flow. Don't break the chain!
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import com.mywebgrocer.util.ActivityResultData;
import java.util.Random;
class Barker : NoiseMaker {
override fun makeSound() {
println("Bark!")
}
}
@wispborne
wispborne / Preferences.kt
Last active July 21, 2022 03:47
Android SharedPreferences helper class for Kotlin. Easy-to-use delegated properties, automatic database creation, and listening for property changes.
import android.content.Context
import android.content.SharedPreferences
import kotlin.reflect.KProperty
/**
* Represents a single [SharedPreferences] file.
*
* Usage:
*
* ```kotlin
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import com.jakewharton.rxrelay.PublishRelay;
import java.util.List;
import rx.Observable;
/**
public void onCreate() {
// Http client
OkHttpClient client = new OkHttpClient();
// Content type
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
// Whatever you want to send, here it's json
RequestBody body = RequestBody.create(JSON, yourJsonString);
package com.thunderclouddev.changelogs.ui
import android.content.Context
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.annotation.StyleRes
import android.support.v7.app.AppCompatActivity
import com.thunderclouddev.changelogs.BaseApplication
import com.thunderclouddev.changelogs.R
import com.thunderclouddev.changelogs.configuration.LanguagePreference
@wispborne
wispborne / KotlinComparisonClass.java
Last active August 17, 2020 19:42
Comparison of Java to Kotlin for various simple methods.
/** Example of a {@link Class} declaration */
public final class MyClass {
private boolean someBool;
public MyClass() {
}
public MyClass(boolean someBool) {
this.someBool = someBool;
}
public void signIn(User user) {
api.signIn(user, onSuccessCallback: {
getLinks(user);
}, onErrorCallback(): {
reauthorize(user);
}
public void getLinks(User user) {
api.getLinks(user, onSuccessCallback: {
postSuccessEvent();
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.v4.graphics.drawable.DrawableCompat
/**
* Created by David Whitman
*/
object TintUtils {