NCTrash.swift 27 KB

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