-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathAppConfigViewController.swift
755 lines (673 loc) · 28.7 KB
/
AppConfigViewController.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//
// Copyright © 2025 Stream.io Inc. All rights reserved.
//
import StreamChat
import StreamChatUI
import UIKit
/// The Demo App Configuration.
struct DemoAppConfig {
/// A Boolean value to define if an additional hard delete message action will be added.
var isHardDeleteEnabled: Bool
/// A Boolean value to define if Atlantis will be started to proxy HTTP and WebSocket calls.
var isAtlantisEnabled: Bool
/// A Boolean value to define if an additional message debugger action will be added.
var isMessageDebuggerEnabled: Bool
/// A Boolean value to define if custom location attachments are enabled.
var isLocationAttachmentsEnabled: Bool
/// Set this value to define if we should mimic token refresh scenarios.
var tokenRefreshDetails: TokenRefreshDetails?
/// A Boolean value that determines if a connection banner UI should be shown.
var shouldShowConnectionBanner: Bool
/// A Boolean value to define if the premium member feature is enabled. This is to test custom member data.
var isPremiumMemberFeatureEnabled: Bool
/// A Boolean value to define if the reminders feature is enabled.
var isRemindersEnabled: Bool
/// The details to generate expirable tokens in the demo app.
struct TokenRefreshDetails {
/// The app secret from the dashboard.
let appSecret: String
/// The duration in seconds until the token is expired.
let expirationDuration: TimeInterval
/// In order to test token refresh fails, we can set a value of how
/// many token refresh will fail before a successful one.
let numberOfFailures: Int
}
}
class AppConfig {
/// The Demo App Configuration.
var demoAppConfig: DemoAppConfig
static var shared = AppConfig()
private init() {
// Default DemoAppConfig
demoAppConfig = DemoAppConfig(
isHardDeleteEnabled: false,
isAtlantisEnabled: false,
isMessageDebuggerEnabled: false,
isLocationAttachmentsEnabled: false,
tokenRefreshDetails: nil,
shouldShowConnectionBanner: false,
isPremiumMemberFeatureEnabled: false,
isRemindersEnabled: true
)
if StreamRuntimeCheck.isStreamInternalConfiguration {
demoAppConfig.isAtlantisEnabled = true
demoAppConfig.isMessageDebuggerEnabled = true
demoAppConfig.isLocationAttachmentsEnabled = true
demoAppConfig.isLocationAttachmentsEnabled = true
demoAppConfig.isHardDeleteEnabled = true
demoAppConfig.shouldShowConnectionBanner = true
demoAppConfig.isPremiumMemberFeatureEnabled = true
demoAppConfig.isRemindersEnabled = true
StreamRuntimeCheck.assertionsEnabled = true
}
}
}
class UserConfig {
var isInvisible = false
var language: TranslationLanguage?
var typingIndicatorsEnabled: Bool?
var readReceiptsEnabled: Bool?
static var shared = UserConfig()
private init() {}
}
class AppConfigViewController: UITableViewController {
var demoAppConfig: DemoAppConfig {
get { AppConfig.shared.demoAppConfig }
set {
AppConfig.shared.demoAppConfig = newValue
tableView.reloadData()
}
}
var chatClientConfig: ChatClientConfig {
get { StreamChatWrapper.shared.config }
set {
StreamChatWrapper.shared.config = newValue
tableView.reloadData()
}
}
var channelListSearchStrategy: ChannelListSearchStrategy? {
get {
Components.default.channelListSearchStrategy
}
set {
Components.default.channelListSearchStrategy = newValue
}
}
init() {
super.init(style: .grouped)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
enum ConfigOption {
case info([DemoAppInfoOption])
case demoApp([DemoAppConfigOption])
case components([ComponentsConfigOption])
case chatClient([ChatClientConfigOption])
case user([UserConfigOption])
var numberOfOptions: Int {
switch self {
case let .info(options):
return options.count
case let .demoApp(options):
return options.count
case let .components(options):
return options.count
case let .chatClient(options):
return options.count
case let .user(options):
return options.count
}
}
var sectionTitle: String {
switch self {
case .demoApp:
return "Demo App Configuration"
case .components:
return "Components Configuration"
case .chatClient:
return "Chat Client Configuration"
case .info:
return "General Info"
case .user:
return "User Info"
}
}
}
enum DemoAppInfoOption: CustomStringConvertible, CaseIterable {
case environment
case pushConfiguration
var description: String {
switch self {
case .environment:
return "App Key"
case .pushConfiguration:
let configuration = Bundle.pushProviderName ?? "Not set"
return "Push Configuration: \(configuration)"
}
}
}
enum DemoAppConfigOption: String, CaseIterable {
case isHardDeleteEnabled
case isAtlantisEnabled
case isMessageDebuggerEnabled
case isLocationAttachmentsEnabled
case tokenRefreshDetails
case shouldShowConnectionBanner
case isPremiumMemberFeatureEnabled
case isRemindersEnabled
}
enum ComponentsConfigOption: String, CaseIterable {
case isUniqueReactionsEnabled
case shouldMessagesStartAtTheTop
case shouldAnimateJumpToMessageWhenOpeningChannel
case shouldJumpToUnreadWhenOpeningChannel
case threadRepliesStartFromOldest
case threadRendersParentMessageEnabled
case isVoiceRecordingEnabled
case isVoiceRecordingConfirmationRequiredEnabled
case channelListSearchStrategy
case isUnreadMessageSeparatorEnabled
case isJumpToUnreadEnabled
case mentionAllAppUsers
case isBlockingUsersEnabled
case isMessageListAnimationsEnabled
case isDownloadFileAttachmentsEnabled
}
enum ChatClientConfigOption: String, CaseIterable {
case baseURL
case isLocalStorageEnabled
case staysConnectedInBackground
case reconnectionTimeout
case shouldShowShadowedMessages
case deletedMessagesVisibility
case isChannelAutomaticFilteringEnabled
}
enum UserConfigOption: String, CaseIterable {
case isInvisible
case language
case typingIndicatorsEnabled
case readReceiptsEnabled
}
let options: [ConfigOption] = [
.info(DemoAppInfoOption.allCases),
.demoApp(DemoAppConfigOption.allCases),
.components(ComponentsConfigOption.allCases),
.chatClient(ChatClientConfigOption.allCases),
.user(UserConfigOption.allCases)
]
override func viewDidLoad() {
super.viewDidLoad()
title = "Configuration"
}
// MARK: Table View Data Source
override func numberOfSections(in tableView: UITableView) -> Int {
options.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
options[section].numberOfOptions
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
options[section].sectionTitle
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
switch options[indexPath.section] {
case let .info(options):
configureDemoAppInfoCell(cell, at: indexPath, options: options)
case let .demoApp(options):
configureDemoAppOptionsCell(cell, at: indexPath, options: options)
case let .components(options):
configureComponentsOptionsCell(cell, at: indexPath, options: options)
case let .chatClient(options):
configureChatClientOptionsCell(cell, at: indexPath, options: options)
case let .user(options):
configureUserOptionsCell(cell, at: indexPath, options: options)
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) else { return }
switch options[indexPath.section] {
case let .info(options):
didSelectInfoOptionsCell(cell, at: indexPath, options: options)
case let .components(options):
didSelectComponentsOptionsCell(cell, at: indexPath, options: options)
case let .chatClient(options):
didSelectChatClientOptionsCell(cell, at: indexPath, options: options)
case let .user(options):
didSelectUserOptionsCell(cell, at: indexPath, options: options)
case let .demoApp(options):
didSelectDemoAppOptionsCell(cell, at: indexPath, options: options)
}
}
// MAKR: - Demo App Info
private func configureDemoAppInfoCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [DemoAppInfoOption]
) {
let option = options[indexPath.row]
cell.textLabel?.text = option.description
switch option {
case .environment:
cell.accessoryType = .disclosureIndicator
cell.detailTextLabel?.text = apiKeyString
case .pushConfiguration:
break
}
}
// MARK: - Demo App Options
private func configureDemoAppOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [DemoAppConfigOption]
) {
let option = options[indexPath.row]
cell.textLabel?.text = option.rawValue
switch option {
case .isHardDeleteEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isHardDeleteEnabled) { [weak self] newValue in
self?.demoAppConfig.isHardDeleteEnabled = newValue
}
case .isAtlantisEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isAtlantisEnabled) { [weak self] newValue in
self?.demoAppConfig.isAtlantisEnabled = newValue
}
case .isMessageDebuggerEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isMessageDebuggerEnabled) { [weak self] newValue in
self?.demoAppConfig.isMessageDebuggerEnabled = newValue
}
case .isLocationAttachmentsEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isLocationAttachmentsEnabled) { [weak self] newValue in
self?.demoAppConfig.isLocationAttachmentsEnabled = newValue
}
case .tokenRefreshDetails:
if let tokenRefreshDuration = demoAppConfig.tokenRefreshDetails?.expirationDuration {
cell.detailTextLabel?.text = "Duration before expired: \(tokenRefreshDuration)s"
} else {
cell.detailTextLabel?.text = "Disabled"
}
cell.accessoryType = .none
case .shouldShowConnectionBanner:
cell.accessoryView = makeSwitchButton(demoAppConfig.shouldShowConnectionBanner) { [weak self] newValue in
self?.demoAppConfig.shouldShowConnectionBanner = newValue
}
case .isPremiumMemberFeatureEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isPremiumMemberFeatureEnabled) { [weak self] newValue in
self?.demoAppConfig.isPremiumMemberFeatureEnabled = newValue
}
case .isRemindersEnabled:
cell.accessoryView = makeSwitchButton(demoAppConfig.isRemindersEnabled) { [weak self] newValue in
self?.demoAppConfig.isRemindersEnabled = newValue
}
}
}
// MARK: - Chat Client Options
private func configureChatClientOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [ChatClientConfigOption]
) {
let option = options[indexPath.row]
cell.textLabel?.text = option.rawValue
switch option {
case .baseURL:
cell.detailTextLabel?.text = chatClientConfig.baseURL.description
cell.accessoryType = .disclosureIndicator
case .isLocalStorageEnabled:
cell.accessoryView = makeSwitchButton(chatClientConfig.isLocalStorageEnabled) { [weak self] newValue in
self?.chatClientConfig.isLocalStorageEnabled = newValue
}
case .staysConnectedInBackground:
cell.accessoryView = makeSwitchButton(chatClientConfig.staysConnectedInBackground) { [weak self] newValue in
self?.chatClientConfig.staysConnectedInBackground = newValue
}
case .reconnectionTimeout:
cell.detailTextLabel?.text = chatClientConfig.reconnectionTimeout.map { "\($0)" } ?? "None"
cell.accessoryType = .disclosureIndicator
case .shouldShowShadowedMessages:
cell.accessoryView = makeSwitchButton(chatClientConfig.shouldShowShadowedMessages) { [weak self] newValue in
self?.chatClientConfig.shouldShowShadowedMessages = newValue
}
case .deletedMessagesVisibility:
cell.detailTextLabel?.text = chatClientConfig.deletedMessagesVisibility.description
cell.accessoryType = .disclosureIndicator
case .isChannelAutomaticFilteringEnabled:
cell.accessoryView = makeSwitchButton(chatClientConfig.isChannelAutomaticFilteringEnabled) { [weak self] newValue in
self?.chatClientConfig.isChannelAutomaticFilteringEnabled = newValue
}
}
}
private func didSelectChatClientOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [ChatClientConfigOption]
) {
let option = options[indexPath.row]
switch option {
case .baseURL:
showBaseURLInputAlert()
case .deletedMessagesVisibility:
pushDeletedMessagesVisibilitySelectorVC()
case .reconnectionTimeout:
pushReconnectionTimeoutSelectorVC()
default:
break
}
}
// MARK: User Options
private func configureUserOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [UserConfigOption]
) {
let option = options[indexPath.row]
cell.textLabel?.text = option.rawValue
switch option {
case .isInvisible:
cell.accessoryView = makeSwitchButton(UserConfig.shared.isInvisible) { newValue in
UserConfig.shared.isInvisible = newValue
}
case .readReceiptsEnabled:
cell.accessoryView = makeSwitchButton(UserConfig.shared.readReceiptsEnabled ?? true) { newValue in
UserConfig.shared.readReceiptsEnabled = newValue
}
case .typingIndicatorsEnabled:
cell.accessoryView = makeSwitchButton(UserConfig.shared.typingIndicatorsEnabled ?? true) { newValue in
UserConfig.shared.typingIndicatorsEnabled = newValue
}
case .language:
cell.detailTextLabel?.text = UserConfig.shared.language?.languageCode
cell.accessoryType = .disclosureIndicator
}
}
// MARK: Components Options
private func configureComponentsOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [ComponentsConfigOption]
) {
let option = options[indexPath.row]
cell.textLabel?.text = option.rawValue
switch option {
case .isUniqueReactionsEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isUniqueReactionsEnabled) { newValue in
Components.default.isUniqueReactionsEnabled = newValue
}
case .shouldMessagesStartAtTheTop:
cell.accessoryView = makeSwitchButton(Components.default.shouldMessagesStartAtTheTop) { newValue in
Components.default.shouldMessagesStartAtTheTop = newValue
}
case .shouldAnimateJumpToMessageWhenOpeningChannel:
cell.accessoryView = makeSwitchButton(Components.default.shouldAnimateJumpToMessageWhenOpeningChannel) { newValue in
Components.default.shouldAnimateJumpToMessageWhenOpeningChannel = newValue
}
case .shouldJumpToUnreadWhenOpeningChannel:
cell.accessoryView = makeSwitchButton(Components.default.shouldJumpToUnreadWhenOpeningChannel) { newValue in
Components.default.shouldJumpToUnreadWhenOpeningChannel = newValue
}
case .threadRepliesStartFromOldest:
cell.accessoryView = makeSwitchButton(Components.default.threadRepliesStartFromOldest) { newValue in
Components.default.threadRepliesStartFromOldest = newValue
}
case .threadRendersParentMessageEnabled:
cell.accessoryView = makeSwitchButton(Components.default.threadRendersParentMessageEnabled) { newValue in
Components.default.threadRendersParentMessageEnabled = newValue
}
case .isVoiceRecordingEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isVoiceRecordingEnabled) { newValue in
Components.default.isVoiceRecordingEnabled = newValue
}
case .isVoiceRecordingConfirmationRequiredEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isVoiceRecordingConfirmationRequiredEnabled) { newValue in
Components.default.isVoiceRecordingConfirmationRequiredEnabled = newValue
}
case .channelListSearchStrategy:
cell.detailTextLabel?.text = channelListSearchStrategy?.name ?? "none"
cell.accessoryType = .disclosureIndicator
case .isUnreadMessageSeparatorEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isUnreadMessagesSeparatorEnabled) { newValue in
Components.default.isUnreadMessagesSeparatorEnabled = newValue
}
case .isJumpToUnreadEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isJumpToUnreadEnabled) { newValue in
Components.default.isJumpToUnreadEnabled = newValue
}
case .mentionAllAppUsers:
cell.accessoryView = makeSwitchButton(Components.default.mentionAllAppUsers) { newValue in
Components.default.mentionAllAppUsers = newValue
}
case .isBlockingUsersEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isBlockingUsersEnabled) { newValue in
Components.default.isBlockingUsersEnabled = newValue
}
case .isMessageListAnimationsEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isMessageListAnimationsEnabled) { newValue in
Components.default.isMessageListAnimationsEnabled = newValue
}
case .isDownloadFileAttachmentsEnabled:
cell.accessoryView = makeSwitchButton(Components.default.isDownloadFileAttachmentsEnabled) { newValue in
Components.default.isDownloadFileAttachmentsEnabled = newValue
}
}
}
private func didSelectInfoOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [DemoAppInfoOption]
) {
let option = options[indexPath.row]
switch option {
case .environment:
pushEnvironmentSelectorVC()
case .pushConfiguration:
break
}
}
private func didSelectComponentsOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [ComponentsConfigOption]
) {
let option = options[indexPath.row]
switch option {
case .channelListSearchStrategy:
pushChannelListSearchStrategySelectorVC()
default:
break
}
}
private func didSelectUserOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [UserConfigOption]
) {
let option = options[indexPath.row]
switch option {
case .language:
pushUserLanguageSelectorVC()
default:
break
}
}
private func didSelectDemoAppOptionsCell(
_ cell: UITableViewCell,
at indexPath: IndexPath,
options: [DemoAppConfigOption]
) {
let option = options[indexPath.row]
switch option {
case .tokenRefreshDetails:
showTokenDetailsAlert()
default:
break
}
}
// MARK: - Helpers
private func makeSwitchButton(_ initialValue: Bool, _ didChangeValue: @escaping (Bool) -> Void) -> SwitchButton {
let switchButton = SwitchButton()
switchButton.isOn = initialValue
switchButton.didChangeValue = didChangeValue
return switchButton
}
private func pushDeletedMessagesVisibilitySelectorVC() {
let selectorViewController = OptionsSelectorViewController(
options: [.alwaysHidden, .alwaysVisible, .visibleForCurrentUser],
initialSelectedOptions: [chatClientConfig.deletedMessagesVisibility],
allowsMultipleSelection: false
)
selectorViewController.didChangeSelectedOptions = { [weak self] options in
guard let selectedOption = options.first else { return }
self?.chatClientConfig.deletedMessagesVisibility = selectedOption
}
navigationController?.pushViewController(selectorViewController, animated: true)
}
private func pushChannelListSearchStrategySelectorVC() {
let selectorViewController = OptionsSelectorViewController(
options: [ChannelListSearchStrategy.channels.name, ChannelListSearchStrategy.messages.name, nil],
initialSelectedOptions: [channelListSearchStrategy?.name],
allowsMultipleSelection: false,
optionFormatter: { option in
option ?? "none"
}
)
selectorViewController.didChangeSelectedOptions = { [weak self] options in
guard let selectedOption = options.first else { return }
if selectedOption == ChannelListSearchStrategy.channels.name {
self?.channelListSearchStrategy = ChannelListSearchStrategy.channels
} else if selectedOption == ChannelListSearchStrategy.messages.name {
self?.channelListSearchStrategy = ChannelListSearchStrategy.messages
} else {
self?.channelListSearchStrategy = nil
}
self?.tableView.reloadData()
}
navigationController?.pushViewController(selectorViewController, animated: true)
}
private func pushUserLanguageSelectorVC() {
let selectorViewController = OptionsSelectorViewController(
options: TranslationLanguage.allCases,
initialSelectedOptions: [nil],
allowsMultipleSelection: false,
optionFormatter: { option in
option?.languageCode ?? "nil"
}
)
selectorViewController.didChangeSelectedOptions = { [weak self] options in
guard let selectedOption = options.first else { return }
UserConfig.shared.language = selectedOption
self?.tableView.reloadData()
}
navigationController?.pushViewController(selectorViewController, animated: true)
}
private func pushReconnectionTimeoutSelectorVC() {
let selectorViewController = OptionsSelectorViewController<TimeInterval?>(
options: [nil, 15.0, 30.0, 45.0, 60.0],
initialSelectedOptions: [chatClientConfig.reconnectionTimeout],
allowsMultipleSelection: false,
optionFormatter: { option in
option.map { "\($0)" } ?? "None"
}
)
selectorViewController.didChangeSelectedOptions = { [weak self] options in
guard let selectedOption = options.first else { return }
self?.chatClientConfig.reconnectionTimeout = selectedOption
self?.tableView.reloadData()
}
navigationController?.pushViewController(selectorViewController, animated: true)
}
private func showBaseURLInputAlert() {
let alert = UIAlertController(
title: "Base URL",
message: "Input the base URL for the Chat Client.",
preferredStyle: .alert
)
alert.addTextField { textField in
textField.placeholder = "Base URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.text = self.chatClientConfig.baseURL.description
textField.textContentType = .URL
}
alert.addAction(.init(title: "Set", style: .default, handler: { _ in
guard let urlString = alert.textFields?.first?.text,
let url = URL(string: urlString)
else {
return
}
self.chatClientConfig.baseURL = .init(url: url)
self.tableView.reloadData()
}))
alert.addAction(.init(title: "Cancel", style: .destructive, handler: nil))
present(alert, animated: true, completion: nil)
}
private func showTokenDetailsAlert() {
let alert = UIAlertController(
title: "Token Refreshing",
message: "Input the app secret from Stream's Dashboard and the desired duration.",
preferredStyle: .alert
)
alert.addTextField { textField in
textField.placeholder = "App Secret"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
if let appSecret = self.demoAppConfig.tokenRefreshDetails?.appSecret {
textField.text = appSecret
}
}
alert.addTextField { textField in
textField.placeholder = "Expiration duration (Seconds)"
textField.keyboardType = .numberPad
if let duration = self.demoAppConfig.tokenRefreshDetails?.expirationDuration {
textField.text = "\(duration)"
}
}
alert.addTextField { textField in
textField.placeholder = "Number of refresh fails"
textField.keyboardType = .numberPad
if let numberOfRefreshes = self.demoAppConfig.tokenRefreshDetails?.numberOfFailures {
textField.text = "\(numberOfRefreshes)"
}
}
alert.addAction(.init(title: "Enable", style: .default, handler: { _ in
guard let appSecret = alert.textFields?[0].text else { return }
guard let duration = alert.textFields?[1].text else { return }
guard let numberOfFailures = alert.textFields?[2].text else { return }
self.demoAppConfig.tokenRefreshDetails = .init(
appSecret: appSecret,
expirationDuration: TimeInterval(duration) ?? 60,
numberOfFailures: Int(numberOfFailures) ?? 0
)
}))
alert.addAction(.init(title: "Disable", style: .destructive, handler: { _ in
self.demoAppConfig.tokenRefreshDetails = nil
}))
present(alert, animated: true, completion: nil)
}
private func pushEnvironmentSelectorVC() {
let selectorViewController = OptionsSelectorViewController<DemoApiKeys>(
options: [.frankfurtC1, .frankfurtC2, .usEastC6],
initialSelectedOptions: [DemoApiKeys(rawValue: apiKeyString)],
allowsMultipleSelection: false,
optionFormatter: { option in
var optionName = option.rawValue
if let appName = option.appName {
optionName += " (\(appName))"
}
return optionName
}
)
selectorViewController.didChangeSelectedOptions = { [weak self] options in
guard let selectedOption = options.first else { return }
apiKeyString = selectedOption.rawValue
StreamChatWrapper.replaceSharedInstance(apiKeyString: apiKeyString)
if let baseURL = selectedOption.customBaseURL {
self?.chatClientConfig.baseURL = .init(url: baseURL)
}
self?.tableView.reloadData()
}
navigationController?.pushViewController(selectorViewController, animated: true)
}
}