damus

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

commit 0bd40c00187803c1a13764b8815cd23bc41365ee
parent ec75769a0fbe2e1e22b593a2d2d53a46670d6c3c
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 21 Apr 2023 16:55:49 -0700

Add Friends filter to DMs

Changelog-Added: Add friends filter to DMs

Diffstat:
Mdamus/ContentView.swift | 2+-
Mdamus/Views/Buttons/FriendsButton.swift | 15++++++++++-----
Mdamus/Views/DirectMessagesView.swift | 34+++++++++++++++++++++++++++-------
Mdamus/Views/Notifications/NotificationsView.swift | 10+---------
4 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -182,7 +182,7 @@ struct ContentView: View { NotificationsView(state: damus, notifications: home.notifications) case .dms: - DirectMessagesView(damus_state: damus_state!, model: damus_state!.dms) + DirectMessagesView(damus_state: damus_state!, model: damus_state!.dms, settings: damus_state!.settings) case .none: EmptyView() diff --git a/damus/Views/Buttons/FriendsButton.swift b/damus/Views/Buttons/FriendsButton.swift @@ -8,13 +8,18 @@ import SwiftUI struct FriendsButton: View { - @Binding var enabled: Bool + @Binding var filter: FriendFilter var body: some View { Button(action: { - self.enabled.toggle() + switch self.filter { + case .all: + self.filter = .friends + case .friends: + self.filter = .all + } }) { - if enabled { + if filter == .friends { LINEAR_GRADIENT .mask(Image(systemName: "person.2.fill") .resizable() @@ -31,9 +36,9 @@ struct FriendsButton: View { } struct FriendsButton_Previews: PreviewProvider { - @State static var enabled: Bool = false + @State static var enabled: FriendFilter = .all static var previews: some View { - FriendsButton(enabled: $enabled) + FriendsButton(filter: $enabled) } } diff --git a/damus/Views/DirectMessagesView.swift b/damus/Views/DirectMessagesView.swift @@ -17,6 +17,7 @@ struct DirectMessagesView: View { @State var dm_type: DMType = .friend @ObservedObject var model: DirectMessagesModel + @ObservedObject var settings: UserSettingsStore func MainContent(requests: Bool) -> some View { ScrollView { @@ -29,12 +30,9 @@ struct DirectMessagesView: View { EmptyTimelineView() } else { let dms = requests ? model.message_requests : model.friend_dms - ForEach(dms, id: \.pubkey) { tup in - MaybeEvent(tup) + ForEach(dms, id: \.pubkey) { dm in + MaybeEvent(dm) .padding(.top, 10) - - Divider() - .padding([.top], 10) } } } @@ -52,11 +50,15 @@ struct DirectMessagesView: View { func MaybeEvent(_ model: DirectMessageModel) -> some View { Group { - if let ev = model.events.last { + let ok = damus_state.settings.friend_filter.filter(contacts: damus_state.contacts, pubkey: model.pubkey) + if ok, let ev = model.events.last { EventView(damus: damus_state, event: ev, pubkey: model.pubkey, options: options) .onTapGesture { self.model.open_dm_by_model(model) } + + Divider() + .padding([.top], 10) } else { EmptyView() } @@ -84,10 +86,28 @@ struct DirectMessagesView: View { } .tabViewStyle(.page(indexDisplayMode: .never)) } + .toolbar { + ToolbarItem(placement: .navigationBarTrailing) { + if would_filter_non_friends_from_dms(contacts: damus_state.contacts, dms: self.model.dms) { + + FriendsButton(filter: $settings.friend_filter) + } + } + } .navigationTitle(NSLocalizedString("DMs", comment: "Navigation title for view of DMs, where DM is an English abbreviation for Direct Message.")) } } +func would_filter_non_friends_from_dms(contacts: Contacts, dms: [DirectMessageModel]) -> Bool { + for dm in dms { + if !FriendFilter.friends.filter(contacts: contacts, pubkey: dm.pubkey) { + return true + } + } + + return false +} + struct DirectMessagesView_Previews: PreviewProvider { static var previews: some View { let ev = NostrEvent(content: "encrypted stuff", @@ -95,6 +115,6 @@ struct DirectMessagesView_Previews: PreviewProvider { kind: 4, tags: []) let ds = test_damus_state() - DirectMessagesView(damus_state: ds, model: ds.dms) + DirectMessagesView(damus_state: ds, model: ds.dms, settings: ds.settings) } } diff --git a/damus/Views/Notifications/NotificationsView.swift b/damus/Views/Notifications/NotificationsView.swift @@ -60,14 +60,6 @@ class NotificationFilter: ObservableObject, Equatable { } } - var fine_filter_binding: Binding<Bool> { - Binding(get: { - return self.fine_filter == .friends - }, set: { v in - self.fine_filter = v ? .friends : .all - }) - } - func filter(contacts: Contacts, items: [NotificationItem]) -> [NotificationItem] { return items.reduce(into: []) { acc, item in @@ -162,7 +154,7 @@ struct NotificationsView: View { .toolbar { ToolbarItem(placement: .navigationBarTrailing) { if would_filter_non_friends_from_notifications(contacts: state.contacts, state: self.filter_state.state, items: self.notifications.notifications) { - FriendsButton(enabled: self.filter_state.fine_filter_binding) + FriendsButton(filter: $filter_state.fine_filter) } } }