RileyLinkListTableViewController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // RileyLinkListTableViewController.swift
  3. // RileyLink
  4. //
  5. // Created by Pete Schwamb on 5/11/16.
  6. // Copyright © 2016 Pete Schwamb. All rights reserved.
  7. //
  8. import UIKit
  9. import RileyLinkKit
  10. class RileyLinkListTableViewController: UITableViewController {
  11. // Retreive the managedObjectContext from AppDelegate
  12. let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. tableView.registerNib(RileyLinkDeviceTableViewCell.nib(), forCellReuseIdentifier: RileyLinkDeviceTableViewCell.className)
  16. dataManagerObserver = NSNotificationCenter.defaultCenter().addObserverForName(nil, object: dataManager, queue: nil) { [weak self = self] (note) -> Void in
  17. if let deviceManager = self?.dataManager.rileyLinkManager {
  18. switch note.name {
  19. case RileyLinkDeviceManager.DidDiscoverDeviceNotification:
  20. self?.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: deviceManager.devices.count - 1, inSection: 0)], withRowAnimation: .Automatic)
  21. case RileyLinkDeviceManager.ConnectionStateDidChangeNotification,
  22. RileyLinkDeviceManager.RSSIDidChangeNotification,
  23. RileyLinkDeviceManager.NameDidChangeNotification:
  24. if let device = note.userInfo?[RileyLinkDeviceManager.RileyLinkDeviceKey] as? RileyLinkDevice, index = deviceManager.devices.indexOf({ $0 === device }) {
  25. self?.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .None)
  26. }
  27. default:
  28. break
  29. }
  30. }
  31. }
  32. }
  33. deinit {
  34. dataManagerObserver = nil
  35. }
  36. private var dataManagerObserver: AnyObject? {
  37. willSet {
  38. if let observer = dataManagerObserver {
  39. NSNotificationCenter.defaultCenter().removeObserver(observer)
  40. }
  41. }
  42. }
  43. private var dataManager: DeviceDataManager {
  44. return DeviceDataManager.sharedManager
  45. }
  46. override func viewDidAppear(animated: Bool) {
  47. super.viewDidAppear(animated)
  48. dataManager.rileyLinkManager.deviceScanningEnabled = true
  49. }
  50. override func viewWillDisappear(animated: Bool) {
  51. super.viewWillDisappear(animated)
  52. dataManager.rileyLinkManager.deviceScanningEnabled = false
  53. }
  54. // MARK: Table view data source
  55. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  56. return 1
  57. }
  58. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. return dataManager.rileyLinkManager.devices.count
  60. }
  61. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  62. let cell: UITableViewCell
  63. let deviceCell = tableView.dequeueReusableCellWithIdentifier(RileyLinkDeviceTableViewCell.className) as! RileyLinkDeviceTableViewCell
  64. let device = dataManager.rileyLinkManager.devices[indexPath.row]
  65. deviceCell.configureCellWithName(device.name,
  66. signal: device.RSSI,
  67. peripheralState: device.peripheral.state
  68. )
  69. deviceCell.connectSwitch.addTarget(self, action: #selector(deviceConnectionChanged(_:)), forControlEvents: .ValueChanged)
  70. cell = deviceCell
  71. return cell
  72. }
  73. func deviceConnectionChanged(connectSwitch: UISwitch) {
  74. let switchOrigin = connectSwitch.convertPoint(.zero, toView: tableView)
  75. if let indexPath = tableView.indexPathForRowAtPoint(switchOrigin)
  76. {
  77. let device = dataManager.rileyLinkManager.devices[indexPath.row]
  78. if connectSwitch.on {
  79. dataManager.connectToRileyLink(device)
  80. } else {
  81. dataManager.disconnectFromRileyLink(device)
  82. }
  83. }
  84. }
  85. // MARK: - UITableViewDelegate
  86. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  87. let vc = RileyLinkDeviceTableViewController()
  88. vc.device = dataManager.rileyLinkManager.devices[indexPath.row]
  89. showViewController(vc, sender: indexPath)
  90. }
  91. /*
  92. // Override to support conditional editing of the table view.
  93. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  94. // Return NO if you do not want the specified item to be editable.
  95. return YES;
  96. }
  97. */
  98. /*
  99. // Override to support editing the table view.
  100. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  101. if (editingStyle == UITableViewCellEditingStyleDelete) {
  102. // Delete the row from the data source
  103. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  104. } else if (editingStyle == UITableViewCellEditingStyleInsert) {
  105. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  106. }
  107. }
  108. */
  109. /*
  110. // Override to support rearranging the table view.
  111. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  112. }
  113. */
  114. /*
  115. // Override to support conditional rearranging of the table view.
  116. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  117. // Return NO if you do not want the item to be re-orderable.
  118. return YES;
  119. }
  120. */
  121. }