RelayStatus.swift (1670B)
1 // 2 // RelayStatus.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-02-10. 6 // 7 8 import SwiftUI 9 10 struct RelayStatus: View { 11 let pool: RelayPool 12 let relay: String 13 14 let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect() 15 16 @State var conn_color: Color = .gray 17 @State var conn_image: String = "network" 18 @State var connecting: Bool = false 19 20 func update_connection() { 21 for relay in pool.relays { 22 if relay.id == self.relay { 23 let c = relay.connection 24 if c.isConnected { 25 conn_image = "globe" 26 conn_color = .green 27 } else if c.isConnecting { 28 connecting = true 29 } else { 30 conn_image = "warning.fill" 31 conn_color = .red 32 } 33 } 34 } 35 } 36 37 var body: some View { 38 HStack { 39 if connecting { 40 ProgressView() 41 .frame(width: 20, height: 20) 42 .padding(.trailing, 5) 43 } else { 44 Image(conn_image) 45 .resizable() 46 .frame(width: 20, height: 20) 47 .foregroundColor(conn_color) 48 .padding(.trailing, 5) 49 } 50 } 51 .onReceive(timer) { _ in 52 update_connection() 53 } 54 .onAppear() { 55 update_connection() 56 } 57 58 } 59 } 60 61 struct RelayStatus_Previews: PreviewProvider { 62 static var previews: some View { 63 RelayStatus(pool: test_damus_state().pool, relay: "relay") 64 } 65 }