Skip to content

Instantly share code, notes, and snippets.

View roschlau's full-sized avatar

Robin roschlau

  • Echometer GmbH
  • Münster, Germany
  • X @lushorac
View GitHub Profile
@roschlau
roschlau / AnsiStyles.kt
Created January 16, 2023 17:11
Utility code to style console output
fun styled(style: AnsiStyle, str: String) = style.code + str + AnsiStyle.RESET
interface AnsiStyle {
val code: String
companion object {
const val RESET = "\u001B[0m"
}
}
@roschlau
roschlau / UploadFilesUseCase.kt
Last active May 9, 2020 07:33
Kotlin Coroutines implementation of "More Granular Retry" from https://www.techyourchance.com/concurrency-frameworks-overrated-android/
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import java.io.File
class UploadFilesUseCase : BaseBusyObservable<UploadFilesUseCase.Listener>() {
interface Listener {
fun onFilesUploaded()
@roschlau
roschlau / dependency.build.gradle
Created March 25, 2019 09:46
Sharing test sources in gradle
configurations {
// Creating testOutput configuration to bundle test dependencies and sources so other projects can depend on them
testOutput.extendsFrom(testCompile)
}
dependencies {
testOutput sourceSets.test.output
}
@roschlau
roschlau / rainbow_brackets.xml
Created November 7, 2018 22:11
Rainbow Brackets Config
<application>
<component name="RainbowSettings">
<option name="darkRoundBracketsColors">
<array>
<option value="0x4f5b62" />
<option value="0x718792" />
<option value="0x8eacbb" />
<option value="0xc1d5e0" />
<option value="0xfefefe" />
</array>
@roschlau
roschlau / ValueTypes.kt
Created September 8, 2018 11:02
A collection of type wrappers that work similar to Kotlin's inline classes, but for Kotlin < 1.3
import java.util.UUID
/**
* Superclass for classes that wrap a single value for type safety purposes.
*/
abstract class WrapperType<T : Any>(val value: T) {
override fun toString() = value.toString()
override fun equals(other: Any?) = other is WrapperType<*> && value == other.value
override fun hashCode() = value.hashCode()
}
@roschlau
roschlau / ImmutableHelpers.kt
Created March 19, 2018 16:18
Some helpers for working with immutable collections in Kotlin
/**
* Returns a new List with the element at [fromIndex] moved to [toIndex]
*/
private fun <T> List<T>.move(fromIndex: Int, toIndex: Int) = this.mapIndexed { index, element -> when {
index == toIndex -> this[fromIndex]
fromIndex < toIndex && index in fromIndex..toIndex -> this[index + 1]
fromIndex > toIndex && index in toIndex..fromIndex -> this[index - 1]
else -> element
} }
@roschlau
roschlau / memoized.kt
Created September 21, 2017 13:09
Simple function to memoize function calls
fun <T, U> memoized(function: (T) -> U): (T) -> U {
val cache = mutableMapOf<T, U>()
return { t -> cache.getOrPut(t) { function(t) } }
}
@roschlau
roschlau / BaseActivity.kt
Last active March 19, 2018 16:12
Utility class for handling permissions on Android
class BaseActivity : Activity {
val permissionHandler: PermissionRequestHandler
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
val handled = permissionHandler.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(!handled) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
@roschlau
roschlau / DiffUtilCallback.kt
Last active March 5, 2017 00:38
Default implementation of a DiffUtil.Callback
import android.support.v7.util.DiffUtil
open class DiffUtilCallback<T>(val oldList: List<T>, val newList: List<T>) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int)
= oldList[oldItemPosition] == newList[newItemPosition]
override fun getOldListSize()
= oldList.size
@roschlau
roschlau / DeferPanicRecover.kt
Last active August 16, 2018 23:34
Implementing the defer, panic and recover functions from golang in Kotlin, because reasons. I have hardly any knowledge of Go, I googled a little bit about how those functions work. The example is copied from https://blog.golang.org/defer-panic-and-recover and results in exactly the same output.
import java.util.Optional
// Actual implementation
fun <T> deferringScope(block: DeferringScope<T>.() -> T): Optional<T> {
val scope = DeferringScope<T>()
try {
scope.result = Optional.of(scope.block())
} catch (e: Throwable) {
scope.error = e.message ?: e.toString()
}