CloudFileViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. //
  2. // CloudFileViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/10/8.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. import Promises
  11. class CloudFileViewController: CloudFileBaseVC {
  12. private let tableBottomContraint = "CFtableViewBottomConstraint"
  13. private var dataList: [DataModel] = []
  14. //上级文件夹 空就是顶层
  15. private var superior: String = ""
  16. private var superiorTitle = ""
  17. //底部工具栏
  18. var toolbarView: UIToolbar!
  19. @IBAction func clickCloseAction(_ sender: UIBarButtonItem) {
  20. print("点击了关闭按钮。。。。。。。。。。。")
  21. self.dismissVC(completion: nil)
  22. }
  23. @IBOutlet weak var listTitleLabel: UILabel!
  24. @IBOutlet weak var tableView: UITableView!
  25. @IBOutlet weak var imageBtn: UIStackView!
  26. @IBOutlet weak var documentBtn: UIStackView!
  27. @IBOutlet weak var musicBtn: UIStackView!
  28. @IBOutlet weak var videoBtn: UIStackView!
  29. @IBOutlet weak var otherBtn: UIStackView!
  30. @IBOutlet weak var shareBtn: UIStackView!
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. self.title = Languager.standardLanguager().string(key: "Cloud Files")
  34. self.navigationItem.rightBarButtonItems = [UIBarButtonItem(image: UIImage(named: "add"), style: .plain, target: self, action: #selector(addEvent))]
  35. self.superiorTitle = Languager.standardLanguager().string(key: "All File")
  36. self.setUI()
  37. //toolbar 初始化底部工具栏 先放在屏幕下面
  38. self.toolbarView = UIToolbar(frame: CGRect(x: 0, y: self.view.height - 44, width: self.view.width, height: 44))
  39. //初始化tableView
  40. self.tableView.dataSource = self
  41. self.tableView.delegate = self
  42. self.tableView.register(UINib.init(nibName: "CFFileTableViewCell", bundle: nil), forCellReuseIdentifier: "CFFileTableViewCell")
  43. self.tableView.register(UINib.init(nibName: "CFFolderTableViewCell", bundle: nil), forCellReuseIdentifier: "CFFolderTableViewCell")
  44. let header = MJRefreshNormalHeader(refreshingBlock: {
  45. self.loadListData()
  46. })
  47. header?.beginRefreshing()
  48. self.tableView.mj_header = header
  49. }
  50. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  51. if segue.identifier == "showListVC" {
  52. if let vc = segue.destination as? CloudFileListController {
  53. let folder = sender as? OOFolder
  54. if let f = folder {
  55. vc.breadcrumbList = [f]
  56. }
  57. }
  58. }else if segue.identifier == "showTypeListView" {
  59. if let typeVC = segue.destination as? CloudFileListBaseController {
  60. if let type = sender as? CloudFileType
  61. {
  62. typeVC.fileType = type
  63. }
  64. }
  65. }
  66. }
  67. // MARK: - private func
  68. private func setUI() {
  69. self.listTitleLabel.text = self.superiorTitle
  70. self.imageBtn.addTapGesture { (tap) in
  71. print("图片按钮。。。。。。。")
  72. self.performSegue(withIdentifier: "showTypeListView", sender: CloudFileType.image)
  73. }
  74. self.documentBtn.addTapGesture { (tap) in
  75. print("文档按钮。。。。。。。")
  76. self.performSegue(withIdentifier: "showTypeListView", sender: CloudFileType.office)
  77. }
  78. self.musicBtn.addTapGesture { (tap) in
  79. print("音频按钮。。。。。。。")
  80. self.performSegue(withIdentifier: "showTypeListView", sender: CloudFileType.music)
  81. }
  82. self.videoBtn.addTapGesture { (tap) in
  83. print("视频按钮。。。。。。。")
  84. self.performSegue(withIdentifier: "showTypeListView", sender: CloudFileType.movie)
  85. }
  86. self.otherBtn.addTapGesture { (tap) in
  87. print("其他按钮。。。。。。。")
  88. self.performSegue(withIdentifier: "showTypeListView", sender: CloudFileType.other)
  89. }
  90. self.shareBtn.addTapGesture { (tap) in
  91. print("分享按钮。。。。。。。")
  92. if let shareToMeVC = self.storyboard?.instantiateViewController(withIdentifier: "cloudFileListMultiModeVC") as? CloudFileListController {
  93. shareToMeVC.showMode = .shareToMe
  94. shareToMeVC.breadcrumbList = []
  95. self.pushVC(shareToMeVC)
  96. }
  97. }
  98. }
  99. //加载数据
  100. override func loadListData() {
  101. self.showLoading()
  102. self.dataList = []
  103. self.checkedFileList = []
  104. self.checkedFolderList = []
  105. all(self.cFileVM.folderList(folderId: self.superior), self.cFileVM.fileList(folderId: self.superior)).then { (result) in
  106. let folderList = result.0
  107. DDLogInfo("文件夹:\(folderList.count)")
  108. for folder in folderList {
  109. self.dataList.append(folder)
  110. }
  111. let fileList = result.1
  112. DDLogInfo("文件:\(fileList.count)")
  113. for file in fileList {
  114. self.dataList.append(file)
  115. }
  116. self.hideLoading()
  117. self.reloadUI()
  118. self.tableView.mj_header.endRefreshing()
  119. }.catch { (error) in
  120. DDLogError(error.localizedDescription)
  121. self.hideLoading()
  122. self.reloadUI()
  123. self.tableView.mj_header.endRefreshing()
  124. }
  125. }
  126. @objc private func moreMenuAction() {
  127. let startX = SCREEN_WIDTH - 10
  128. let startY = CGFloat(0.0)
  129. DDLogDebug("startx = \(startX),starty = \(startY)")
  130. let startPoint = CGPoint(x: startX, y: startY)
  131. let menu = [
  132. Languager.standardLanguager().string(key: "Rename"),
  133. Languager.standardLanguager().string(key: "Move"),
  134. Languager.standardLanguager().string(key: "Delete"),
  135. Languager.standardLanguager().string(key: "Share")
  136. ]
  137. let color = [
  138. UIColor.gray,
  139. UIColor.gray,
  140. base_color,
  141. UIColor.gray
  142. ]
  143. AZPopMenu.show(self.view, startPoint: startPoint, items: menu, colors: color) { (index) in
  144. DDLogInfo("點擊了菜單: \(menu[index])")
  145. }
  146. }
  147. //点击新建按钮
  148. @objc private func addEvent() {
  149. var actions: [UIAlertAction] = []
  150. let uploadFile = Languager.standardLanguager().string(key: "Upload File")
  151. let newFile = UIAlertAction(title: uploadFile, style: .default) { (action) in
  152. DDLogInfo("选择文件 上传")
  153. self.choosePhotoAndUpload()
  154. }
  155. let newFolderTitle = Languager.standardLanguager().string(key: "New Folder")
  156. let newFolder = UIAlertAction(title: newFolderTitle, style: .default) { (action) in
  157. DDLogInfo("新建文件夹")
  158. self.createFolder()
  159. }
  160. actions.append(newFile)
  161. actions.append(newFolder)
  162. let newMsg = Languager.standardLanguager().string(key: "New")
  163. self.showSheetAction(title: "", message: newMsg, actions: actions)
  164. }
  165. //新建文件夹
  166. private func createFolder() {
  167. let newFolderTitle = Languager.standardLanguager().string(key: "New Folder")
  168. self.showPromptAlert(title: newFolderTitle, message: "", inputText: "") { (ok, result) in
  169. if result != "" {
  170. self.cFileVM.createFolder(name: result).then({ (id) in
  171. self.loadListData()
  172. }).catch({ (error) in
  173. DDLogError("创建文件失败,\(error.localizedDescription)")
  174. let errTitle = Languager.standardLanguager().string(key: "Create Folder Error Message")
  175. self.showError(title: errTitle)
  176. })
  177. }
  178. }
  179. }
  180. private func choosePhotoAndUpload() {
  181. self.choosePhotoWithImagePicker { (fileName, imageData) in
  182. self.showLoading()
  183. self.cFileVM.uploadFile(folderId: O2.O2_First_ID, fileName: fileName, file: imageData)
  184. .then { result in
  185. DDLogInfo("上传成功,\(result)")
  186. self.hideLoading()
  187. self.loadListData()
  188. }.catch { (error) in
  189. DDLogError(error.localizedDescription)
  190. self.hideLoading()
  191. }
  192. }
  193. }
  194. private func isFolderChecked(_ folder: OOFolder) -> Bool {
  195. return self.checkedFolderList.contains(folder)
  196. }
  197. private func isFileChecked(_ file: OOAttachment) -> Bool {
  198. return self.checkedFileList.contains(file)
  199. }
  200. //底部工具栏中的单个按钮生成
  201. fileprivate func generateBottomButton(_ items: inout [UIBarButtonItem], name: String, tapCall: @escaping ()->Void) {
  202. let spaceItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
  203. let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
  204. btn.setTitle(name, for: .normal)
  205. btn.setTitleColor(base_color, for: .normal)
  206. btn.addTapGesture { (tap) in
  207. tapCall()
  208. }
  209. let item = UIBarButtonItem(customView: btn)
  210. items.append(spaceItem)
  211. items.append(item)
  212. items.append(spaceItem)
  213. }
  214. //重新生成刷新底部工具栏和按钮
  215. private func refreshBottomToolBar() {
  216. let totalCount = self.checkedFileList.count + self.checkedFolderList.count
  217. if totalCount > 0 {
  218. var items: [UIBarButtonItem] = []
  219. if totalCount == 1 {
  220. let reName = Languager.standardLanguager().string(key: "Rename")
  221. generateBottomButton(&items, name: reName, tapCall: {
  222. self.renameOp()
  223. })
  224. }
  225. //其他按钮 删除 移动 分享
  226. let deleteName = Languager.standardLanguager().string(key: "Delete")
  227. self.generateBottomButton(&items, name: deleteName) {
  228. self.deleteOp()
  229. }
  230. let moveName = Languager.standardLanguager().string(key: "Move")
  231. self.generateBottomButton(&items, name: moveName) {
  232. self.moveOp()
  233. }
  234. let shareName = Languager.standardLanguager().string(key: "Share")
  235. self.generateBottomButton(&items, name: shareName) {
  236. self.shareOp()
  237. }
  238. self.layoutBottomBar(items: items)
  239. }else {
  240. var c = false
  241. self.view.constraints.forEach { (constraint) in
  242. if constraint.identifier == self.tableBottomContraint {
  243. c = true
  244. }
  245. }
  246. if !c {
  247. let bottom = NSLayoutConstraint(item: self.view as Any, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.tableView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
  248. bottom.identifier = self.tableBottomContraint
  249. self.toolbarView.removeConstraints(self.toolbarView.constraints)
  250. self.toolbarView.removeFromSuperview()
  251. self.view.addConstraint(bottom)
  252. self.view.layoutIfNeeded()
  253. }
  254. }
  255. }
  256. //布局底部工具栏
  257. private func layoutBottomBar(items: [UIBarButtonItem]) {
  258. DDLogDebug("layout bottom \(items.count)")
  259. let toolBarHeight = CGFloat(44.0)
  260. var bottomSpace = CGFloat(0)
  261. if iPhoneX {
  262. bottomSpace = CGFloat(-34.0)
  263. }
  264. if items.count > 0 {
  265. self.toolbarView.items = items
  266. self.view.addSubview(self.toolbarView)
  267. self.toolbarView.translatesAutoresizingMaskIntoConstraints = false
  268. //高度约束
  269. 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)
  270. self.toolbarView.addConstraint(heightC)
  271. //底部约束
  272. 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)
  273. //右边约束
  274. 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)
  275. //左边约束
  276. 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)
  277. self.view.addConstraints([bottom, leading, trailing])
  278. self.view.constraints.forEach { (constraint) in
  279. //删除原来tableView的底部约束
  280. if constraint.identifier == self.tableBottomContraint {
  281. self.view.removeConstraint(constraint)
  282. }
  283. }
  284. //添加tableView和底部工具栏的约束
  285. let webcTop = NSLayoutConstraint(item: self.tableView as Any, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.toolbarView as Any, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
  286. self.view.addConstraint(webcTop)
  287. self.view.layoutIfNeeded()
  288. }
  289. }
  290. private func reloadUI() {
  291. self.refreshBottomToolBar()
  292. self.tableView.reloadData()
  293. }
  294. }
  295. // MARK: - UITableView
  296. extension CloudFileViewController: UITableViewDelegate, UITableViewDataSource {
  297. func numberOfSections(in tableView: UITableView) -> Int {
  298. return 1
  299. }
  300. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  301. return self.dataList.count
  302. }
  303. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  304. return UIView(frame: CGRect.zero)
  305. }
  306. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  307. let item = self.dataList[indexPath.row]
  308. if item is OOFolder {
  309. let folder = item as! OOFolder
  310. if let cell = tableView.dequeueReusableCell(withIdentifier: "CFFolderTableViewCell", for: indexPath) as? CFFolderTableViewCell {
  311. cell.clickdelegate = self
  312. cell.setData(folder: folder, checked: self.isFolderChecked(folder))
  313. return cell
  314. } else {
  315. return UITableViewCell()
  316. }
  317. } else if item is OOAttachment {
  318. let file = item as! OOAttachment
  319. if let cell = tableView.dequeueReusableCell(withIdentifier: "CFFileTableViewCell", for: indexPath) as? CFFileTableViewCell {
  320. cell.clickdelegate = self
  321. cell.setData(file: file, checked: self.isFileChecked(file))
  322. return cell
  323. } else {
  324. return UITableViewCell()
  325. }
  326. }else {
  327. return UITableViewCell()
  328. }
  329. }
  330. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  331. //点击处理
  332. let item = self.dataList[indexPath.row]
  333. if item is OOFolder { //点击文件夹进入下一层
  334. let folder = item as! OOFolder
  335. self.performSegue(withIdentifier: "showListVC", sender: folder)
  336. } else if item is OOAttachment { //点击文件
  337. let file = item as! OOAttachment
  338. self.clickFile(file: file)
  339. }
  340. tableView.deselectRow(at: indexPath, animated: true)
  341. }
  342. }
  343. extension CloudFileViewController: CloudFileCheckClickDelegate {
  344. func clickFolder(_ folder: OOFolder) {
  345. if self.checkedFolderList.contains(folder) {
  346. self.checkedFolderList.removeFirst(folder)
  347. }else {
  348. self.checkedFolderList.append(folder)
  349. }
  350. self.reloadUI()
  351. }
  352. func clickFile(_ file: OOAttachment) {
  353. if self.checkedFileList.contains(file) {
  354. self.checkedFileList.removeFirst(file)
  355. }else {
  356. self.checkedFileList.append(file)
  357. }
  358. self.reloadUI()
  359. }
  360. }