damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 28854fdc93e38b1d883abfff94cee504855f093d
parent 2901cc860f4cd6218978c852ec2a73e24364b4a6
Author: Bryan Montz <bryanmontz@me.com>
Date:   Sat, 17 Jun 2023 07:19:51 -0500

simplify and fix issues with RelayStatus

Diffstat:
Mdamus/Views/Relays/RelayDetailView.swift | 16+++++++++++-----
Mdamus/Views/Relays/RelayStatus.swift | 52++++++++++------------------------------------------
Mdamus/Views/Relays/RelayToggle.swift | 8+++++++-
Mdamus/Views/Relays/RelayView.swift | 8++++++--
4 files changed, 34 insertions(+), 50 deletions(-)

diff --git a/damus/Views/Relays/RelayDetailView.swift b/damus/Views/Relays/RelayDetailView.swift @@ -75,11 +75,13 @@ struct RelayDetailView: View { UserViewRow(damus_state: state, pubkey: pubkey) } } - Section(NSLocalizedString("Relay", comment: "Label to display relay address.")) { - HStack { - Text(relay) - Spacer() - RelayStatus(pool: state.pool, relay: relay) + if let relay_connection { + Section(NSLocalizedString("Relay", comment: "Label to display relay address.")) { + HStack { + Text(relay) + Spacer() + RelayStatus(connection: relay_connection) + } } } if nip11.is_paid { @@ -134,6 +136,10 @@ struct RelayDetailView: View { } return attrString } + + private var relay_connection: RelayConnection? { + state.pool.get_relay(relay)?.connection + } } struct RelayDetailView_Previews: PreviewProvider { diff --git a/damus/Views/Relays/RelayStatus.swift b/damus/Views/Relays/RelayStatus.swift @@ -8,58 +8,26 @@ import SwiftUI struct RelayStatus: View { - let pool: RelayPool - let relay: String - - let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect() - - @State var conn_color: Color = .gray - @State var conn_image: String = "network" - @State var connecting: Bool = false - - func update_connection() { - for relay in pool.relays { - if relay.id == self.relay { - let c = relay.connection - if c.isConnected { - conn_image = "globe" - conn_color = .green - } else if c.isConnecting { - connecting = true - } else { - conn_image = "warning.fill" - conn_color = .red - } - } - } - } + @ObservedObject var connection: RelayConnection var body: some View { - HStack { - if connecting { + Group { + if connection.isConnecting { ProgressView() - .frame(width: 20, height: 20) - .padding(.trailing, 5) } else { - Image(conn_image) + Image(connection.isConnected ? "globe" : "warning.fill") .resizable() - .frame(width: 20, height: 20) - .foregroundColor(conn_color) - .padding(.trailing, 5) + .foregroundColor(connection.isConnected ? .green : .red) } } - .onReceive(timer) { _ in - update_connection() - } - .onAppear() { - update_connection() - } - + .frame(width: 20, height: 20) + .padding(.trailing, 5) } } -struct RelayStatus_Previews: PreviewProvider { +struct RelayStatusView_Previews: PreviewProvider { static var previews: some View { - RelayStatus(pool: test_damus_state().pool, relay: "relay") + let connection = test_damus_state().pool.get_relay("relay")!.connection + RelayStatus(connection: connection) } } diff --git a/damus/Views/Relays/RelayToggle.swift b/damus/Views/Relays/RelayToggle.swift @@ -26,12 +26,18 @@ struct RelayToggle: View { var body: some View { HStack { - RelayStatus(pool: state.pool, relay: relay_id) + if let relay_connection { + RelayStatus(connection: relay_connection) + } RelayType(is_paid: state.relay_metadata.lookup(relay_id: relay_id)?.is_paid ?? false) Toggle(relay_id, isOn: toggle_binding(relay_id: relay_id)) .toggleStyle(SwitchToggleStyle(tint: .accentColor)) } } + + private var relay_connection: RelayConnection? { + state.pool.get_relay(relay_id)?.connection + } } struct RelayToggle_Previews: PreviewProvider { diff --git a/damus/Views/Relays/RelayView.swift b/damus/Views/Relays/RelayView.swift @@ -20,8 +20,8 @@ struct RelayView: View { if showActionButtons { RemoveButton(privkey: privkey, showText: false) } - else { - RelayStatus(pool: state.pool, relay: relay) + else if let relay_connection { + RelayStatus(connection: relay_connection) } } @@ -67,6 +67,10 @@ struct RelayView: View { } } + private var relay_connection: RelayConnection? { + state.pool.get_relay(relay)?.connection + } + func CopyAction(relay: String) -> some View { Button { UIPasteboard.general.setValue(relay, forPasteboardType: "public.plain-text")