Skip to content

Instantly share code, notes, and snippets.

Avatar

Rafa Vázquez Sloy

  • Adevinta Spain (InfoJobs)
  • Remote
  • Twitter @sloydev
View GitHub Profile
@Sloy
Sloy / dart-futures.dart
Created May 7, 2021
Playing with dart async/await delayed
View dart-futures.dart
void main() async {
Stopwatch stopwatch = new Stopwatch()..start();
var p1 = delay(Duration(seconds: 2));
var p2 = delay(Duration(seconds: 4));
await p1;
await p2;
print('Executed in ${stopwatch.elapsed}');
View FirebasePerformanceWrapper.kt
import com.google.firebase.perf.FirebasePerformance
object FirebasePerformanceWrapper {
fun <T> trace(name: String, block: () -> T): T {
val trace = runCatching { FirebasePerformance.getInstance().newTrace(name) }.getOrNull()
trace?.start()
val result = block()
trace?.stop()
return result
View Futures.java
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class SyncAsync {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
@Sloy
Sloy / Most changed java files
Created Jul 24, 2019
Print the 10 Java files most changed since a given date
View Most changed java files
git log --after={2019-06-01} --pretty=format: --name-only | sort | uniq -c | sort -rg | grep "\.java" | head -10
@Sloy
Sloy / gist:d45b88101e0ffac0e6a6d89988988490
Created Nov 12, 2018
Most changed files in a git repo by number of commits
View gist:d45b88101e0ffac0e6a6d89988988490
git log --name-status $* --after='2018-01-01 00:00:00' | \
grep -E '^[A-Z]\s+' | \
cut -c3-500 | \
sort | \
uniq -c | \
grep -vE '^ {6}1 ' | \
sort -n | \
grep -E '.+\.java?' | \
tail -n 20 | \
sort -n -r
@Sloy
Sloy / fibonacci.kt
Last active Oct 31, 2018
Fibonacci Dependencies
View fibonacci.kt
fun fibKotlinClass(n: Int): String {
return when (n) {
1, 2 -> "class Fib$n"
else -> "class Fib$n(val fibM1: Fib${n - 1}, val fibM2: Fib${n - 2})"
}
}
fun fibKotlinKoinFactory(n: Int): String {
return when (n) {
1, 2 -> "factory { Fib$n() }"
@Sloy
Sloy / Counter.kt
Last active Jul 19, 2018
Reproducing issue with Koin shared state in Android Tests. Inspired by the Dagger 1 version: https://gist.github.com/Sloy/c45dc3291403e603b4b88755e4a67660
View Counter.kt
open class Counter(context: Context) {
open var count: Int = 0
open fun hit() = count++
}
@Sloy
Sloy / LiveDataExtensions.kt
Created Feb 6, 2018
Kotlin extensions for LiveData
View LiveDataExtensions.kt
package com.sloydev.macookbuk.infrastructure.extensions
import android.arch.lifecycle.*
fun <T, R> LiveData<T>.map(transformation: (T) -> R): LiveData<R> {
return Transformations.map(this, transformation)
}
fun <A, B, C> LiveData<A>.zipWith(other: LiveData<B>, zipFunc: (A, B) -> C): LiveData<C> {
return ZippedLiveData<A, B, C>(this, other, zipFunc)
@Sloy
Sloy / DebugNotification.kt
Created Jan 26, 2018
Utility class to show a debug notification from anywhere in your app
View DebugNotification.kt
object DebugNotification {
private const val DEBUG_CHANNEL_ID = "debug_channel"
@JvmStatic
@JvmOverloads
fun show(context: Context, title: String, text: String = "Debug notification") {
if (BuildConfig.DEBUG) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel(context)
@Sloy
Sloy / FirestoreLiveData.kt
Created Jan 14, 2018
Kotlin wrappers for observing Firebase Firestore database queries and documents using LiveData
View FirestoreLiveData.kt
fun Query.observeLiveData(): FirestoreQueryLiveData {
return FirestoreQueryLiveData(this)
}
fun DocumentReference.observeLiveData(): FirestoreDocumentLiveData {
return FirestoreDocumentLiveData(this)
}
class FirestoreQueryLiveData(