Skip to content

Instantly share code, notes, and snippets.

View brendanw's full-sized avatar

Brendan brendanw

View GitHub Profile
// Function to generate AccountSalt
import { buildPoseidon } from 'circomlibjs';
/**
* Attempt at reproducing the account salt generation that zkemail does on the backend at ->
* https://github.com/zkemail/relayer-utils/blob/aefb00fdcfbfde7fced50e5a0b086e5cc2ff64cd/src/cryptos.rs#L198
* @param email
* @param accountCode
*/
export async function generateAccountSalt(email: string, accountCode: bigint): Promise<string> {
open class BaseViewModel<T : Any, R : Any>(
initialState: T
) : CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext = Dispatchers.Main + job
protected val _state = ConflatedBroadcastChannel<T>(initialState)
val state = _state.asFlow()
val cState = state.wrap()
@brendanw
brendanw / IPlainClient.kt
Last active April 21, 2020 00:19
A hacky means for making api calls with coroutines-native-mt while we wait for ktor bugs to get fixed.
/**
* An http client to use until ktor works with multithreaded coroutines.
* This goes in a common module that ios and android clients depend on.
* We just return a string as shared components can use kotlinx serialization
* thereafter to handle response parsing.
*/
interface IPlainClient {
fun get(
baseUrl: String,
path: String,
@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.
*
@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
/**
* 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 / 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();
@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 / 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: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;