-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhiteBoardView.swift
152 lines (133 loc) · 5.51 KB
/
WhiteBoardView.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
//
// WhiteBoardView.swift
// APIExample_RTM2x
//
// Created by BBC on 2024/4/24.
//
import SwiftUI
import AgoraRtmKit
struct WhiteBoardView: View {
// Agora RTM
@StateObject var agoraRTMVM: WhiteBoardViewModel = WhiteBoardViewModel()
@State var isLoading: Bool = false
var serviceIcon: String = "hand.draw"
// Show alert
@State var showAlert: Bool = false
@State var alertMessage: String = "Error"
// Whiteboard properties
@State private var currentDrawing: Drawing = Drawing()
@Binding var path: NavigationPath
var body: some View {
ZStack(alignment: .center){
// 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{
// Whiteboar doesn't work properly if streamToken is not inputted
if agoraRTMVM.tokenRTC.isEmpty {
throw customTokenError.tokenEmptyError
}
try await agoraRTMVM.loginRTM()
let _ = await agoraRTMVM.subscribeChannel()
await agoraRTMVM.createAndJoinStreamChannel()
await agoraRTMVM.preJoinSubTopics()
let _ = await agoraRTMVM.getDrawingsFromStorage()
}catch {
if let agoraError = error as? AgoraRtmErrorInfo {
alertMessage = "\(agoraError.code) : \(agoraError.reason)"
}else{
alertMessage = error.localizedDescription
}
withAnimation {
isLoading = false
showAlert.toggle()
}
}
}
}
}
// MARK: Main View
if agoraRTMVM.isLoggedIn {
Canvas(currentDrawing: $currentDrawing, drawings: $agoraRTMVM.drawings) { action in
switch action {
case .delete(let uuid):
Task {
// Publish delete request to remote users
let _ = await agoraRTMVM.publishDeleteDrawing(drawingID: uuid)
}
break
case .submitNewDrawing(let newDrawing):
Task {
// Publish new drawing to remote users
let _ = await agoraRTMVM.publishNewDrawing(drawing: newDrawing)
}
break
case .submitFinishedDrawing(_):
Task {
// Save new finished drawing to Storage
let _ = await agoraRTMVM.saveDrawingsToStorage()
}
break
case .update(let drawingPoint):
Task {
// Publish new points of currentDrawint to remote users
await agoraRTMVM.publishDrawingUpdate(newPoint: drawingPoint)
}
break
case .move(_):
break
}
}
// TESTING
// VStack{
// Text("Fails # \(agoraRTMVM.fails)")
// .onTapGesture {
// print(agoraRTMVM.drawings)
// }
// Spacer()
// }
}
// MARK: SHOW CUSTOM ALERT
if showAlert {
CustomAlert(displayAlert: $showAlert, title: "Alert", message: alertMessage)
}
}
.navigationBarBackButtonHidden(true)
.navigationBarTitleDisplayMode(.inline)
.navigationTitle(agoraRTMVM.isLoggedIn ? "Whiteboard (\(agoraRTMVM.users.count))" : "Login")
.toolbar{
if agoraRTMVM.isLoggedIn {
// Clear All drawings
ToolbarItem(placement: .topBarTrailing) {
Button(action : {
Task {
await agoraRTMVM.publishDeleteAllDrawing()
}
}){
HStack{
Text("Clear all").foregroundStyle(Color.red)
}
}
}
}
// 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")
}
}
}
}
}
}
#Preview {
WhiteBoardView(path: .constant(NavigationPath()))
}