damus

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

commit 8b9958a4ad0abf90ab4818381a66992a36c47de6
parent 87a0bdac9494ce4f9987100dd807b07c6a3224e5
Author: Joel Klabo <joelklabo@gmail.com>
Date:   Sat, 18 Feb 2023 15:41:39 -0800

Add Bookmarking (Local to device)

Changelog-Added: Bookmarking
Closes: #649

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 8++++++++
Adamus/Models/BookmarksManager.swift | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mdamus/Util/Notifications.swift | 3+++
Adamus/Views/BookmarksView.swift | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdamus/Views/Events/EventMenu.swift | 19+++++++++++++++++++
Mdamus/Views/SideMenuView.swift | 4++++
6 files changed, 153 insertions(+), 0 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -220,6 +220,8 @@ DD597CBD2963D85A00C64D32 /* MarkdownTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD597CBC2963D85A00C64D32 /* MarkdownTests.swift */; }; E990020F2955F837003BBC5A /* EditMetadataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E990020E2955F837003BBC5A /* EditMetadataView.swift */; }; E9E4ED0B295867B900DD7078 /* ThreadV2View.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E4ED0A295867B900DD7078 /* ThreadV2View.swift */; }; + F75BA12D29A1855400E10810 /* BookmarksManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75BA12C29A1855400E10810 /* BookmarksManager.swift */; }; + F75BA12F29A18EF500E10810 /* BookmarksView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75BA12E29A18EF500E10810 /* BookmarksView.swift */; }; F7908E92298B0F0700AB113A /* RelayDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7908E91298B0F0700AB113A /* RelayDetailView.swift */; }; F7908E97298B1FDF00AB113A /* NIPURLBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7908E96298B1FDF00AB113A /* NIPURLBuilder.swift */; }; F7F0BA25297892BD009531F3 /* SwipeToDismiss.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F0BA24297892BD009531F3 /* SwipeToDismiss.swift */; }; @@ -537,6 +539,8 @@ DD597CBC2963D85A00C64D32 /* MarkdownTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarkdownTests.swift; sourceTree = "<group>"; }; E990020E2955F837003BBC5A /* EditMetadataView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditMetadataView.swift; sourceTree = "<group>"; }; E9E4ED0A295867B900DD7078 /* ThreadV2View.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadV2View.swift; sourceTree = "<group>"; }; + F75BA12C29A1855400E10810 /* BookmarksManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksManager.swift; sourceTree = "<group>"; }; + F75BA12E29A18EF500E10810 /* BookmarksView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksView.swift; sourceTree = "<group>"; }; F7908E91298B0F0700AB113A /* RelayDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayDetailView.swift; sourceTree = "<group>"; }; F7908E96298B1FDF00AB113A /* NIPURLBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NIPURLBuilder.swift; sourceTree = "<group>"; }; F7F0BA24297892BD009531F3 /* SwipeToDismiss.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwipeToDismiss.swift; sourceTree = "<group>"; }; @@ -665,6 +669,7 @@ 4C63334F283D40E500B1C9C3 /* HomeModel.swift */, 4C633351283D419F00B1C9C3 /* SignalModel.swift */, 4C5F9113283D694D0052CD1C /* FollowTarget.swift */, + F75BA12C29A1855400E10810 /* BookmarksManager.swift */, 4C5F9115283D855D0052CD1C /* EventsModel.swift */, 4C5F9117283D88E40052CD1C /* FollowingModel.swift */, 4C987B56283FD07F0042CE38 /* FollowersModel.swift */, @@ -702,6 +707,7 @@ 4CB88387296AF97C00DC99E7 /* ActionBar */, 4CE4F9E228528C5200C00DD9 /* AddRelayView.swift */, 4C363A8728236948006E126D /* BlocksView.swift */, + F75BA12E29A18EF500E10810 /* BookmarksView.swift */, 4C285C8128385570008A31F1 /* CarouselView.swift */, 4C0A3F8B280F5FCA000448DE /* ChatroomView.swift */, 4C0A3F90280F6528000448DE /* ChatView.swift */, @@ -1235,6 +1241,7 @@ 4C285C8A2838B985008A31F1 /* ProfilePictureSelector.swift in Sources */, 4C75EFB92804A2740006080F /* EventView.swift in Sources */, 3AA247FD297E3CFF0090C62D /* RepostsModel.swift in Sources */, + F75BA12F29A18EF500E10810 /* BookmarksView.swift in Sources */, 4CB883B6297730E400DC99E7 /* LNUrls.swift in Sources */, 4C7FF7D52823313F009601DB /* Mentions.swift in Sources */, 4C633350283D40E500B1C9C3 /* HomeModel.swift in Sources */, @@ -1282,6 +1289,7 @@ 31D2E847295218AF006D67F8 /* Shimmer.swift in Sources */, F7908E97298B1FDF00AB113A /* NIPURLBuilder.swift in Sources */, 4C285C8228385570008A31F1 /* CarouselView.swift in Sources */, + F75BA12D29A1855400E10810 /* BookmarksManager.swift in Sources */, 4C3EA67F28FFC01D00C48A62 /* InvoiceView.swift in Sources */, 4CE8794829941DA700F758CC /* RelayFilters.swift in Sources */, 4CEE2B02280B39E800AB5EEF /* EventActionBar.swift in Sources */, diff --git a/damus/Models/BookmarksManager.swift b/damus/Models/BookmarksManager.swift @@ -0,0 +1,50 @@ +// +// BookmarksManager.swift +// damus +// +// Created by Joel Klabo on 2/18/23. +// + +import Foundation + +class BookmarksManager { + + private let userDefaults = UserDefaults.standard + private let pubkey: String + + init(pubkey: String) { + self.pubkey = pubkey + } + + var bookmarks: [String] { + get { + return userDefaults.stringArray(forKey: storageKey()) ?? [] + } + set { + let uniqueBookmarks = Array(Set(newValue)) + if uniqueBookmarks != bookmarks { + userDefaults.set(uniqueBookmarks, forKey: storageKey()) + } + } + } + + func isBookmarked(_ string: String) -> Bool { + return bookmarks.contains(string) + } + + func updateBookmark(_ string: String) { + if isBookmarked(string) { + bookmarks = bookmarks.filter { $0 != string } + } else { + bookmarks.append(string) + } + } + + func clearAll() { + bookmarks = [] + } + + private func storageKey() -> String { + pk_setting_key(pubkey, key: "bookmarks") + } +} diff --git a/damus/Util/Notifications.swift b/damus/Util/Notifications.swift @@ -101,6 +101,9 @@ extension Notification.Name { static var update_stats: Notification.Name { return Notification.Name("update_stats") } + static var update_bookmarks: Notification.Name { + return Notification.Name("update_bookmarks") + } } func handle_notify(_ name: Notification.Name) -> NotificationCenter.Publisher { diff --git a/damus/Views/BookmarksView.swift b/damus/Views/BookmarksView.swift @@ -0,0 +1,69 @@ +// +// BookmarksView.swift +// damus +// +// Created by Joel Klabo on 2/18/23. +// + +import SwiftUI + +struct BookmarksView: View { + let state: DamusState + private let noneFilter: (NostrEvent) -> Bool = { _ in true } + private let bookmarksTitle = NSLocalizedString("Bookmarks", comment: "Title of bookmarks view") + + @State private var bookmarkEvents: [NostrEvent] = [] + + init(state: DamusState) { + self.state = state + } + + var body: some View { + Group { + if bookmarkEvents.isEmpty { + VStack { + Image(systemName: "bookmark") + .resizable() + .scaledToFit() + .frame(width: 32.0, height: 32.0) + Text(NSLocalizedString("You have no bookmarks yet, add them in the context menu", comment: "Text indicating that there are no bookmarks to be viewed")) + } + .task { + updateBookmarks() + } + } else { + ScrollView { + InnerTimelineView(events: EventHolder(events: bookmarkEvents, incoming: []), damus: state, show_friend_icon: true, filter: noneFilter) + + } + } + } + .navigationBarTitleDisplayMode(.inline) + .navigationTitle(bookmarksTitle) + .toolbar { + if !bookmarkEvents.isEmpty { + Button(NSLocalizedString("Clear All", comment: "Button for clearing bookmarks data.")) { + BookmarksManager(pubkey: state.pubkey).clearAll() + bookmarkEvents = [] + } + } + } + .onReceive(handle_notify(.update_bookmarks)) { _ in + updateBookmarks() + } + } + + private func updateBookmarks() { + bookmarkEvents = BookmarksManager(pubkey: state.pubkey).bookmarks.compactMap { bookmark_json in + event_from_json(dat: bookmark_json) + } + } +} + +/* +struct BookmarksView_Previews: PreviewProvider { + static var previews: some View { + BookmarksView() + } +} + */ diff --git a/damus/Views/Events/EventMenu.swift b/damus/Views/Events/EventMenu.swift @@ -12,6 +12,8 @@ struct EventMenuContext: View { let keypair: Keypair let target_pubkey: String + @State private var isBookmarked: Bool = false + var body: some View { Button { @@ -37,6 +39,23 @@ struct EventMenuContext: View { } label: { Label(NSLocalizedString("Copy Note JSON", comment: "Context menu option for copying the JSON text from the note."), systemImage: "square.on.square") } + + Button { + let event_json = event_to_json(ev: event) + BookmarksManager(pubkey: keypair.pubkey).updateBookmark(event_json) + isBookmarked = BookmarksManager(pubkey: keypair.pubkey).isBookmarked(event_json) + notify(.update_bookmarks, event) + } label: { + let imageName = isBookmarked ? "bookmark.fill" : "bookmark" + let unBookmarkString = NSLocalizedString("Un-Bookmark", comment: "Context menu option for un-bookmarking a note") + let bookmarkString = NSLocalizedString("Bookmark", comment: "Context menu optoin for bookmarking a note") + Label(isBookmarked ? unBookmarkString : bookmarkString, systemImage: imageName) + } + .onAppear { + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { + isBookmarked = BookmarksManager(pubkey: keypair.pubkey).isBookmarked(event_to_json(ev: event)) + } + } Button { NotificationCenter.default.post(name: .broadcast_event, object: event) diff --git a/damus/Views/SideMenuView.swift b/damus/Views/SideMenuView.swift @@ -100,6 +100,10 @@ struct SideMenuView: View { navLabel(title: NSLocalizedString("Relays", comment: "Sidebar menu label for Relays view."), systemImage: "network") } + NavigationLink(destination: BookmarksView(state: damus_state)) { + navLabel(title: NSLocalizedString("Bookmarks", comment: "Sidebar menu label for Bookmarks view."), systemImage: "bookmark") + } + NavigationLink(destination: ConfigView(state: damus_state)) { navLabel(title: NSLocalizedString("Settings", comment: "Sidebar menu label for accessing the app settings"), systemImage: "gear") }