NCTrash.swift 28 KB

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