MainFileViewController.swift 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. //
  2. // MainFileViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/17.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import BSImagePicker
  15. import Photos
  16. import QuickLook
  17. import CocoaLumberjack
  18. private let kLivelyRedColor = base_color
  19. class MainFileViewController: UIViewController {
  20. @IBOutlet weak var tableView: ZLBaseTableView!
  21. var myFiles:[OOFile] = []
  22. var myFileShare:[FileShare] = []
  23. var myFileRecive:[FileShare] = []
  24. //移动选择的文件夹
  25. var moveSourceFile:OOFile?
  26. var moveTargeFolder:OOFile?
  27. //分享的人员列表
  28. var sharedPersons:[PersonV2] = []
  29. //分享的文件
  30. var currentSharedFile:OOFile?
  31. //文件夹深度队列
  32. var folderQueue:[OOFile] = []
  33. var segmentedControl:SegmentedControl?{
  34. didSet {
  35. tabIndex = (segmentedControl?.selectedIndex)!
  36. }
  37. }
  38. var tabIndex:Int = 0
  39. //预览
  40. let quickLookController = QLPreviewController()
  41. var fileURLs = [URL]()
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. //self.tableView.registerClass(Class() , forCellReuseIdentifier: "FileShareTableViewCell")
  45. setupUI()
  46. self.tableView.contentInset = UIEdgeInsets(top: 40.0, left: 0, bottom: 0, right: 0)
  47. self.tableView.delegate = self
  48. self.tableView.dataSource = self
  49. self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  50. self.loadDataByTabIndex(tabIndex: self.tabIndex)
  51. })
  52. self.loadDataByTabIndex(tabIndex:tabIndex)
  53. quickLookController.dataSource = self
  54. quickLookController.delegate = self
  55. }
  56. func loadDataRequestFileCompleted(_ url:String){
  57. self.showLoading(title: "加载中")
  58. AF.request(url).responseJSON { response in
  59. self.myFiles.removeAll()
  60. switch response.result {
  61. case .success(let val):
  62. let type = JSON(val)["type"]
  63. if type == "success" {
  64. let json = JSON(val)["data"]
  65. let files = Mapper<OOFile>().mapArray(JSONString:json["attachmentList"].description)
  66. let folders = Mapper<OOFile>().mapArray(JSONString:json["folderList"].description)
  67. DispatchQueue.main.async {
  68. self.myFiles.append(contentsOf: files!)
  69. self.myFiles.append(contentsOf: folders!)
  70. self.tableView.reloadData()
  71. self.showSuccess(title: "加载完成")
  72. }
  73. }else{
  74. DispatchQueue.main.async {
  75. DDLogError(JSON(val).description)
  76. self.tableView.reloadData()
  77. self.showError(title: "加载失败")
  78. }
  79. }
  80. case .failure(let err):
  81. DispatchQueue.main.async {
  82. DDLogError(err.localizedDescription)
  83. self.tableView.reloadData()
  84. self.showError(title: "加载失败")
  85. }
  86. }
  87. DispatchQueue.main.async {
  88. if self.tableView.mj_header.isRefreshing(){
  89. self.tableView.mj_header.endRefreshing()
  90. }
  91. }
  92. }
  93. }
  94. func loadDataRequestFileShareCompleted(_ url:String){
  95. self.showLoading(title: "加载中")
  96. AF.request(url).responseArray( keyPath: "data", context: nil)
  97. { (response:AFDataResponse<[FileShare]>) in
  98. self.myFileShare.removeAll()
  99. debugPrint(response.result)
  100. switch response.result {
  101. case .success(let shares):
  102. DispatchQueue.main.async {
  103. self.myFileShare.append(contentsOf: shares)
  104. self.tableView.reloadData()
  105. self.showSuccess(title: "加载完成")
  106. }
  107. case .failure(let err):
  108. DispatchQueue.main.async {
  109. DDLogError(err.localizedDescription)
  110. self.tableView.reloadData()
  111. self.showError(title: "加载失败")
  112. }
  113. }
  114. DispatchQueue.main.async {
  115. if self.tableView.mj_header.isRefreshing(){
  116. self.tableView.mj_header.endRefreshing()
  117. }
  118. }
  119. }
  120. }
  121. func loadDataRequestFileReciveCompleted(_ url:String) {
  122. self.showLoading(title: "加载中")
  123. AF.request(url).responseArray(keyPath: "data", context: nil) {
  124. (response:AFDataResponse<[FileShare]>) in
  125. self.myFileRecive.removeAll()
  126. switch response.result {
  127. case .success(let shares):
  128. DispatchQueue.main.async {
  129. self.myFileRecive.append(contentsOf: shares)
  130. self.tableView.reloadData()
  131. self.showSuccess(title: "加载完成")
  132. }
  133. case .failure(let err):
  134. DispatchQueue.main.async {
  135. DDLogError(err.localizedDescription)
  136. self.tableView.reloadData()
  137. self.showError(title: "加载失败")
  138. }
  139. }
  140. DispatchQueue.main.async {
  141. if self.tableView.mj_header.isRefreshing(){
  142. self.tableView.mj_header.endRefreshing()
  143. }
  144. }
  145. }
  146. }
  147. func loadDataByTabIndex(tabIndex index:Int){
  148. //计算URL
  149. var url = ""
  150. switch index {
  151. case 0:
  152. self.tableView.emptyTitle = "没有文件数据"
  153. if self.folderQueue.count <= 0 {
  154. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileTopListQuery, parameter: nil)!
  155. self.loadDataRequestFileCompleted(url)
  156. }else{
  157. let f = self.folderQueue.last
  158. //读取
  159. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderItemIdQuery, parameter: ["##id##":f!.id! as AnyObject])
  160. self.loadDataRequestFileCompleted(url!)
  161. }
  162. case 1:
  163. self.tableView.emptyTitle = "没有收到共享的文件"
  164. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileShareQuery, parameter: nil)!
  165. self.loadDataRequestFileShareCompleted(url)
  166. case 2:
  167. self.tableView.emptyTitle = "没有共享文件给其它人"
  168. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileEditorQuery, parameter: nil)!
  169. self.loadDataRequestFileReciveCompleted(url)
  170. default:
  171. url = ""
  172. }
  173. }
  174. func setupUI(){
  175. self.initSegmentedControl()
  176. }
  177. func initSegmentedControl(){
  178. let titleStrings = ["我的文件","共享文件","收到文件"]
  179. let titles: [NSAttributedString] = {
  180. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: UIColor.black]
  181. var titles = [NSAttributedString]()
  182. for titleString in titleStrings {
  183. let title = NSAttributedString(string: titleString, attributes: attributes)
  184. titles.append(title)
  185. }
  186. return titles
  187. }()
  188. let selectedTitles: [NSAttributedString] = {
  189. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16), NSAttributedString.Key.foregroundColor: kLivelyRedColor]
  190. var selectedTitles = [NSAttributedString]()
  191. for titleString in titleStrings {
  192. let selectedTitle = NSAttributedString(string: titleString, attributes: attributes)
  193. selectedTitles.append(selectedTitle)
  194. }
  195. return selectedTitles
  196. }()
  197. self.segmentedControl = SegmentedControl.initWithTitles(titles, selectedTitles: selectedTitles)
  198. self.segmentedControl!.delegate = self
  199. self.segmentedControl!.backgroundColor = toolbar_background_color
  200. self.segmentedControl!.autoresizingMask = [.flexibleRightMargin, .flexibleWidth]
  201. self.segmentedControl!.selectionIndicatorStyle = .bottom
  202. self.segmentedControl!.selectionIndicatorColor = kLivelyRedColor
  203. self.segmentedControl!.selectionIndicatorHeight = 3
  204. self.segmentedControl!.segmentWidth = SCREEN_WIDTH / 3
  205. self.segmentedControl!.frame.origin.y = 0
  206. self.segmentedControl!.frame.size = CGSize(width: UIScreen.main.bounds.width, height: 40)
  207. view.insertSubview(self.segmentedControl!, belowSubview: navigationController!.navigationBar)
  208. }
  209. override func didReceiveMemoryWarning() {
  210. super.didReceiveMemoryWarning()
  211. }
  212. @IBAction func showMyFile(_ sender: UIBarButtonItem) {
  213. self.performSegue(withIdentifier: "showMyDownloadSegue", sender: nil)
  214. }
  215. @IBAction func backToHome(_ sender: UIBarButtonItem) {
  216. let backType = AppConfigSettings.shared.appBackType
  217. if backType == 1 {
  218. self.performSegue(withIdentifier: "backToMain", sender: nil)
  219. }else if backType == 2 {
  220. self.performSegue(withIdentifier: "backToApps", sender: nil)
  221. }
  222. }
  223. @IBAction func uploadFileMenu(_ sender: UIBarButtonItem) {
  224. let menuAlertController = UIAlertController(title: "文件操作", message: "文件或文件夹操作", preferredStyle: .actionSheet)
  225. let fileUploadAlert = UIAlertAction(title: "上传文件", style: .destructive) { (fileUploadAction) in
  226. self.uploadFile()
  227. }
  228. let folderCreateAlert = UIAlertAction(title: "创建文件夹", style: .default) { (createFolderAction) in
  229. self.createFolder()
  230. }
  231. let cancelAlert = UIAlertAction(title: "取消", style: .cancel) { (cancelAction) in
  232. }
  233. menuAlertController.addAction(fileUploadAlert)
  234. menuAlertController.addAction(folderCreateAlert)
  235. menuAlertController.addAction(cancelAlert)
  236. self.present(menuAlertController, animated: true, completion: nil)
  237. }
  238. func uploadFile(){
  239. var url = ""
  240. if self.folderQueue.count == 0 {
  241. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileUploadTopQuery, parameter: nil,coverted: true)!
  242. }else{
  243. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileUploadSubQuery, parameter: ["##id##":(self.folderQueue.last?.id!)! as AnyObject],coverted: true)!
  244. }
  245. url = url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
  246. let vc = FileBSImagePickerViewController().bsImagePicker()
  247. presentImagePicker(vc, select: { (asset: PHAsset) -> Void in
  248. // User selected an asset.
  249. // Do something with it, start upload perhaps?
  250. }, deselect: { (asset: PHAsset) -> Void in
  251. // User deselected an assets.
  252. // Do something, cancel upload?
  253. }, cancel: { (assets: [PHAsset]) -> Void in
  254. // User cancelled. And this where the assets currently selected.
  255. }, finish: { (assets: [PHAsset]) -> Void in
  256. for asset in assets {
  257. switch asset.mediaType {
  258. case .audio:
  259. DDLogDebug("Audio")
  260. case .image:
  261. let options = PHImageRequestOptions()
  262. options.isSynchronous = true
  263. options.deliveryMode = .fastFormat
  264. options.resizeMode = .none
  265. // let headers:HTTPHeaders = ["x-token":(O2AuthSDK.shared.myInfo()?.token!)!]
  266. //获取文件名
  267. let fName = (asset.value(forKey: "filename") as? String) ?? "untitle.png"
  268. PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: { (imageData, result, imageOrientation, dict) in
  269. //debugPrint(imageData,result,imageOrientation,dict)
  270. //DDLogDebug("result = \(result) imageOrientation = \(imageOrientation) \(dict)")
  271. // let fileURL = dict?["PHImageFileURLKey"] as! URL
  272. // let fileSrcName = fileURL.description
  273. // let fName = fileSrcName.components(separatedBy: "/").last!
  274. let fExtName = fName.components(separatedBy: ".").last!
  275. let fPreName = fName.components(separatedBy: ".").first!
  276. DispatchQueue.main.async {
  277. self.showLoading(title:"上传中...")
  278. AF.upload(multipartFormData: { (mData) in
  279. let formatter = DateFormatter()
  280. formatter.dateFormat = "yyyyMMddHHmmss"
  281. let str = formatter.string(from: Date())
  282. let fileName = "\(fPreName.lowercased())_\(str).\(fExtName.lowercased())"
  283. mData.append(imageData!, withName: "file", fileName: fileName, mimeType: "image/\(fExtName)")
  284. }, to: url).responseJSON { (response) in
  285. print(response)
  286. if let err = response.error {
  287. DispatchQueue.main.async {
  288. DDLogError(err.localizedDescription)
  289. DispatchQueue.main.async {
  290. self.showError(title: "上传失败")
  291. }
  292. }
  293. }else {
  294. DispatchQueue.main.async {
  295. self.showSuccess(title: "上传成功")
  296. Timer.after(0.8, {
  297. self.tableView.mj_header.beginRefreshing()
  298. })
  299. }
  300. }
  301. }
  302. // AF.upload(multipartFormData: { (mData) in
  303. // let formatter = DateFormatter()
  304. // formatter.dateFormat = "yyyyMMddHHmmss"
  305. // let str = formatter.string(from: Date())
  306. // let fileName = "\(fPreName.lowercased())_\(str).\(fExtName.lowercased())"
  307. // mData.append(imageData!, withName: "file", fileName: fileName, mimeType: "image/\(fExtName)")
  308. // }, to: url,headers:headers,encodingCompletion: { (encodingResult) in
  309. // switch encodingResult {
  310. // case .success(let upload,_,_):
  311. // debugPrint(upload)
  312. // upload.responseJSON { response in
  313. // debugPrint(response)
  314. // DispatchQueue.main.async {
  315. // self.showSuccess(title: "上传成功")
  316. // Timer.after(0.8, {
  317. // self.tableView.mj_header.beginRefreshing()
  318. // })
  319. //
  320. // }
  321. // }
  322. // case .failure(let errType):
  323. // DispatchQueue.main.async {
  324. // DDLogError(errType.localizedDescription)
  325. // DispatchQueue.main.async {
  326. // self.showError(title: "上传失败")
  327. // }
  328. // }
  329. // }
  330. //
  331. // })
  332. }
  333. })
  334. case .video:
  335. let options = PHVideoRequestOptions()
  336. options.deliveryMode = .fastFormat
  337. options.isNetworkAccessAllowed = true
  338. options.progressHandler = { (progress,err, stop,dict) in
  339. DDLogDebug("progress = \(progress) dict = \(dict)")
  340. }
  341. PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: { (avAsset, avAudioMx, dict) in
  342. })
  343. case .unknown:
  344. DDLogDebug("Unknown")
  345. }
  346. }
  347. }, completion: nil)
  348. }
  349. func createFolder(){
  350. let createAlertController = UIAlertController(title: "创建文件夹", message: "", preferredStyle: .alert)
  351. let okAction = UIAlertAction(title: "确定", style: .destructive) { ok in
  352. DDLogDebug("ok Click")
  353. let textInputField = createAlertController.textFields![0]
  354. if let text = textInputField.text {
  355. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderCreateQuery, parameter: nil)
  356. let s = self.folderQueue.count == 0 ? "":self.folderQueue.last?.id!
  357. let parameter = ["name":text,"superior":s!]
  358. AF.request(url!, method:.post, parameters: parameter, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: { (response) in
  359. switch response.result {
  360. case .success(let val):
  361. let json = JSON(val)
  362. if json["type"] == "success" {
  363. DispatchQueue.main.async {
  364. self.showSuccess(title: "创建成功")
  365. Timer.after(0.5, {
  366. self.tableView.mj_header.beginRefreshing()
  367. })
  368. }
  369. }else{
  370. DispatchQueue.main.async {
  371. DDLogError(json.description)
  372. self.showError(title: "创建失败")
  373. }
  374. }
  375. case .failure(let err):
  376. DispatchQueue.main.async {
  377. DDLogError(err.localizedDescription)
  378. self.showError(title: "创建失败")
  379. }
  380. }
  381. })
  382. // AF.request(.POST, url!, parameters: parameter, encoding: .json, headers: nil).responseJSON(completionHandler: { (response) in
  383. // switch response.result {
  384. // case .success(let val):
  385. // let json = JSON(val)
  386. // if json["type"] == "success" {
  387. // ProgressHUD.showSuccess("创建成功")
  388. // }else{
  389. // DDLogError(json)
  390. // ProgressHUD.showError("创建失败")
  391. // }
  392. // case .failure(let err):
  393. // ProgressHUD.showError("创建失败")
  394. // DDLogError(err.localizedDescription)
  395. // }
  396. // })
  397. }
  398. }
  399. let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancel) in
  400. DDLogDebug("cancel Click")
  401. }
  402. createAlertController.addTextField { (folderInputTextField) in
  403. }
  404. createAlertController.addAction(okAction)
  405. createAlertController.addAction(cancelAction)
  406. self.present(createAlertController, animated: true, completion: nil)
  407. }
  408. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  409. if segue.identifier == "showSharePersonSegue" {
  410. // TODO: 这里需要修改,----后续。
  411. // let navVC = segue.destination as! ZLNavigationController
  412. // let destVC = navVC.topViewController as! MeetingPersonListViewController
  413. // destVC.delegate = self
  414. // destVC.selectPersons = self.sharedPersons
  415. }else if segue.identifier == "showSelectFolderSegue"{
  416. let navVC = segue.destination as! ZLNavigationController
  417. let destVC = navVC.topViewController as! FileFolderSelectViewController
  418. destVC.delegate = self
  419. }else if segue.identifier == "showMyShareSegue" {
  420. // let navVC = segue.destinationViewController as! ZLNavigationController
  421. // let destVC = navVC.topViewController as! FileMyShareListViewController
  422. let destVC = segue.destination as! FileMyShareListViewController
  423. destVC.myFileURL = sender as? String
  424. }
  425. }
  426. }
  427. //extension MainFileViewController:MeetingPersonListPassValue {
  428. // func selectPersonPassValue(_ persons: [PersonV2]) {
  429. // self.sharedPersons.removeAll()
  430. // self.sharedPersons.append(contentsOf: persons)
  431. // self.fileShareAction()
  432. // }
  433. //}
  434. extension MainFileViewController: SegmentedControlDelegate {
  435. func segmentedControl(_ segmentedControl: SegmentedControl, didSelectIndex selectedIndex: Int) {
  436. print("Did select index \(selectedIndex)")
  437. tabIndex = selectedIndex
  438. self.loadDataByTabIndex(tabIndex: tabIndex)
  439. switch segmentedControl.style {
  440. case .text:
  441. print("The title is “\(segmentedControl.titles[selectedIndex].string)”\n")
  442. case .image:
  443. print("The image is “\(segmentedControl.images[selectedIndex])”\n")
  444. }
  445. }
  446. func segmentedControl(_ segmentedControl: SegmentedControl, didLongPressIndex longPressIndex: Int) {
  447. print("Did long press index \(longPressIndex)")
  448. if UIDevice.current.userInterfaceIdiom == .pad {
  449. let viewController = UIViewController()
  450. viewController.modalPresentationStyle = .popover
  451. viewController.preferredContentSize = CGSize(width: 200, height: 300)
  452. if let popoverController = viewController.popoverPresentationController {
  453. popoverController.sourceView = view
  454. let yOffset: CGFloat = 10
  455. popoverController.sourceRect = view.convert(CGRect(origin: CGPoint(x: 70 * CGFloat(longPressIndex), y: yOffset), size: CGSize(width: 70, height: 30)), from: navigationItem.titleView)
  456. popoverController.permittedArrowDirections = .any
  457. present(viewController, animated: true, completion: nil)
  458. }
  459. } else {
  460. let message = segmentedControl.style == .text ? "Long press title “\(segmentedControl.titles[longPressIndex].string)”" : "Long press image “\(segmentedControl.images[longPressIndex])”"
  461. let alert = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)
  462. let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
  463. alert.addAction(cancelAction)
  464. present(alert, animated: true, completion: nil)
  465. }
  466. }
  467. }
  468. extension MainFileViewController:UITableViewDelegate,UITableViewDataSource{
  469. func numberOfSections(in tableView: UITableView) -> Int {
  470. // switch tabIndex {
  471. // case 0:
  472. // return 1
  473. // case 1:
  474. // return self.myFileShare.count > 0 ? self.myFileShare.count : 1
  475. // case 2:
  476. // return self.myFileRecive.count > 0 ? self.myFileRecive.count : 1
  477. // default:
  478. // return 1
  479. // }
  480. return 1
  481. }
  482. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  483. switch tabIndex {
  484. case 0:
  485. return self.myFiles.count
  486. case 1:
  487. return self.myFileShare.count
  488. case 2:
  489. return self.myFileRecive.count
  490. default:
  491. return 0
  492. }
  493. }
  494. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  495. //按不同类型生成相应的cell
  496. switch tabIndex {
  497. case 0:
  498. let file = self.myFiles[(indexPath as NSIndexPath).row]
  499. let cell = tableView.dequeueReusableCell(withIdentifier: "FileTableViewCell", for: indexPath) as! FileTableViewCell
  500. cell.delegate = self
  501. cell.file = file
  502. return cell
  503. case 1:
  504. let fileShare = self.myFileShare[(indexPath as NSIndexPath).row]
  505. let cell = tableView.dequeueReusableCell(withIdentifier: "FileShareTableViewCell",for: indexPath) as! FileShareTableViewCell
  506. cell.fileShare = fileShare
  507. return cell
  508. case 2:
  509. let fileRecive = self.myFileRecive[(indexPath as NSIndexPath).row]
  510. let cell = tableView.dequeueReusableCell(withIdentifier: "FileShareTableViewCell", for: indexPath) as! FileShareTableViewCell
  511. cell.fileShare = fileRecive
  512. return cell
  513. default:
  514. return UITableViewCell()
  515. }
  516. }
  517. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  518. if tabIndex == 0 {
  519. return 40.0
  520. }else{
  521. return 0.0
  522. }
  523. }
  524. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  525. if tabIndex == 0 {
  526. let headerView = FolderHeaderView()
  527. headerView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 40)
  528. headerView.folderQueue = self.folderQueue
  529. headerView.delegate = self
  530. return headerView
  531. }else{
  532. return UIView()
  533. }
  534. }
  535. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  536. DDLogDebug("cell clicked row = \(indexPath.row)")
  537. if tabIndex == 0 {
  538. //我的文件
  539. let f = self.myFiles[indexPath.row]
  540. if f.fileType == .folder {
  541. //文件夹显示文件夹内容
  542. self.folderQueue.append(f)
  543. //读取
  544. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderItemIdQuery, parameter: ["##id##":f.id! as AnyObject])
  545. self.loadDataRequestFileCompleted(url!)
  546. }else{
  547. self.filePreview(f)
  548. }
  549. }else if tabIndex == 1 {
  550. //共享文件
  551. let fileShare = self.myFileShare[(indexPath as NSIndexPath).row]
  552. //读取共享文件列表
  553. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileMyShareListQuery, parameter: ["##name##":fileShare.name! as AnyObject])
  554. self.performSegue(withIdentifier: "showMyShareSegue", sender: url)
  555. }else if tabIndex == 2 {
  556. let fileRecive = self.myFileRecive[(indexPath as NSIndexPath).row]
  557. //接收的文件
  558. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileMyEditorListQuery, parameter: ["##name##":fileRecive.name! as AnyObject])
  559. self.performSegue(withIdentifier: "showMyShareSegue", sender: url)
  560. }
  561. }
  562. func filePreview(_ f:OOFile){
  563. //文件就打开
  564. let fileURL = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileDownloadItemIdQuery, parameter: ["##id##":f.id! as AnyObject])
  565. //缓存到本地
  566. var fileLocalURL:URL?
  567. let destination:DownloadRequest.Destination = { temporaryURL, response in
  568. let baseURL = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
  569. let folder = URL(fileURLWithPath:baseURL).appendingPathComponent("tmpFile", isDirectory: true)
  570. //判断文件夹是否存在,不存在则创建
  571. let exist = FileManager.default.fileExists(atPath: folder.path)
  572. if !exist {
  573. try! FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true,
  574. attributes: nil)
  575. }
  576. //增加随机数
  577. let preName = f.name?.components(separatedBy: ".").first!
  578. let extName = f.name?.components(separatedBy: ".").last!
  579. let timestamp = Date().timeIntervalSince1970.description
  580. fileLocalURL = folder.appendingPathComponent("\(preName!)_\(timestamp).\(extName!)")
  581. return (fileLocalURL!,[.removePreviousFile, .createIntermediateDirectories])
  582. }
  583. self.showLoading(title: "下载中...")
  584. AF.download(fileURL!,to: destination).downloadProgress(closure: { (progress) in
  585. print("progress.fractionCompleted = \(progress.fractionCompleted)")
  586. if progress.completedUnitCount == progress.totalUnitCount {
  587. DispatchQueue.main.async {
  588. self.hideLoading()
  589. }
  590. }
  591. }).responseData { resp in
  592. switch resp.result {
  593. case .success(_):
  594. DispatchQueue.main.async {
  595. self.hideLoading()
  596. }
  597. self.fileURLs.removeAll(keepingCapacity: true)
  598. if QLPreviewController.canPreview(fileLocalURL! as QLPreviewItem){
  599. self.fileURLs.append(fileLocalURL!)
  600. self.quickLookController.reloadData()
  601. self.quickLookController.currentPreviewItemIndex = 0
  602. self.navigationController?.pushViewController(self.quickLookController, animated: true)
  603. }
  604. case .failure(let err):
  605. DDLogError(err.localizedDescription)
  606. }
  607. }
  608. // .response { (request, response, data, error) in
  609. // if let err = error {
  610. // DDLogError(err.localizedDescription)
  611. // }else{
  612. // ProgressHUD.dismiss()
  613. // self.fileURLs.removeAll()
  614. // if QLPreviewController.canPreview(fileLocalURL!){
  615. // self.fileURLs.append(fileLocalURL!)
  616. // self.quickLookController.reloadData()
  617. // self.quickLookController.currentPreviewItemIndex = 0
  618. // self.navigationController?.pushViewController(self.quickLookController, animated: true)
  619. // }
  620. // }
  621. // }
  622. }
  623. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  624. DDLogDebug("touch")
  625. for touch in touches {
  626. DDLogDebug("touch view = \(touch.view)")
  627. }
  628. }
  629. }
  630. private let FileMenu = ["重命名","删除","移动","下载","分享"]
  631. private let FileFolderMenu = ["重命名","删除","移动"]
  632. private let FileMenuColor = [UIColor.white,
  633. UIColor.purple,
  634. UIColor.cyan,
  635. UIColor.yellow,
  636. UIColor.green]
  637. private let FileFolderMenuColor = [UIColor.white,
  638. UIColor.purple,
  639. UIColor.cyan]
  640. extension MainFileViewController:FileTableViewCellDelegate{
  641. func cellDidClicked(_ cell: FileTableViewCell, file: OOFile) {
  642. DDLogDebug("tableView contentOffset = \(self.tableView.contentOffset)")
  643. DDLogDebug("file clicked id = \(file.id!)")
  644. let startX = SCREEN_WIDTH - 10
  645. //let startY = self.tableView.contentOffset.y > 0 ? cell.center.y - self.tableView.contentOffset.y : cell.center.y
  646. let startY = cell.center.y - self.tableView.contentOffset.y
  647. DDLogDebug("startx = \(startX),starty = \(startY)")
  648. let startPoint = CGPoint(x: startX, y: startY)
  649. if file.fileType == .folder {
  650. //显示文件夹上下文菜单
  651. AZPopMenu.show(self.view, startPoint: startPoint, items: FileFolderMenu, colors: FileFolderMenuColor, selected: { (itemSelected) in
  652. DDLogDebug("\(itemSelected)")
  653. switch itemSelected {
  654. case 0:
  655. self.fileRename(file)
  656. case 1:
  657. self.fileDelete(file)
  658. case 2:
  659. self.fileMove(file)
  660. default:
  661. DDLogDebug("no action")
  662. }
  663. })
  664. }else if file.fileType == .file {
  665. //显示文件上下文菜单
  666. AZPopMenu.show(self.view, startPoint: startPoint, items: FileMenu, colors: FileMenuColor, selected: { (itemSelected) in
  667. DDLogDebug("\(itemSelected)")
  668. switch itemSelected {
  669. case 0:
  670. self.fileRename(file)
  671. case 1:
  672. self.fileDelete(file)
  673. case 2:
  674. self.fileMove(file)
  675. case 3:
  676. self.fileDownload(file)
  677. case 4:
  678. self.fileShare(file)
  679. default:
  680. DDLogDebug("no action")
  681. }
  682. })
  683. }
  684. }
  685. fileprivate func renameFileAction(sourceFile f:OOFile,newFileName name:String){
  686. var url = ""
  687. var parameter:Dictionary<String,String> = [:]
  688. if f.fileType == .file {
  689. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileRenameQuery, parameter: ["##id##":f.id! as AnyObject])!
  690. parameter["name"] = name
  691. parameter["folder"] = f.folder!
  692. }else if f.fileType == .folder {
  693. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderActionIdQuery,parameter: ["##id##":f.id! as AnyObject])!
  694. parameter["name"] = name
  695. parameter["superior"] = f.superior!
  696. }
  697. self.showLoading(title: "更新中")
  698. AF.request(url, method: .put, parameters: parameter, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  699. switch response.result {
  700. case .success(let val):
  701. let json = JSON(val)
  702. if json["type"] == "success" {
  703. self.myFiles.forEachEnumerated({ (index, file) in
  704. if file.id == f.id {
  705. file.name = name
  706. }
  707. })
  708. DispatchQueue.main.async {
  709. self.tableView.reloadData()
  710. self.showSuccess(title: "重命名完成")
  711. }
  712. }else{
  713. }
  714. case .failure(let err):
  715. DispatchQueue.main.async {
  716. DDLogError(err.localizedDescription)
  717. self.showError(title: "重命名失败")
  718. }
  719. }
  720. }
  721. }
  722. //重命名
  723. func fileRename(_ f:OOFile){
  724. let renameViewController = UIAlertController(title: "重命名", message: "原名称:\(f.name!)", preferredStyle: .alert)
  725. let okAction = UIAlertAction(title: "确定", style: .destructive) { (okAction) in
  726. let textField = renameViewController.textFields![0]
  727. if let text = textField.text {
  728. if text.isEmpty {
  729. DDLogDebug("empty name = \(text)")
  730. }else{
  731. DDLogDebug("new name = \(text)")
  732. self.renameFileAction(sourceFile: f, newFileName: text)
  733. }
  734. }
  735. }
  736. let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (cancelAction) in
  737. }
  738. renameViewController.addTextField { (textField) in
  739. textField.placeholder = "请输入新名称"
  740. textField.textColor = UIColor.red
  741. }
  742. renameViewController.addAction(okAction)
  743. renameViewController.addAction(cancelAction)
  744. self.present(renameViewController, animated: true, completion: nil)
  745. }
  746. fileprivate func deleteFileAction(_ f:OOFile){
  747. var url = ""
  748. if f.fileType == .file {
  749. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileDeleteQuery, parameter: ["##id##":f.id! as AnyObject])!
  750. }else if f.fileType == .folder {
  751. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderActionIdQuery, parameter: ["##id##":f.id! as AnyObject])!
  752. }
  753. self.showLoading(title: "删除中")
  754. AF.request(url,method:.delete, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  755. switch response.result {
  756. case .success(let val):
  757. let json = JSON(val)
  758. if json["type"] == "success" {
  759. self.myFiles.remove(at: self.myFiles.index(where: { (file) -> Bool in
  760. if f.id == file.id {
  761. return true
  762. }else{
  763. return false
  764. }
  765. })!)
  766. DispatchQueue.main.async {
  767. self.tableView.reloadData()
  768. self.showSuccess(title: "删除成功")
  769. }
  770. }else{
  771. DispatchQueue.main.async {
  772. DDLogError("删除失败:\(json)")
  773. self.showError(title: "删除失败")
  774. }
  775. }
  776. case .failure(let err):
  777. DispatchQueue.main.async {
  778. DDLogError("删除失败:\(err)")
  779. self.showError(title: "删除失败")
  780. }
  781. }
  782. }
  783. }
  784. //删除
  785. func fileDelete(_ f:OOFile){
  786. let deleViewController = UIAlertController(title: "删除", message: "删除文件:\(f.name!)", preferredStyle: .alert)
  787. let okAction = UIAlertAction(title: "确定", style: .destructive) { (okAction) in
  788. //执行删除
  789. self.deleteFileAction(f)
  790. }
  791. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
  792. deleViewController.addAction(okAction)
  793. deleViewController.addAction(cancelAction)
  794. self.present(deleViewController,animated: true,completion: nil)
  795. }
  796. func fileMoveAction(){
  797. //移动
  798. var url = ""
  799. var parameter:[String:AnyObject] = [:]
  800. if moveSourceFile?.fileType == .file {
  801. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileRenameQuery, parameter: ["##id##":(moveSourceFile?.id)! as AnyObject])!
  802. parameter["name"] = moveSourceFile?.name as AnyObject?
  803. parameter["folder"] = moveTargeFolder?.id as AnyObject?
  804. }else if moveSourceFile?.fileType == .folder {
  805. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderActionIdQuery, parameter: ["##id##":(moveSourceFile?.id)! as AnyObject])!
  806. parameter["name"] = moveSourceFile?.name as AnyObject?
  807. parameter["superior"] = moveTargeFolder?.id as AnyObject?
  808. }
  809. AF.request(url,method:.put, parameters: parameter, encoding: JSONEncoding.default , headers: nil).responseJSON { (response) in
  810. switch response.result {
  811. case .success(let val):
  812. let json = JSON(val)
  813. if json["type"] == "success" {
  814. self.myFiles.remove(at: self.myFiles.index(where: { (file) -> Bool in
  815. if self.moveSourceFile?.id == file.id {
  816. return true
  817. }else{
  818. return false
  819. }
  820. })!)
  821. DispatchQueue.main.async {
  822. self.tableView.reloadData()
  823. self.showSuccess(title: "移动完成")
  824. }
  825. }else{
  826. DispatchQueue.main.async {
  827. DDLogError(json.description)
  828. self.showError(title: "移动失败")
  829. }
  830. }
  831. case .failure(let err):
  832. DispatchQueue.main.async {
  833. DDLogError(err.localizedDescription)
  834. self.showError(title: "移动失败")
  835. }
  836. }
  837. }
  838. }
  839. //移动
  840. func fileMove(_ f:OOFile){
  841. self.moveSourceFile = f
  842. self.performSegue(withIdentifier: "showSelectFolderSegue", sender: nil)
  843. }
  844. func getRect() -> CGRect {
  845. return CGRect(
  846. x: view.centerX - 100 / 2,
  847. y: view.centerY - 100 / 2 - 33,
  848. width: 100,
  849. height: 100)
  850. }
  851. //下载
  852. func fileDownload(_ f:OOFile){
  853. //存储到私有目录
  854. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileDownloadItemIdQuery, parameter: ["##id##":f.id! as AnyObject])
  855. let destination:DownloadRequest.Destination = { temporaryURL, response in
  856. let baseURL = NSSearchPathForDirectoriesInDomains(.downloadsDirectory, .userDomainMask, true)[0]
  857. let folder = URL(fileURLWithPath:baseURL).appendingPathComponent("file", isDirectory: true)
  858. //判断文件夹是否存在,不存在则创建
  859. let exist = FileManager.default.fileExists(atPath: folder.path)
  860. if !exist {
  861. try! FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true,
  862. attributes: nil)
  863. }
  864. return (folder.appendingPathComponent(f.name!), [.removePreviousFile, .createIntermediateDirectories])
  865. }
  866. self.showLoading(title: "下载中...")
  867. let utilityQueue = DispatchQueue.global(qos: .utility)
  868. AF.download(url!, to: destination).downloadProgress(queue: utilityQueue) { (progress) in
  869. print("progress.fractionCompleted = \(progress.fractionCompleted)")
  870. }.responseData { (resp) in
  871. DispatchQueue.main.async {
  872. self.hideLoading()
  873. }
  874. switch resp.result {
  875. case .success( _):
  876. DispatchQueue.main.async {
  877. self.showSuccess(title: "下载完成")
  878. }
  879. case .failure(let err):
  880. DispatchQueue.main.async {
  881. DDLogError(err.localizedDescription)
  882. self.showError(title: "下载失败")
  883. }
  884. }
  885. }
  886. }
  887. //分享
  888. func fileShare(_ f:OOFile){
  889. currentSharedFile = f
  890. self.performSegue(withIdentifier: "showSharePersonSegue", sender: nil)
  891. }
  892. func fileShareAction(){
  893. var names:[String] = []
  894. sharedPersons.forEach { (p) in
  895. names.append(p.name!)
  896. }
  897. let url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileShareActionQuery, parameter: ["##id##":(currentSharedFile?.id)! as AnyObject])
  898. self.showLoading(title: "分享中...")
  899. AF.request(url!, method:.put , parameters: ["shareList":names], encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
  900. switch response.result {
  901. case .success(let val):
  902. let json = JSON(val)
  903. if json["type"] == "success" {
  904. DispatchQueue.main.async {
  905. self.showSuccess(title: "分享成功")
  906. }
  907. }else{
  908. DispatchQueue.main.async {
  909. self.showError(title: "分享失败")
  910. }
  911. }
  912. case .failure(let err):
  913. DispatchQueue.main.async {
  914. DDLogError(err.localizedDescription)
  915. self.showError(title: "分享失败")
  916. }
  917. }
  918. }
  919. }
  920. }
  921. extension MainFileViewController:FileFolderPassValueDelegate{
  922. func selectedFolder(_ f: OOFile) {
  923. self.moveTargeFolder = f
  924. self.fileMoveAction()
  925. }
  926. }
  927. extension MainFileViewController:FolderHeaderViewDelegate{
  928. func headerClickSelected(currentFile f: OOFile, folderQueue fQueue: [OOFile]) {
  929. self.folderQueue = fQueue
  930. var url = ""
  931. if f.id == "0" {
  932. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileTopListQuery, parameter: nil)!
  933. }else{
  934. url = AppDelegate.o2Collect.generateURLWithAppContextKey(FileContext.fileContextKey, query: FileContext.fileFolderItemIdQuery, parameter: ["##id##":f.id! as AnyObject])!
  935. }
  936. self.loadDataRequestFileCompleted(url)
  937. }
  938. }
  939. //quick look
  940. extension MainFileViewController:QLPreviewControllerDataSource,QLPreviewControllerDelegate{
  941. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  942. return fileURLs.count
  943. }
  944. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  945. return fileURLs[index] as QLPreviewItem
  946. }
  947. }