-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discussions.swift
478 lines (408 loc) · 19.1 KB
/
Discussions.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
//
// Discussions.swift
// PreSales-Huddle
//
// Created by Vinaya Mandke on 22/07/15.
// Copyright (c) 2015 synerzip. All rights reserved.
//
import UIKit
class Discussions: UITableViewController {
// MARK: Class variables
var prospectID = -1 {
didSet {
fetchData()
}
}
var imgData:NSData?
let viewAllQA = "discussion/view/prospectid/"
let updateQA = "discussion/update/"
let addQURL = "discussion/add/"
var tableData = [String]();
var allQAs = [[String: AnyObject]]() {
//for mock screens
didSet {
if var lastDiscussion = allQAs.last {
if lastDiscussion["DiscussionID"] == nil {
lastDiscussion["DiscussionID"] = 60 + allQAs.count
lastDiscussion["UserID"] = currentUserEmail
allQAs.removeLast()
allQAs.append(lastDiscussion)
}
}
}
}
var currentUser:GIDGoogleUser! {
return GIDSignIn.sharedInstance().currentUser
}
var currentUserEmail:String {
let user = currentUser
return user.profile.email
}
var cachedAnswers = [Int: String]()
var arrayForBool = [Bool]()
override func viewDidLoad() {
// get user's profile pic
let user = currentUser
if(user.profile.hasImage) {
let imgURL = user.profile.imageURLWithDimension(35)
if let data = NSData(contentsOfURL: imgURL) {
imgData = data
}
}
super.viewDidLoad()
tableView.scrollEnabled = true
tableView.bounces = false
stylize()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func addAQuestion(sender: UIBarButtonItem) {
cacheAnswers()
let alert = UIAlertController(title: "Ask a Question", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in
textField.placeholder = "Enter a Question"
}
let defaultAction = UIAlertAction(title: "Submit", style: .Default, handler: { (action:UIAlertAction) -> Void in
if let questionField = alert.textFields?.first {
let question = questionField.text
// Take user id from NSDefaults; currently defaulting to "USER1"
var userID = "Unknown"
if let id = NSUserDefaults.standardUserDefaults().stringForKey("userID") {
userID = id
}
let dataStore : [String:AnyObject] = ["UserID": userID, "ProspectID": self.prospectID,"Query":"\(question)", "Answer": [[String:AnyObject]]()]
//for mock-screens
self.allQAs.append(dataStore)
//for mock screens reload tableview
self.fetchData()
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)
alert.addAction(cancelAction)
alert.addAction(defaultAction)
presentViewController(alert, animated: true, completion: nil)
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return allQAs.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if(arrayForBool.count > section && arrayForBool[section])
{
if allQAs.count > section {
let qa = allQAs[section] as [String: AnyObject]
if let ans = qa["Answer"] as? [[String:AnyObject]] {
return ans.count + 1
}
}
}
return 0;
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// Height for table cell is predetermined
// try and calculate it here:-
if allQAs.count > section {
let qa = allQAs[section] as [String: AnyObject]
if let query = qa["Query"] as? String {
let attrs = [NSFontAttributeName:UIFont.systemFontOfSize(UIFont.systemFontSize())]
let size = (query as NSString).sizeWithAttributes(attrs)
return size.height + 40
}
}
return UITableViewAutomaticDimension
// return 50
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(arrayForBool[indexPath.section]){
// Height for table cell is predetermined
// try and calculate it here:-
//hard-coded for mock-screens
return 90
}
return 1;
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let hVWidth = tableView.frame.size.width
let hvHeight = CGFloat(40)
let headerView = UIView(frame: CGRectMake(0, 0, hVWidth, hvHeight))
headerView.backgroundColor = Appearance.tableheaderBG
headerView.tag = section
let headerString = UILabel(frame: CGRect(x: 60, y: 15, width: hVWidth-50, height: hvHeight - 10)) as UILabel
if allQAs.count > section {
let qa = allQAs[section] as [String: AnyObject]
if let query = qa["Query"] as? String {
headerString.text = query
headerString.textColor = UIColor.brownColor()
let userid = qa["UserID"] as! String
headerView.addSubview(getprofilePic(userid, frame: CGRect(x: 10, y: 15, width: 35, height: 35)))
}
if let answer = qa["Answer"] as? [[String:AnyObject]] {
if answer.isEmpty {
let unansweredLabel = UILabel(frame: CGRectMake(60, 0, 75, 15))
unansweredLabel.text = "Unanswered"
unansweredLabel.textColor = UIColor.redColor()
unansweredLabel.font = UIFont.systemFontOfSize(10)
headerView.addSubview(unansweredLabel)
}
}
}
headerView.addSubview(headerString)
let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
headerView .addGestureRecognizer(headerTapped)
styleTheView(headerView)
return headerView
}
func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
cacheAnswers()
let indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!)
if (indexPath.row == 0) {
var collapsed = arrayForBool[indexPath.section]
collapsed = !collapsed;
arrayForBool[indexPath.section] = collapsed
//reload specific section animated
let range = NSMakeRange(indexPath.section, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
self.tableView.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let qa = allQAs[indexPath.section] as [String: AnyObject]
//TODO (vinaya.mandke) currently if no ans provided answer is provided as empty string
if let ans = qa["Answer"] as? [[String:AnyObject]] {
if ans.count > indexPath.row {
let query = ans[indexPath.row]
cell.textLabel?.text = query["data"] as? String
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel?.numberOfLines = 0
cell.textLabel?.preferredMaxLayoutWidth = tableView.frame.size.width
cell.textLabel?.sizeToFit()
if let labelFrame = cell.textLabel?.frame {
cell.frame = labelFrame
}
cell.sizeToFit()
cell.textLabel?.textColor = Appearance.textViewTextColor
let userid = query["UserID"] as! String
let imageView = getprofilePic(userid, frame: CGRect(x: 10, y: 15, width: 35, height: 35))
cell.accessoryView = imageView
} else {
let answerblock = UITextView(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-20, height: cell.bounds.height)) as UITextView
answerblock.layer.borderWidth = 2
answerblock.layer.borderColor = UIColor.grayColor().CGColor
answerblock.layer.cornerRadius = 5.0
let postButton = UIButton(frame: CGRect(x: 10, y: 60, width: 50, height: 20))
// tag button with section id
postButton.tag = indexPath.section + 1
answerblock.tag = indexPath.section + 1
postButton.setTitle("POST", forState: UIControlState.Normal)
postButton.backgroundColor = Appearance.tableheaderBG
postButton.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)
postButton.titleLabel?.font = UIFont.systemFontOfSize(CGFloat(10))
postButton.layer.cornerRadius = 1.0
postButton.addTarget(self, action: "postAnswer:", forControlEvents: UIControlEvents.TouchUpInside)
styleTheView(answerblock)
styleTheView(postButton)
cell.addSubview(answerblock)
cell.addSubview(postButton)
cell.sizeToFit()
// try to get cached answer value
if let discussionID = qa["DiscussionID"] as? Int {
if let cachedValue = cachedAnswers[discussionID] {
answerblock.text = cachedValue
// remove the cached value
cachedAnswers[discussionID] = nil
}
}
}
}
styleTheView(cell)
return cell
}
func postAnswer(sender: UIButton) {
// POST the answer to API use sender.tag as identifier in allQAs
NSLog("hello \(sender.tag)")
let sectionID = sender.tag - 1
let answerView = tableView.viewWithTag(sender.tag) as? UITextView
let answer = answerView?.text
if (answer != nil && !answer!.isEmpty) {
let alert = UIAlertController(title: "Preview Answer", message: answer!, preferredStyle: UIAlertControllerStyle.Alert)
let submitActionHandler = { (action:UIAlertAction!) -> Void in
//POST the discussion
var qa = self.allQAs[sectionID] as [String: AnyObject]
var allAns = qa["Answer"] as! [[String:AnyObject]]
// for mock-screens add in qa
allAns.append(["data":answer!, "UserID":self.currentUserEmail])
qa["Answer"] = allAns
self.allQAs[sectionID] = qa
let range = NSMakeRange(sectionID, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
dispatch_async(dispatch_get_main_queue()) {
var collapsed = self.arrayForBool[sectionID]
collapsed = !collapsed;
self.arrayForBool[sectionID] = collapsed
self.tableView.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
}
}
let defaultAction = UIAlertAction(title: "Submit", style: .Default, handler: submitActionHandler)
let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: {(action:UIAlertAction) -> Void in
self.cacheAnswers()
let range = NSMakeRange(sectionID, 1)
let sectionToReload = NSIndexSet(indexesInRange: range)
dispatch_async(dispatch_get_main_queue()) {
var collapsed = self.arrayForBool[sectionID]
collapsed = !collapsed;
self.arrayForBool[sectionID] = collapsed
self.tableView.reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
}
})
alert.addAction(cancelAction)
alert.addAction(defaultAction)
presentViewController(alert, animated: true, completion: nil)
}
}
private func commonHandler() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func service_success() -> Void {
commonHandler()
// for mock-screens
let dataToFill = fillData
allQAs.removeAll(keepCapacity: true)
for item in dataToFill {
let dict = item as [String: AnyObject]
allQAs.append(dict)
}
for _ in (0...allQAs.count-1) {
arrayForBool.append(false)
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
func service_success_post(data: NSData) -> Void {
commonHandler()
fetchData()
}
func network_error( error: NSError) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Network error",
message: "Code: \(error.code)\n\(error.localizedDescription)")
}
}
func service_error(response: NSHTTPURLResponse) -> Void {
commonHandler()
dispatch_async(dispatch_get_main_queue()) {
self.showMessage("Webservice Error",
message: "Error received from webservice: \(response.statusCode)")
}
}
func showMessage(title:String, message: String) {
let alert = UIAlertController(title: title, message: message,
preferredStyle: .Alert)
let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
}
func fetchData() {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
service_success()
}
func postUpdate(dataEncoded: NSData, url: String) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
let nc = NetworkCommunication()
let _ = nc.postData(dataEncoded,
successHandler: service_success_post,
serviceErrorHandler: service_error,
errorHandler: network_error,
request: nil,
relativeURL: url)
}
func cacheAnswers() {
let count = allQAs.count
if count > 0 {
for i in (1...count) {
let answerView = tableView.viewWithTag(i) as? UITextView
let answer = answerView?.text
if (answer != nil && !answer!.isEmpty) {
let qa = self.allQAs[i-1] as [String: AnyObject]
if let discussionID = qa["DiscussionID"] as? Int {
cachedAnswers[discussionID] = answer!
}
}
}
}
}
// MARK: styles
struct Appearance {
static var tintColor = UIColor.grayColor()
static var backgroundColor = UIColor.whiteColor()
static var textViewTextColor = UIColor(red: 0.0/255.0, green: 122.0/255.0, blue: 255.0/255.0, alpha: 1.00)
static var textViewBackgroundColor = UIColor.whiteColor()
static var tableheaderBG = UIColor(red: 237.0/255.0, green: 247.0/255.0, blue: 250.0/255.0, alpha: 1.00)
}
func styleTheView(textView: UIView) {
textView.layer.rasterizationScale = UIScreen.mainScreen().scale
textView.layer.shouldRasterize = true
textView.layer.cornerRadius = 5.0
textView.layer.borderWidth = 1.0
textView.layer.borderColor = UIColor(white: 0.0, alpha: 0.2).CGColor
}
func stylize() {
tableView.backgroundColor = Appearance.textViewBackgroundColor
tableView.tintColor = Appearance.tintColor
tableView.backgroundColor = Appearance.backgroundColor
}
func getprofilePic(userid: String, frame: CGRect) -> UIImageView {
let img:UIImage?
if currentUserEmail == userid {
img = getprofilePicForMe()
} else {
img = UIImage(named: userid)
}
let imageView = UIImageView(frame: frame)
if let profielPic = img {
imageView.image = profielPic
}
// circle
imageView.layer.borderWidth=1.0
imageView.layer.masksToBounds = false
imageView.layer.borderColor = UIColor.whiteColor().CGColor
imageView.layer.cornerRadius = 13
imageView.layer.cornerRadius = imageView.frame.size.height/2
imageView.clipsToBounds = true
return imageView
}
private func getprofilePicForMe() -> UIImage? {
if let userImage = imgData {
return UIImage(data: userImage)
} else {
return UIImage(named: "Unknown")
}
}
var fillData : [[String: AnyObject]] {
get {
if self.allQAs.isEmpty {
let discussion1:[String : AnyObject] = ["DiscussionID":55,"ProspectID":53,"UserID":"himanshu.phirke@synerzip.com","Query":"What is the expected team size ?","Answer":[["UserID":"himanshu.phirke@synerzip.com","data":"About 20 odd people"], ["UserID":"uttam.gandhi@synerzip.com","data":"Are any QAs required?"]]]
let discussion2:[String : AnyObject] = ["DiscussionID":56,"ProspectID":53,"UserID":"vinaya.mandke@synerzip.com","Query":"What is the expected start date ?","Answer":[["UserID":"sachin.avhad@synerzip.com","data":"End of this month"]]]
let discussion3:[String : AnyObject] = ["DiscussionID":57,"ProspectID":53,"UserID":"uttam.gandhi@synerzip.com","Query":"What are the technologies involved ?","Answer":[String]()]
let discussion4:[String : AnyObject] = ["DiscussionID":58,"ProspectID":53,"UserID":"sachin.avhad@synerzip.com","Query":"Is a webservice required ?","Answer":[["UserID":"himanshu.phirke@synerzip.com","data":"Yes"], ["UserID":"uttam.gandhi@synerzip.com","data":"Using Go?"]]]
var fillData = [[String: AnyObject]]()
fillData.append(discussion1)
fillData.append(discussion2)
fillData.append(discussion3)
fillData.append(discussion4)
return fillData
} else {
return self.allQAs
}
}
}
}