Skip to content

Instantly share code, notes, and snippets.

//region model
sealed interface NotesUiModel : UiModel {
object Loading : NotesUiModel
data class Data(
val notes: List<Note>,
val events: EventHandler<Event>
) : NotesUiModel {
data class Note(
@jpetitto
jpetitto / MainActivity.java
Created March 6, 2018 00:09
RecyclerView Animation Bug
public class MainActivity extends AppCompatActivity {
List<Adapter.ViewHolder> listeners = new ArrayList<>();
private boolean visible = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.addItemDecoration(new SpacingItemDecoration(context, 16));
public class SpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spacing;
public SpacingItemDecoration(Context context, int padding) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
spacing = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, padding, metrics);
}
@Override
public final void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:clipToPadding="false" />
@jpetitto
jpetitto / RxGroupBy.java
Last active November 18, 2015 19:55
RxJava "groupBy" Operator
Observable.just(1, 2, 3, 4, 5)
.groupBy(i -> i % 2 == 0 ? "even" : "odd")
.subscribe(obs -> {
if (obs.getKey().equals("even")) {
obs.subscribe(i -> System.out.println("even: " + i));
} else {
obs.subscribe(i -> System.out.println("odd: " + i));
}
});
@jpetitto
jpetitto / RxDynamicPoll.java
Created November 5, 2015 16:35
Polling at Dynamic Rate with RxJava
BehaviorSubject<Integer> timerSubject = BehaviorSubject.create(1);
timerSubject
.switchMap(interval -> Observable.timer(interval, TimeUnit.SECONDS, Schedulers.newThread()))
.flatMap(i -> Observable.just(randomInt()))
.doOnNext(timerSubject::onNext)
.subscribe(System.out::println);
@jpetitto
jpetitto / BestLoop.java
Created October 29, 2015 15:41
Least Hacky Loop
List<Item> allBucket = ...
List<Item> genderBucket = ...
List<Item> sizeBucket = ...
List<Item> colorBucket = ...
List<Item> recommendations = new ArrayList<>();
picker(recommendations, limit, colorBucket, sizeBucket, genderBucket, allBucket);
private <T> int picker(List<T> src, List<T> dst, int limit) {
Iterator<T> iterator = src.iterator();
@jpetitto
jpetitto / BetterLoop.java
Created October 29, 2015 15:33
Less Hacky Loop
private <T> int picker(List<T> src, List<T> dst, int limit) {
Iterator<T> iterator = src.iterator();
while (iterator.hasNext() && limit > 0) {
dst.add(iterator.next());
limit--;
}
return limit;
}
@jpetitto
jpetitto / HackLoop.java
Last active October 29, 2015 15:20
Hack Week Loop
for (int i = 0, remaining = 5; i < colorBucket.size() && remaining > 0; i++, remaining--) {
...
}