mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
Delete user messages
This commit is contained in:
parent
fb1c943c38
commit
0a90ccaa54
2 changed files with 54 additions and 3 deletions
|
|
@ -75,6 +75,21 @@ public func deleteChannelMessages(channelIndex: Int32, context: NSManagedObjectC
|
|||
}
|
||||
}
|
||||
|
||||
public func deleteUserMessages(user: UserEntity, context: NSManagedObjectContext) {
|
||||
|
||||
let fetchUserMessagesRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "MessageEntity")
|
||||
fetchUserMessagesRequest.predicate = NSPredicate(format: "((toUser.num == %lld) OR (fromUser.num == %lld)) AND toUser != nil AND fromUser != nil AND admin == false", Int64(user.num), Int64(user.num))
|
||||
do {
|
||||
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchUserMessagesRequest)
|
||||
try context.executeAndMergeChanges(using: deleteRequest)
|
||||
try context.save()
|
||||
|
||||
} catch let error as NSError {
|
||||
print("Error: \(error.localizedDescription)")
|
||||
abort()
|
||||
}
|
||||
}
|
||||
|
||||
public func clearCoreDataDatabase(context: NSManagedObjectContext) {
|
||||
|
||||
let persistenceController = PersistenceController.shared.container
|
||||
|
|
|
|||
|
|
@ -20,8 +20,9 @@ struct Contacts: View {
|
|||
|
||||
private var users: FetchedResults<UserEntity>
|
||||
@State var node: NodeInfoEntity? = nil
|
||||
|
||||
@State private var selection: UserEntity? = nil // Nothing selected by default.
|
||||
@State private var isPresentingDeleteChannelMessagesConfirm: Bool = false
|
||||
@State private var isPresentingDeleteUserMessagesConfirm: Bool = false
|
||||
|
||||
var body: some View {
|
||||
|
||||
|
|
@ -83,11 +84,24 @@ struct Contacts: View {
|
|||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: 80, alignment: .leading)
|
||||
.contextMenu {
|
||||
if channel.allPrivateMessages.count > 0 {
|
||||
Button(role: .destructive) {
|
||||
isPresentingDeleteChannelMessagesConfirm = true
|
||||
} label: {
|
||||
Label("Delete Messages", systemImage: "trash")
|
||||
}
|
||||
}
|
||||
}
|
||||
.confirmationDialog(
|
||||
"This conversation will be deleted.",
|
||||
isPresented: $isPresentingDeleteChannelMessagesConfirm,
|
||||
titleVisibility: .visible
|
||||
) {
|
||||
|
||||
Button(role: .destructive) {
|
||||
print("I want to delete all the messages for \(channel.name ?? String(channel.index))")
|
||||
deleteChannelMessages(channelIndex: channel.index, context: context)
|
||||
} label: {
|
||||
Label("Delete Messages", systemImage: "trash")
|
||||
Text("Delete")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -143,6 +157,28 @@ struct Contacts: View {
|
|||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: 80, alignment: .leading)
|
||||
.contextMenu {
|
||||
if user.messageList.count > 0 {
|
||||
Button(role: .destructive) {
|
||||
print("I want to delete all the messages for \(user.longName)")
|
||||
isPresentingDeleteUserMessagesConfirm = true
|
||||
} label: {
|
||||
Label("Delete Messages", systemImage: "trash")
|
||||
}
|
||||
}
|
||||
}
|
||||
.confirmationDialog(
|
||||
"This conversation will be deleted.",
|
||||
isPresented: $isPresentingDeleteUserMessagesConfirm,
|
||||
titleVisibility: .visible
|
||||
) {
|
||||
|
||||
Button(role: .destructive) {
|
||||
deleteUserMessages(user: user, context: context)
|
||||
} label: {
|
||||
Text("Delete")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue