Skip to content

Instantly share code, notes, and snippets.

@sproctor
sproctor / ScrollableColumn.jvm.kt
Last active February 17, 2024 19:34
ScrollableColumn and LazyScrollableColumn for Compose for desktop
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.VerticalScrollbar
import androidx.compose.foundation.gestures.FlingBehavior
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
@sproctor
sproctor / gist:e04b86f2904b5b0d56a59cb113b00120
Created February 8, 2023 15:09
KspComposeBug error message
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'ComposeBug'.
> Failed to notify project evaluation listener.
> Cannot change attributes of dependency configuration ':generatedByKspKotlinJsApiDependenciesMetadata' after it has been resolved
> Cannot change attributes of dependency configuration ':generatedByKspKotlinJsImplementationDependenciesMetadata' after it has been resolved
> Cannot change attributes of dependency configuration ':generatedByKspKotlinJsCompileOnlyDependenciesMetadata' after it has been resolved
> Cannot change attributes of dependency configuration ':generatedByKspKotlinJsRuntimeOnlyDependenciesMetadata' after it has been resolved
> Cannot change attributes of dependency configuration ':generatedByKspKotlinJsIntransitiveDependenciesMetadata' after it has been resolved
@sproctor
sproctor / gist:540f023fd3507ac35bffd3827e412e55
Created October 6, 2022 16:46
kotlin compiler exception
e: org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /home/sproctor/Code/ScrapReceipts/ReceiptsApp/common/data/src/commonMain/kotlin/com/scraptickets/data/iscrapapp/IscrapDataSourceImpl.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:96)
at org.jetbrai
@sproctor
sproctor / SecretAuthenticationProvider.kt
Created May 20, 2022 07:38
SecretAuthenticationProvider for Ktor 1.6.x
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.request.*
class SecretAuthenticationProvider internal constructor(config: Configuration) : AuthenticationProvider(config) {
internal val key: (ApplicationCall) -> String? = config.key
internal val expectedKey: String? = config.expectedKey
class Configuration internal constructor(name: String?) : AuthenticationProvider.Configuration(name) {
@sproctor
sproctor / SecretAuthenticationProvider.kt
Created May 20, 2022 07:35
SecretAuthenticationProvider for Ktor 2.x
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
class SecretAuthenticationProvider internal constructor(config: Config) : AuthenticationProvider(config) {
internal val key: (ApplicationCall) -> String? = { call -> call.request.header("secret") }
private val expectedKey: String = config.expectedKey ?: throw Exception("No key provided")
val client = HttpClient()
val response = client.post<HttpResponse> {
url("https://$domain/oauth/token")
header("content-type", "application/x-www-form-urlencoded")
body = "grant_type=authorization_code&client_id=$clientId&code_verifier=$verifier" +
"&code=$code&redirect_uri=$encodedRedirectUri"
}
println("response: ${response.readText()}")
val code = suspendCancellableCoroutine { continuation ->
val server = HttpServer.create(InetSocketAddress(5789), 0)
server.createContext("/callback") { http ->
val parameters = http.requestURI.query?.let { decodeQueryString(it) }
val code = parameters?.get("code") ?: throw RuntimeException("Received a response with no code")
sendResponse(http)
continuation.resume(code)
class AuthenticationManager {
fun authenticateUser(
domain: String,
clientId: String,
redirectUri: String,
scope: String,
audience: String,
) {
val verifier = createVerifier()
@sproctor
sproctor / UseErrorBoundary.kt
Created March 17, 2022 13:41
use-error-boundary bindings for kotlin/js
@file:JsModule("use-error-boundary")
@file:JsNonModule
package externals
import react.*
external interface ErrorState {
var didCatch: Boolean?
var error: Any?
@sproctor
sproctor / launchedeffect.kt
Last active October 17, 2021 15:51
launched effect example
@Composable
fun TimeDisplay(viewModel: TimeDisplayViewModel) {
val properties: Map<String, Long> by viewModel.properties.collectAsState()
var timeRemaining by rememeber { mutableStateOf(0) }
Text(timeRemaining.toString)
val timeProperty = properties["time"]
LaunchedEffect(timeProperty) {
val currentTime = System.currentTimeMillis()