damus

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

RelayConfigView.swift (5336B)


      1 //
      2 //  RelayConfigView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-01-30.
      6 //
      7 
      8 import SwiftUI
      9 
     10 enum RelayTab: Int, CaseIterable{
     11     case myRelays = 0
     12     case recommended
     13     
     14     var title: String{
     15         switch self {
     16         case .myRelays:
     17             return "My relays"
     18         case .recommended:
     19             return "Recommended"
     20         }
     21     }
     22 }
     23 
     24 struct RelayConfigView: View {
     25     let state: DamusState
     26     @State var relays: [RelayDescriptor]
     27     @State private var showActionButtons = false
     28     @State var show_add_relay: Bool = false
     29     @State var selectedTab = 0
     30     
     31     @Environment(\.dismiss) var dismiss
     32     
     33     init(state: DamusState) {
     34         self.state = state
     35         _relays = State(initialValue: state.pool.our_descriptors)
     36         UITabBar.appearance().isHidden = true
     37     }
     38     
     39     var recommended: [RelayDescriptor] {
     40         let rs: [RelayDescriptor] = []
     41         let recommended_relay_addresses = get_default_bootstrap_relays()
     42         return recommended_relay_addresses.reduce(into: rs) { xs, x in
     43             xs.append(RelayDescriptor(url: x, info: .rw))
     44         }
     45     }
     46 
     47     var body: some View {
     48         NavigationView {
     49             ZStack(alignment: .bottom){
     50                 TabView(selection: $selectedTab) {
     51                     RelayList(title: "My Relays", relayList: relays, recommended: false)
     52                         .tag(0)
     53 
     54                     RelayList(title: "Recommended", relayList: recommended, recommended: true)
     55                         .tag(1)
     56                 }
     57                 ZStack{
     58                     HStack{
     59                         ForEach((RelayTab.allCases), id: \.self){ item in
     60                             Button{
     61                                 selectedTab = item.rawValue
     62                             } label: {
     63                                 CustomTabItem(title: item.title, isActive: (selectedTab == item.rawValue))
     64                             }
     65                         }
     66                     }
     67                 }
     68                 .frame(width: 235, height: 35)
     69                 .background(.damusNeutral3)
     70                 .cornerRadius(30)
     71                 .padding(.horizontal, 26)
     72             }
     73         }
     74         .navigationTitle(NSLocalizedString("Relays", comment: "Title of relays view"))
     75         .navigationBarTitleDisplayMode(.inline)
     76         .navigationBarBackButtonHidden(true)
     77         .navigationBarItems(leading: BackNav())
     78         .sheet(isPresented: $show_add_relay, onDismiss: { self.show_add_relay = false }) {
     79             AddRelayView(state: state)
     80                 .presentationDetents([.height(300)])
     81                 .presentationDragIndicator(.visible)
     82         }
     83         .toolbar {
     84             if state.keypair.privkey != nil && selectedTab == 0 {
     85                 if showActionButtons {
     86                     Button("Done") {
     87                         withAnimation {
     88                             showActionButtons.toggle()
     89                         }
     90                     }
     91                 } else {
     92                     Button("Edit") {
     93                         withAnimation {
     94                             showActionButtons.toggle()
     95                         }
     96                     }
     97                 }
     98             }
     99         }
    100         .onReceive(handle_notify(.relays_changed)) { _ in
    101             self.relays = state.pool.our_descriptors
    102         }
    103         .onAppear {
    104             notify(.display_tabbar(false))
    105         }
    106         .onDisappear {
    107             notify(.display_tabbar(true))
    108         }
    109         .ignoresSafeArea(.all)
    110     }
    111     
    112     func RelayList(title: String, relayList: [RelayDescriptor], recommended: Bool) -> some View {
    113         ScrollView(showsIndicators: false) {
    114             HStack {
    115                 Text(title)
    116                     .font(.system(size: 32, weight: .bold))
    117                 
    118                 
    119                 Spacer()
    120                 
    121                 if state.keypair.privkey != nil {
    122                     Button(action: {
    123                         show_add_relay.toggle()
    124                     }) {
    125                         HStack {
    126                             Text("Add relay", comment: "Button text to add a relay")
    127                                 .padding(10)
    128                         }
    129                     }
    130                     .buttonStyle(NeutralButtonStyle())
    131                 }
    132             }
    133             .padding(.top, 5)
    134 
    135             ForEach(relayList, id: \.url) { relay in
    136                 Group {
    137                     RelayView(state: state, relay: relay.url, showActionButtons: $showActionButtons, recommended: recommended)
    138                     Divider()
    139                 }
    140             }
    141             
    142             Spacer()
    143                 .padding(25)
    144         }
    145         .padding(.horizontal)
    146     }
    147 }
    148 
    149 extension RelayConfigView{
    150     func CustomTabItem(title: String, isActive: Bool) -> some View {
    151         HStack {
    152             Text(title)
    153                 .font(.system(size: 12, weight: isActive ? .bold : .regular))
    154                 .foregroundColor(isActive ? .damusAdaptableBlack : .damusAdaptableBlack.opacity(0.7))
    155         }
    156         .frame(width: 110, height: 30)
    157         .background(isActive ? .damusAdaptableWhite.opacity(0.9) : .clear)
    158         .cornerRadius(30)
    159     }
    160 }
    161 
    162 struct RelayConfigView_Previews: PreviewProvider {
    163     static var previews: some View {
    164         RelayConfigView(state: test_damus_state)
    165     }
    166 }