Skip to content

Instantly share code, notes, and snippets.

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
class BiSourceMergerLiveData<T, U, R>(
source1: LiveData<T>,
source2: LiveData<U>,
block: (T?, U?) -> R?
) : MediatorLiveData<R>() {
@abdalin
abdalin / Event.kt
Last active June 9, 2021 09:54
A wrapper for one shot use data representing an event. The consumers of this event can provide a scope to let each consumer consume it only once
open class Event<out T>(private val data: T) {
/**
* Collection of distinct scopes this [Event] has been consumed with
*/
private val consumedScopes by lazy { hashSetOf<String>() }
/**
* Checks if this [Event] is consumed for the given scope
*/
@abdalin
abdalin / MultiComparator.java
Created July 3, 2018 16:06
java special aggregate of multiple comparators
class MultiComparator<T> implements Comparator<T> {
private final List<Comparator<T>> comparators;
public MultiComparator(List<Comparator<T>> comparators) {
this.comparators = comparators;
}
public MultiComparator(Comparator<T>... comparators) {
this(Arrays.asList(comparators));
@abdalin
abdalin / DividerItemDecorator.java
Created April 8, 2018 21:18
divider item decoration for recyclerview with space removed after last item
public class DividerItemDecorator extends ItemDecoration {
public static final int HORIZONTAL = LinearLayout.HORIZONTAL;
public static final int VERTICAL = LinearLayout.VERTICAL;
private Drawable mDivider;
private int mOrientation;
private final Rect mBounds = new Rect();
public DividerItemDecorator(Drawable divider, int orientation) {
@abdalin
abdalin / RecyclerViewSnapListener.java
Last active April 8, 2018 21:36
listener to get notified using LinearSnapHelper with RecyclerView
private void attachSnapHelper(RecyclerView recyclerView, final RecyclerView.LayoutManager layoutManager) {
final SnapHelper snapHelper = new LinearSnapHelper();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
boolean mScrolled = false;
int snapPosition;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);