Skip to content

Instantly share code, notes, and snippets.

View gtomek's full-sized avatar

Tomek Giszczak gtomek

  • Zurich, CH
View GitHub Profile
@gtomek
gtomek / ConcurrencyHelpers.kt
Created June 9, 2020 10:38 — forked from objcode/ConcurrencyHelpers.kt
Helpers to control concurrency for one shot requests using Kotlin coroutines.
import kotlinx.coroutines.CoroutineStart.LAZY
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.yield
import java.util.concurrent.atomic.AtomicReference
import kotlin.DeprecationLevel.ERROR
/*
* Copyright (C) 2014 The Android Open Source Project
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
const val ID = "id"
class MyActivity : Activity() {
private val id1 by extra<String>(ID) // String?
private val id2 by extraNotNull<String>(ID) // String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requireNotNull(id1, id2)
inline fun <reified T: Any> Fragment.extra(key: String, default: T? = null) = lazy {
val value = arguments?.get(key)
if (value is T) value else default
}
inline fun <reified T: Any> Fragment.extraNotNull(key: String, default: T? = null) = lazy {
val value = arguments?.get(key)
requireNotNull(if (value is T) value else default) { key }
}
inline fun <reified T: Any> Activity.extra(key: String, default: T? = null) = lazy {
val value = intent?.extras?.get(key)
if (value is T) value else default
}
inline fun <reified T: Any> Activity.extraNotNull(key: String, default: T? = null) = lazy {
val value = intent?.extras?.get(key)
requireNotNull(if (value is T) value else default) { key }
}
@gtomek
gtomek / Interceptor.java
Created November 27, 2018 16:18 — forked from alex-shpak/Interceptor.java
Refreshing OAuth token with okhttp interceptors. All requests will wait until token refresh finished, and then will continue with the new token.
private class HttpInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//Build new request
Request.Builder builder = request.newBuilder();
builder.header("Accept", "application/json"); //if necessary, say to consume JSON
@Test
public void switchMap() throws Exception {
final List<String> items = Lists.newArrayList("a", "b", "c", "d", "e", "f");
final TestScheduler scheduler = new TestScheduler();
Observable.from(items)
.concatMap( s -> {
final int delay = new Random().nextInt(10);
return Observable.just(s + "x")
@Test
public void switchMap() throws Exception {
final List<String> items = Lists.newArrayList("a", "b", "c", "d", "e", "f");
final TestScheduler scheduler = new TestScheduler();
Observable.from(items)
.switchMap( s -> {
final int delay = new Random().nextInt(10);
return Observable.just(s + "x")
@gtomek
gtomek / logging.kt
Created January 2, 2018 10:52 — forked from tomaszpolanski/logging.kt
RxLogging
@file:Suppress("NOTHING_TO_INLINE")
import android.util.Log
import io.reactivex.*
inline fun <reified T> printEvent(tag: String, success: T?, error: Throwable?) =
when {
success == null && error == null -> Log.d(tag, "Complete") /* Only with Maybe */
success != null -> Log.d(tag, "Success $success")
error != null -> Log.d(tag, "Error $error")