-
Notifications
You must be signed in to change notification settings - Fork 873
/
Copy pathCallFeedbackController.swift
264 lines (216 loc) · 9.08 KB
/
CallFeedbackController.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// CallFeedbackController.swift
// Telegram
//
// Created by Mikhail Filimonov on 05/10/2020.
// Copyright © 2020 Telegram. All rights reserved.
//
import Cocoa
import TGUIKit
import Postbox
import SwiftSignalKit
import TelegramCore
private final class CallFeedbackControllerArguments {
let updateComment: (String) -> Void
let scrollToComment: () -> Void
let toggleReason: (CallFeedbackReason, Bool) -> Void
let toggleIncludeLogs: (Bool) -> Void
init(updateComment: @escaping (String) -> Void, scrollToComment: @escaping () -> Void, toggleReason: @escaping (CallFeedbackReason, Bool) -> Void, toggleIncludeLogs: @escaping (Bool) -> Void) {
self.updateComment = updateComment
self.scrollToComment = scrollToComment
self.toggleReason = toggleReason
self.toggleIncludeLogs = toggleIncludeLogs
}
}
private enum CallFeedbackReason: Int32, CaseIterable {
case videoDistorted
case videoLowQuality
case echo
case noise
case interruption
case distortedSpeech
case silentLocal
case silentRemote
case dropped
var hashtag: String {
switch self {
case .echo:
return "echo"
case .noise:
return "noise"
case .interruption:
return "interruptions"
case .distortedSpeech:
return "distorted_speech"
case .silentLocal:
return "silent_local"
case .silentRemote:
return "silent_remote"
case .dropped:
return "dropped"
case .videoDistorted:
return "distorted_video"
case .videoLowQuality:
return "pixelated_video"
}
}
var isVideoRelated: Bool {
switch self {
case .videoDistorted, .videoLowQuality:
return true
default:
return false
}
}
var localizedString: String {
switch self {
case .echo:
return strings().callFeedbackReasonEcho
case .noise:
return strings().callFeedbackReasonNoise
case .interruption:
return strings().callFeedbackReasonInterruption
case .distortedSpeech:
return strings().callFeedbackReasonDistortedSpeech
case .silentLocal:
return strings().callFeedbackReasonSilentLocal
case .silentRemote:
return strings().callFeedbackReasonSilentRemote
case .dropped:
return strings().callFeedbackReasonDropped
case .videoDistorted:
return strings().callFeedbackVideoReasonDistorted
case .videoLowQuality:
return strings().callFeedbackVideoReasonLowQuality
}
}
}
private struct CallFeedbackState: Equatable {
let reasons: Set<CallFeedbackReason>
let comment: String
let includeLogs: Bool
init(reasons: Set<CallFeedbackReason> = Set(), comment: String = "", includeLogs: Bool = true) {
self.reasons = reasons
self.comment = comment
self.includeLogs = includeLogs
}
func withUpdatedReasons(_ reasons: Set<CallFeedbackReason>) -> CallFeedbackState {
return CallFeedbackState(reasons: reasons, comment: self.comment, includeLogs: self.includeLogs)
}
func withUpdatedComment(_ comment: String) -> CallFeedbackState {
return CallFeedbackState(reasons: self.reasons, comment: comment, includeLogs: self.includeLogs)
}
func withUpdatedIncludeLogs(_ includeLogs: Bool) -> CallFeedbackState {
return CallFeedbackState(reasons: self.reasons, comment: self.comment, includeLogs: includeLogs)
}
}
private func _id_reason(_ reason: CallFeedbackReason) -> InputDataIdentifier {
return InputDataIdentifier.init("_id_reason_\(reason.hashtag)")
}
private let _id_comment: InputDataIdentifier = InputDataIdentifier("_id_comment")
private let _id_logs: InputDataIdentifier = InputDataIdentifier("_id_logs")
private func callFeedbackControllerEntries(state: CallFeedbackState, isVideo: Bool, arguments: CallFeedbackControllerArguments) -> [InputDataEntry] {
var entries: [InputDataEntry] = []
var sectionId:Int32 = 0
var index: Int32 = 0
// entries.append(.sectionId(sectionId, type: .normal))
// sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().callFeedbackWhatWentWrong), data: .init(color: theme.colors.listGrayText, viewType: .textTopItem)))
index += 1
let reasons = CallFeedbackReason.allCases.filter { value in
if isVideo {
return true
} else if !isVideo && !value.isVideoRelated {
return true
}
return false
}
for reason in reasons {
entries.append(.general(sectionId: sectionId, index: index, value: .none, error: nil, identifier: _id_reason(reason), data: .init(name: reason.localizedString, color: theme.colors.text, type: .switchable(state.reasons.contains(reason)), viewType: bestGeneralViewType(reasons, for: reason), action: {
arguments.toggleReason(reason, !state.reasons.contains(reason))
})))
index += 1
}
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.input(sectionId: sectionId, index: index, value: .string(state.comment), error: nil, identifier: _id_comment, mode: .plain, data: .init(viewType: .singleItem, canMakeTransformations: false), placeholder: nil, inputPlaceholder: strings().callFeedbackAddComment, filter: { $0 }, limit: 255))
index += 1
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
entries.append(.general(sectionId: sectionId, index: index, value: .none, error: nil, identifier: _id_logs, data: .init(name: strings().callFeedbackIncludeLogs, color: theme.colors.text, type: .switchable(state.includeLogs), viewType: .singleItem, action: {
arguments.toggleIncludeLogs(!state.includeLogs)
})))
index += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .plain(strings().callFeedbackIncludeLogsInfo), data: .init(color: theme.colors.listGrayText, viewType: .textBottomItem)))
index += 1
entries.append(.sectionId(sectionId, type: .normal))
sectionId += 1
return entries
}
func CallFeedbackController(context: AccountContext, callId: CallId, starsCount: Int, userInitiated: Bool, isVideo: Bool) -> ModalViewController {
let initialState = CallFeedbackState()
let statePromise = ValuePromise(initialState, ignoreRepeated: true)
let stateValue = Atomic(value: initialState)
let updateState: ((CallFeedbackState) -> CallFeedbackState) -> Void = { f in
statePromise.set(stateValue.modify { f($0) })
}
let arguments = CallFeedbackControllerArguments.init(updateComment: { value in
updateState {
$0.withUpdatedComment(value)
}
}, scrollToComment: {
}, toggleReason: { reason, value in
updateState { current in
var reasons = current.reasons
if value {
reasons.insert(reason)
} else {
reasons.remove(reason)
}
return current.withUpdatedReasons(reasons)
}
}, toggleIncludeLogs: { value in
updateState { $0.withUpdatedIncludeLogs(value) }
})
let signal = statePromise.get() |> map { state in
return InputDataSignalValue(entries: callFeedbackControllerEntries(state: state, isVideo: isVideo, arguments: arguments))
}
let controller = InputDataController(dataSignal: signal, title: strings().callFeedbackTitle)
var close: (()->Void)? = nil
let modalInteractions = ModalInteractions(acceptTitle: strings().modalSend, accept: { [weak controller] in
controller?.validateInputValues()
close?()
}, singleButton: true)
controller.leftModalHeader = ModalHeaderData(image: theme.icons.modalClose, handler: {
close?()
})
controller.updateDatas = { data in
return .none
}
controller.validateData = { data in
let state = stateValue.with { $0 }
var comment = state.comment
var hashtags = ""
for reason in CallFeedbackReason.allCases {
if state.reasons.contains(reason) {
if !hashtags.isEmpty {
hashtags.append(" ")
}
hashtags.append("#\(reason.hashtag)")
}
}
if !comment.isEmpty && !state.reasons.isEmpty {
comment.append("\n")
}
comment.append(hashtags)
let _ = rateCallAndSendLogs(context: context, callId: callId, starsCount: starsCount, comment: comment, userInitiated: userInitiated, includeLogs: state.includeLogs).start()
return .success(.custom({
close?()
}))
}
let modalController = InputDataModalController(controller, modalInteractions: modalInteractions, closeHandler: { f in f() }, size: NSMakeSize(300, 300))
close = { [weak modalController] in
modalController?.close()
}
return modalController
}