Skip to content

Instantly share code, notes, and snippets.

View ntoskrnl's full-sized avatar

Anton Danshin ntoskrnl

View GitHub Profile
@ntoskrnl
ntoskrnl / ColorUtil.kt
Created October 17, 2021 00:54
Circular avatar placeholder with initials in Jetpack Compose
import androidx.annotation.ColorInt
import androidx.core.graphics.ColorUtils
import kotlin.math.absoluteValue
@ColorInt
fun String.toHslColor(saturation: Float = 0.5f, lightness: Float = 0.4f): Int {
val hue = fold(0) { acc, char -> char.code + acc * 37 } % 360
return ColorUtils.HSLToColor(floatArrayOf(hue.absoluteValue.toFloat(), saturation, lightness))
}
@ntoskrnl
ntoskrnl / JsonRpcClientTest2.kt
Last active September 30, 2017 19:59
Usage of annotation-based JsonRpcClient
val client: JsonRpcClient = ...
val service = createJsonRpcService(MyService::class, client)
val result = service.myMethod(100, "value", listOf(1, 2, 3, 4))
@ntoskrnl
ntoskrnl / JsonRpc.kt
Last active October 7, 2017 19:48
Creating a retrofit-like service based on annotations.
fun <T> createJsonRpcService(service: Class<T>, client: JsonRpcClient): T {
val classLoader = service.classLoader
val interfaces = arrayOf<Class<*>>(service)
val invocationHandler = createInvocationHandler(service, client)
@Suppress("UNCHECKED_CAST")
return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler) as T
}
@ntoskrnl
ntoskrnl / JsonRpcServiceExample.kt
Last active September 30, 2017 19:47
Annotation-based approach to build JSON-RPC requests
interface MyService {
@JsonRpc("myMethod")
fun myMethod(@JsonRpc("param1") a: Int,
@JsonRpc("param2") b: String,
@JsonRpc("param3") c: List<Int> = emptyList()): Single<MyResponse>
}
@ntoskrnl
ntoskrnl / JsonRpcClientTest1.kt
Last active September 30, 2017 19:42
An example of inconveniences when using JsonRpcClient
val client: JsonRpcClient = ...
val request = JsonRpcRequest(
id = generateUniqueId(),
method = "myMethod",
params = mapOf(
"param1" to 100,
"param2" to "value",
"param3 to listOf(1, 2, 3)
)
)
@ntoskrnl
ntoskrnl / JsonRpcClientImpl.kt
Last active August 17, 2019 11:22
A simple JSON-RPC client over RxWebSocket
class JsonRpcClientImpl(private val rxWebSocket: RxWebSocket<String>,
private val gson: Gson) : JsonRpcClient {
override fun <R> call(request: JsonRpcRequest, responseType: Type): Single<R> {
val requestStr = gson.toJson(request)
return Single.zip(
captureResponse<R>(jsonRpcRequest.id, responseType),
rxWebSocket.sendMessage(requestStr),
{ response, _ -> response }
)
class JsonRpcRequest(
val id: Long,
val method: String,
val params: Map<String, Any?> = emptyMap()
)
data class JsonRpcResponse(
val id: Long,
val result: JsonElement,
val error: JsonRpcError
@ntoskrnl
ntoskrnl / JsonRpcClient.kt
Last active September 30, 2017 17:54
An interface for simple JSON-RPC client
interface JsonRpcClient {
fun <R> call(request: JsonRpcRequest, responseType: Type): Single<R>
}
@ntoskrnl
ntoskrnl / HorizontalWrapContentViewContainer.java
Last active September 30, 2017 14:01
A work around for when you need to display a dynamically-sized view with width="wrap_content" and some other views to the right.
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
/**
* A work around for when you need to display a dynamically-sized view with width="wrap_content" and some other views to the right.
*
* How it works:
@ntoskrnl
ntoskrnl / RxWebSocket.kt
Last active September 20, 2017 00:05
RxWebSocket interface (example)
interface RxWebSocket<T> {
fun sendMessage(message: String): Single<Unit>
fun observeMessages(): Observable<T>
fun observeState(): Observable<RxWebSocketState>
}