Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.06 KB | None | 0 0
  1. import UIKit
  2.  
  3. import Firebase
  4.  
  5. class ActiveCallsTableViewController: UITableViewController {
  6. let cellId = "callCell"
  7. var users = [User]()
  8. var unAcceptedUsers = [User]()
  9. var enRoute = [User]()
  10. var onSite = [User]()
  11.  
  12. var isFiltered = Bool()
  13. let section = ["Unaccepted", "Enroute", "Onsite"]
  14.  
  15.  
  16.  
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20.  
  21. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(handleCancel))
  22.  
  23. tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
  24.  
  25.  
  26. fetchUser()
  27.  
  28. }
  29. func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  30. print("titleForHeader")
  31. return self.section[section]
  32.  
  33.  
  34. }
  35.  
  36. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  37.  
  38. return 3
  39.  
  40. }
  41.  
  42. /// Returns the amount of minutes from another date
  43.  
  44. func fetchUser() {
  45.  
  46. print("This is fetchUser")
  47.  
  48. FIRDatabase.database().reference().child("Calls").observe(.childAdded, with: { (snapshot) in
  49.  
  50. if let dictionary = snapshot.value as? [String: AnyObject]{
  51. let user = User()
  52.  
  53.  
  54. user.setValuesForKeys(dictionary)
  55. self.users.append(user)
  56.  
  57.  
  58.  
  59. if user.Job_Status == "Unaccepted" {
  60. self.isFiltered = true
  61. self.unAcceptedUsers.append(user)
  62. print("(user.Name) True")
  63. }else if user.Job_Status == "Enroute"{
  64. self.isFiltered = false
  65. self.enRoute.append(user)
  66. print("(user.Name) False")
  67. }else{
  68. self.onSite.append(user)
  69. }
  70.  
  71.  
  72. //print(self.user)
  73. //DispatchQueue.main.asynchronously(execute: {self.tableView.reloadData()})
  74. DispatchQueue.main.async (execute: { self.tableView.reloadData()
  75.  
  76. print("Later in fetchUser")
  77. })
  78.  
  79.  
  80. }
  81.  
  82. }, withCancel: nil)
  83.  
  84. }
  85.  
  86.  
  87. func handleCancel() {
  88. dismiss(animated: true, completion: nil)
  89. }
  90. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  91.  
  92.  
  93. //var rowCount: Int
  94.  
  95. print(" This is numbers Of Rows")
  96. if section == 0 {
  97. //rowCount = unAcceptedUsers.count
  98. print(unAcceptedUsers.count)
  99. return unAcceptedUsers.count
  100.  
  101. }
  102. else if section == 1{
  103. return enRoute.count
  104. //rowCount = enRoute.count
  105.  
  106. }else {
  107. return onSite.count
  108. }
  109.  
  110. //return rowCount
  111. }
  112.  
  113. func userForIndexPath(indexPath: NSIndexPath) -> User {
  114. if indexPath.section == 0 {
  115. // print(unAcceptedUsers)
  116. return unAcceptedUsers[indexPath.row]
  117.  
  118. }
  119. print(enRoute)
  120. return unAcceptedUsers[indexPath.row]
  121. }
  122.  
  123.  
  124. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  125. //print("Call Cell")
  126. //let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
  127.  
  128. print("CellForRow")
  129.  
  130. let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath)
  131. let user:User
  132. //let user = userForIndexPath(indexPath)
  133.  
  134. if indexPath.section == 0 {
  135. user = unAcceptedUsers[indexPath.row]
  136. }else if indexPath.section == 1{
  137. user = enRoute[indexPath.row]
  138. }else{
  139. user = onSite[indexPath.row]
  140. }
  141.  
  142. let dateFormatter = DateFormatter()
  143. dateFormatter.timeStyle = .short
  144. let date3 = dateFormatter.date(from: user.Time_Logged!)
  145. print("THE TIME IS (date3)")
  146.  
  147. let elapesedTime = NSDate().timeIntervalSince(date3!)
  148. //let minutesPassed = (elapesedTime / 3600)
  149.  
  150. //let dateMinutes = NSDate().timeIntervalSinceReferenceDate (date3)
  151.  
  152. // let calcendar = NSCalendar.currentCalendar()
  153. //let dateComponents = calcendar.components(NSCalendarUnit.Minute, fromDate: date3!, toDate: NSDate(), options: nil)
  154. //let minutesPassed = dateComponents
  155. print ("THE TIME IS (elapesedTime)")
  156.  
  157. let duration = Int(elapesedTime)
  158. let minutesLogged = String(duration)
  159. print(duration)
  160.  
  161.  
  162. //let user = users[indexPath.row]
  163. cell.textLabel?.text = user.Name
  164. cell.detailTextLabel?.text = minutesLogged
  165. //print(user.Adress)
  166.  
  167. return cell
  168.  
  169. }
  170. //var valueToPass:String!
  171. var valueToPass:String!
  172. //var productsValue = [unAcceptedCallDataVie]]
  173.  
  174. func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  175.  
  176.  
  177. tableView.deselectRow(at: indexPath as IndexPath, animated: true)
  178.  
  179. let callInfoView:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Callinfo") as UIViewController
  180. let unAcceptedView:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "unAcceptedCall") as UIViewController
  181.  
  182. let currentCell = tableView.cellForRow(at: indexPath as IndexPath) as UITableViewCell!;
  183.  
  184. if indexPath.section == 0 {
  185. valueToPass = currentCell?.textLabel!.text
  186. //valueToPass = currentCell2
  187. performSegue(withIdentifier: "passData", sender: self)
  188. self.present(unAcceptedView, animated: true, completion: nil)
  189.  
  190. //print(valueToPass)
  191. }else if indexPath.section == 1{
  192. self.present(callInfoView, animated: true, completion: nil)
  193. print("Enroute")
  194. }else {
  195. self.present(callInfoView, animated: true, completion: nil)
  196. print("Onsite")
  197. }
  198. // print(user.Adress)
  199. }
  200.  
  201.  
  202. func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  203. if (segue.identifier == "passData") {
  204. //let user:User
  205. // initialize new view controller and cast it as your view controller
  206. let viewController = segue.destination as! unAcceptedCallDataView
  207.  
  208. // your new view controller should have property that will store passed value
  209. //var passedValue = viewController.nameLable.text
  210. //print(user.Adress)
  211. viewController.LableText = valueToPass
  212.  
  213. }
  214. class UserCell: UITableViewCell {
  215. override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
  216. super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  217. }
  218. required init?(coder aDecoder: NSCoder){
  219. fatalError("init(coder:) has not been implemented")
  220. }}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement