Skip to content

Instantly share code, notes, and snippets.

View brendanw's full-sized avatar

Brendan brendanw

View GitHub Profile
@RBusarow
RBusarow / SharedFlow.kt
Last active February 17, 2020 05:03
Resettable lazy version of a shared Flow, which allows for multiple observers to consume a single emitter, with an automatic reset after the last consumer stops and an automatic resume when another consumer starts.
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
fun <T> Flow<T>.shareIn(
scope: CoroutineScope
): Flow<T> = SharedFlow(
BroadcastManager(
@Muzietto
Muzietto / MinMaxDivision.js
Created December 31, 2016 08:37
Human-readable solution for the MinMaxDivision puzzle at Codility (https://codility.com/programmers/lessons/14-binary_search_algorithm/min_max_division/)
function solution(K, M, A) {
// M is a red herring
return minimalLargeSumBinarySearch(K, A);
}
function minimalLargeSumBinarySearch(maxNumBlocks, arra) {
var lowerBoundLargeSum = Math.max.apply(null, arra);
var upperBoundLargeSum = arra.reduce((a,c)=>a+c,0);
var result = -1;
@deepak786
deepak786 / InstagramLikeColorTransition.txt
Last active April 27, 2022 14:30
Instagram Like Gradient Color Transition in Android
/******This Gist explains how to create instagram like Gradient color transition in android.******/
1. Create some gradient color drawables inside drawable Folder.
a) color1.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#c44e4e"
android:endColor="#dcb9b9"
android:angle="0"/>
@steveliles
steveliles / Foreground.java
Last active January 22, 2024 18:06
Class for detecting and eventing whether an Android app is currently foreground or background (requires API level 14+)
package com.sjl.util;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.util.List;
@davetrux
davetrux / SQLiteDebugADB
Created March 24, 2014 14:42
Verbose Logging of SQLite statements in Android
adb shell setprop log.tag.SQLiteLog V
adb shell setprop log.tag.SQLiteStatements V
adb shell stop
adb shell start
@dbachelder
dbachelder / gist:9587898
Last active December 11, 2018 21:29
style the actionbar compat searchview. inspired by this article: http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/
final SupportMenuItem searchMenuItem = (SupportMenuItem) menu.findItem(R.id.menu_search);
if (searchMenuItem == null) throw new IllegalArgumentException("menu item is null and that is very suspicious.");
this.searchView = (SearchView) searchMenuItem.getActionView();
if (searchView == null) throw new IllegalArgumentException("search view is null and that is very suspicious.");
searchView.setQueryHint(getActivity().getString(R.string.search_hint));
// wow. much hack. style search box..
Resources resources = searchView.getContext().getResources();
@cnnrhill
cnnrhill / ListViewScrollTracker.java
Created September 24, 2013 04:24
Helper class for calculating relative scroll offsets in a ListView or GridView by tracking the position of child views.
import android.util.SparseArray;
import android.widget.AbsListView;
/**
* Helper class for calculating relative scroll offsets in a ListView or GridView by tracking the
* position of child views.
*/
public class ListViewScrollTracker {
private AbsListView mListView;
private SparseArray<Integer> mPositions;