damus

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

DirectMessagesView.swift (4158B)


      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         .padding(.bottom, tabHeight)
     39     }
     40     
     41     func filter_dms(dms: [DirectMessageModel]) -> [DirectMessageModel] {
     42         return dms.filter({ dm in
     43             return damus_state.settings.friend_filter.filter(contacts: damus_state.contacts, pubkey: dm.pubkey) && !damus_state.mutelist_manager.is_muted(.user(dm.pubkey, nil))
     44         })
     45     }
     46     
     47     var options: EventViewOptions {
     48         /*
     49         if self.damus_state.settings.translate_dms {
     50             return [.truncate_content, .no_action_bar]
     51         }
     52          */
     53 
     54         return [.truncate_content, .no_action_bar, .no_translate]
     55     }
     56     
     57     func MaybeEvent(_ model: DirectMessageModel) -> some View {
     58         Group {
     59             if let ev = model.events.last(where: { should_show_event(state: damus_state, ev: $0) }) {
     60                 EventView(damus: damus_state, event: ev, pubkey: model.pubkey, options: options)
     61                     .onTapGesture {
     62                         self.model.set_active_dm_model(model)
     63                         damus_state.nav.push(route: Route.DMChat(dms: self.model.active_model))
     64                     }
     65                 
     66                 Divider()
     67                     .padding([.top], 10)
     68             } else {
     69                 EmptyView()
     70             }
     71         }
     72     }
     73     
     74     var body: some View {
     75         VStack(spacing: 0) {
     76             CustomPicker(tabs: [
     77                 (NSLocalizedString("DMs", comment: "Picker option for DM selector for seeing only DMs that have been responded to. DM is the English abbreviation for Direct Message."), DMType.friend),
     78                 (NSLocalizedString("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"), DMType.rando),
     79             ], selection: $dm_type)
     80 
     81             Divider()
     82                 .frame(height: 1)
     83             
     84             TabView(selection: $dm_type) {
     85                 MainContent(requests: false)
     86                     .tag(DMType.friend)
     87                 
     88                 MainContent(requests: true)
     89                     .tag(DMType.rando)
     90             }
     91             .tabViewStyle(.page(indexDisplayMode: .never))
     92         }
     93         .toolbar {
     94             ToolbarItem(placement: .navigationBarTrailing) {
     95                 if would_filter_non_friends_from_dms(contacts: damus_state.contacts, dms: self.model.dms) {
     96                     
     97                     FriendsButton(filter: $settings.friend_filter)
     98                 }
     99             }
    100         }
    101         .navigationTitle(NSLocalizedString("DMs", comment: "Navigation title for view of DMs, where DM is an English abbreviation for Direct Message."))
    102     }
    103 }
    104 
    105 func would_filter_non_friends_from_dms(contacts: Contacts, dms: [DirectMessageModel]) -> Bool {
    106     for dm in dms {
    107         if !FriendFilter.friends_of_friends.filter(contacts: contacts, pubkey: dm.pubkey) {
    108             return true
    109         }
    110     }
    111     
    112     return false
    113 }
    114 
    115 struct DirectMessagesView_Previews: PreviewProvider {
    116     static var previews: some View {
    117         let ds = test_damus_state
    118         DirectMessagesView(damus_state: ds, model: ds.dms, settings: ds.settings)
    119     }
    120 }