質問編集履歴
1
こーどの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,7 +20,211 @@
|
|
20
20
|
|
21
21
|
```Swift
|
22
22
|
|
23
|
+
import UIKit
|
24
|
+
|
25
|
+
import ChameleonFramework
|
26
|
+
|
27
|
+
import Firebase
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
class ChatViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate {
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
@IBOutlet weak var tableView: UITableView!
|
42
|
+
|
43
|
+
@IBOutlet weak var messageTextField: UITextField!
|
44
|
+
|
45
|
+
@IBOutlet weak var sendButton: UIButton!
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
//スクリーンのサイズ
|
50
|
+
|
51
|
+
let screenSize = UIScreen.main.bounds.size
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
var chatArray = [Message]()
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
override func viewDidLoad() {
|
62
|
+
|
63
|
+
super.viewDidLoad()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
tableView.delegate = self
|
68
|
+
|
69
|
+
tableView.dataSource = self
|
70
|
+
|
71
|
+
messageTextField.delegate = self
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
//セルを反映
|
76
|
+
|
77
|
+
tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "Cell")
|
78
|
+
|
79
|
+
//行の高さを可変
|
80
|
+
|
81
|
+
tableView.rowHeight = UITableView.automaticDimension
|
82
|
+
|
83
|
+
//見積もりの高さ
|
84
|
+
|
85
|
+
tableView.estimatedRowHeight = 75
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
//キーボード開く時
|
90
|
+
|
91
|
+
//NotificationCenterに反応がおこった場合こったどれが呼ばるか
|
92
|
+
|
93
|
+
//keyboardWillShowNotification時にkeyboardWillShow(メソッドが呼ばれる)
|
94
|
+
|
95
|
+
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillShow(_ :)), name: UIResponder.keyboardWillShowNotification, object: nil)
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
//キーボード閉じる時
|
100
|
+
|
101
|
+
NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.keyboardWillHide(_ :)), name: UIResponder.keyboardWillHideNotification, object: nil)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
//Firebaseからデータをfetchしてくる
|
106
|
+
|
107
|
+
fetchChatData()
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
//tableViewのセルの線を消す
|
112
|
+
|
113
|
+
tableView.separatorStyle = .none
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
//引数としてNSNotification型が取れる
|
126
|
+
|
127
|
+
//#selectorはobjectCの名残だから@objc
|
128
|
+
|
129
|
+
//notificationは上の情報を持っている
|
130
|
+
|
131
|
+
@objc func keyboardWillShow(_ notification:NSNotification){
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//キーボードと一緒にViewも上げる方法
|
136
|
+
|
137
|
+
//cgRectValue端末ごとの高さを取得
|
138
|
+
|
139
|
+
let keyboardHeight = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as Any) as AnyObject).cgRectValue.height
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
messageTextField.frame.origin.y = screenSize.height - keyboardHeight - messageTextField.frame.height
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
@objc func keyboardWillHide(_ notification:NSNotification){
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
messageTextField.frame.origin.y = screenSize.height - messageTextField.frame.height
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
//キーボード全般の大きさを取得
|
160
|
+
|
161
|
+
guard let rect = (notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue,
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
//guardとして並行している
|
166
|
+
|
167
|
+
//キーボードが下がる時間をdurationとして取得
|
168
|
+
|
169
|
+
let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else{return}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
UIView.animate(withDuration: duration){
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
let transform = CGAffineTransform(translationX: 0, y: 0)
|
180
|
+
|
181
|
+
self.view.transform = transform
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
//buttonをタッチした場合に起こる
|
194
|
+
|
195
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
messageTextField.resignFirstResponder()
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
//textFieldを閉じる方法
|
208
|
+
|
209
|
+
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
textField.resignFirstResponder()
|
214
|
+
|
215
|
+
return true
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
23
|
-
//cellをスワイプで削除
|
227
|
+
//cellをスワイプで削除
|
24
228
|
|
25
229
|
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
|
26
230
|
|
@@ -34,4 +238,254 @@
|
|
34
238
|
|
35
239
|
}
|
36
240
|
|
241
|
+
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
//メッセージの数
|
250
|
+
|
251
|
+
return chatArray.count
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
return 1
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
cell.massageLabel.text = chatArray[indexPath.row].message
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
cell.userNamelabel.text = chatArray[indexPath.row].sender
|
284
|
+
|
285
|
+
cell.iconImageView.image = UIImage(named: "dogAvatarImage")
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//Loginしているユーザーが自分のemailアドレスなら
|
290
|
+
|
291
|
+
if cell.userNamelabel.text == Auth.auth().currentUser?.email as! String{
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
//massageLabelの背景色を変更
|
296
|
+
|
297
|
+
cell.massageLabel.backgroundColor = UIColor.flatGreen()
|
298
|
+
|
299
|
+
//セルのラベルを丸くする
|
300
|
+
|
301
|
+
cell.massageLabel.layer.cornerRadius = 10
|
302
|
+
|
303
|
+
cell.massageLabel.layer.masksToBounds = true
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
}else{
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
cell.massageLabel.backgroundColor = UIColor.flatBlue()
|
312
|
+
|
313
|
+
//セルのラベルを丸くする
|
314
|
+
|
315
|
+
cell.massageLabel.layer.cornerRadius = 20
|
316
|
+
|
317
|
+
cell.massageLabel.layer.masksToBounds = true
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
return cell
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
//セルの高さを返す
|
336
|
+
|
337
|
+
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return view.frame.size.height/10
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
@IBAction func sendAction(_ sender: Any) {
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
//入力が終わったから
|
352
|
+
|
353
|
+
messageTextField.endEditing(true)
|
354
|
+
|
355
|
+
messageTextField.isEnabled = false
|
356
|
+
|
357
|
+
sendButton.isEnabled = false
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
//送信する文字数の制限
|
364
|
+
|
365
|
+
if messageTextField.text!.count > 20{
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
print("20文字以上です")
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
return
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
//Firebase.Datebaseのhitsが作り方
|
382
|
+
|
383
|
+
let chatDB = Database.database().reference().child("chats")
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
//キーバリュー型で内容を送信(Dictionary型)
|
388
|
+
|
389
|
+
//送信したいものを(キー値とValue)一緒にもつ変数
|
390
|
+
|
391
|
+
let messageInfo = ["sender":Auth.auth().currentUser?.email,"message":messageTextField.text!]
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
//chatDBに入れる
|
396
|
+
|
397
|
+
chatDB.childByAutoId().setValue(messageInfo) { (error, result) in
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
if error != nil{
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
print(error)
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
}else{
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
print("送信完了")
|
414
|
+
|
415
|
+
self.messageTextField.isEnabled = true
|
416
|
+
|
417
|
+
self.sendButton.isEnabled = true
|
418
|
+
|
419
|
+
//TextFieldを自動的に消す作業
|
420
|
+
|
421
|
+
self.messageTextField.text = ""
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
//引っ張ってくる
|
438
|
+
|
439
|
+
func fetchChatData(){
|
440
|
+
|
441
|
+
|
442
|
+
|
443
|
+
//どこからデータを引っ張ってくるのか
|
444
|
+
|
445
|
+
let fechdataRef = Database.database().reference().child("chats")
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
//新しく更新があったときだけ取得したい
|
450
|
+
|
451
|
+
fechdataRef.observe(.childAdded) { (snapShot) in
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
//snapShotの値にmessageとsenderがある
|
456
|
+
|
457
|
+
//snapShotDataのキー値はmessageという所に保存されている
|
458
|
+
|
459
|
+
let snapShotData = snapShot.value as! AnyObject
|
460
|
+
|
461
|
+
let text = snapShotData.value(forKey: "message")
|
462
|
+
|
463
|
+
let sender = snapShotData.value(forKey: "sender")
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
//初期化
|
468
|
+
|
469
|
+
let message = Message()
|
470
|
+
|
471
|
+
message.message = text as! String
|
472
|
+
|
473
|
+
message.sender = sender as! String
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
//tableViewのchatArrayにmessageを表示できる
|
478
|
+
|
479
|
+
self.chatArray.append(message)
|
480
|
+
|
481
|
+
//データ更新
|
482
|
+
|
483
|
+
self.tableView.reloadData()
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
37
491
|
```
|