CloudFileListBaseController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // CloudFileListBaseController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/28.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. enum CloudFileType {
  11. case image
  12. case office
  13. case movie
  14. case music
  15. case other
  16. }
  17. protocol CloudFileListBaseChildDelegate {
  18. func reloadUI()
  19. func reloadDataAndUI()
  20. }
  21. class CloudFileListBaseController: CloudFileBaseVC {
  22. @IBOutlet weak var containerView: UIView!
  23. //底部工具栏
  24. private var toolbarView: UIToolbar!
  25. //底部toolbar和containerView之间的约束名称
  26. private let containerBottomContraintName = "containerBottomContraint"
  27. var childDelegate: CloudFileListBaseChildDelegate?
  28. var fileType: CloudFileType = .image
  29. override func viewDidLoad() {
  30. super.viewDidLoad()
  31. var title = Languager.standardLanguager().string(key: "Cloud File Type Image")
  32. switch self.fileType {
  33. case .image:
  34. title = Languager.standardLanguager().string(key: "Cloud File Type Image")
  35. break
  36. case .office:
  37. title = Languager.standardLanguager().string(key: "Cloud File Type Document")
  38. break
  39. case .movie:
  40. title = Languager.standardLanguager().string(key: "Cloud File Type Video")
  41. break
  42. case .music:
  43. title = Languager.standardLanguager().string(key: "Cloud File Type Music")
  44. break
  45. case .other:
  46. title = Languager.standardLanguager().string(key: "Cloud File Type Other")
  47. break
  48. }
  49. self.title = title
  50. //toolbar 初始化底部工具栏 先放在屏幕下面
  51. self.toolbarView = UIToolbar(frame: CGRect(x: 0, y: self.view.height - 44, width: self.view.width, height: 44))
  52. switch self.fileType {
  53. case .image:
  54. if let imageVC = self.storyboard?.instantiateViewController(withIdentifier: "cloudFileImageVC") as? CloudFileImageCollectionController {
  55. self.addChild(imageVC)
  56. imageVC.view.frame = self.containerView.bounds
  57. self.childDelegate = imageVC
  58. self.containerView.addSubview(imageVC.view)
  59. }
  60. default:
  61. if let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "cloudFileTypeListVC") as? CloudFileTypeListController {
  62. otherVC.fileType = self.fileType
  63. self.addChild(otherVC)
  64. otherVC.view.frame = self.containerView.bounds
  65. self.childDelegate = otherVC
  66. self.containerView.addSubview(otherVC.view)
  67. }
  68. }
  69. }
  70. // MARK: - private method
  71. private func reloadUI() {
  72. self.refreshBottomToolBar()
  73. self.childDelegate?.reloadUI()
  74. }
  75. override func loadListData() {
  76. self.checkedFileList.removeAll()
  77. self.refreshBottomToolBar()
  78. self.childDelegate?.reloadDataAndUI()
  79. }
  80. //底部工具栏中的单个按钮生成
  81. fileprivate func generateBottomButton(_ items: inout [UIBarButtonItem], name: String, tapCall: @escaping ()->Void) {
  82. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  83. let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  84. btn.setTitle(name, for: .normal)
  85. btn.setTitleColor(base_color, for: .normal)
  86. btn.addTapGesture { (tap) in
  87. tapCall()
  88. }
  89. let item = UIBarButtonItem(customView: btn)
  90. items.append(spaceItem)
  91. items.append(item)
  92. items.append(spaceItem)
  93. }
  94. //重新生成刷新底部工具栏和按钮
  95. private func refreshBottomToolBar() {
  96. let totalCount = self.checkedFileList.count
  97. if totalCount > 0 {
  98. var items: [UIBarButtonItem] = []
  99. if totalCount == 1 {
  100. let reName = Languager.standardLanguager().string(key: "Rename")
  101. generateBottomButton(&items, name: reName, tapCall: {
  102. self.renameOp()
  103. })
  104. }
  105. //其他按钮 删除 移动 分享
  106. let deleteName = Languager.standardLanguager().string(key: "Delete")
  107. self.generateBottomButton(&items, name: deleteName) {
  108. self.deleteOp()
  109. }
  110. let moveName = Languager.standardLanguager().string(key: "Move")
  111. self.generateBottomButton(&items, name: moveName) {
  112. self.moveOp()
  113. }
  114. let shareName = Languager.standardLanguager().string(key: "Share")
  115. self.generateBottomButton(&items, name: shareName) {
  116. self.shareOp()
  117. }
  118. self.layoutBottomBar(items: items)
  119. }else {
  120. var c = false
  121. self.view.constraints.forEach { (constraint) in
  122. if constraint.identifier == self.containerBottomContraintName {
  123. c = true
  124. }
  125. }
  126. if !c {
  127. let bottom = NSLayoutConstraint(item: self.view as Any, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
  128. bottom.identifier = self.containerBottomContraintName
  129. self.toolbarView.removeConstraints(self.toolbarView.constraints)
  130. self.toolbarView.removeFromSuperview()
  131. self.view.addConstraint(bottom)
  132. self.view.layoutIfNeeded()
  133. }
  134. }
  135. }
  136. //布局底部工具栏
  137. private func layoutBottomBar(items: [UIBarButtonItem]) {
  138. DDLogDebug("layout bottom \(items.count)")
  139. //适配iPhoneX之后的版本手机
  140. let toolBarHeight = CGFloat(44.0)
  141. var bottomSpace = CGFloat(0)
  142. if iPhoneX {
  143. bottomSpace = CGFloat(-34.0)
  144. }
  145. if items.count > 0 {
  146. self.toolbarView.items = items
  147. self.view.addSubview(self.toolbarView)
  148. self.toolbarView.translatesAutoresizingMaskIntoConstraints = false
  149. //高度约束
  150. let heightC = NSLayoutConstraint(item: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 0.0, constant: toolBarHeight)
  151. self.toolbarView.addConstraint(heightC)
  152. //底部约束
  153. let bottom = NSLayoutConstraint(item: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: bottomSpace)
  154. //右边约束
  155. let trailing = NSLayoutConstraint(item: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
  156. //左边约束
  157. let leading = NSLayoutConstraint(item: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.view, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
  158. self.view.addConstraints([bottom, leading, trailing])
  159. self.view.constraints.forEach { (constraint) in
  160. //删除原来tableView的底部约束
  161. if constraint.identifier == self.containerBottomContraintName {
  162. self.view.removeConstraint(constraint)
  163. }
  164. }
  165. //添加tableView和底部工具栏的约束
  166. let webcTop = NSLayoutConstraint(item: self.containerView as Any, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
  167. self.view.addConstraint(webcTop)
  168. self.view.layoutIfNeeded()
  169. }
  170. }
  171. func isFileChecked(_ file: OOAttachment) -> Bool {
  172. let c = self.checkedFileList.contains { (item) -> Bool in
  173. return item.id == file.id
  174. }
  175. return c
  176. }
  177. }
  178. extension CloudFileListBaseController: CloudFileCheckClickDelegate {
  179. func clickFolder(_ folder: OOFolder) {
  180. //
  181. }
  182. func clickFile(_ file: OOAttachment) {
  183. DDLogDebug("base list click file")
  184. if self.checkedFileList.contains(file) {
  185. self.checkedFileList.removeFirst(file)
  186. }else {
  187. self.checkedFileList.append(file)
  188. }
  189. self.reloadUI()
  190. }
  191. }