SettingsTableViewController.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // SettingsTableViewController.swift
  3. // RileyLink
  4. //
  5. // Created by Nathan Racklyeft on 8/29/15.
  6. // Copyright © 2015 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. import RileyLinkKit
  10. private let ConfigCellIdentifier = "ConfigTableViewCell"
  11. private let TapToSetString = NSLocalizedString("Tap to set", comment: "The empty-state text for a configuration value")
  12. class SettingsTableViewController: UITableViewController, TextFieldTableViewControllerDelegate {
  13. private enum Section: Int {
  14. case About = 0
  15. case Upload
  16. case Configuration
  17. static let count = 3
  18. }
  19. private enum AboutRow: Int {
  20. case Version = 0
  21. static let count = 1
  22. }
  23. private enum UploadRow: Int {
  24. case Upload = 0
  25. static let count = 1
  26. }
  27. private enum ConfigurationRow: Int {
  28. case PumpID = 0
  29. case Nightscout
  30. static let count = 2
  31. }
  32. private var dataManager: DeviceDataManager {
  33. return DeviceDataManager.sharedManager
  34. }
  35. // MARK: - UITableViewDataSource
  36. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  37. return Section.count
  38. }
  39. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  40. switch Section(rawValue: section)! {
  41. case .About:
  42. return AboutRow.count
  43. case .Upload:
  44. return UploadRow.count
  45. case .Configuration:
  46. return ConfigurationRow.count
  47. }
  48. }
  49. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  50. let cell: UITableViewCell
  51. switch Section(rawValue: indexPath.section)! {
  52. case .About:
  53. switch AboutRow(rawValue: indexPath.row)! {
  54. case .Version:
  55. let versionCell = UITableViewCell(style: .Default, reuseIdentifier: "Version")
  56. versionCell.selectionStyle = .None
  57. let version = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString")!
  58. versionCell.textLabel?.text = "RileyLink iOS v\(version)"
  59. return versionCell
  60. }
  61. case .Upload:
  62. switch UploadRow(rawValue: indexPath.row)! {
  63. case .Upload:
  64. let switchCell = tableView.dequeueReusableCellWithIdentifier(SwitchTableViewCell.className, forIndexPath: indexPath) as! SwitchTableViewCell
  65. switchCell.`switch`?.on = Config.sharedInstance().uploadEnabled
  66. switchCell.titleLabel.text = NSLocalizedString("Upload To Nightscout", comment: "The title text for the nightscout upload enabled switch cell")
  67. switchCell.`switch`?.addTarget(self, action: #selector(uploadEnabledChanged(_:)), forControlEvents: .ValueChanged)
  68. return switchCell
  69. }
  70. case .Configuration:
  71. let configCell = tableView.dequeueReusableCellWithIdentifier(ConfigCellIdentifier, forIndexPath: indexPath)
  72. switch ConfigurationRow(rawValue: indexPath.row)! {
  73. case .PumpID:
  74. configCell.textLabel?.text = NSLocalizedString("Pump ID", comment: "The title text for the pump ID config value")
  75. configCell.detailTextLabel?.text = DeviceDataManager.sharedManager.pumpID ?? TapToSetString
  76. case .Nightscout:
  77. let nightscoutService = dataManager.remoteDataManager.nightscoutService
  78. configCell.textLabel?.text = nightscoutService.title
  79. configCell.detailTextLabel?.text = nightscoutService.siteURL?.absoluteString ?? TapToSetString
  80. }
  81. cell = configCell
  82. }
  83. return cell
  84. }
  85. override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  86. switch Section(rawValue: section)! {
  87. case .About:
  88. return NSLocalizedString("About", comment: "The title of the about section")
  89. case .Upload:
  90. return nil
  91. case .Configuration:
  92. return NSLocalizedString("Configuration", comment: "The title of the configuration section in settings")
  93. }
  94. }
  95. // MARK: - UITableViewDelegate
  96. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  97. switch Section(rawValue: indexPath.section)! {
  98. case .Configuration:
  99. let sender = tableView.cellForRowAtIndexPath(indexPath)
  100. switch ConfigurationRow(rawValue: indexPath.row)! {
  101. case .PumpID:
  102. performSegueWithIdentifier(TextFieldTableViewController.className, sender: sender)
  103. case .Nightscout:
  104. let service = dataManager.remoteDataManager.nightscoutService
  105. let vc = AuthenticationViewController(authentication: service)
  106. vc.authenticationObserver = { [unowned self] (service) in
  107. self.dataManager.remoteDataManager.nightscoutService = service
  108. self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
  109. }
  110. showViewController(vc, sender: indexPath)
  111. }
  112. case .Upload, .About:
  113. break
  114. }
  115. }
  116. override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  117. switch Section(rawValue: section)! {
  118. case .Upload, .Configuration, .About:
  119. return nil
  120. }
  121. }
  122. // MARK: - Navigation
  123. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  124. if let
  125. cell = sender as? UITableViewCell,
  126. indexPath = tableView.indexPathForCell(cell)
  127. {
  128. switch segue.destinationViewController {
  129. case let vc as TextFieldTableViewController:
  130. switch ConfigurationRow(rawValue: indexPath.row)! {
  131. case .PumpID:
  132. vc.placeholder = NSLocalizedString("Enter the 6-digit pump ID", comment: "The placeholder text instructing users how to enter a pump ID")
  133. vc.value = DeviceDataManager.sharedManager.pumpID
  134. default:
  135. break
  136. }
  137. vc.title = cell.textLabel?.text
  138. vc.indexPath = indexPath
  139. vc.delegate = self
  140. default:
  141. break
  142. }
  143. }
  144. }
  145. // MARK: - Uploader mangement
  146. func uploadEnabledChanged(sender: UISwitch) {
  147. Config.sharedInstance().uploadEnabled = sender.on
  148. }
  149. // MARK: - TextFieldTableViewControllerDelegate
  150. func textFieldTableViewControllerDidEndEditing(controller: TextFieldTableViewController) {
  151. if let indexPath = controller.indexPath {
  152. switch ConfigurationRow(rawValue: indexPath.row)! {
  153. case .PumpID:
  154. DeviceDataManager.sharedManager.pumpID = controller.value
  155. default:
  156. break
  157. }
  158. }
  159. tableView.reloadData()
  160. }
  161. }