1
1
//
2
- // OpenChannelViewController .swift
2
+ // BaseJoinChannelViewController .swift
3
3
//
4
4
// BeagleIM
5
- // Copyright (C)
2020 "Tigase, Inc." <
[email protected] >
5
+ // Copyright (C)
2022 "Tigase, Inc." <
[email protected] >
6
6
//
7
7
// This program is free software: you can redistribute it and/or modify
8
8
// it under the terms of the GNU General Public License as published by
19
19
// If not, see https://www.gnu.org/licenses/.
20
20
//
21
21
22
+ import Foundation
22
23
import AppKit
23
24
import TigaseSwift
24
25
25
- class OpenChannelViewController : NSViewController , OpenChannelViewControllerTabViewDelegate {
26
+ class BaseJoinChannelViewController : NSViewController {
26
27
27
- private var tabView : NSTabView ? {
28
- didSet {
29
- // (tabView?.selectedTabViewItem?.view as? OpenChannelViewControllerTabView)?.delegate = self;
30
- showDisclosure ( false ) ;
31
- if let tabView = self . tabView {
32
- for viewItem in tabView. tabViewItems {
33
- if let view = viewItem. view as? OpenChannelViewControllerTabView {
34
- view. delegate = self ;
35
- }
36
- }
37
- }
38
- }
39
- }
40
28
@IBOutlet var accountButton : NSPopUpButton ! ;
41
29
@IBOutlet var componentDomainField : NSTextField ! ;
42
30
@IBOutlet var progressIndication : NSProgressIndicator ! ;
43
-
44
- private var accountHeightConstraint : NSLayoutConstraint ! ;
45
-
46
- var account : BareJID ? {
47
- didSet {
48
- if let tabView = self . tabView {
49
- for viewItem in tabView. tabViewItems {
50
- if let view = viewItem. view as? OpenChannelViewControllerTabView {
51
- view. account = self . account;
52
- }
53
- }
54
- }
55
- }
56
- }
57
-
58
31
@IBOutlet var submitButton : NSButton ! ;
59
32
60
- private var components : [ Component ] = [ ] {
61
- didSet {
62
- if let tabView = self . tabView {
63
- for viewItem in tabView. tabViewItems {
64
- if let view = viewItem. view as? OpenChannelViewControllerTabView {
65
- view. components = self . components;
66
- }
67
- }
68
- }
69
- }
70
- }
33
+ private var accountHeightConstraint : NSLayoutConstraint ! ;
34
+
35
+ var account : BareJID ? ;
36
+ var components : [ Component ] = [ ] ;
71
37
72
38
override func viewDidLoad( ) {
73
39
super. viewDidLoad ( ) ;
74
40
accountHeightConstraint = accountButton. heightAnchor. constraint ( equalToConstant: 0 ) ;
75
41
}
76
42
77
43
override func viewWillAppear( ) {
78
- self . submitButton. title = tabView? . selectedTabViewItem? . label ?? " " ;
79
44
self . accountButton. addItem ( withTitle: " " ) ;
80
- if let tabView = self . tabView {
81
- for viewItem in tabView. tabViewItems {
82
- if let view = viewItem. view as? OpenChannelViewControllerTabView {
83
- view. delegate = self ;
84
- }
85
- }
86
- }
87
45
AccountManager . getAccounts ( ) . filter { account -> Bool in
88
46
return XmppService . instance. getClient ( for: account) != nil
89
47
} . forEach { ( account) in
@@ -100,19 +58,11 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
100
58
self . accountButton. title = self . accountButton. itemTitle ( at: 1 ) ;
101
59
self . account = BareJID ( self . accountButton. itemTitle ( at: 1 ) ) ;
102
60
}
61
+ showDisclosure ( AccountManager . getAccounts ( ) . count != 1 ) ;
103
62
findComponents ( ) ;
104
- self . updateSubmitState ( ) ;
63
+ updateSubmitState ( ) ;
105
64
}
106
65
107
- // func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
108
- // submitButton.title = tabViewItem?.label ?? "";
109
- //// (tabViewItem?.view as? OpenChannelViewControllerTabView)?.delegate = self;
110
- // (tabViewItem?.view as? OpenChannelViewControllerTabView)?.account = self.account;
111
- // (tabViewItem?.view as? OpenChannelViewControllerTabView)?.components = self.components;
112
- // (tabViewItem?.view as? OpenChannelViewControllerTabView)?.disclosureChanged(state: !accountHeightConstraint.isActive);
113
- // self.updateSubmitState();
114
- // }
115
-
116
66
@IBAction func accountSelectionChanged( _ sender: NSPopUpButton ) {
117
67
guard let title = sender. selectedItem? . title else {
118
68
return ;
@@ -140,39 +90,18 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
140
90
@IBAction func disclosureChangedState( _ sender: NSButton ) {
141
91
showDisclosure ( sender. state == . on) ;
142
92
}
143
-
93
+
144
94
func showDisclosure( _ state: Bool ) {
145
95
accountButton. isHidden = !state;
146
96
accountHeightConstraint. isActive = !state;
147
- if let tabView = self . tabView {
148
- for viewItem in tabView. tabViewItems {
149
- if let view = viewItem. view as? OpenChannelViewControllerTabView {
150
- view. disclosureChanged ( state: state) ;
151
- }
152
- }
153
- }
154
97
}
155
98
156
99
@IBAction func cancelClicked( _ sender: NSButton ) {
157
- guard let view = self . tabView? . selectedTabViewItem? . view as? OpenChannelViewControllerTabView else {
158
- close ( ) ;
159
- return ;
160
- }
161
- view. cancelClicked ( completionHandler: {
162
- self . close ( )
163
- } )
100
+ close ( ) ;
164
101
}
165
102
166
103
@IBAction func submitClicked( _ sender: NSButton ) {
167
- guard let view = self . tabView? . selectedTabViewItem? . view as? OpenChannelViewControllerTabView else {
168
- close ( ) ;
169
- return ;
170
- }
171
- view. submitClicked ( completionHandler: { result in
172
- if result {
173
- self . close ( )
174
- }
175
- } )
104
+ close ( ) ;
176
105
}
177
106
178
107
func operationStarted( ) {
@@ -184,25 +113,25 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
184
113
}
185
114
186
115
func updateSubmitState( ) {
187
- submitButton. isEnabled = ( self . tabView ? . selectedTabViewItem ? . view as? OpenChannelViewControllerTabView ) ? . canSubmit ( ) ?? false ;
116
+ submitButton. isEnabled = canSubmit ( ) ;
188
117
}
189
118
190
- private func close ( ) {
191
- self . view . window ? . sheetParent ? . endSheet ( self . view . window! ) ;
119
+ func canSubmit ( ) -> Bool {
120
+ return account != nil && !components . isEmpty ;
192
121
}
193
122
194
- func askForNickname ( completionHandler : @escaping ( String ) -> Void ) {
195
- OpenChannelViewController . askForNickname ( for : self . account! , window: self . view. window!, completionHandler : completionHandler ) ;
123
+ func close ( ) {
124
+ self . view . window? . sheetParent ? . endSheet ( self . view. window!) ;
196
125
}
197
126
198
- static func askForNickname( for account: BareJID , window: NSWindow , completionHandler: @escaping ( String ) -> Void ) {
127
+ static func askForNickname( for account: BareJID , suggestedNickname : String ? = nil , window: NSWindow , completionHandler: @escaping ( String ) -> Void ) {
199
128
let alert = NSAlert ( ) ;
200
129
alert. alertStyle = . informational;
201
130
alert. icon = NSImage ( named: NSImage . userName) ;
202
131
alert. messageText = NSLocalizedString ( " Nickname " , comment: " alert window title " ) ;
203
132
alert. informativeText = NSLocalizedString ( " Enter a nickname which you want to use in this channel. " , comment: " alert window message " ) ;
204
133
let nicknameField = NSTextField ( frame: NSRect ( x: 0 , y: 0 , width: 200 , height: 7 + NSFont. systemFontSize) ) ;
205
- nicknameField. stringValue = AccountManager . getAccount ( for: account) ? . nickname ?? " "
134
+ nicknameField. stringValue = suggestedNickname ?? AccountManager . getAccount ( for: account) ? . nickname ?? " "
206
135
alert. accessoryView = nicknameField;
207
136
alert. addButton ( withTitle: NSLocalizedString ( " Submit " , comment: " Button " ) ) ;
208
137
alert. addButton ( withTitle: NSLocalizedString ( " Cancel " , comment: " Button " ) ) ;
@@ -219,7 +148,7 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
219
148
}
220
149
} )
221
150
}
222
-
151
+
223
152
func findComponents( for account: BareJID , at domain: String ) {
224
153
let domainJid = JID ( domain) ;
225
154
guard let discoModule = XmppService . instance. getClient ( for: account) ? . module ( . disco) else {
@@ -271,13 +200,6 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
271
200
} )
272
201
}
273
202
274
- override func prepare( for segue: NSStoryboardSegue , sender: Any ? ) {
275
- if let tabViewController = segue. destinationController as? OpenChannelViewTabViewController {
276
- tabViewController. delegate = self ;
277
- self . tabView = tabViewController. tabView;
278
- }
279
- }
280
-
281
203
private func retrieveComponent( from jid: JID , name: String ? , discoModule: DiscoveryModule , completionHandler: @escaping ( Result < Component , XMPPError > ) -> Void ) {
282
204
discoModule. getInfo ( for: jid, completionHandler: { result in
283
205
switch result {
@@ -293,7 +215,7 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
293
215
} )
294
216
295
217
}
296
-
218
+
297
219
enum ComponentType {
298
220
case muc
299
221
case mix
@@ -329,45 +251,4 @@ class OpenChannelViewController: NSViewController, OpenChannelViewControllerTabV
329
251
}
330
252
}
331
253
332
- protocol OpenChannelViewControllerTabView : AnyObject {
333
-
334
- var account : BareJID ? { get set }
335
- var components : [ OpenChannelViewController . Component ] { get set }
336
- var delegate : OpenChannelViewControllerTabViewDelegate ? { get set }
337
-
338
- func canSubmit( ) -> Bool ;
339
-
340
- func disclosureChanged( state: Bool ) ;
341
-
342
- func cancelClicked( completionHandler: ( ( ) -> Void ) ? ) ;
343
- func submitClicked( completionHandler: ( ( Bool ) -> Void ) ? ) ;
344
-
345
- func viewWillAppear( ) ;
346
- func viewDidDisappear( ) ;
347
- }
348
-
349
- protocol OpenChannelViewControllerTabViewDelegate : AnyObject {
350
-
351
- func askForNickname( completionHandler: @escaping ( String ) -> Void )
352
- func operationStarted( ) ;
353
- func operationFinished( ) ;
354
-
355
- func updateSubmitState( ) ;
356
- }
357
254
358
- class OpenChannelViewTabViewController : NSTabViewController {
359
-
360
- weak var delegate : OpenChannelViewController ? ;
361
-
362
- override func tabView( _ tabView: NSTabView , didSelect tabViewItem: NSTabViewItem ? ) {
363
- super. tabView ( tabView, didSelect: tabViewItem) ;
364
- let newTabLabel = tabViewItem? . label ?? " " ;
365
- for view in tabView. tabViewItems {
366
- if !( view. label == newTabLabel) {
367
- ( view. view as? OpenChannelViewControllerTabView ) ? . viewDidDisappear ( ) ;
368
- }
369
- }
370
- ( tabViewItem? . view as? OpenChannelViewControllerTabView ) ? . viewWillAppear ( ) ;
371
- }
372
-
373
- }
0 commit comments