DeviceListViewController.swift 2.3 KB

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