1
1
//
2
2
// ChatView.swift
3
- // Comentor-Neue
3
+ // Comentor
4
4
//
5
5
// Created by 徐嗣苗 on 2023/6/9.
6
6
//
@@ -24,7 +24,32 @@ struct ChatView: View {
24
24
25
25
var body : some View {
26
26
List ( selection: $selection) {
27
- ForEach ( chats) { chat in
27
+ if chats. contains ( where: { $0. isPinned } ) {
28
+ Section ( " Pinned " ) {
29
+ ForEach ( chats. filter ( { $0. isPinned } ) ) { chat in
30
+ ChatListItem ( chat: chat)
31
+ . swipeActions ( edge: . trailing) {
32
+ Button ( role: . destructive) {
33
+ deleteChat ( chat)
34
+ } label: {
35
+ Label ( " Delete " , systemImage: " trash " )
36
+ }
37
+ }
38
+ . swipeActions ( edge: . leading) {
39
+ Button {
40
+ withAnimation {
41
+ chat. isPinned. toggle ( )
42
+ }
43
+ } label: {
44
+ Label ( chat. isPinned ? " Unpin " : " Pin " ,
45
+ systemImage: chat. isPinned ? " pin.slash.fill " : " pin.fill " )
46
+ . tint ( . yellow)
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ ForEach ( chats. filter ( { !$0. isPinned } ) ) { chat in
28
53
ChatListItem ( chat: chat)
29
54
. swipeActions ( edge: . trailing) {
30
55
Button ( role: . destructive) {
@@ -33,6 +58,16 @@ struct ChatView: View {
33
58
Label ( " Delete " , systemImage: " trash " )
34
59
}
35
60
}
61
+ . swipeActions ( edge: . leading) {
62
+ Button {
63
+ withAnimation {
64
+ chat. isPinned. toggle ( )
65
+ }
66
+ } label: {
67
+ Label ( " Pin " , systemImage: chat. isPinned ? " pin.slash.fill " : " pin.fill " )
68
+ . tint ( . yellow)
69
+ }
70
+ }
36
71
}
37
72
}
38
73
. listStyle ( . plain)
@@ -73,19 +108,18 @@ struct ChatView: View {
73
108
actions: {
74
109
TextField ( " Enter title here… " , text: $newChatTitle)
75
110
Button ( " Add " ) {
76
- if !newChatTitle. isEmpty {
77
- withAnimation {
78
- if chats. contains ( where: { $0. title == newChatTitle} ) {
79
- invalidTitle = true
80
- } else {
81
- let newChat = ComentorChat ( newChatTitle)
82
- modelContext. insert ( newChat)
83
- newChatTitle = " "
84
- selection = chats. first ( where: { $0. title == newChat. title} )
85
- }
111
+ withAnimation {
112
+ if chats. contains ( where: { $0. title == newChatTitle} ) {
113
+ invalidTitle = true
114
+ } else {
115
+ let newChat = ComentorChat ( newChatTitle)
116
+ modelContext. insert ( newChat)
117
+ newChatTitle = " "
118
+ selection = chats. first ( where: { $0. title == newChat. title} )
86
119
}
87
120
}
88
121
}
122
+ . disabled ( newChatTitle. isEmpty)
89
123
90
124
Button ( " Cancel " , role: . cancel) {
91
125
newChatTitle = " "
0 commit comments