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 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)) | |
} |
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
val client: JsonRpcClient = ... | |
val service = createJsonRpcService(MyService::class, client) | |
val result = service.myMethod(100, "value", listOf(1, 2, 3, 4)) |
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 <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 | |
} |
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
interface MyService { | |
@JsonRpc("myMethod") | |
fun myMethod(@JsonRpc("param1") a: Int, | |
@JsonRpc("param2") b: String, | |
@JsonRpc("param3") c: List<Int> = emptyList()): Single<MyResponse> | |
} |
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
val client: JsonRpcClient = ... | |
val request = JsonRpcRequest( | |
id = generateUniqueId(), | |
method = "myMethod", | |
params = mapOf( | |
"param1" to 100, | |
"param2" to "value", | |
"param3 to listOf(1, 2, 3) | |
) | |
) |
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 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 } | |
) |
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 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 |
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
interface JsonRpcClient { | |
fun <R> call(request: JsonRpcRequest, responseType: Type): Single<R> | |
} |
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.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: |
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
interface RxWebSocket<T> { | |
fun sendMessage(message: String): Single<Unit> | |
fun observeMessages(): Observable<T> | |
fun observeState(): Observable<RxWebSocketState> | |
} |
NewerOlder