From f7d4dbcdb14fe99c396955bbb6756bf2572f73a4 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Fri, 17 Apr 2026 09:27:10 +0700 Subject: [PATCH] chore: init nostr --- composeApp/build.gradle.kts | 1 + .../kotlin/su/reya/coop/coop/App.kt | 17 -------------- .../kotlin/su/reya/coop/coop/Greeting.kt | 9 -------- .../kotlin/su/reya/coop/coop/MainActivity.kt | 16 +++++++++++++ .../kotlin/su/reya/coop/coop/Nostr.kt | 23 +++++++++++++++++++ 5 files changed, 40 insertions(+), 26 deletions(-) delete mode 100644 composeApp/src/androidMain/kotlin/su/reya/coop/coop/Greeting.kt create mode 100644 composeApp/src/androidMain/kotlin/su/reya/coop/coop/Nostr.kt diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 3ced8a7..36faa3a 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -29,6 +29,7 @@ kotlin { implementation(libs.compose.uiToolingPreview) implementation(libs.androidx.lifecycle.viewmodelCompose) implementation(libs.androidx.lifecycle.runtimeCompose) + implementation("org.rust-nostr:nostr-sdk-kmp:0.44.3") } commonTest.dependencies { implementation(libs.kotlin.test) diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/App.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/App.kt index 2719d53..621fe4d 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/App.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/App.kt @@ -1,11 +1,8 @@ package su.reya.coop.coop -import androidx.compose.animation.AnimatedVisibility -import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.safeContentPadding import androidx.compose.material3.Button import androidx.compose.material3.MaterialTheme @@ -14,10 +11,6 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview -import org.jetbrains.compose.resources.painterResource - -import coop.composeapp.generated.resources.Res -import coop.composeapp.generated.resources.compose_multiplatform @Composable @Preview @@ -34,16 +27,6 @@ fun App() { Button(onClick = { showContent = !showContent }) { Text("Click me!") } - AnimatedVisibility(showContent) { - val greeting = remember { Greeting().greet() } - Column( - modifier = Modifier.fillMaxWidth(), - horizontalAlignment = Alignment.CenterHorizontally, - ) { - Image(painterResource(Res.drawable.compose_multiplatform), null) - Text("Compose: $greeting") - } - } } } } \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Greeting.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Greeting.kt deleted file mode 100644 index 4d99a16..0000000 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Greeting.kt +++ /dev/null @@ -1,9 +0,0 @@ -package su.reya.coop.coop - -class Greeting { - private val platform = getPlatform() - - fun greet(): String { - return "Hello, ${platform.name}!" - } -} \ No newline at end of file diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/MainActivity.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/MainActivity.kt index 1ab3c9f..b11c533 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/MainActivity.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/MainActivity.kt @@ -6,12 +6,28 @@ import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.Composable import androidx.compose.ui.tooling.preview.Preview +import androidx.lifecycle.lifecycleScope +import kotlinx.coroutines.launch +import java.io.File class MainActivity : ComponentActivity() { + private val nostr = Nostr() + override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) + val dbDir = File(filesDir, "nostr") + dbDir.mkdirs() + + // Initialize nostr client + nostr.init(dbDir.absolutePath) + + // Connect to bootstrap relays + lifecycleScope.launch { + nostr.connect() + } + setContent { App() } diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Nostr.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Nostr.kt new file mode 100644 index 0000000..b7dee97 --- /dev/null +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/coop/Nostr.kt @@ -0,0 +1,23 @@ +package su.reya.coop.coop + +import rust.nostr.sdk.* + +class Nostr { + var client: Client? = null + private set + + fun init(dbPath: String) { + val lmdb = NostrDatabase.lmdb(dbPath) + val gossip = NostrGossip.inMemory() + val opts = ClientOptions().automaticAuthentication(false) + + client = ClientBuilder().database(lmdb).gossip(gossip).opts(opts).build() + } + + suspend fun connect() { + this.client?.addRelay(RelayUrl.parse("wss://relay.damus.io")) + this.client?.addRelay(RelayUrl.parse("wss://relay.primal.net")) + this.client?.addRelay(RelayUrl.parse("wss://user.kindpag.es")) + this.client?.connect() + } +} \ No newline at end of file