Bookmarks Refactor

- Don't do async loading stuff
- Move bookmarkmanager to damus state
- Remove bookmarks update notififcation and switch to observed object
- Switch api to use events explicitly instead of strings
This commit is contained in:
William Casarin
2023-03-03 11:57:18 -05:00
parent 5423704980
commit b58baca227
12 changed files with 76 additions and 60 deletions

View File

@@ -12,15 +12,20 @@ struct BookmarksView: View {
private let noneFilter: (NostrEvent) -> Bool = { _ in true }
private let bookmarksTitle = NSLocalizedString("Bookmarks", comment: "Title of bookmarks view")
@State private var bookmarkEvents: [NostrEvent] = []
@ObservedObject var manager: BookmarksManager
init(state: DamusState) {
self.state = state
self._manager = ObservedObject(initialValue: state.bookmarks)
}
var bookmarks: [NostrEvent] {
manager.bookmarks
}
var body: some View {
Group {
if bookmarkEvents.isEmpty {
if bookmarks.isEmpty {
VStack {
Image(systemName: "bookmark")
.resizable()
@@ -28,12 +33,9 @@ struct BookmarksView: View {
.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)
InnerTimelineView(events: EventHolder(events: bookmarks, incoming: []), damus: state, show_friend_icon: true, filter: noneFilter)
}
}
@@ -41,22 +43,12 @@ struct BookmarksView: View {
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(bookmarksTitle)
.toolbar {
if !bookmarkEvents.isEmpty {
if !bookmarks.isEmpty {
Button(NSLocalizedString("Clear All", comment: "Button for clearing bookmarks data.")) {
BookmarksManager(pubkey: state.pubkey).clearAll()
bookmarkEvents = []
}
manager.clearAll()
}
}
}
.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)
}
}
}

View File

@@ -24,7 +24,7 @@ struct ChatroomView: View {
next_ev: ind == count-1 ? nil : thread.events[ind+1],
damus_state: damus
)
.event_context_menu(ev, keypair: damus.keypair, target_pubkey: ev.pubkey)
.event_context_menu(ev, keypair: damus.keypair, target_pubkey: ev.pubkey, bookmarks: damus.bookmarks)
.onTapGesture {
if thread.initial_event.id == ev.id {
//dismiss()

View File

@@ -19,7 +19,7 @@ struct DMChatView: View {
VStack(alignment: .leading) {
ForEach(Array(zip(dms.events, dms.events.indices)), id: \.0.id) { (ev, ind) in
DMView(event: dms.events[ind], damus_state: damus_state)
.event_context_menu(ev, keypair: damus_state.keypair, target_pubkey: ev.pubkey)
.event_context_menu(ev, keypair: damus_state.keypair, target_pubkey: ev.pubkey, bookmarks: damus_state.bookmarks)
}
EndBlock(height: 80)
}

View File

@@ -126,9 +126,9 @@ extension View {
}
}
func event_context_menu(_ event: NostrEvent, keypair: Keypair, target_pubkey: String) -> some View {
func event_context_menu(_ event: NostrEvent, keypair: Keypair, target_pubkey: String, bookmarks: BookmarksManager) -> some View {
return self.contextMenu {
EventMenuContext(event: event, keypair: keypair, target_pubkey: target_pubkey)
EventMenuContext(event: event, keypair: keypair, target_pubkey: target_pubkey, bookmarks: bookmarks)
}
}

View File

@@ -23,7 +23,7 @@ struct EmbeddedEventView: View {
EventBody(damus_state: damus_state, event: event, size: .small)
}
.event_context_menu(event, keypair: damus_state.keypair, target_pubkey: pubkey)
.event_context_menu(event, keypair: damus_state.keypair, target_pubkey: pubkey, bookmarks: damus_state.bookmarks)
}
}

View File

@@ -11,9 +11,20 @@ struct EventMenuContext: View {
let event: NostrEvent
let keypair: Keypair
let target_pubkey: String
let bookmarks: BookmarksManager
@State private var isBookmarked: Bool = false
init(event: NostrEvent, keypair: Keypair, target_pubkey: String, bookmarks: BookmarksManager) {
let bookmarked = bookmarks.isBookmarked(event)
self._isBookmarked = State(initialValue: bookmarked)
self.bookmarks = bookmarks
self.event = event
self.keypair = keypair
self.target_pubkey = target_pubkey
}
var body: some View {
Button {
@@ -41,21 +52,14 @@ struct EventMenuContext: View {
}
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)
self.bookmarks.updateBookmark(event)
isBookmarked = self.bookmarks.isBookmarked(event)
} label: {
let imageName = isBookmarked ? "bookmark.fill" : "bookmark"
let removeBookmarkString = NSLocalizedString("Remove Bookmark", comment: "Context menu option for removing a note bookmark.")
let addBookmarkString = NSLocalizedString("Add Bookmark", comment: "Context menu option for adding a note bookmark.")
Label(isBookmarked ? removeBookmarkString : addBookmarkString, 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

@@ -60,7 +60,7 @@ struct SelectedEventView: View {
self.bar.update(damus: self.damus, evid: target)
}
.padding([.leading], 2)
.event_context_menu(event, keypair: damus.keypair, target_pubkey: event.pubkey)
.event_context_menu(event, keypair: damus.keypair, target_pubkey: event.pubkey, bookmarks: damus.bookmarks)
}
}
}

View File

@@ -66,7 +66,7 @@ struct TextEvent: View {
.id(event.id)
.frame(maxWidth: .infinity, minHeight: PFP_SIZE)
.padding([.bottom], 2)
.event_context_menu(event, keypair: damus.keypair, target_pubkey: pubkey)
.event_context_menu(event, keypair: damus.keypair, target_pubkey: pubkey, bookmarks: damus.bookmarks)
}
}