NCTrash.swift 30 KB

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