simplify and fix issues with RelayStatus
This commit is contained in:
committed by
William Casarin
parent
2901cc860f
commit
28854fdc93
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user