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
@@ -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()
}
}
}
}