add nostr view model
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package su.reya.coop
|
||||
|
||||
class Greeting {
|
||||
private val platform = getPlatform()
|
||||
|
||||
fun greet(): String {
|
||||
return "Hello, ${platform.name}!"
|
||||
}
|
||||
}
|
||||
@@ -25,4 +25,8 @@ class Nostr {
|
||||
this.client?.addRelay(RelayUrl.parse("wss://user.kindpag.es"))
|
||||
this.client?.connect()
|
||||
}
|
||||
|
||||
suspend fun disconnect() {
|
||||
this.client?.shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
44
shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt
Normal file
44
shared/src/commonMain/kotlin/su/reya/coop/NostrViewModel.kt
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package su.reya.coop
|
||||
|
||||
interface Platform {
|
||||
val name: String
|
||||
}
|
||||
|
||||
expect fun getPlatform(): Platform
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user