SearchHomeView.swift (4497B)
1 // 2 // SearchHomeView.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-05-19. 6 // 7 8 import SwiftUI 9 import CryptoKit 10 import NaturalLanguage 11 12 struct SearchHomeView: View { 13 let damus_state: DamusState 14 @StateObject var model: SearchHomeModel 15 @State var search: String = "" 16 @FocusState private var isFocused: Bool 17 18 var content_filter: (NostrEvent) -> Bool { 19 let filters = ContentFilters.defaults(damus_state: self.damus_state) 20 return ContentFilters(filters: filters).filter 21 } 22 23 var SearchInput: some View { 24 HStack { 25 HStack{ 26 Image("search") 27 .foregroundColor(.gray) 28 TextField(NSLocalizedString("Search...", comment: "Placeholder text to prompt entry of search query."), text: $search) 29 .autocorrectionDisabled(true) 30 .textInputAutocapitalization(.never) 31 .focused($isFocused) 32 } 33 .padding(10) 34 .background(.secondary.opacity(0.2)) 35 .cornerRadius(20) 36 37 if(!search.isEmpty) { 38 Text("Cancel", comment: "Cancel out of search view.") 39 .foregroundColor(.accentColor) 40 .padding(EdgeInsets(top: 0.0, leading: 0.0, bottom: 0.0, trailing: 10.0)) 41 .onTapGesture { 42 self.search = "" 43 isFocused = false 44 } 45 } 46 } 47 } 48 49 var GlobalContent: some View { 50 return TimelineView<AnyView>( 51 events: model.events, 52 loading: $model.loading, 53 damus: damus_state, 54 show_friend_icon: true, 55 filter: { ev in 56 if !content_filter(ev) { 57 return false 58 } 59 60 let event_muted = damus_state.mutelist_manager.is_event_muted(ev) 61 if event_muted { 62 return false 63 } 64 65 return true 66 }, 67 content: { 68 AnyView(VStack { 69 SuggestedHashtagsView(damus_state: damus_state, max_items: 5, events: model.events) 70 71 Divider() 72 .frame(height: 1) 73 74 HStack { 75 Image("notes.fill") 76 Text("All recent notes", comment: "A label indicating that the notes being displayed below it are all recent notes") 77 Spacer() 78 } 79 .foregroundColor(.secondary) 80 .padding(.top, 20) 81 .padding(.horizontal) 82 }.padding(.bottom, 50)) 83 } 84 ) 85 .refreshable { 86 // Fetch new information by unsubscribing and resubscribing to the relay 87 model.unsubscribe() 88 model.subscribe() 89 } 90 } 91 92 var SearchContent: some View { 93 SearchResultsView(damus_state: damus_state, search: $search) 94 .refreshable { 95 // Fetch new information by unsubscribing and resubscribing to the relay 96 model.unsubscribe() 97 model.subscribe() 98 } 99 } 100 101 var MainContent: some View { 102 Group { 103 if search.isEmpty { 104 GlobalContent 105 } else { 106 SearchContent 107 } 108 } 109 } 110 111 @Environment(\.colorScheme) var colorScheme 112 113 var body: some View { 114 VStack { 115 MainContent 116 } 117 .safeAreaInset(edge: .top, spacing: 0) { 118 VStack(spacing: 0) { 119 SearchInput 120 //.frame(maxWidth: 275) 121 .padding() 122 Divider() 123 .frame(height: 1) 124 } 125 .background(colorScheme == .dark ? Color.black : Color.white) 126 } 127 .onReceive(handle_notify(.new_mutes)) { _ in 128 self.model.filter_muted() 129 } 130 .onAppear { 131 if model.events.events.isEmpty { 132 model.subscribe() 133 } 134 } 135 .onDisappear { 136 model.unsubscribe() 137 } 138 } 139 } 140 141 struct SearchHomeView_Previews: PreviewProvider { 142 static var previews: some View { 143 let state = test_damus_state 144 SearchHomeView(damus_state: state, model: SearchHomeModel(damus_state: state)) 145 } 146 }