Skip to content

Instantly share code, notes, and snippets.

Avatar

Lucio Maciel luciofm

View GitHub Profile
@luciofm
luciofm / DebounceLiveData.kt
Created May 13, 2019
A Debouncing LiveData helper
View DebounceLiveData.kt
class DebounceLiveData<Source>(
private val source: LiveData<Source>,
private val debounceMs: Long
) : LiveData<Source>(), CoroutineScope {
private val job = SupervisorJob()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
private var debounceJob: Job? = null
private val observer = Observer<Source> { source ->
@luciofm
luciofm / batch.kt
Last active Nov 26, 2018
Batch actor with coroutines
View batch.kt
private inline fun <T> createBatchActor(context: CoroutineContext = CommonPool,
parent: Job? = null,
maxSize: Int = 100,
maxTime: Int = 500,
crossinline block: (List<T>) -> Unit): SendChannel<T> {
return actor(context, parent = parent) {
val batch = ArrayList<T>(maxSize)
var deadline = 0L // deadline for sending this batch to callback block
while(true) {
@luciofm
luciofm / ChatRoomsFragment.kt
Last active Jun 13, 2020
How to run LiveData transformations on a coroutine
View ChatRoomsFragment.kt
class ChatRoomsFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProviders.of(this, factory).get(ChatRoomsViewModel::class.java)
subscribeUi()
}
private fun subscribeUi() {
@luciofm
luciofm / AlphaSpan.kt
Created Apr 26, 2018
Animate alhpa of each character separately
View AlphaSpan.kt
package com.luciofm.playground
import android.text.TextPaint
import android.text.style.CharacterStyle
import android.text.style.UpdateAppearance
class AlphaSpan(var alpha: Int) : CharacterStyle(), UpdateAppearance {
override fun updateDrawState(ds: TextPaint) {
ds.alpha = alpha
View FallbackSealedClass.kt
/*
* Copyright 2018 Lucio Maciel, Rocket.Chat
* Copyright 2016 Serj Lotutovici
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
View AmazonS3RequestFactory.java
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import okio.BufferedSource;
import okio.ByteString;
@luciofm
luciofm / build.gradle
Created Dec 2, 2015
Auto increment version number on release builds... You can change the build type on versionCode.gradle, you also will need to commit and push gradle.properties on your CI
View build.gradle
apply from: 'versionCode.gradle'
android {
defaultConfig {
versionName VERSION_NAME
versionCode Integer.parseInt(VERSION_CODE)
}
}
@luciofm
luciofm / Pager.java
Created Nov 4, 2015 — forked from mttkay/Pager.java
A simple Rx based pager
View Pager.java
public class Pager<I, O> {
private static final Observable FINISH_SEQUENCE = Observable.never();
private PublishSubject<Observable<I>> pages;
private Observable<I> nextPage = finish();
private Subscription subscription = Subscriptions.empty();
private final PagingFunction<I> pagingFunction;
private final Func1<I, O> pageTransformer;
View twoway-view stacktrace
E Process: org.lucasr.twowayview.sample, PID: 7606
E java.lang.NullPointerException: Attempt to read from field 'int org.lucasr.twowayview.widget.TWSpannableGridLayoutManager$SpannableItemEntry.rowSpan' on a
null object reference
E at org.lucasr.twowayview.widget.TWSpannableGridLayoutManager$SpannableItemEntry.access$100(TWSpannableGridLayoutManager.java:40)
E at org.lucasr.twowayview.widget.TWSpannableGridLayoutManager.getFirstChildCountInLanes(TWSpannableGridLayoutManager.java:223)
E at org.lucasr.twowayview.widget.TWSpannableGridLayoutManager.moveLayoutToPosition(TWSpannableGridLayoutManager.java:262)
E at org.lucasr.twowayview.widget.TWLanedLayoutManager.onLayoutChildren(TWLanedLayoutManager.java:236)
E at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1461)
E at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:1600)
E at android.view.View.layout(View.java:15273)
You can’t perform that action at this time.