Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / App.java
Last active August 6, 2017 16:49
Does this Dagger subcomponent need an empty module in the parent component?
import dagger.BindsInstance;
import dagger.Component;
import dagger.Subcomponent;
import javax.inject.Inject;
final class App {
private final Sub.Builder subBuilder;
private final AppDependency appDependency;
@Inject App(AppDependency appDependency, Sub.Builder subBuilder) {
@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
class View {
Data data;
void setData(Data data) {
this.data = data;
setOnClickListener(v -> run(data));
}
}
class View {
Data data;
@NightlyNexus
NightlyNexus / AmazonS3RequestFactory.java
Created March 11, 2017 20:34
Creates okhttp3.Requests for uploading files to an Amazon S3 storage bucket. Implements https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import okio.BufferedSource;
import okio.ByteString;