O2MindMapViewController.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // O2MindMapViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/12/15.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. let o2MindMapDefaultFolderRoot = "根目录"
  11. let o2MindMapDefaultFolderRootId = "root"
  12. class O2MindMapViewController: UIViewController {
  13. @IBOutlet weak var folderLabel: UILabel!
  14. @IBOutlet weak var folderBar: UIView! // 目录工具条
  15. @IBOutlet weak var tableView: UITableView!
  16. private lazy var currentFolder: MindFolder = {
  17. let f = MindFolder()
  18. f.id = o2MindMapDefaultFolderRootId
  19. f.name = o2MindMapDefaultFolderRoot
  20. return f
  21. }()
  22. private lazy var viewModel: O2MindMapViewModel = {
  23. return O2MindMapViewModel()
  24. }()
  25. private var isLoading = false
  26. private var nextId = O2.O2_First_ID
  27. private var mindMapList:[MindMapItem] = []
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. self.title = "脑图"
  31. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .plain, target: self, action: #selector(closeWindow))
  32. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "创建", style: .plain, target: self, action: #selector(addMenus))
  33. self.tableView.tableFooterView = UIView(frame: CGRect.zero)
  34. self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  35. DDLogDebug("下拉刷新???")
  36. self.nextId = O2.O2_First_ID
  37. self.loadMindMapList()
  38. })
  39. self.tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
  40. DDLogDebug("上拉加载。。。。。。。。。。。。")
  41. self.loadMindMapList()
  42. })
  43. self.tableView.delegate = self
  44. self.tableView.dataSource = self
  45. self.folderBar.addTapGesture(target: self, action: #selector(openFolderSelector))
  46. self.folderLabel.text = currentFolder.name
  47. }
  48. override func viewWillAppear(_ animated: Bool) {
  49. self.tableView.mj_header.beginRefreshing()
  50. }
  51. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  52. if segue.identifier == "showSelectFolder" {
  53. if let vc = segue.destination as? O2MindSelectViewController {
  54. vc.currentFolder = self.currentFolder
  55. vc.delegate = { folder in
  56. self.currentFolder = folder
  57. self.folderLabel.text = folder.name
  58. // 重新查询列表
  59. self.nextId = O2.O2_First_ID
  60. self.loadMindMapList()
  61. }
  62. }
  63. } else if segue.identifier == "showMindMapCanvas" {
  64. if let id = sender as? String, let vc = segue.destination as? O2MindMapCanvasController {
  65. vc.id = id // 设置脑图id
  66. }
  67. }
  68. }
  69. private func loadMindMapList() {
  70. guard let folderId = self.currentFolder.id else {
  71. return
  72. }
  73. if self.isLoading { // 正在查询
  74. return
  75. }
  76. self.isLoading = true
  77. if self.nextId == O2.O2_First_ID {
  78. self.mindMapList = []
  79. }
  80. self.viewModel.listMindMapFilter(nextId: nextId, folderId: folderId).then { list in
  81. for item in list {
  82. self.mindMapList.append(item)
  83. }
  84. self.enRefresh(list: list)
  85. }.catch { err in
  86. DDLogError("获取脑图数据错误,\(err.localizedDescription)")
  87. self.showError(title: "查询出错!")
  88. self.enRefresh(list: [])
  89. }
  90. }
  91. private func enRefresh(list: [MindMapItem]) {
  92. if list.count < O2.defaultPageSize {
  93. self.tableView.mj_footer.endRefreshingWithNoMoreData()
  94. }else{
  95. self.tableView.mj_footer.endRefreshing()
  96. }
  97. self.tableView.mj_header.endRefreshing()
  98. if list.count > 0 {
  99. self.nextId = list[list.count - 1].id!
  100. }
  101. self.tableView.reloadData()
  102. self.isLoading = false
  103. }
  104. //新建脑图
  105. private func createNewMindMap() {
  106. self.showDefaultConfirm(title: "提示", message: "确定要在当前目录【\(self.currentFolder.name ?? "")】中创建一个脑图?") { action in
  107. self.showPromptAlert(title: "创建", message: "请输入脑图名称", inputText: "") { action, result in
  108. if result == "" || result.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  109. self.showError(title: "请输入脑图名称!")
  110. return
  111. }
  112. self.showLoading()
  113. self.viewModel.createMindMap(name: result, folderId: self.currentFolder.id ?? "")
  114. .then { id in
  115. DDLogDebug("保存成功,打开脑图 \(id)")
  116. self.openMindMapView(id: id)
  117. }.catch { err in
  118. DDLogError("创建脑图失败,\(err.localizedDescription)")
  119. self.showError(title: "创建脑图失败!")
  120. }
  121. }
  122. }
  123. }
  124. private func createNewFolder() {
  125. self.showDefaultConfirm(title: "提示", message: "确定要在当前目录【\(self.currentFolder.name ?? "")】下创建一个子目录?") { action in
  126. self.showPromptAlert(title: "创建", message: "请输入目录名称", inputText: "") { action, result in
  127. if result == "" || result.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  128. self.showError(title: "请输入目录名称!")
  129. return
  130. }
  131. self.showLoading()
  132. self.viewModel.createFolder(name: result, parentId: self.currentFolder.id ?? "")
  133. .then { id in
  134. DDLogDebug("创建目录成功,\(id)")
  135. self.loadFolderListData(id)
  136. }.catch { err in
  137. DDLogError("创建目录失败,\(err.localizedDescription)")
  138. self.showError(title: "创建目录失败!")
  139. }
  140. }
  141. }
  142. }
  143. private func loadFolderListData(_ forcreateId: String? = nil) {
  144. self.viewModel.myFolderTree().then { list in
  145. self.hideLoading()
  146. if forcreateId != nil {
  147. for item in list {
  148. if item.id == forcreateId {
  149. self.currentFolder = item
  150. self.folderLabel.text = item.name
  151. // 重新查询列表
  152. self.nextId = O2.O2_First_ID
  153. self.loadMindMapList()
  154. }
  155. }
  156. }
  157. }.catch { error in
  158. DDLogError(error.localizedDescription)
  159. self.showError(title: "请求数据异常!")
  160. }
  161. }
  162. //MARK: - private func
  163. @objc func closeWindow() {
  164. self.navigationController?.dismiss(animated: true, completion: nil)
  165. }
  166. @objc func addMenus() {
  167. var menus :[UIAlertAction] = []
  168. let chooseImg = UIAlertAction(title: "新建脑图", style: .default) { (ok) in
  169. DDLogInfo("新建脑图")
  170. self.createNewMindMap()
  171. }
  172. menus.append(chooseImg)
  173. let camera = UIAlertAction(title: "新建目录", style: .default) { (ok) in
  174. DDLogInfo("新建目录")
  175. self.createNewFolder()
  176. }
  177. menus.append(camera)
  178. self.showSheetAction(title: "新建", message: "", actions: menus)
  179. }
  180. // 选择目录
  181. @objc func openFolderSelector() {
  182. DDLogDebug("打开目录选择")
  183. self.performSegue(withIdentifier: "showSelectFolder", sender: nil)
  184. }
  185. // 打开脑图
  186. @objc func openMindMapView(id: String) {
  187. DDLogDebug("打开脑图, id: \(id)")
  188. self.performSegue(withIdentifier: "showMindMapCanvas", sender: id)
  189. }
  190. }
  191. extension O2MindMapViewController: UITableViewDelegate, UITableViewDataSource {
  192. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  193. return mindMapList.count
  194. }
  195. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  196. if let cell = tableView.dequeueReusableCell(withIdentifier: "MindMapItemTableViewCell", for: indexPath) as? MindMapItemTableViewCell {
  197. cell.setItem(item: mindMapList[indexPath.row])
  198. return cell
  199. }
  200. return UITableViewCell()
  201. }
  202. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  203. tableView.deselectRow(at: indexPath, animated: false)
  204. if let id = mindMapList[indexPath.row].id {
  205. self.openMindMapView(id: id)
  206. } else {
  207. DDLogError("mind map item id 为空。。。。。")
  208. }
  209. }
  210. }