add nostr view model

This commit is contained in:
2026-04-25 08:52:42 +07:00
parent 8c6b70304d
commit 3240382498
11 changed files with 100 additions and 94 deletions

View File

@@ -24,6 +24,8 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation("org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose:2.10.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
implementation("org.rust-nostr:nostr-sdk-kmp:0.44.3")
}
commonTest.dependencies {

View File

@@ -1,9 +0,0 @@
package su.reya.coop
class Greeting {
private val platform = getPlatform()
fun greet(): String {
return "Hello, ${platform.name}!"
}
}

View File

@@ -25,4 +25,8 @@ class Nostr {
this.client?.addRelay(RelayUrl.parse("wss://user.kindpag.es"))
this.client?.connect()
}
suspend fun disconnect() {
this.client?.shutdown()
}
}

View File

@@ -0,0 +1,44 @@
package su.reya.coop
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import su.reya.coop.storage.SecretStorage
class NostrViewModel(
private val nostr: Nostr,
private val secretStore: SecretStorage
) : ViewModel() {
private val _isConnected = MutableStateFlow(false)
val isConnected = _isConnected.asStateFlow()
fun initAndConnect(dbPath: String) {
// Initialize nostr client
nostr.init(dbPath)
viewModelScope.launch {
try {
// Connect to bootstrap relays
nostr.connect()
_isConnected.value = true
} catch (e: Exception) {
_isConnected.value = false
println(e)
}
}
}
override fun onCleared() {
super.onCleared()
// Ensure all relays are disconnect
viewModelScope.launch {
withContext(NonCancellable) {
nostr.disconnect()
}
}
}
}

View File

@@ -1,7 +0,0 @@
package su.reya.coop
interface Platform {
val name: String
}
expect fun getPlatform(): Platform

View File

@@ -0,0 +1,8 @@
package su.reya.coop.storage
interface SecretStorage {
suspend fun get(key: String): String?
suspend fun set(key: String, value: String)
suspend fun clear(key: String)
suspend fun has(key: String): Boolean
}