Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Fu alexfu

View GitHub Profile
@alexfu
alexfu / ghof
Last active Aug 22, 2022
GitHub Open File (GHOF)
View ghof
#!/usr/bin/env bash
# Prints the GitHub URL of the chosen file. Requires fzf.
FILE=$1
if [ -z "${FILE}" ]
then
FILE=$(fzf)
fi
CURRENT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
@alexfu
alexfu / DateTimeExtensions.kt
Created Jul 22, 2022
Do you wish there was an easier way to write dates in Kotlin?
View DateTimeExtensions.kt
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Month
import java.time.ZonedDateTime
import java.util.TimeZone
fun January(day: Int, year: Int) = LocalDate.of(year, Month.JANUARY, day)
fun February(day: Int, year: Int) = LocalDate.of(year, Month.FEBRUARY, day)
fun March(day: Int, year: Int) = LocalDate.of(year, Month.MARCH, day)
fun April(day: Int, year: Int) = LocalDate.of(year, Month.APRIL, day)
@alexfu
alexfu / PrefixedEditText.java
Last active Jul 11, 2022
EditText with support for prefixes.
View PrefixedEditText.java
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
/**
@alexfu
alexfu / EqualSpacingItemDecoration.java
Last active May 13, 2022
Add equal spacing to RecyclerView items automatically. Can handle horizontal, vertical, and grid display modes
View EqualSpacingItemDecoration.java
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
private final int spacing;
private int displayMode;
public static final int HORIZONTAL = 0;
@alexfu
alexfu / geocode
Created Nov 1, 2018
Bash script that geocodes a location.
View geocode
#!/bin/bash
BASE_URL="https://maps.googleapis.com/maps/api/geocode/json"
API_KEY="" # Obtain an API key from https://developers.google.com/maps/documentation/geocoding/get-api-key
ADDRESS=$1
curl -s -G $BASE_URL --data-urlencode "address=$1" --data-urlencode "key=$API_KEY" | jq '.results[] | { name: .formatted_address, latLng: "\(.geometry.location.lat),\(.geometry.location.lng)" }'
View git-add-interactive.md

git addi

Add git files interactively from the command line.

asciicast

Requirements

@alexfu
alexfu / docker-compose.yml
Last active Aug 26, 2021
Sourcegraph Docker Compose
View docker-compose.yml
version: "3.8"
services:
sourcegraph:
image: sourcegraph/server:3.29.1
container_name: leafly-sourcegraph
ports:
- 7080:7080
- 3370:3370
volumes:
- "~/.sourcegraph/config:/etc/sourcegraph"
@alexfu
alexfu / git-eject
Last active May 13, 2021
git eject
View git-eject
#!/bin/bash
# Bails on current working branch and switches to main
# Install: Place this script anywhere that is in your $PATH and make it executable (chmod +x)
BRANCH_TO_EJECT=$(git rev-parse --abbrev-ref HEAD)
git reset HEAD --hard
git checkout main
git br -D $BRANCH_TO_EJECT
@alexfu
alexfu / FragmentObserver.java
Last active Apr 6, 2021
Observer pattern for notifying Fragments of a ViewPager to update their views. This will update the current Fragment, as well as the off screen Fragments that are retained.
View FragmentObserver.java
public class FragmentObserver extends Observable {
@Override
public void notifyObservers() {
setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
super.notifyObservers();
}
}
@alexfu
alexfu / AndroidAsync.java
Last active Feb 7, 2021
Helpful RxJava functions for daily use
View AndroidAsync.java
/**
* Transforms an observable to run on a new thread
* and to be observed on Androids main thread.
*/
public static <T> Observable.Transformer<T, T> androidAsync() {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> observable) {
return observable
.subscribeOn(Schedulers.newThread())