mute: integrate new MutelistManager
This patch is slightly large (I think still within the guidelines tho) , but also pretty straightforward. I thought for a while about how I could split this up in a straightforward way, and I couldn’t come up with anything without breaking intermediate builds. - Deleted a lot of old/unnecessary code (ie. the Collection extension for MuteItem, since we’re now using the MutelistManager sets) - Changed damus_state.contacts to damus_state.mutelist_manager for all mute list manager work - Updated MutelistView to take advantage of new code (this is probably the largest change in this patch) Lighting-Address: fishcharlie@strike.me Signed-off-by: Charlie Fish <contact@charlie.fish> Reviewed-by: William Casarin <jb55@jb55.com> Link: 20240210163650.42884-4-contact@charlie.fish Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
William Casarin
parent
a9baef7a21
commit
1d4d2b0204
@@ -9,12 +9,16 @@ import SwiftUI
|
||||
|
||||
struct MutelistView: View {
|
||||
let damus_state: DamusState
|
||||
@State var mutelist_items: Set<MuteItem> = Set<MuteItem>()
|
||||
@State var show_add_muteitem: Bool = false
|
||||
|
||||
@State var users: [MuteItem] = []
|
||||
@State var hashtags: [MuteItem] = []
|
||||
@State var threads: [MuteItem] = []
|
||||
@State var words: [MuteItem] = []
|
||||
|
||||
func RemoveAction(item: MuteItem) -> some View {
|
||||
Button {
|
||||
guard let mutelist = damus_state.contacts.mutelist,
|
||||
guard let mutelist = damus_state.mutelist_manager.event,
|
||||
let keypair = damus_state.keypair.to_full(),
|
||||
let new_ev = remove_from_mutelist(keypair: keypair,
|
||||
prev: mutelist,
|
||||
@@ -23,74 +27,88 @@ struct MutelistView: View {
|
||||
return
|
||||
}
|
||||
|
||||
damus_state.contacts.set_mutelist(new_ev)
|
||||
damus_state.mutelist_manager.set_mutelist(new_ev)
|
||||
damus_state.postbox.send(new_ev)
|
||||
mutelist_items = new_ev.mute_list ?? Set<MuteItem>()
|
||||
updateMuteItems()
|
||||
} label: {
|
||||
Label(NSLocalizedString("Delete", comment: "Button to remove a user from their mutelist."), image: "delete")
|
||||
}
|
||||
.tint(.red)
|
||||
}
|
||||
|
||||
func updateMuteItems() {
|
||||
users = Array(damus_state.mutelist_manager.users)
|
||||
hashtags = Array(damus_state.mutelist_manager.hashtags)
|
||||
threads = Array(damus_state.mutelist_manager.threads)
|
||||
words = Array(damus_state.mutelist_manager.words)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
Section(NSLocalizedString("Users", comment: "Section header title for a list of muted users.")) {
|
||||
ForEach(mutelist_items.users, id: \.self) { pubkey in
|
||||
UserViewRow(damus_state: damus_state, pubkey: pubkey)
|
||||
.id(pubkey)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .user(pubkey, nil))
|
||||
}
|
||||
.onTapGesture {
|
||||
damus_state.nav.push(route: Route.ProfileByKey(pubkey: pubkey))
|
||||
}
|
||||
ForEach(users, id: \.self) { user in
|
||||
if case let MuteItem.user(pubkey, _) = user {
|
||||
UserViewRow(damus_state: damus_state, pubkey: pubkey)
|
||||
.id(pubkey)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .user(pubkey, nil))
|
||||
}
|
||||
.onTapGesture {
|
||||
damus_state.nav.push(route: Route.ProfileByKey(pubkey: pubkey))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Section(NSLocalizedString("Hashtags", comment: "Section header title for a list of hashtags that are muted.")) {
|
||||
ForEach(mutelist_items.hashtags, id: \.hashtag) { hashtag in
|
||||
Text("#\(hashtag.hashtag)")
|
||||
.id(hashtag.hashtag)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .hashtag(hashtag, nil))
|
||||
}
|
||||
.onTapGesture {
|
||||
damus_state.nav.push(route: Route.Search(search: SearchModel.init(state: damus_state, search: NostrFilter(hashtag: [hashtag.hashtag]))))
|
||||
}
|
||||
ForEach(hashtags, id: \.self) { item in
|
||||
if case let MuteItem.hashtag(hashtag, _) = item {
|
||||
Text("#\(hashtag.hashtag)")
|
||||
.id(hashtag.hashtag)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .hashtag(hashtag, nil))
|
||||
}
|
||||
.onTapGesture {
|
||||
damus_state.nav.push(route: Route.Search(search: SearchModel.init(state: damus_state, search: NostrFilter(hashtag: [hashtag.hashtag]))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Section(NSLocalizedString("Words", comment: "Section header title for a list of words that are muted.")) {
|
||||
ForEach(mutelist_items.words, id: \.self) { word in
|
||||
Text("\(word)")
|
||||
.id(word)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .word(word, nil))
|
||||
}
|
||||
ForEach(words, id: \.self) { item in
|
||||
if case let MuteItem.word(word, _) = item {
|
||||
Text("\(word)")
|
||||
.id(word)
|
||||
.swipeActions {
|
||||
RemoveAction(item: .word(word, nil))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Section(NSLocalizedString("Threads", comment: "Section header title for a list of threads that are muted.")) {
|
||||
ForEach(mutelist_items.threads, id: \.self) { note_id in
|
||||
if let event = damus_state.events.lookup(note_id) {
|
||||
EventView(damus: damus_state, event: event)
|
||||
.id(note_id.hex())
|
||||
.swipeActions {
|
||||
RemoveAction(item: .thread(note_id, nil))
|
||||
}
|
||||
} else {
|
||||
Text(NSLocalizedString("Error retrieving muted event", comment: "Text for an item that application failed to retrieve the muted event for."))
|
||||
ForEach(threads, id: \.self) { item in
|
||||
if case let MuteItem.thread(note_id, _) = item {
|
||||
if let event = damus_state.events.lookup(note_id) {
|
||||
EventView(damus: damus_state, event: event)
|
||||
.id(note_id.hex())
|
||||
.swipeActions {
|
||||
RemoveAction(item: .thread(note_id, nil))
|
||||
}
|
||||
} else {
|
||||
Text(NSLocalizedString("Error retrieving muted event", comment: "Text for an item that application failed to retrieve the muted event for."))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle(NSLocalizedString("Muted", comment: "Navigation title of view to see list of muted users & phrases."))
|
||||
.onAppear {
|
||||
mutelist_items = damus_state.contacts.mutelist?.mute_list ?? Set<MuteItem>()
|
||||
updateMuteItems()
|
||||
}
|
||||
.onReceive(handle_notify(.new_mutes)) { new_mutes in
|
||||
mutelist_items = mutelist_items.union(new_mutes)
|
||||
updateMuteItems()
|
||||
}
|
||||
.onReceive(handle_notify(.new_unmutes)) { new_unmutes in
|
||||
mutelist_items = mutelist_items.subtracting(new_unmutes)
|
||||
updateMuteItems()
|
||||
}
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .topBarTrailing) {
|
||||
@@ -115,11 +133,6 @@ struct MutelistView: View {
|
||||
|
||||
struct MutelistView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
MutelistView(damus_state: test_damus_state, mutelist_items: Set([
|
||||
.user(test_note.pubkey, nil),
|
||||
.hashtag(Hashtag(hashtag: "test"), nil),
|
||||
.word("test", nil),
|
||||
.thread(test_note.id, nil)
|
||||
]))
|
||||
MutelistView(damus_state: test_damus_state)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user