Skip to content

Instantly share code, notes, and snippets.

@NightlyNexus
NightlyNexus / OkHttpStack.java
Created May 13, 2016 21:27
OkHttp 3 implementation of Volley's HttpStack
/*
* Copyright (C) 2016 Eric Cochran
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@NightlyNexus
NightlyNexus / EnumWithDefaultValueJsonAdapter.java
Last active November 3, 2022 17:58
Update: This is now in the Moshi adapters artifact. https://github.com/square/moshi/blob/5153295988e09e6a1bfe76eecff8b22bf49e25de/adapters/src/main/java/com/squareup/moshi/adapters/EnumJsonAdapter.java An enum JsonAdapter for Moshi that allows for a fallback value when deserializing unknown strings. NOTE: Allows null for the default.
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.IOException;
public final class EnumWithDefaultValueJsonAdapter<T extends Enum<T>> extends JsonAdapter<T> {
private final Class<T> enumType;
private final String[] nameStrings;
private final T[] constants;
@NightlyNexus
NightlyNexus / RangeInputFilter.kt
Created November 2, 2022 16:51
An Android InputFilter for a decimal range
import android.text.InputFilter
import android.text.Spanned
class RangeInputFilter(private val min: Int, private val max: Int) : InputFilter {
override fun filter(
source: CharSequence,
start: Int,
end: Int,
dest: Spanned,
dstart: Int,
@NightlyNexus
NightlyNexus / flashlight.lua
Created September 17, 2021 01:10
A script for the Valve flashlight, meant for VR. This fixes bugs, adds some small features, and cleans up the code from the sample here: https://developer.valvesoftware.com/wiki/SteamVR/Environments/Scripting/flashlight_script_code_full
-- Disable this flag to remove the glow particle around the flashlight's lens.
local enableGlow = true;
local glowParticle;
local lightTable = {
classname = "light_spot",
targetname = "light" .. thisEntity:entindex(),
enabled = 0,
brightness = "1.5",
range = "400",
@NightlyNexus
NightlyNexus / MarriageProblemSolver.kt
Created August 28, 2021 01:09
A Kotlin implementation of the Gale–Shapley algorithm for the stable marriage problem.
import MarriageProblemSolver.Person
class MarriageProblemSolver {
class Person(val name: String) {
internal lateinit var preferences: List<Person>
internal var proposals = 0
var match: Person? = null
internal set
// Do a lateinit to allow the circular dependency of people with their preferences of people.
@NightlyNexus
NightlyNexus / ConverterCallAdapterFactory.java
Last active September 11, 2018 10:47
ConverterCallAdapterFactory uses Retrofit's CallAdapter to pass the okhttp3.Request to a serializer like Jsoup. Idea and corrections from https://github.com/square/retrofit/issues/2267/.
/*
* Copyright (C) 2017 Eric Cochran
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@NightlyNexus
NightlyNexus / TypeNames.java
Created March 13, 2018 09:02
A helper to add @generated to a JavaPoet's TypeSpec
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.TypeSpec;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import javax.lang.model.SourceVersion;
@NightlyNexus
NightlyNexus / DelegatingEventListenerFactory.java
Last active January 26, 2018 08:10
A DelegatingEventListenerFactory for OkHttp to allow new OkHttpClient instances to add onto a base client's EventListener.Factory.
/*
* Copyright (C) 2018 Eric Cochran
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@NightlyNexus
NightlyNexus / AnalyticsNetworkLogger.java
Last active November 30, 2017 05:53
A Retrofit 2 Call Adapter Factory for logging. This mostly exists because Interceptors do not know if the Call was only canceled when the request failed (https://github.com/square/okhttp/issues/3039). It is the responsibility of the Logger to check for canceled failed calls. The AnalyticsNetworkLogger is only a sample implementation of the logge…
/*
* Copyright (C) 2016 Eric Cochran
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
public final class AutoRetryCallAdapterFactory extends CallAdapter.Factory {
final ArrayList<Action<?>> actions = new ArrayList<>();
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean connected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (!connected) {
return;
}