DirectMessagesView.swift (4226B)
1 // 2 // DirectMessagesView.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-06-29. 6 // 7 8 import SwiftUI 9 10 enum DMType: Hashable { 11 case rando 12 case friend 13 } 14 15 struct DirectMessagesView: View { 16 let damus_state: DamusState 17 18 @State var dm_type: DMType = .friend 19 @ObservedObject var model: DirectMessagesModel 20 @ObservedObject var settings: UserSettingsStore 21 22 func MainContent(requests: Bool) -> some View { 23 ScrollView { 24 LazyVStack(spacing: 0) { 25 let dms = requests ? model.message_requests : model.friend_dms 26 let filtered_dms = filter_dms(dms: dms) 27 if filtered_dms.isEmpty, !model.loading { 28 EmptyTimelineView() 29 } else { 30 ForEach(filtered_dms, id: \.pubkey) { dm in 31 MaybeEvent(dm) 32 .padding(.top, 10) 33 } 34 } 35 } 36 .padding(.horizontal) 37 } 38 } 39 40 func filter_dms(dms: [DirectMessageModel]) -> [DirectMessageModel] { 41 return dms.filter({ dm in 42 return damus_state.settings.friend_filter.filter(contacts: damus_state.contacts, pubkey: dm.pubkey) && !damus_state.mutelist_manager.is_muted(.user(dm.pubkey, nil)) 43 }) 44 } 45 46 var options: EventViewOptions { 47 /* 48 if self.damus_state.settings.translate_dms { 49 return [.truncate_content, .no_action_bar] 50 } 51 */ 52 53 return [.truncate_content, .no_action_bar, .no_translate] 54 } 55 56 func MaybeEvent(_ model: DirectMessageModel) -> some View { 57 Group { 58 if let ev = model.events.last(where: { should_show_event(state: damus_state, ev: $0, keypair: damus_state.keypair) }) { 59 EventView(damus: damus_state, event: ev, pubkey: model.pubkey, options: options) 60 .onTapGesture { 61 self.model.set_active_dm_model(model) 62 damus_state.nav.push(route: Route.DMChat(dms: self.model.active_model)) 63 } 64 65 Divider() 66 .padding([.top], 10) 67 } else { 68 EmptyView() 69 } 70 } 71 } 72 73 var body: some View { 74 VStack(spacing: 0) { 75 CustomPicker(selection: $dm_type, content: { 76 Text("DMs", comment: "Picker option for DM selector for seeing only DMs that have been responded to. DM is the English abbreviation for Direct Message.") 77 .tag(DMType.friend) 78 Text("Requests", comment: "Picker option for DM selector for seeing only message requests (DMs that someone else sent the user which has not been responded to yet). DM is the English abbreviation for Direct Message.") 79 .tag(DMType.rando) 80 }) 81 82 Divider() 83 .frame(height: 1) 84 85 TabView(selection: $dm_type) { 86 MainContent(requests: false) 87 .tag(DMType.friend) 88 89 MainContent(requests: true) 90 .tag(DMType.rando) 91 } 92 .tabViewStyle(.page(indexDisplayMode: .never)) 93 } 94 .toolbar { 95 ToolbarItem(placement: .navigationBarTrailing) { 96 if would_filter_non_friends_from_dms(contacts: damus_state.contacts, dms: self.model.dms) { 97 98 FriendsButton(filter: $settings.friend_filter) 99 } 100 } 101 } 102 .navigationTitle(NSLocalizedString("DMs", comment: "Navigation title for view of DMs, where DM is an English abbreviation for Direct Message.")) 103 } 104 } 105 106 func would_filter_non_friends_from_dms(contacts: Contacts, dms: [DirectMessageModel]) -> Bool { 107 for dm in dms { 108 if !FriendFilter.friends.filter(contacts: contacts, pubkey: dm.pubkey) { 109 return true 110 } 111 } 112 113 return false 114 } 115 116 struct DirectMessagesView_Previews: PreviewProvider { 117 static var previews: some View { 118 let ds = test_damus_state 119 DirectMessagesView(damus_state: ds, model: ds.dms, settings: ds.settings) 120 } 121 }