NCTrash.swift 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. //
  2. // NCTrash.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. class NCTrash: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCTrashListDelegate, NCTrashGridDelegate, NCTrashHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  10. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  11. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  12. var path = ""
  13. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  14. var datasource = [tableTrash]()
  15. var datasourceSorted = ""
  16. var datasourceAscending = true
  17. var isEditMode = false
  18. var selectFileID = [String]()
  19. var listLayout: ListLayout!
  20. var gridLayout: GridLayout!
  21. private let highHeader: CGFloat = 50
  22. private let refreshControl = UIRefreshControl()
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "cell-list")
  26. collectionView.register(UINib.init(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "cell-grid")
  27. collectionView.alwaysBounceVertical = true
  28. listLayout = ListLayout()
  29. gridLayout = GridLayout()
  30. if CCUtility.getLayoutTrash() == "list" {
  31. collectionView.collectionViewLayout = listLayout
  32. } else {
  33. collectionView.collectionViewLayout = gridLayout
  34. }
  35. // Add Refresh Control
  36. if #available(iOS 10.0, *) {
  37. collectionView.refreshControl = refreshControl
  38. } else {
  39. collectionView.addSubview(refreshControl)
  40. }
  41. // Configure Refresh Control
  42. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  43. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  44. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  45. // empty Data Source
  46. self.collectionView.emptyDataSetDelegate = self;
  47. self.collectionView.emptyDataSetSource = self;
  48. }
  49. override func viewWillAppear(_ animated: Bool) {
  50. super.viewWillAppear(animated)
  51. self.navigationItem.title = titleCurrentFolder
  52. if path == "" {
  53. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  54. path = k_dav + "/trashbin/" + userID! + "/trash/"
  55. }
  56. datasourceSorted = CCUtility.getOrderSettings()
  57. datasourceAscending = CCUtility.getAscendingSettings()
  58. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending) else {
  59. return
  60. }
  61. self.datasource = datasource
  62. collectionView.reloadData()
  63. loadListingTrash()
  64. }
  65. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  66. super.viewWillTransition(to: size, with: coordinator)
  67. coordinator.animate(alongsideTransition: nil) { _ in
  68. self.collectionView.collectionViewLayout.invalidateLayout()
  69. }
  70. }
  71. // MARK: DZNEmpty
  72. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  73. return NCBrandColor.sharedInstance.backgroundView
  74. }
  75. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  76. return CCGraphics.changeThemingColorImage(UIImage.init(named: "trashNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.graySoft)
  77. }
  78. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  79. let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
  80. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  81. return NSAttributedString.init(string: text, attributes: attributes)
  82. }
  83. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  84. return true
  85. }
  86. // MARK: TAP EVENT
  87. func tapSwitchHeaderMenu(sender: Any) {
  88. if collectionView.collectionViewLayout == gridLayout {
  89. // list layout
  90. UIView.animate(withDuration: 0.0, animations: {
  91. self.collectionView.collectionViewLayout.invalidateLayout()
  92. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  93. self.collectionView.reloadData()
  94. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  95. })
  96. })
  97. CCUtility.setLayoutTrash("list")
  98. } else {
  99. // grid layout
  100. UIView.animate(withDuration: 0.0, animations: {
  101. self.collectionView.collectionViewLayout.invalidateLayout()
  102. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  103. self.collectionView.reloadData()
  104. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  105. })
  106. })
  107. CCUtility.setLayoutTrash("grid")
  108. }
  109. }
  110. func tapOrderHeaderMenu(sender: Any) {
  111. var menuView: DropdownMenu?
  112. var selectedRow = 0
  113. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  114. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  115. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  116. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  117. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  118. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  119. switch datasourceSorted {
  120. case "fileName":
  121. if datasourceAscending == true { item1.style = .highlight; selectedRow = 0 }
  122. if datasourceAscending == false { item2.style = .highlight; selectedRow = 1 }
  123. case "date":
  124. if datasourceAscending == false { item3.style = .highlight; selectedRow = 2 }
  125. if datasourceAscending == true { item4.style = .highlight; selectedRow = 3 }
  126. case "size":
  127. if datasourceAscending == true { item5.style = .highlight; selectedRow = 4 }
  128. if datasourceAscending == false { item6.style = .highlight; selectedRow = 5 }
  129. default:
  130. print("")
  131. }
  132. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2, item3, item4, item5, item6], selectedRow: selectedRow)
  133. menuView?.token = "tapOrderHeaderMenu"
  134. menuView?.delegate = self
  135. menuView?.rowHeight = 50
  136. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  137. menuView?.tableView.alwaysBounceVertical = false
  138. let header = (sender as? UIButton)?.superview as! NCTrashHeaderMenu
  139. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  140. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  141. menuView?.topOffsetY = CGFloat(menuOffsetY)
  142. menuView?.showMenu()
  143. }
  144. func tapMoreHeaderMenu(sender: Any) {
  145. var menuView: DropdownMenu?
  146. if isEditMode {
  147. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  148. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_selected_", comment: ""))
  149. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_selected_", comment: ""))
  150. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  151. menuView?.token = "tapMoreHeaderMenuSelect"
  152. } else {
  153. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  154. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  155. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  156. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  157. menuView?.token = "tapMoreHeaderMenu"
  158. }
  159. menuView?.delegate = self
  160. menuView?.rowHeight = 50
  161. menuView?.tableView.alwaysBounceVertical = false
  162. let header = (sender as? UIButton)?.superview as! NCTrashHeaderMenu
  163. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  164. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  165. menuView?.topOffsetY = CGFloat(menuOffsetY)
  166. menuView?.showMenu()
  167. }
  168. func tapRestoreItem(with fileID: String, sender: Any) {
  169. if !isEditMode {
  170. restoreItem(with: fileID)
  171. } else {
  172. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  173. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  174. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  175. }
  176. }
  177. func tapMoreItem(with fileID: String, sender: Any) {
  178. if !isEditMode {
  179. var items = [ActionSheetItem]()
  180. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  181. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  182. let actionSheet = ActionSheet(items: items) { sheet, item in
  183. if item is ActionSheetDangerButton { self.deleteItem(with: fileID) }
  184. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  185. }
  186. let headerView = actionSheetHeader(with: fileID)
  187. actionSheet.headerView = headerView
  188. actionSheet.headerView?.frame.size.height = 50
  189. actionSheet.present(in: self, from: sender as! UIButton)
  190. } else {
  191. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  192. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  193. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  194. }
  195. }
  196. func tapMoreGridItem(with fileID: String, sender: Any) {
  197. if !isEditMode {
  198. var items = [ActionSheetItem]()
  199. let appearanceDelete = ActionSheetItemAppearance.init()
  200. appearanceDelete.textColor = UIColor.red
  201. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  202. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  203. itemDelete.customAppearance = appearanceDelete
  204. items.append(itemDelete)
  205. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  206. let actionSheet = ActionSheet(items: items) { sheet, item in
  207. if item.value as? Int == 0 { self.restoreItem(with: fileID) }
  208. if item.value as? Int == 1 { self.deleteItem(with: fileID) }
  209. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  210. }
  211. let headerView = actionSheetHeader(with: fileID)
  212. actionSheet.headerView = headerView
  213. actionSheet.headerView?.frame.size.height = 50
  214. actionSheet.present(in: self, from: sender as! UIButton)
  215. } else {
  216. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  217. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  218. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  219. }
  220. }
  221. // MARK: DROP-DOWN-MENU
  222. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  223. if dropdownMenu.token == "tapOrderHeaderMenu" {
  224. switch indexPath.row {
  225. case 0: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(true)
  226. case 1: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(false)
  227. case 2: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(false)
  228. case 3: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(true)
  229. case 4: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(true)
  230. case 5: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(false)
  231. default: print("")
  232. }
  233. datasourceSorted = CCUtility.getOrderSettings()
  234. datasourceAscending = CCUtility.getAscendingSettings()
  235. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending) else {
  236. return
  237. }
  238. self.datasource = datasource
  239. collectionView.reloadData()
  240. }
  241. if dropdownMenu.token == "tapMoreHeaderMenu" {
  242. // Select
  243. if indexPath.row == 0 {
  244. isEditMode = true
  245. collectionView.reloadData()
  246. }
  247. // Restore ALL
  248. if indexPath.row == 1 {
  249. for record: tableTrash in self.datasource {
  250. restoreItem(with: record.fileID)
  251. }
  252. }
  253. // Delete ALL
  254. if indexPath.row == 2 {
  255. var items = [ActionSheetItem]()
  256. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  257. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  258. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  259. let actionSheet = ActionSheet(items: items) { sheet, item in
  260. if item is ActionSheetDangerButton {
  261. for record: tableTrash in self.datasource {
  262. self.deleteItem(with: record.fileID)
  263. }
  264. }
  265. if item is ActionSheetCancelButton { return }
  266. }
  267. actionSheet.present(in: self, from: self.view)
  268. }
  269. }
  270. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  271. // Cancel
  272. if indexPath.row == 0 {
  273. isEditMode = false
  274. selectFileID.removeAll()
  275. collectionView.reloadData()
  276. }
  277. // Restore selected files
  278. if indexPath.row == 1 {
  279. for fileID in selectFileID {
  280. restoreItem(with: fileID)
  281. }
  282. isEditMode = false
  283. selectFileID.removeAll()
  284. collectionView.reloadData()
  285. }
  286. // Delete selected files
  287. if indexPath.row == 2 {
  288. var items = [ActionSheetItem]()
  289. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_selected_", comment: "")))
  290. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  291. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  292. let actionSheet = ActionSheet(items: items) { sheet, item in
  293. if item is ActionSheetDangerButton {
  294. for fileID in self.selectFileID {
  295. self.deleteItem(with: fileID)
  296. }
  297. self.isEditMode = false
  298. self.selectFileID.removeAll()
  299. self.collectionView.reloadData()
  300. }
  301. if item is ActionSheetCancelButton { return }
  302. }
  303. actionSheet.present(in: self, from: self.view)
  304. }
  305. }
  306. }
  307. /*
  308. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  309. if dropdownMenu.token == "tapOrderHeaderMenu" {
  310. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  311. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  312. trashHeader.buttonOrder.setTitle(title, for: .normal)
  313. }
  314. }
  315. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  316. if dropdownMenu.token == "tapOrderHeaderMenu" {
  317. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  318. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  319. trashHeader.buttonOrder.setTitle(title, for: .normal)
  320. }
  321. }
  322. */
  323. // MARK: NC API
  324. @objc func loadListingTrash() {
  325. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  326. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  327. self.refreshControl.endRefreshing()
  328. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  329. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  330. let results = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending)
  331. if (results != nil) {
  332. self.datasource = results!
  333. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  334. self.collectionView.reloadData()
  335. }
  336. }
  337. }, failure: { (message, errorCode) in
  338. self.refreshControl.endRefreshing()
  339. print("error " + message!)
  340. })
  341. }
  342. func restoreItem(with fileID: String) {
  343. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  344. return
  345. }
  346. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  347. let fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  348. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  349. ocNetworking?.moveFileOrFolder(fileName, fileNameTo: fileNameTo, success: {
  350. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID)
  351. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending) else {
  352. return
  353. }
  354. self.datasource = datasource
  355. self.collectionView.reloadData()
  356. }, failure: { (message, errorCode) in
  357. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  358. })
  359. }
  360. func deleteItem(with fileID: String) {
  361. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  362. return
  363. }
  364. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  365. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  366. ocNetworking?.deleteFileOrFolder(path, completion: { (message, errorCode) in
  367. if errorCode == 0 {
  368. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID)
  369. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending) else {
  370. return
  371. }
  372. self.datasource = datasource
  373. self.collectionView.reloadData()
  374. } else {
  375. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  376. }
  377. })
  378. }
  379. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  380. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  381. ocNetworking?.downloadPreviewTrash(withFileID: tableTrash.fileID, fileName: tableTrash.fileName, completion: { (message, errorCode) in
  382. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  383. self.collectionView.reloadItems(at: [indexPath])
  384. }
  385. })
  386. }
  387. // MARK: COLLECTIONVIEW METHODS
  388. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  389. if kind == UICollectionView.elementKindSectionHeader {
  390. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCTrashHeaderMenu
  391. if collectionView.collectionViewLayout == gridLayout {
  392. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  393. } else {
  394. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  395. }
  396. trashHeader.delegate = self
  397. if self.datasource.count == 0 {
  398. trashHeader.buttonSwitch.isEnabled = false
  399. trashHeader.buttonOrder.isEnabled = false
  400. trashHeader.buttonMore.isEnabled = false
  401. } else {
  402. trashHeader.buttonSwitch.isEnabled = true
  403. trashHeader.buttonOrder.isEnabled = true
  404. trashHeader.buttonMore.isEnabled = true
  405. }
  406. // Order (∨∧▽△)
  407. var title = ""
  408. switch datasourceSorted {
  409. case "fileName":
  410. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  411. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  412. case "date":
  413. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  414. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  415. case "size":
  416. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  417. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  418. default:
  419. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  420. }
  421. trashHeader.buttonOrder.setTitle(title + " ▽", for: .normal)
  422. return trashHeader
  423. } else {
  424. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCTrashFooterMenu
  425. trashFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  426. var folders: Int = 0, foldersText = ""
  427. var files: Int = 0, filesText = ""
  428. var size: Double = 0
  429. for record: tableTrash in self.datasource {
  430. if record.directory {
  431. folders += 1
  432. } else {
  433. files += 1
  434. size = size + record.size
  435. }
  436. }
  437. if folders > 1 {
  438. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  439. } else if folders == 1 {
  440. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  441. }
  442. if files > 1 {
  443. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  444. } else if files == 1 {
  445. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  446. }
  447. if foldersText == "" {
  448. trashFooter.labelFooter.text = filesText
  449. } else if filesText == "" {
  450. trashFooter.labelFooter.text = foldersText
  451. } else {
  452. trashFooter.labelFooter.text = foldersText + ", " + filesText
  453. }
  454. return trashFooter
  455. }
  456. }
  457. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  458. return CGSize(width: collectionView.frame.width, height: highHeader)
  459. }
  460. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  461. return CGSize(width: collectionView.frame.width, height: highHeader)
  462. }
  463. func numberOfSections(in collectionView: UICollectionView) -> Int {
  464. return 1
  465. }
  466. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  467. return datasource.count
  468. }
  469. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  470. let tableTrash = datasource[indexPath.item]
  471. var image: UIImage?
  472. if tableTrash.iconName.count > 0 {
  473. image = UIImage.init(named: tableTrash.iconName)
  474. } else {
  475. image = UIImage.init(named: "file")
  476. }
  477. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  478. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  479. } else {
  480. if tableTrash.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  481. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  482. }
  483. }
  484. if collectionView.collectionViewLayout == listLayout {
  485. // LIST
  486. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  487. cell.delegate = self
  488. cell.fileID = tableTrash.fileID
  489. cell.indexPath = indexPath
  490. cell.labelTitle.text = tableTrash.trashbinFileName
  491. if tableTrash.directory {
  492. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  493. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  494. } else {
  495. cell.imageItem.image = image
  496. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  497. }
  498. if isEditMode {
  499. cell.imageItemLeftConstraint.constant = 45
  500. cell.imageSelect.isHidden = false
  501. if selectFileID.contains(tableTrash.fileID) {
  502. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  503. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  504. } else {
  505. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  506. cell.backgroundView = nil
  507. }
  508. } else {
  509. cell.imageItemLeftConstraint.constant = 10
  510. cell.imageSelect.isHidden = true
  511. cell.backgroundView = nil
  512. }
  513. return cell
  514. } else {
  515. // GRID
  516. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  517. cell.delegate = self
  518. cell.fileID = tableTrash.fileID
  519. cell.indexPath = indexPath
  520. cell.labelTitle.text = tableTrash.trashbinFileName
  521. if tableTrash.directory {
  522. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  523. } else {
  524. cell.imageItem.image = image
  525. }
  526. if isEditMode {
  527. cell.imageSelect.isHidden = false
  528. if selectFileID.contains(tableTrash.fileID) {
  529. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  530. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  531. } else {
  532. cell.imageSelect.isHidden = true
  533. cell.backgroundView = nil
  534. }
  535. } else {
  536. cell.imageSelect.isHidden = true
  537. cell.backgroundView = nil
  538. }
  539. return cell
  540. }
  541. }
  542. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  543. let tableTrash = datasource[indexPath.item]
  544. if isEditMode {
  545. if let index = selectFileID.index(of: tableTrash.fileID) {
  546. selectFileID.remove(at: index)
  547. } else {
  548. selectFileID.append(tableTrash.fileID)
  549. }
  550. collectionView.reloadItems(at: [indexPath])
  551. return
  552. }
  553. if tableTrash.directory {
  554. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  555. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  556. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  557. self.navigationController?.pushViewController(ncTrash, animated: true)
  558. }
  559. }
  560. // MARK: UTILITY
  561. private func cellBlurEffect(with frame: CGRect) -> UIView {
  562. let blurEffect = UIBlurEffect(style: .extraLight)
  563. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  564. blurEffectView.frame = frame
  565. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  566. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  567. return blurEffectView
  568. }
  569. private func actionSheetHeader(with fileID: String) -> UIView? {
  570. var image: UIImage?
  571. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  572. return nil
  573. }
  574. // Header
  575. if tableTrash.iconName.count > 0 {
  576. image = UIImage.init(named: tableTrash.iconName)
  577. } else {
  578. image = UIImage.init(named: "file")
  579. }
  580. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  581. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  582. }
  583. let headerView = UINib(nibName: "NCActionSheetHeaderView", bundle: nil).instantiate(withOwner: self, options: nil).first as! NCActionSheetHeaderView
  584. headerView.imageItem.image = image
  585. headerView.label.text = tableTrash.trashbinFileName
  586. headerView.label.textColor = NCBrandColor.sharedInstance.icon
  587. return headerView
  588. }
  589. }
  590. class ListLayout: UICollectionViewFlowLayout {
  591. let itemHeight: CGFloat = 60
  592. override init() {
  593. super.init()
  594. minimumInteritemSpacing = 0
  595. minimumLineSpacing = 1
  596. self.scrollDirection = .vertical
  597. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  598. }
  599. required init?(coder aDecoder: NSCoder) {
  600. fatalError("init(coder:) has not been implemented")
  601. }
  602. override var itemSize: CGSize {
  603. get {
  604. if let collectionView = collectionView {
  605. let itemWidth: CGFloat = collectionView.frame.width
  606. return CGSize(width: itemWidth, height: self.itemHeight)
  607. }
  608. // Default fallback
  609. return CGSize(width: 100, height: 100)
  610. }
  611. set {
  612. super.itemSize = newValue
  613. }
  614. }
  615. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  616. return proposedContentOffset
  617. }
  618. }
  619. class GridLayout: UICollectionViewFlowLayout {
  620. let heightLabelPlusButton: CGFloat = 45
  621. override init() {
  622. super.init()
  623. minimumInteritemSpacing = 0
  624. minimumLineSpacing = 1
  625. self.scrollDirection = .vertical
  626. self.sectionInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)
  627. }
  628. required init?(coder aDecoder: NSCoder) {
  629. fatalError("init(coder:) has not been implemented")
  630. }
  631. override var itemSize: CGSize {
  632. get {
  633. if let collectionView = collectionView {
  634. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(collectionView.bounds.width / 90.0))
  635. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  636. return CGSize(width: itemWidth, height: itemHeight)
  637. }
  638. // Default fallback
  639. return CGSize(width: 100, height: 100)
  640. }
  641. set {
  642. super.itemSize = newValue
  643. }
  644. }
  645. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  646. return proposedContentOffset
  647. }
  648. }