-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathChatScreen.swift
203 lines (191 loc) · 6.81 KB
/
ChatScreen.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
///
// ChatScreen.swift
// DittoChat
//
// Created by Eric Turner on 02/24/23.
//
// Copyright © 2023 DittoLive Incorporated. All rights reserved.
import SwiftUI
import PhotosUI
import SwiftUI
struct ChatScreen: View {
@StateObject var viewModel: ChatScreenVM
@EnvironmentObject var errorHandler: ErrorHandler
init(room: Room) {
self._viewModel = StateObject(wrappedValue: ChatScreenVM(room: room))
}
var navBarTitle: String {
if viewModel.isBasicChatScreen {
return appTitleKey
} else {
return viewModel.roomName
}
}
var body: some View {
VStack {
ScrollViewReader { proxy in
ScrollView {
LazyVStack(spacing: 0) {
ForEach(viewModel.messagesWithUsers) { usrMsg in
MessageBubbleView(
messageWithUser: usrMsg,
messagesId: viewModel.room.messagesId,
messageOpCallback: viewModel.messageOperationCallback,
isEditing: $viewModel.isEditing
)
.id(usrMsg.message.id)
.transition(.slide)
}
}
}
#if !os(visionOS)
.scrollDismissesKeyboard(.interactively)
#endif
.onAppear {
DispatchQueue.main.async {
scrollToBottom(proxy: proxy)
}
}
.onChange(of: viewModel.messagesWithUsers.count) { value in
DispatchQueue.main.async {
withAnimation {
scrollToBottom(proxy: proxy)
}
}
}
.onChange(of: viewModel.keyboardStatus) { status in
guard !viewModel.presentEditingView else { return }
if status == .willShow || status == .willHide { return }
withAnimation {
scrollToBottom(proxy: proxy)
}
}
}
HStack(alignment: .center, spacing: 0) {
photosPickerButtonView
ChatInputView(
text: $viewModel.inputText,
onSendButtonTappedCallback: viewModel.sendMessage
)
.padding(.leading, 0)
}
}
.listStyle(.inset)
.navigationTitle(navBarTitle)
.navigationBarTitleDisplayMode(.inline)
.fullScreenCover(
isPresented: $viewModel.presentAttachmentView,
onDismiss: {
viewModel.cleanupAttachmentAttribs()
}
) {
AttachmentPreview(
vm: MessageBubbleVM(
viewModel.attachmentMessage!,
messagesId: viewModel.room.messagesId
),
errorHandler: errorHandler
)
}
.sheet(isPresented: $viewModel.presentShareRoomScreen) {
if let codeStr = viewModel.shareQRCode() {
QRCodeView(
roomName: viewModel.roomName,
codeString: codeStr
)
} else {
NavigationView {
GeneralErrorView(message: AppError.qrCodeFail.localizedDescription)
}
}
}
.sheet(isPresented: $viewModel.presentProfileScreen) {// basic chat mode
ProfileScreen()
}
.sheet(isPresented: $viewModel.presentSettingsView) {// basic chat mode
SettingsScreen()
}
.toolbar {
// basic chat mode
if viewModel.isBasicChatScreen {
ToolbarItemGroup(placement: .navigationBarLeading ) {
Button {
viewModel.presentProfileScreen = true
} label: {
Image(systemName: personCircleKey)
}
Button {
viewModel.presentSettingsView = true
} label: {
Image(systemName: gearShapeKey)
}
}
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
if viewModel.room.isPrivate {
Button {
viewModel.presentShareRoomScreen = true
} label: {
Image(systemName: qrCodeKey)
}
}
}
}
.fullScreenCover(isPresented: $viewModel.presentEditingView) {
if let msgsUsers = try? viewModel.editMessagesWithUsers() {
NavigationView {
MessageEditView(
msgsUsers,
roomName: viewModel.roomName,
saveEditCallback: viewModel.saveEditedTextMessage,
cancelEditCallback: viewModel.cancelEditCallback
)
}
} else {
EmptyView()
}
}
}
var photosPickerButtonView: some View {
PhotosPicker(selection: $viewModel.selectedItem,
matching: .images,
photoLibrary: .shared()
) {
Image(systemName: cameraFillKey)
.symbolRenderingMode(.multicolor)
.font(.system(size: 28))
.foregroundColor(.accentColor)
}
.padding(.leading, 12)
.padding(.trailing, 4)
.frame(width: 56, height: 44)
.buttonStyle(.borderless)
.onChange(of: viewModel.selectedItem) { newValue in
Task {
do {
let imageData = try await newValue?.loadTransferable(type: Data.self)
if let image = UIImage(data: imageData ?? Data()) {
viewModel.selectedImage = image
do {
try await viewModel.sendImageMessage()
} catch {
self.errorHandler.handle(error: error)
}
}
} catch {
self.errorHandler.handle(error: AttachmentError.iCloudLibraryImageFail)
}
}
}
}
func scrollToBottom(proxy: ScrollViewProxy) {
proxy.scrollTo(viewModel.messagesWithUsers.last?.id)
}
}
#if DEBUG
struct ChatScreen_Previews: PreviewProvider {
static var previews: some View {
ChatScreen(room: Room(id: "abc", name: "My Room", messagesId: "def", isPrivate: true))
}
}
#endif