CloudFileMoveFolderController.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CloudFileMoveFolderController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/25.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. typealias PickedFolderCallback = (_ folder: OOFolder) -> Void ///< 定义确认回调
  11. class CloudFileMoveFolderController: UIViewController {
  12. static func chooseFolderVC(_ callback: @escaping PickedFolderCallback) -> CloudFileMoveFolderController? {
  13. let storyBoard = UIStoryboard(name: "CloudFile", bundle: nil)
  14. let destVC = storyBoard.instantiateViewController(withIdentifier: "cloudFileMovePicker") as? CloudFileMoveFolderController
  15. destVC?.callback = callback
  16. return destVC
  17. }
  18. var callback: PickedFolderCallback?
  19. //面包屑 列表 可以传入顶级组织的OOFolder
  20. private var breadcrumbList: [OOFolder] = []
  21. private lazy var cFileVM: CloudFileViewModel = {
  22. return CloudFileViewModel()
  23. }()
  24. //table 数据
  25. private var dataList: [OOFolder] = []
  26. @IBOutlet weak var tableView: UITableView!
  27. @IBOutlet weak var breadcrumbView: UIScrollView!
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. //最后一个 设置title
  31. self.title = Languager.standardLanguager().string(key: "Choose Move Position")
  32. //初始化数据
  33. if self.breadcrumbList.isEmpty {
  34. let all = OOFolder()
  35. all.id = ""
  36. all.name = Languager.standardLanguager().string(key: "All File")
  37. self.breadcrumbList.append(all)
  38. }
  39. //初始化tableView
  40. self.tableView.dataSource = self
  41. self.tableView.delegate = self
  42. self.tableView.register(UINib.init(nibName: "CloudFileMoveFolderCell", bundle: nil), forCellReuseIdentifier: "CloudFileMoveFolderCell")
  43. self.loadListData()
  44. }
  45. // MARK: - 后台数据服务
  46. private func loadListData() {
  47. self.refreshBreadcrumbBar()
  48. self.showLoading()
  49. //清空数据
  50. self.dataList = []
  51. let id = self.breadcrumbList[self.breadcrumbList.count-1].id ?? ""
  52. self.cFileVM.folderList(folderId: id)
  53. .then { (result) in
  54. self.dataList = result
  55. self.hideLoading()
  56. self.tableView.reloadData()
  57. }.catch { (error) in
  58. DDLogError(error.localizedDescription)
  59. self.hideLoading()
  60. }
  61. }
  62. private func refreshBreadcrumbBar() {
  63. self.breadcrumbView.removeSubviews()
  64. var oX = CGFloat(4.0)
  65. let arrowW = CGFloat(24)
  66. let arrowH = CGFloat(32)
  67. breadcrumbList.forEachEnumerated { (index, bar) in
  68. let name = bar.name ?? ""
  69. var textColor:UIColor
  70. if self.breadcrumbList.count == (index+1) {
  71. textColor = UIColor(hex:"#666666")
  72. }else {
  73. textColor = base_color
  74. }
  75. let firstSize = name.getSize(with: 15)
  76. let oY = (self.breadcrumbView.bounds.height - firstSize.height) / 2
  77. let firstLabel = UILabel(frame: CGRect(x: CGFloat(oX), y: oY, width: firstSize.width, height: firstSize.height))
  78. firstLabel.textAlignment = .left
  79. let textAttributes = [NSAttributedString.Key.foregroundColor: textColor,NSAttributedString.Key.font:UIFont(name:"PingFangSC-Regular",size:15)!]
  80. firstLabel.attributedText = NSMutableAttributedString(string: name, attributes: textAttributes)
  81. firstLabel.sizeToFit()
  82. self.breadcrumbView.addSubview(firstLabel)
  83. oX += firstSize.width
  84. if self.breadcrumbList.count != (index+1) {
  85. let arrowY = (self.breadcrumbView.bounds.height - arrowH) / 2
  86. let arrowImage = UIImageView(frame: CGRect(x: CGFloat(oX), y: arrowY, width: arrowW, height: arrowH))
  87. arrowImage.image = UIImage(named: "arrow_r")
  88. arrowImage.contentMode = .scaleAspectFit
  89. self.breadcrumbView.addSubview(arrowImage)
  90. oX += arrowW
  91. }
  92. firstLabel.addTapGesture(action: { (rec) in
  93. DDLogDebug("点击了 \(index)")
  94. if self.breadcrumbList.count != (index+1) {
  95. var newList:[OOFolder] = []
  96. for i in 0...index {
  97. newList.append(self.breadcrumbList[i])
  98. }
  99. self.breadcrumbList = newList
  100. self.loadListData()
  101. }
  102. })
  103. }
  104. var size = self.breadcrumbView.contentSize
  105. size.width = oX
  106. self.breadcrumbView.showsHorizontalScrollIndicator = true
  107. self.breadcrumbView.contentSize = size
  108. self.breadcrumbView.bounces = true
  109. }
  110. }
  111. // MARK: - TableView delegate
  112. extension CloudFileMoveFolderController: UITableViewDelegate, UITableViewDataSource {
  113. func numberOfSections(in tableView: UITableView) -> Int {
  114. return 1
  115. }
  116. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  117. return self.dataList.count
  118. }
  119. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  120. return UIView(frame: CGRect.zero)
  121. }
  122. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  123. let folder = self.dataList[indexPath.row]
  124. if let cell = tableView.dequeueReusableCell(withIdentifier: "CloudFileMoveFolderCell", for: indexPath) as? CloudFileMoveFolderCell {
  125. cell.delegate = self
  126. cell.setData(folder: folder)
  127. return cell
  128. } else {
  129. return UITableViewCell()
  130. }
  131. }
  132. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  133. //点击处理
  134. let folder = self.dataList[indexPath.row]
  135. self.breadcrumbList.append(folder)
  136. self.loadListData()
  137. tableView.deselectRow(at: indexPath, animated: true)
  138. }
  139. }
  140. extension CloudFileMoveFolderController: CloudFileMoveChooseDelegate {
  141. func choose(folder: OOFolder) {
  142. self.callback?(folder)
  143. self.popVC()
  144. }
  145. }