SearchHomeView.swift (4848B)
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 func content_filter(_ fstate: FilterState) -> ((NostrEvent) -> Bool) { 19 var filters = ContentFilters.defaults(damus_state: damus_state) 20 filters.append(fstate.filter) 21 return ContentFilters(filters: filters).filter 22 } 23 24 var SearchInput: some View { 25 HStack { 26 HStack{ 27 Image("search") 28 .foregroundColor(.gray) 29 TextField(NSLocalizedString("Search...", comment: "Placeholder text to prompt entry of search query."), text: $search) 30 .autocorrectionDisabled(true) 31 .textInputAutocapitalization(.never) 32 .focused($isFocused) 33 } 34 .padding(10) 35 .background(.secondary.opacity(0.2)) 36 .cornerRadius(20) 37 38 if(!search.isEmpty) { 39 Text("Cancel", comment: "Cancel out of search view.") 40 .foregroundColor(.accentColor) 41 .padding(EdgeInsets(top: 0.0, leading: 0.0, bottom: 0.0, trailing: 10.0)) 42 .onTapGesture { 43 self.search = "" 44 isFocused = false 45 } 46 } 47 } 48 } 49 50 var GlobalContent: some View { 51 return TimelineView<AnyView>( 52 events: model.events, 53 loading: $model.loading, 54 damus: damus_state, 55 show_friend_icon: true, 56 filter:content_filter(FilterState.posts), 57 content: { 58 AnyView(VStack(alignment: .leading) { 59 HStack { 60 Image(systemName: "sparkles") 61 .foregroundStyle(PinkGradient) 62 Text("Follow Packs", comment: "A label indicating that the items below it are follow packs") 63 .foregroundStyle(PinkGradient) 64 } 65 .padding(.top) 66 .padding(.horizontal) 67 68 FollowPackTimelineView<AnyView>(events: model.events, loading: $model.loading, damus: damus_state, show_friend_icon: true,filter:content_filter(FilterState.follow_list) 69 ).padding(.bottom) 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 }