NCTrash.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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 tapRestoreItem(with fileID: String, sender: Any) {
  88. restoreItem(with: fileID)
  89. }
  90. func tapMoreItem(with fileID: String, sender: Any) {
  91. var items = [ActionSheetItem]()
  92. items.append(ActionSheetTitle(title: NSLocalizedString("_delete_selected_files_", comment: "")))
  93. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  94. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  95. let actionSheet = ActionSheet(items: items) { sheet, item in
  96. if item is ActionSheetDangerButton { self.deleteItem(with: fileID) }
  97. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  98. }
  99. actionSheet.present(in: self, from: sender as! UIButton)
  100. }
  101. func tapSwitchHeaderMenu(sender: Any) {
  102. if collectionView.collectionViewLayout == gridLayout {
  103. // list layout
  104. UIView.animate(withDuration: 0.0, animations: {
  105. self.collectionView.collectionViewLayout.invalidateLayout()
  106. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  107. self.collectionView.reloadData()
  108. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  109. })
  110. })
  111. CCUtility.setLayoutTrash("list")
  112. } else {
  113. // grid layout
  114. UIView.animate(withDuration: 0.0, animations: {
  115. self.collectionView.collectionViewLayout.invalidateLayout()
  116. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  117. self.collectionView.reloadData()
  118. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  119. })
  120. })
  121. CCUtility.setLayoutTrash("grid")
  122. }
  123. }
  124. func tapMoreGridItem(with fileID: String, sender: Any) {
  125. var items = [ActionSheetItem]()
  126. let appearanceDelete = ActionSheetItemAppearance.init()
  127. appearanceDelete.textColor = UIColor.red
  128. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  129. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  130. itemDelete.customAppearance = appearanceDelete
  131. items.append(itemDelete)
  132. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  133. let actionSheet = ActionSheet(items: items) { sheet, item in
  134. if item.value as? Int == 0 { self.restoreItem(with: fileID) }
  135. if item.value as? Int == 1 { self.deleteItem(with: fileID) }
  136. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  137. }
  138. /*
  139. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  140. return
  141. }
  142. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(fileID, fileNameView: tableTrash.fileName)) {
  143. actionSheet.headerView = UIImageView(image: UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(fileID, fileNameView: tableTrash.fileName)))
  144. actionSheet.headerView?.frame.size.height = 150
  145. }
  146. */
  147. actionSheet.present(in: self, from: sender as! UIButton)
  148. }
  149. func tapMoreHeaderMenu(sender: Any) {
  150. var menuView: DropdownMenu?
  151. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  152. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  153. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2], selectedRow: -1)
  154. menuView?.token = "tapMoreHeaderMenu"
  155. menuView?.delegate = self
  156. menuView?.rowHeight = 50
  157. menuView?.tableView.alwaysBounceVertical = false
  158. let header = (sender as? UIButton)?.superview as! NCTrashHeaderMenu
  159. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  160. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  161. menuView?.topOffsetY = CGFloat(menuOffsetY)
  162. menuView?.showMenu()
  163. }
  164. func tapOrderHeaderMenu(sender: Any) {
  165. var menuView: DropdownMenu?
  166. var selectedRow = 0
  167. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  168. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  169. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  170. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  171. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  172. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  173. switch datasourceSorted {
  174. case "fileName":
  175. if datasourceAscending == true { item1.style = .highlight; selectedRow = 0 }
  176. if datasourceAscending == false { item2.style = .highlight; selectedRow = 1 }
  177. case "date":
  178. if datasourceAscending == false { item3.style = .highlight; selectedRow = 2 }
  179. if datasourceAscending == true { item4.style = .highlight; selectedRow = 3 }
  180. case "size":
  181. if datasourceAscending == true { item5.style = .highlight; selectedRow = 4 }
  182. if datasourceAscending == false { item6.style = .highlight; selectedRow = 5 }
  183. default:
  184. print("")
  185. }
  186. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2, item3, item4, item5, item6], selectedRow: selectedRow)
  187. menuView?.token = "tapOrderHeaderMenu"
  188. menuView?.delegate = self
  189. menuView?.rowHeight = 50
  190. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  191. menuView?.tableView.alwaysBounceVertical = false
  192. let header = (sender as? UIButton)?.superview as! NCTrashHeaderMenu
  193. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  194. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  195. menuView?.topOffsetY = CGFloat(menuOffsetY)
  196. menuView?.showMenu()
  197. }
  198. // MARK: DROP-DOWN-MENU
  199. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  200. if dropdownMenu.token == "tapOrderHeaderMenu" {
  201. switch indexPath.row {
  202. case 0: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(true)
  203. case 1: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(false)
  204. case 2: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(false)
  205. case 3: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(true)
  206. case 4: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(true)
  207. case 5: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(false)
  208. default: print("")
  209. }
  210. datasourceSorted = CCUtility.getOrderSettings()
  211. datasourceAscending = CCUtility.getAscendingSettings()
  212. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending) else {
  213. return
  214. }
  215. self.datasource = datasource
  216. collectionView.reloadData()
  217. }
  218. if dropdownMenu.token == "tapMoreHeaderMenu" {
  219. if indexPath.row == 0 {
  220. for record: tableTrash in self.datasource {
  221. restoreItem(with: record.fileID)
  222. }
  223. }
  224. if indexPath.row == 1 {
  225. var items = [ActionSheetItem]()
  226. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  227. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  228. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  229. let actionSheet = ActionSheet(items: items) { sheet, item in
  230. if item is ActionSheetDangerButton {
  231. for record: tableTrash in self.datasource {
  232. self.deleteItem(with: record.fileID)
  233. }
  234. }
  235. if item is ActionSheetCancelButton { return }
  236. }
  237. actionSheet.present(in: self, from: self.view)
  238. }
  239. }
  240. }
  241. /*
  242. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  243. if dropdownMenu.token == "tapOrderHeaderMenu" {
  244. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  245. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  246. trashHeader.buttonOrder.setTitle(title, for: .normal)
  247. }
  248. }
  249. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  250. if dropdownMenu.token == "tapOrderHeaderMenu" {
  251. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  252. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  253. trashHeader.buttonOrder.setTitle(title, for: .normal)
  254. }
  255. }
  256. */
  257. // MARK: NC API
  258. @objc func loadListingTrash() {
  259. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  260. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  261. self.refreshControl.endRefreshing()
  262. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  263. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  264. let results = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending)
  265. if (results != nil) {
  266. self.datasource = results!
  267. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  268. self.collectionView.reloadData()
  269. }
  270. }
  271. }, failure: { (message, errorCode) in
  272. self.refreshControl.endRefreshing()
  273. print("error " + message!)
  274. })
  275. }
  276. func restoreItem(with fileID: String) {
  277. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  278. return
  279. }
  280. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  281. let fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  282. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  283. ocNetworking?.moveFileOrFolder(fileName, fileNameTo: fileNameTo, success: {
  284. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID)
  285. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending) else {
  286. return
  287. }
  288. self.datasource = datasource
  289. self.collectionView.reloadData()
  290. }, failure: { (message, errorCode) in
  291. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  292. })
  293. }
  294. func deleteItem(with fileID: String) {
  295. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  296. return
  297. }
  298. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  299. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  300. ocNetworking?.deleteFileOrFolder(path, completion: { (message, errorCode) in
  301. if errorCode == 0 {
  302. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID)
  303. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending) else {
  304. return
  305. }
  306. self.datasource = datasource
  307. self.collectionView.reloadData()
  308. } else {
  309. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  310. }
  311. })
  312. }
  313. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  314. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  315. ocNetworking?.downloadPreviewTrash(withFileID: tableTrash.fileID, fileName: tableTrash.fileName, completion: { (message, errorCode) in
  316. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  317. self.collectionView.reloadItems(at: [indexPath])
  318. }
  319. })
  320. }
  321. // MARK: COLLECTIONVIEW METHODS
  322. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  323. if kind == UICollectionView.elementKindSectionHeader {
  324. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCTrashHeaderMenu
  325. if collectionView.collectionViewLayout == gridLayout {
  326. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  327. } else {
  328. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  329. }
  330. trashHeader.delegate = self
  331. if self.datasource.count == 0 {
  332. trashHeader.buttonSwitch.isEnabled = false
  333. trashHeader.buttonMore.isEnabled = false
  334. } else {
  335. trashHeader.buttonSwitch.isEnabled = true
  336. trashHeader.buttonMore.isEnabled = true
  337. }
  338. // Order (∨∧▽△)
  339. var title = ""
  340. switch datasourceSorted {
  341. case "fileName":
  342. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  343. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  344. case "date":
  345. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  346. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  347. case "size":
  348. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  349. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  350. default:
  351. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  352. }
  353. trashHeader.buttonOrder.setTitle(title + " ▽", for: .normal)
  354. return trashHeader
  355. } else {
  356. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCTrashFooterMenu
  357. trashFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  358. var folders: Int = 0, foldersText = ""
  359. var files: Int = 0, filesText = ""
  360. var size: Double = 0
  361. for record: tableTrash in self.datasource {
  362. if record.directory {
  363. folders += 1
  364. } else {
  365. files += 1
  366. size = size + record.size
  367. }
  368. }
  369. if folders > 1 {
  370. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  371. } else if folders == 1 {
  372. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  373. }
  374. if files > 1 {
  375. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  376. } else if files == 1 {
  377. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  378. }
  379. if foldersText == "" {
  380. trashFooter.labelFooter.text = filesText
  381. } else if filesText == "" {
  382. trashFooter.labelFooter.text = foldersText
  383. } else {
  384. trashFooter.labelFooter.text = foldersText + ", " + filesText
  385. }
  386. return trashFooter
  387. }
  388. }
  389. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  390. return CGSize(width: collectionView.frame.width, height: highHeader)
  391. }
  392. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  393. return CGSize(width: collectionView.frame.width, height: highHeader)
  394. }
  395. func numberOfSections(in collectionView: UICollectionView) -> Int {
  396. return 1
  397. }
  398. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  399. return datasource.count
  400. }
  401. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  402. let tableTrash = datasource[indexPath.item]
  403. var image: UIImage?
  404. if tableTrash.iconName.count > 0 {
  405. image = UIImage.init(named: tableTrash.iconName)
  406. } else {
  407. image = UIImage.init(named: "file")
  408. }
  409. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  410. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  411. } else {
  412. if tableTrash.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  413. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  414. }
  415. }
  416. if collectionView.collectionViewLayout == listLayout {
  417. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  418. cell.delegate = self
  419. cell.fileID = tableTrash.fileID
  420. cell.indexPath = indexPath
  421. cell.labelTitle.text = tableTrash.trashbinFileName
  422. if tableTrash.directory {
  423. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  424. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  425. } else {
  426. cell.imageItem.image = image
  427. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  428. }
  429. return cell
  430. } else {
  431. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  432. cell.delegate = self
  433. cell.fileID = tableTrash.fileID
  434. cell.indexPath = indexPath
  435. cell.labelTitle.text = tableTrash.trashbinFileName
  436. if tableTrash.directory {
  437. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  438. } else {
  439. cell.imageItem.image = image
  440. }
  441. return cell
  442. }
  443. }
  444. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  445. let tableTrash = datasource[indexPath.item]
  446. if tableTrash.directory {
  447. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  448. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  449. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  450. self.navigationController?.pushViewController(ncTrash, animated: true)
  451. }
  452. }
  453. func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  454. if !isEditMode {
  455. return
  456. }
  457. }
  458. }
  459. class ListLayout: UICollectionViewFlowLayout {
  460. let itemHeight: CGFloat = 60
  461. override init() {
  462. super.init()
  463. self.scrollDirection = .vertical
  464. self.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
  465. }
  466. required init?(coder aDecoder: NSCoder) {
  467. fatalError("init(coder:) has not been implemented")
  468. }
  469. override var itemSize: CGSize {
  470. get {
  471. if let collectionView = collectionView {
  472. let itemWidth: CGFloat = collectionView.frame.width
  473. return CGSize(width: itemWidth, height: self.itemHeight)
  474. }
  475. // Default fallback
  476. return CGSize(width: 100, height: 100)
  477. }
  478. set {
  479. super.itemSize = newValue
  480. }
  481. }
  482. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  483. return proposedContentOffset
  484. }
  485. }
  486. class GridLayout: UICollectionViewFlowLayout {
  487. let heightLabelPlusButton: CGFloat = 45
  488. override init() {
  489. super.init()
  490. minimumInteritemSpacing = 0
  491. self.scrollDirection = .vertical
  492. self.sectionInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)
  493. }
  494. required init?(coder aDecoder: NSCoder) {
  495. fatalError("init(coder:) has not been implemented")
  496. }
  497. override var itemSize: CGSize {
  498. get {
  499. if let collectionView = collectionView {
  500. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(collectionView.bounds.width / 90.0))
  501. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  502. return CGSize(width: itemWidth, height: itemHeight)
  503. }
  504. // Default fallback
  505. return CGSize(width: 100, height: 100)
  506. }
  507. set {
  508. super.itemSize = newValue
  509. }
  510. }
  511. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  512. return proposedContentOffset
  513. }
  514. }