-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserView.swift
54 lines (47 loc) · 1.42 KB
/
UserView.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
//
// UserView.swift
// TestApp2.0
//
// Created by Руслан Плешкунов on 28.10.2024.
//
import SwiftUI
import SwiftData
struct UserView: View {
@Environment(\.modelContext) var modelContext
@Query var users: [DataUser]
var body: some View {
List{
ForEach(users){ user in
NavigationLink(value: user){
VStack(alignment: .listRowSeparatorLeading) {
Text("\(user.firstName) \(user.middleName)")
Text("\(user.username)")
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
}
.onDelete(perform: deleteUser)
}
}
init(searchString: String = "", sortOrder:[SortDescriptor<DataUser>] = []){
_users = Query(filter: #Predicate{ user in
if searchString.isEmpty {
true
} else {
user.firstName.localizedStandardContains(searchString)
|| user.secondName.localizedStandardContains(searchString)
|| user.username.localizedStandardContains(searchString)
}
}, sort: sortOrder)
}
func deleteUser(at offsets: IndexSet) {
for offset in offsets {
let user = users[offset]
modelContext.delete(user)
}
}
}
#Preview {
UserView()
}