This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extension function for RxJava's Flowable for writing bytes from a stream into a file | |
* The [FileOutputStream] will be automatically flushed and then closed when the flowable | |
* terminates or is disposed. | |
* | |
* Example usage: | |
* | |
* getAudioStream() | |
* .observeOn(AndroidSchedulers.mainThread()) | |
* .subscribeOn(Schedulers.computation()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Looper | |
import kotlinx.coroutines.experimental.* | |
import kotlin.system.measureTimeMillis | |
abstract class SuspendClassInitializer<out T> constructor( | |
private val initDispatcher: CoroutineDispatcher, | |
instanceFactory: () -> T | |
) { | |
private val delegate = lazy(instanceFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 Google LLC | |
* | |
* 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 | |
* | |
* https://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Set a click listener on a view and back it up by an actor that allows | |
* execution of a single action block at a time. Subsequent requests will be discarded until action returns. | |
*/ | |
fun View.setOnRendezvousClickListener(action: suspend (View) -> Unit) { | |
val eventActor = actor<View>(UI, capacity = 0) { | |
for (event in channel) action(event) | |
} | |
setOnClickListener { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ClosedRange<Int>.random() = Random().nextInt(endInclusive - start) + start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Makes the height match the width, using the set ratio | |
*/ | |
class RatioImageView : ImageView { | |
private var ratio = 1f | |
constructor(context: Context) : this(context, null, 0) | |
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage can be found in the following repository: https://github.com/mrmitew/Bonjour-iOS | |
extension NetService { | |
func getIpV4() -> String? { | |
if let ipData = self.addresses?.first { | |
return (ipData as NSData).getIpV4() | |
} | |
return nil | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
if (args[0] == "start") { | |
launch(CommonPool) { | |
println("Launching") | |
SerialisationHelper.test() | |
println("Finished") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InFlightJobCache<T> { | |
private val jobs = mutableMapOf<String, Deferred<T>>() | |
private val mux = Mutex() | |
suspend fun getOrFetch(key: String, factory: suspend () -> T): Deferred<T> = mux.withLock { | |
val request = jobs.getOrDefault(key, async(start = CoroutineStart.LAZY) { factory() }) | |
jobs[key] = request | |
return request | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReactiveCache<in K, V>(private val generator: Function<K, Observable<V>>) { | |
private val requests: MutableMap<K, ConnectableObservable<V>> = HashMap() | |
@Throws(Exception::class) | |
operator fun get(key: K): Observable<V> { | |
var result: ConnectableObservable<V> = Observable.empty<V>().replay() | |
synchronized(requests) { | |
val current = requests[key] | |
if (current != null) { | |
return current |
NewerOlder