commit e5e0fb2db279a5a2763d39b15d5fff20bec26fdd
parent 054714794dd743fa87d7ab88b5d6380960fcd515
Author: William Casarin <jb55@jb55.com>
Date: Sun, 16 Oct 2022 11:08:32 -0700
Add RelayView with connection statuses
Changelog-Added: Show relay connection status in config
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
2 files changed, 70 insertions(+), 20 deletions(-)
diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift
@@ -15,25 +15,6 @@ struct ConfigView: View {
@State var confirm_logout: Bool = false
@State var new_relay: String = ""
- func Relay(_ ev: NostrEvent, relay: String) -> some View {
- return Text(relay)
- .swipeActions {
- if let privkey = state.keypair.privkey {
- Button {
- guard let new_ev = remove_relay( ev: ev, privkey: privkey, relay: relay) else {
- return
- }
-
- state.contacts.event = new_ev
- state.pool.send(.event(new_ev))
- } label: {
- Label("Delete", systemImage: "trash")
- }
- .tint(.red)
- }
- }
- }
-
var body: some View {
ZStack(alignment: .leading) {
Form {
@@ -41,7 +22,7 @@ struct ConfigView: View {
Section("Relays") {
if let relays = decode_json_relays(ev.content) {
List(Array(relays.keys.sorted()), id: \.self) { relay in
- Relay(ev, relay: relay)
+ RelayView(state: state, ev: ev, relay: relay)
}
}
diff --git a/damus/Views/RelayView.swift b/damus/Views/RelayView.swift
@@ -0,0 +1,69 @@
+//
+// RelayView.swift
+// damus
+//
+// Created by William Casarin on 2022-10-16.
+//
+
+import SwiftUI
+
+struct RelayView: View {
+ let state: DamusState
+ let ev: NostrEvent
+ let relay: String
+
+ let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()
+ @State var conn_color: Color = .gray
+
+ func update_connection_color() {
+ for relay in state.pool.relays {
+ if relay.id == self.relay {
+ let c = relay.connection
+ if c.isConnected {
+ conn_color = .green
+ } else if c.isConnecting || c.isReconnecting {
+ conn_color = .yellow
+ } else {
+ conn_color = .red
+ }
+ }
+ }
+ }
+
+ var body: some View {
+ HStack {
+ Circle()
+ .frame(width: 8.0, height: 8.0)
+ .foregroundColor(conn_color)
+ Text(relay)
+ }
+ .onReceive(timer) { _ in
+ update_connection_color()
+ }
+ .onAppear() {
+ update_connection_color()
+ }
+ .swipeActions {
+ if let privkey = state.keypair.privkey {
+ Button {
+ guard let new_ev = remove_relay( ev: ev, privkey: privkey, relay: relay) else {
+ return
+ }
+
+ state.contacts.event = new_ev
+ state.pool.send(.event(new_ev))
+ } label: {
+ Label("Delete", systemImage: "trash")
+ }
+ .tint(.red)
+ }
+ }
+ }
+
+}
+
+struct RelayView_Previews: PreviewProvider {
+ static var previews: some View {
+ RelayView(state: test_damus_state(), ev: NostrEvent(content: "content", pubkey: "pk"), relay: "wss://relay.damus.io", conn_color: .red)
+ }
+}