Skip to content

Instantly share code, notes, and snippets.

View brendanw's full-sized avatar

Brendan brendanw

View GitHub Profile
package com.simplelisp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class SimpleLisp {
//(+ 3 (+ 3 2) (+ 4 5))
package com.cinemagraph;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
@brendanw
brendanw / gist:e79d0ad972fb715e96c5
Created February 19, 2016 23:32
Detect when a ViewPager smoothScroll initialized by viewPager.setCurrentItem() has completed
import android.support.v4.view.ViewPager;
import android.view.ViewTreeObserver;
/**
* A class that can be added to a viewTreeObserver for ViewPager's in order to determine
* when a smoothScroll transition is completed by viewPager.setCurrentItem();
*/
public class PagerScrollListener implements ViewTreeObserver.OnScrollChangedListener {
private int scrollX;
private int totalChange = 0;
@brendanw
brendanw / gist:1f3c60a1e886a365c084
Created February 19, 2016 23:32
Detect when a ViewPager smoothScroll initialized by viewPager.setCurrentItem() has completed
import android.support.v4.view.ViewPager;
import android.view.ViewTreeObserver;
/**
* A class that can be added to a viewTreeObserver for ViewPager's in order to determine
* when a smoothScroll transition is completed by viewPager.setCurrentItem();
*/
public class PagerScrollListener implements ViewTreeObserver.OnScrollChangedListener {
private int scrollX;
private int totalChange = 0;
@brendanw
brendanw / StickyRxBus.java
Created August 17, 2016 06:06
RxBus with Sticky Events
/**
* An RxJava-backed EventBus class that can support sending and receiving multiple event types.
*
* Based on https://gist.github.com/benjchristensen/04eef9ca0851f3a5d7bf
*/
public class EventBus<T> {
private static EventBus<Object> INSTANCE;
private List<T> events;
@brendanw
brendanw / gist:f71557a77d2a86d87a5286bd0465e8b4
Last active February 3, 2017 18:38
RecyclerView Scroll Tracking using a Sentinel View
headerTrackScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
float sentinelYPos = adapter.getSentinelViewYPos();
if (sentinelYPos >= Utility.getScreenHeight(getContext()) || !areAuxiliaryTouchEventsEnabled) {
return;
}
float topOfListScrollY = sentinelYPos - headerHeight;
@brendanw
brendanw / gist:512f4befbb8cb664cf25285d87960720
Created February 3, 2017 01:20
Pared Down NestedScrollingParent and NestedScrollingChild
public interface NestedScrollingParent {
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);
public void onStopNestedScroll(View target);
public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed);
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
public boolean onNestedPreFling(View target, float velocityX, float velocityY);
public int getNestedScrollAxes();
/**
* Need to return true here to continue hearing updates from the child
* for the duration of the scroll
*/
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return true;
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
@brendanw
brendanw / RangeBar.kt
Created April 23, 2019 21:34
RangeBar with Two Thumbs Kotlin Implementation
/**
* RangeBar is a bar that allows a user to define a range of values by moving two thumbs.
*
* Normalized value refers to a number as it exists in a range from [min, max]
* Px value refers to a number as it exists in a range from [startX, endX]
*/
class RangeBar : View {
companion object {
// The diameter of the circle
@brendanw
brendanw / StateMachine.kt
Last active August 19, 2020 14:42
StateMachine.kt is a 34-line implementation of a Mealy state machine tested for multi-threaded coroutines in kotlin multiplatform. SearchStateMachine.kt is an example usage of the state machine; Search.kt defines the input actions and set of states. For more background, read http://brendanweinstein.com/a-statemachine-for-multithreaded-coroutines…
import com.basebeta.utility.logging.kprint
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
/**
* Only can use with coroutines 1.3.6-native-mt. See alternative implementation at the bottom for 1.3.5 and under.
*