OOAppEditController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // OOAppEditController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/10.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. protocol AppEditControllerUpdater:class {
  10. func appEditControllerUpdater()
  11. }
  12. class OOAppEditController: UITableViewController {
  13. private var mainApps:[O2App] = []
  14. private var noMainApps:[O2App] = []
  15. var delegate:AppEditControllerUpdater?
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. title = "应用管理"
  19. navigationItem.rightBarButtonItem = UIBarButtonItem(title: "完成", style: .plain, target: self, action: #selector(_saveCustomApps))
  20. tableView.isEditing = true
  21. loadData()
  22. }
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27. @objc private func _saveCustomApps() {
  28. mainApps.forEachEnumerated { (i, app) in
  29. app.mainOrder = i
  30. DBManager.shared.updateData(app, 1)
  31. }
  32. noMainApps.forEachEnumerated { (i, app) in
  33. app.order = i
  34. DBManager.shared.updateData(app, 0)
  35. }
  36. delegate?.appEditControllerUpdater()
  37. self.showMessage(msg: "更新成功")
  38. }
  39. func loadData() {
  40. mainApps = DBManager.shared.queryMainData()
  41. noMainApps = DBManager.shared.queryNoMainData()
  42. tableView.reloadData()
  43. }
  44. // MARK: - Table view data source
  45. override func numberOfSections(in tableView: UITableView) -> Int {
  46. return 2
  47. }
  48. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  49. if section == 0 {
  50. return mainApps.count
  51. }else if section == 1 {
  52. return noMainApps.count
  53. }
  54. return 0
  55. }
  56. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  57. let cell = tableView.dequeueReusableCell(withIdentifier: "OOAppEditCell", for: indexPath) as! (OOAppEditCell & Configurable)
  58. let section = indexPath.section
  59. if section == 0 {
  60. let item = mainApps[indexPath.row]
  61. cell.config(withItem: item)
  62. }else if section == 1 {
  63. let item = noMainApps[indexPath.row]
  64. cell.config(withItem: item)
  65. }
  66. return cell
  67. }
  68. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  69. if section == 0 {
  70. return "主页应用"
  71. }else if section == 1 {
  72. return "所有应用"
  73. }
  74. return nil
  75. }
  76. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  77. return true
  78. }
  79. override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
  80. return .none
  81. }
  82. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  83. return true
  84. }
  85. override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  86. //section相同
  87. if sourceIndexPath.section == destinationIndexPath.section {
  88. if sourceIndexPath.section == 0 {
  89. if sourceIndexPath.row != destinationIndexPath.row {
  90. swap(&mainApps[sourceIndexPath.row], &mainApps[destinationIndexPath.row])
  91. }
  92. }else if sourceIndexPath.section == 1 {
  93. if sourceIndexPath.row != destinationIndexPath.row {
  94. swap(&noMainApps[sourceIndexPath.row], &noMainApps[destinationIndexPath.row])
  95. }
  96. }
  97. }else{
  98. //section不同
  99. if sourceIndexPath.section == 0 && destinationIndexPath.section == 1 {
  100. noMainApps.insert(mainApps.remove(at: sourceIndexPath.row), at: destinationIndexPath.row)
  101. }else if sourceIndexPath.section == 1 && destinationIndexPath.section == 0 {
  102. mainApps.insert(noMainApps.remove(at: sourceIndexPath.row), at: destinationIndexPath.row)
  103. }
  104. }
  105. }
  106. }