Add Bookmarking (Local to device)

Changelog-Added: Bookmarking
Closes: #649
This commit is contained in:
Joel Klabo
2023-02-18 15:41:39 -08:00
committed by William Casarin
parent 87a0bdac94
commit 8b9958a4ad
6 changed files with 153 additions and 0 deletions

View File

@@ -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")
}
}

View File

@@ -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 {

View File

@@ -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()
}
}
*/

View File

@@ -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)

View File

@@ -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")
}