DeviceListViewController.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // DeviceListViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/5/7.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. protocol DeviceUnbindBtnClickListener {
  10. func onClick(device: O2BindDeviceModel)
  11. }
  12. class DeviceListViewController: UITableViewController {
  13. private let viewModel: DeviceManagerViewModel = {
  14. return DeviceManagerViewModel()
  15. }()
  16. private var deviceList: [O2BindDeviceModel] = []
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. self.title = "常用设备管理"
  20. self.loadDeviceList()
  21. }
  22. private func loadDeviceList() {
  23. viewModel.getDeviceList().then { (list) in
  24. self.deviceList.removeAll()
  25. list.forEach({ (device) in
  26. self.deviceList.append(device)
  27. })
  28. self.tableView.reloadData()
  29. }
  30. }
  31. // MARK: - Table view data source
  32. override func numberOfSections(in tableView: UITableView) -> Int {
  33. return 1
  34. }
  35. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  36. return self.deviceList.count
  37. }
  38. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  39. let cell = tableView.dequeueReusableCell(withIdentifier: "deviceTableViewCell", for: indexPath) as! DeviceTableViewCell
  40. cell.deviceData = self.deviceList[indexPath.row]
  41. cell.unbindClickDelegate = self
  42. return cell
  43. }
  44. }
  45. // MARK: - 解绑点击事件
  46. extension DeviceListViewController: DeviceUnbindBtnClickListener {
  47. func onClick(device: O2BindDeviceModel) {
  48. guard let deviceToken = device.name else {
  49. return
  50. }
  51. guard let token = O2AuthSDK.shared.bindDevice()?.name else {
  52. return
  53. }
  54. if token != deviceToken {
  55. self.showDefaultConfirm(title: "提示", message: "确定要解绑 \(device.deviceType) 设备") { (action) in
  56. self.viewModel.unbindDevice(token: deviceToken).then({ (result) in
  57. if (result) {
  58. self.loadDeviceList()
  59. }else {
  60. self.showError(title: "解绑失败!")
  61. }
  62. })
  63. }
  64. }
  65. }
  66. }