-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamMessagingView.swift
244 lines (212 loc) · 9.35 KB
/
StreamMessagingView.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// StreamMessagingView.swift
// APIExample_RTM2x
//
// Created by BBC on 2024/3/15.
//
import SwiftUI
import AgoraRtmKit
struct StreamMessagingView: View {
@StateObject var agoraRTMVM: StreamMessagingViewModel = StreamMessagingViewModel()
@FocusState private var keyboardIsFocused: Bool
@State var isLoading: Bool = false
// show alert
@State var showAlert: Bool = false
@State var alertMessage: String = "Error"
@State var presentAlertSubscribe = false
@State var newTopic = ""
let columns: [GridItem] = [
GridItem(.flexible()),
GridItem(.flexible()),
]
var serviceIcon: String = "message"
@Binding var path: NavigationPath
var body: some View {
ZStack {
// MARK: LOGIN VIEW
if !agoraRTMVM.isLoggedIn {
LoginRTMView(isLoading: $isLoading, userID: $agoraRTMVM.userID, token: $agoraRTMVM.token, channelName: $agoraRTMVM.mainChannel, isLoggedIn: $agoraRTMVM.isLoggedIn, icon: serviceIcon, isStreamChannel: true, streamToken: $agoraRTMVM.tokenRTC) {
Task {
do{
try await agoraRTMVM.loginRTM()
await agoraRTMVM.createAndJoinStreamChannel()
// await agoraRTMVM.preJoinSubTopics()
}catch {
if let agoraError = error as? AgoraRtmErrorInfo {
alertMessage = "\(agoraError.code) : \(agoraError.reason)"
}else{
alertMessage = error.localizedDescription
}
withAnimation {
isLoading = false
showAlert.toggle()
}
}
}
}
}
// MARK: Display list of subscribed channels
if agoraRTMVM.isLoggedIn {
VStack {
Text("Stream Channel: \(agoraRTMVM.mainChannel)")
.padding()
LazyVGrid(columns: columns, spacing: 16) {
ForEach(agoraRTMVM.customStreamTopicList, id: \.id) { topicChannel in
HStack {
VStack(alignment: .leading) {
Text(topicChannel.topic)
.font(.headline)
.lineLimit(1)
.minimumScaleFactor(0.8)
Text(topicChannel.lastMessage)
.font(.callout)
.foregroundStyle(Color.secondary)
.lineLimit(1)
Text("#\(topicChannel.users.count)")
}
Spacer()
}
.padding(24)
.background(Color.gray.opacity(0.2))
.clipShape(RoundedRectangle(cornerSize: CGSize(width: 12, height: 12)))
.onTapGesture {
path.append(CustomChildNavType.StreamMessagingDetailedView(selectedTopic: topicChannel.topic))
}
}
}
.padding()
Spacer()
// Displayed Logged in Username
Text("Logged in as \(agoraRTMVM.userID)")
}
.onChange(of: agoraRTMVM.isLoggedIn) { oldValue, newValue in
if newValue {
isLoading = false
}
}
}
// MARK: SHOW CUSTOM ALERT
if showAlert {
CustomAlert(displayAlert: $showAlert, title: "Alert", message: alertMessage)
}
}
.navigationBarBackButtonHidden(true)
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(agoraRTMVM.isLoggedIn ? "Topics" : "Login")
.toolbar{
if agoraRTMVM.isLoggedIn {
ToolbarItem(placement: .topBarTrailing){
Button(action: {
withAnimation {
presentAlertSubscribe.toggle()
}
}, label: {
Text("Subscribe")
})
}
}
// Back button
ToolbarItem(placement: .topBarLeading) {
Button(action : {
agoraRTMVM.logoutRTM()
if path.count > 0 {
path.removeLast()
}
}){
HStack{
Image(systemName: "arrow.left")
Text(agoraRTMVM.isLoggedIn ? "Logout" : "Back")
}
}
}
}
.navigationDestination(for: CustomChildNavType.self) { value in
switch value {
case .StreamMessagingDetailedView(let selectedTopic):
StreamMessagingDetailedView(agoraRTMVM: agoraRTMVM, selectedTopic: selectedTopic, path: $path)
default:
Text("ChannelMessagingView Not found")
}
}
.alert("Subscribe", isPresented: $presentAlertSubscribe, actions: {
TextField("Enter channelname", text: $newTopic)
.focused($keyboardIsFocused)
Button("Subscribe", action: {
Task{
keyboardIsFocused = false // dismiss keyboard
if agoraRTMVM.customStreamTopicList.contains(where: { $0.topic == newTopic}) {
return
}
let _ = await agoraRTMVM.JoinAndSubTopic(topic: newTopic)
newTopic = "" //Reset
}
})
Button("Cancel", role: .cancel, action: {})
}, message: {
Text("Subscribe to another channel")
})
}
}
#Preview {
StreamMessagingView(path: .constant(NavigationPath()))
}
// MARK: TO SHOW THE LIST OF MESSAGES OF SPECIFIED CHANNEL
struct StreamMessagingDetailedView: View {
@ObservedObject var agoraRTMVM: StreamMessagingViewModel
@FocusState private var keyboardIsFocused: Bool
@State var selectedTopic: String = ""
@State var message: String = ""
@Binding var path: NavigationPath
var body: some View {
// List of messages
VStack {
// MARK: DISPLAY LIST OF MESSAGES
ScrollViewReader {proxy in
ScrollView{
ForEach(agoraRTMVM.customStreamTopicList.first(where: {$0.topic == selectedTopic})?.messages ?? [], id: \.self) { message in
if message.publisher == agoraRTMVM.userID {
MessageItemLocalView(from: "\(message.publisher) \(message.channelTopic)", message: "\(message.message.stringData ?? "")")
.listRowSeparator(.hidden)
.listItemTint(.clear)
}else{
MessageItemRemoteView(from: "\(message.publisher) \(message.channelTopic)", message: "\(message.message.stringData ?? "")")
.listRowSeparator(.hidden)
.listItemTint(.clear)
}
}
}
.onChange(of: agoraRTMVM.customStreamTopicList.first(where: {$0.topic == selectedTopic})?.messages.count ?? 0) { oldValue, newValue in
withAnimation {
if newValue != 0 {
proxy.scrollTo(newValue-1)
}
}
}
}
// MARK: SEND MESSAGE VIEW
HStack{
TextField("Enter Message", text: $message)
.textFieldStyle(.roundedBorder)
.focused($keyboardIsFocused)
Button(action: {
Task{
let result = await agoraRTMVM.publishToTopic(topic: selectedTopic, message: message)
if result {
message = ""
}
keyboardIsFocused = false // dismiss keyboard
}
}, label: {
Text("Publish")
})
.buttonStyle(.bordered)
.disabled(selectedTopic.isEmpty || message.isEmpty)
}
}
.padding(.horizontal)
.navigationTitle("\(selectedTopic) (\(agoraRTMVM.customStreamTopicList.first(where: {$0.topic == selectedTopic})?.users.count ?? 0))")
}
}
#Preview {
StreamMessagingDetailedView(agoraRTMVM: StreamMessagingViewModel(), path: .constant(NavigationPath()))
}