-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllMessages.swift
57 lines (50 loc) · 1.52 KB
/
AllMessages.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
// AllMessages.swift
// YFTP
//
// Created by Tjerk Dijkstra on 14/05/2021.
//
import SwiftUI
struct AllMessages: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: Message.entity(), sortDescriptors: []
) var archive: FetchedResults<Message>
var body: some View {
HStack() {
VStack(alignment: .leading, spacing: 20) {
List {
if(archive.count == 0){
Text("No messages yet!")
}
ForEach(archive, id: \.body) {
MessageRow(message: $0)
}.onDelete(perform: deleteMessage)
}.listRowBackground(Color.black)
.frame(maxHeight:.infinity)
Spacer()
}.padding(.top, 75)
}
.edgesIgnoringSafeArea(.all)
}
func deleteMessage(at offsets: IndexSet) {
offsets.forEach { index in
let message = self.archive[index]
self.managedObjectContext.delete(message)
}
saveContext()
}
func saveContext() {
do {
try managedObjectContext.save()
} catch {
print("Error saving managed object context: \(error)")
}
}
}
//
//struct AllMessages_Previews: PreviewProvider {
// static var previews: some View {
// AllMessages()
// }
//}