1
- //
2
- // ExampleViewController.swift
3
- // Example
4
- //
5
- // Created by Nathan Tannar on 2018-06-03.
6
- // Copyright © 2018 MessageKit. All rights reserved.
7
- //
1
+ /*
2
+ MIT License
3
+
4
+ Copyright (c) 2017-2018 MessageKit
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ */
8
24
9
25
import UIKit
10
26
import MessageInputBar
@@ -39,6 +55,25 @@ final class ExampleViewController: UITableViewController {
39
55
return true
40
56
}
41
57
58
+ /// The object that manages attachments
59
+ lazy var attachmentManager : AttachmentManager = { [ unowned self] in
60
+ let manager = AttachmentManager ( )
61
+ manager. delegate = self
62
+ return manager
63
+ } ( )
64
+
65
+ /// The object that manages autocomplete
66
+ lazy var autocompleteManager : AutocompleteManager = { [ unowned self] in
67
+ let manager = AutocompleteManager ( for: self . messageInputBar. inputTextView)
68
+ manager. delegate = self
69
+ manager. dataSource = self
70
+ return manager
71
+ } ( )
72
+
73
+ let users = [ " nathantannar4 " , " SD10 " ]
74
+
75
+ let hastags = [ " MessageKit " , " MessageInputBar " ]
76
+
42
77
// MARK: - MessageInputBar
43
78
44
79
private let messageInputBar : MessageInputBar
@@ -59,8 +94,15 @@ final class ExampleViewController: UITableViewController {
59
94
override func viewDidLoad( ) {
60
95
super. viewDidLoad ( )
61
96
view. backgroundColor = . white
62
- messageInputBar. delegate = self
63
97
tableView. keyboardDismissMode = . interactive
98
+ messageInputBar. delegate = self
99
+ messageInputBar. plugins = [ attachmentManager, autocompleteManager]
100
+
101
+ autocompleteManager. register ( prefix: " @ " , with: [ . font: UIFont . preferredFont ( forTextStyle: . body) , . foregroundColor: UIColor ( red: 0 , green: 122 / 255 , blue: 1 , alpha: 1 ) , . backgroundColor: UIColor ( red: 0 , green: 122 / 255 , blue: 1 , alpha: 0.1 ) ] )
102
+ autocompleteManager. register ( prefix: " # " )
103
+
104
+ // Want to return custom cells? Set the dataSource
105
+ // attachmentManager.dataSource = self
64
106
}
65
107
66
108
}
@@ -83,3 +125,119 @@ extension ExampleViewController: MessageInputBarDelegate {
83
125
84
126
}
85
127
128
+ extension ExampleViewController : AttachmentManagerDelegate {
129
+
130
+
131
+ // MARK: - AttachmentManagerDelegate
132
+
133
+ func attachmentManager( _ manager: AttachmentManager , shouldBecomeVisible: Bool ) {
134
+ setAttachmentManager ( active: shouldBecomeVisible)
135
+ }
136
+
137
+ func attachmentManager( _ manager: AttachmentManager , didReloadTo attachments: [ AttachmentManager . Attachment ] ) {
138
+ messageInputBar. sendButton. isEnabled = manager. attachments. count > 0
139
+ }
140
+
141
+ func attachmentManager( _ manager: AttachmentManager , didInsert attachment: AttachmentManager . Attachment , at index: Int ) {
142
+ messageInputBar. sendButton. isEnabled = manager. attachments. count > 0
143
+ }
144
+
145
+ func attachmentManager( _ manager: AttachmentManager , didRemove attachment: AttachmentManager . Attachment , at index: Int ) {
146
+ messageInputBar. sendButton. isEnabled = manager. attachments. count > 0
147
+ }
148
+
149
+ func attachmentManager( _ manager: AttachmentManager , didSelectAddAttachmentAt index: Int ) {
150
+ let imagePicker = UIImagePickerController ( )
151
+ imagePicker. delegate = self
152
+ imagePicker. sourceType = . photoLibrary
153
+ present ( imagePicker, animated: true , completion: nil )
154
+ }
155
+
156
+ // MARK: - AttachmentManagerDelegate Helper
157
+
158
+ func setAttachmentManager( active: Bool ) {
159
+
160
+ let topStackView = messageInputBar. topStackView
161
+ if active && !topStackView. arrangedSubviews. contains ( attachmentManager. attachmentView) {
162
+ topStackView. insertArrangedSubview ( attachmentManager. attachmentView, at: topStackView. arrangedSubviews. count)
163
+ topStackView. layoutIfNeeded ( )
164
+ } else if !active && topStackView. arrangedSubviews. contains ( attachmentManager. attachmentView) {
165
+ topStackView. removeArrangedSubview ( attachmentManager. attachmentView)
166
+ topStackView. layoutIfNeeded ( )
167
+ }
168
+ }
169
+ }
170
+
171
+ extension ExampleViewController : AutocompleteManagerDelegate , AutocompleteManagerDataSource {
172
+
173
+ // MARK: - AutocompleteManagerDataSource
174
+
175
+ func autocompleteManager( _ manager: AutocompleteManager , autocompleteSourceFor prefix: String ) -> [ AutocompleteCompletion ] {
176
+ if prefix == " @ " {
177
+ return users. map { AutocompleteCompletion ( $0) }
178
+ } else if prefix == " # " {
179
+ return hastags. map { AutocompleteCompletion ( $0) }
180
+ }
181
+ return [ ]
182
+ }
183
+
184
+ func autocompleteManager( _ manager: AutocompleteManager , tableView: UITableView , cellForRowAt indexPath: IndexPath , for session: AutocompleteSession ) -> UITableViewCell {
185
+
186
+ guard let cell = tableView. dequeueReusableCell ( withIdentifier: AutocompleteCell . reuseIdentifier, for: indexPath) as? AutocompleteCell else {
187
+ fatalError ( " Oops, some unknown error occurred " )
188
+ }
189
+ cell. textLabel? . attributedText = manager. attributedText ( matching: session, fontSize: 15 )
190
+ return cell
191
+ }
192
+
193
+ // MARK: - AutocompleteManagerDelegate
194
+
195
+ func autocompleteManager( _ manager: AutocompleteManager , shouldBecomeVisible: Bool ) {
196
+ setAutocompleteManager ( active: shouldBecomeVisible)
197
+ }
198
+
199
+ // Optional
200
+ func autocompleteManager( _ manager: AutocompleteManager , shouldRegister prefix: String , at range: NSRange ) -> Bool {
201
+ return true
202
+ }
203
+
204
+ // Optional
205
+ func autocompleteManager( _ manager: AutocompleteManager , shouldUnregister prefix: String ) -> Bool {
206
+ return true
207
+ }
208
+
209
+ // Optional
210
+ func autocompleteManager( _ manager: AutocompleteManager , shouldComplete prefix: String , with text: String ) -> Bool {
211
+ return true
212
+ }
213
+
214
+ // MARK: - AutocompleteManagerDelegate Helper
215
+
216
+ func setAutocompleteManager( active: Bool ) {
217
+
218
+ let topStackView = messageInputBar. topStackView
219
+ if active && !topStackView. arrangedSubviews. contains ( autocompleteManager. tableView) {
220
+ topStackView. insertArrangedSubview ( autocompleteManager. tableView, at: topStackView. arrangedSubviews. count)
221
+ topStackView. layoutIfNeeded ( )
222
+ } else if !active && topStackView. arrangedSubviews. contains ( autocompleteManager. tableView) {
223
+ topStackView. removeArrangedSubview ( autocompleteManager. tableView)
224
+ topStackView. layoutIfNeeded ( )
225
+ }
226
+ }
227
+
228
+ }
229
+
230
+ extension ExampleViewController : UIImagePickerControllerDelegate , UINavigationControllerDelegate {
231
+
232
+ func imagePickerController( _ picker: UIImagePickerController , didFinishPickingMediaWithInfo info: [ String : Any ] ) {
233
+
234
+ dismiss ( animated: true , completion: {
235
+ if let pickedImage = info [ UIImagePickerControllerOriginalImage] as? UIImage {
236
+ let handled = self . attachmentManager. handleInput ( of: pickedImage)
237
+ if !handled {
238
+ // throw error
239
+ }
240
+ }
241
+ } )
242
+ }
243
+ }
0 commit comments