-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
195 lines (168 loc) · 9.04 KB
/
ViewController.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
//
// ViewController.swift
// anonymous
//
// Created by Zhongcai Ng on 18/12/15.
// Copyright © 2015 Cloudilly Private Limited. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate, CloudillyDelegate {
var width: CGFloat!
var height: CGFloat!
var msgsTableView: UITableView!
var msgs: NSMutableArray!
var bottom: UIView!
var field: UITextField!
var cloudilly: Cloudilly!
convenience init() {
self.init(nibName:nil, bundle:nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChangeFrame:", name: UIKeyboardWillChangeFrameNotification, object: nil)
self.msgs = []
let app = "<INSERT YOUR APP NAME>"
let access = "<INSERT YOUR ACCESS KEY>"
self.cloudilly = Cloudilly(app: app, andAccess: access, withCallback: {
self.addMsg("CONNECTING...")
self.cloudilly.connect()
})
self.cloudilly.addDelegate(self)
}
override func loadView() {
self.width = UIScreen.mainScreen().bounds.size.width
self.height = UIScreen.mainScreen().bounds.size.height
view = UIView(frame: CGRectMake(0.0, 0.0, self.width, self.height))
view.backgroundColor = UIColor.whiteColor()
let status = UIView(frame: CGRectMake(0.0, 0.0, self.width, 20.0))
status.backgroundColor = UIColor.blackColor()
view.addSubview(status)
let top = UIView(frame: CGRectMake(0.0, 20.0, self.width, 50.0))
top.backgroundColor = UIColor.blackColor()
view.addSubview(top)
let title = UILabel(frame: CGRectMake(0.0, 26.0, self.width, 36.0))
title.font = UIFont(name: "ChalkboardSE-Bold", size: 22.0)
title.backgroundColor = UIColor.clearColor()
title.textAlignment = NSTextAlignment.Center
title.textColor = UIColor.whiteColor()
title.text = "Cloudilly"
view.addSubview(title)
self.msgsTableView = UITableView(frame: CGRectMake(0.0, 70.0, self.width, self.height - 120.0), style: UITableViewStyle.Plain)
self.msgsTableView.backgroundColor = UIColor.whiteColor()
self.msgsTableView.delegate = self
self.msgsTableView.dataSource = self
self.msgsTableView.separatorStyle = UITableViewCellSeparatorStyle.None
view.addSubview(self.msgsTableView)
self.bottom = UIView(frame: CGRectMake(0.0, self.height - 50.0, self.width, 50.0))
self.bottom.userInteractionEnabled = true
self.bottom.backgroundColor = UIColor.grayColor()
view.addSubview(self.bottom)
let text = UIView(frame: CGRectMake(5.0, 5.0, self.width - 70.0, 40.0))
text.backgroundColor = UIColor.whiteColor()
self.bottom.addSubview(text)
self.field = UITextField(frame: CGRectMake(10.0, 0.0, self.width - 80.0, 40.0))
self.field.keyboardAppearance = UIKeyboardAppearance.Dark
self.field.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
self.field.autocorrectionType = UITextAutocorrectionType.Yes
self.field.font = UIFont.systemFontOfSize(22.0)
self.field.returnKeyType = UIReturnKeyType.Send
self.field.delegate = self
text.addSubview(self.field)
let sendBtn = UIButton(type: UIButtonType.Custom)
sendBtn.addTarget(self, action: "fireSend", forControlEvents: UIControlEvents.TouchUpInside)
sendBtn.setTitle("Send", forState: UIControlState.Normal)
sendBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
sendBtn.titleLabel?.font = UIFont(name: "AvenirNextCondensed-DemiBold", size: 22.0)
sendBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
sendBtn.frame = CGRectMake(self.width - 60.0, 0.0, 60.0, 50.0)
self.bottom.addSubview(sendBtn)
view.addSubview(self.bottom)
}
func fireSend() {
if(self.field.text!.characters.count == 0) { return }
let payload: NSMutableDictionary = ["msg": self.field.text!]
self.cloudilly.postGroup("public", withPayload: payload, withCallback: { dict in
if((dict["status"]!.isEqualToString("fail"))) { print((dict["msg"] as! String)); return; }
print("@@@@@@ POST " + String(dict))
})
self.field.text = ""
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if(self.field.text!.characters.count == 0) { return false }
let payload: NSMutableDictionary = ["msg": self.field.text!]
self.cloudilly.postGroup("public", withPayload: payload, withCallback: { dict in
if((dict["status"]!.isEqualToString("fail"))) { print((dict["msg"] as! String)); return; }
print("@@@@@@ POST " + String(dict))
})
self.field.text = ""
return false
}
func returnMsgHeight(msg: String) -> CGFloat {
let attributeNormal = [NSFontAttributeName : UIFont.systemFontOfSize(18.0)]
return msg.boundingRectWithSize(CGSizeMake(width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributeNormal, context: nil).size.height
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let msg = msgs.objectAtIndex(indexPath.row) as! String
return indexPath.row == 0 ? self.returnMsgHeight(msg) + 20.0 : self.returnMsgHeight(msg) + 10.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.msgs.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
if(cell == nil) { cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL") }
cell!.textLabel!.text = (self.msgs.objectAtIndex(indexPath.row) as! String)
cell!.textLabel!.numberOfLines = 0
return cell!
}
func keyboardWillChangeFrame(notification: NSNotification) {
let keyboardFrame = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue
let keyboardHeight = self.height - keyboardFrame.origin.y
let duration = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationDurationUserInfoKey)!.doubleValue
UIView.animateWithDuration(duration, animations: {
self.msgsTableView.frame = CGRectMake(0.0, 70.0, self.width, self.height - 70.0 - 49.0 - keyboardHeight)
self.bottom.frame = CGRectMake(0.0, self.height - 50.0 - keyboardHeight, self.width, 50.0)
self.scrollToBottom()
})
}
func scrollToBottom() {
if(self.msgsTableView.contentSize.height < self.msgsTableView.frame.size.height) { return; }
let delayInSeconds = 0.1
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue(), {
let offset = CGPointMake(0.0, self.msgsTableView.contentSize.height - self.msgsTableView.frame.size.height)
self.msgsTableView.setContentOffset(offset, animated: true)
})
}
func addMsg(msg: NSString) {
self.msgs.addObject(msg)
self.msgsTableView.reloadData()
self.scrollToBottom()
}
func socketConnected(dict: [NSObject : AnyObject]!) {
if((dict["status"]!.isEqualToString("fail"))) { print((dict["msg"] as! String)); return; }
print("@@@@@@ CONNECTED " + String(dict))
self.addMsg("CONNECTED AS " + (dict["device"] as! String).uppercaseString)
self.cloudilly.joinGroup("public", withCallback: { dict in
if((dict["status"]!.isEqualToString("fail"))) { print((dict["msg"] as! String)); return; }
print("@@@@@@ JOIN " + String(dict))
self.addMsg("DEVICE PRESENT IN PUBLIC " + "\(dict["total_devices"] as! NSNumber)")
})
}
func socketDisconnected() {
print("@@@@@@ DISCONNECTED")
msgs.removeAllObjects()
self.msgs.addObject("DISCONNECTED")
}
func socketReceivedDevice(dict: [NSObject : AnyObject]!) {
print("@@@@@@ RECEIVED DEVICE " + String(dict))
let other = (dict["device"] as! String).uppercaseString
let action = (dict["timestamp"] as! NSNumber).isEqualToNumber(NSNumber.init(int: 0)) ? "JOINED" : "LEFT"
self.addMsg(other + " " + action + " PUBLIC")
}
func socketReceivedPost(dict: [NSObject : AnyObject]!) {
print("@@@@@@ RECEIVED POST " + String(dict))
let other = (dict["device"] as! String).uppercaseString
self.addMsg(other + ": " + (dict["payload"]!["msg"] as! String))
}
override func preferredStatusBarStyle() -> UIStatusBarStyle { return UIStatusBarStyle.LightContent }
override func viewDidLoad() { super.viewDidLoad() }
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
}