NCTrash.swift 36 KB

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