NCTrash.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. if !isEditMode {
  170. restoreItem(with: fileID)
  171. } else {
  172. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  173. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  174. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  175. }
  176. }
  177. func tapMoreItem(with fileID: String, sender: Any) {
  178. if !isEditMode {
  179. var items = [ActionSheetItem]()
  180. items.append(ActionSheetTitle(title: NSLocalizedString("_delete_selected_files_", comment: "")))
  181. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  182. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  183. let actionSheet = ActionSheet(items: items) { sheet, item in
  184. if item is ActionSheetDangerButton { self.deleteItem(with: fileID) }
  185. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  186. }
  187. actionSheet.present(in: self, from: sender as! UIButton)
  188. } else {
  189. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  190. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  191. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  192. }
  193. }
  194. func tapMoreGridItem(with fileID: String, sender: Any) {
  195. if !isEditMode {
  196. var items = [ActionSheetItem]()
  197. let appearanceDelete = ActionSheetItemAppearance.init()
  198. appearanceDelete.textColor = UIColor.red
  199. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  200. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  201. itemDelete.customAppearance = appearanceDelete
  202. items.append(itemDelete)
  203. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  204. let actionSheet = ActionSheet(items: items) { sheet, item in
  205. if item.value as? Int == 0 { self.restoreItem(with: fileID) }
  206. if item.value as? Int == 1 { self.deleteItem(with: fileID) }
  207. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  208. }
  209. /*
  210. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  211. return
  212. }
  213. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(fileID, fileNameView: tableTrash.fileName)) {
  214. actionSheet.headerView = UIImageView(image: UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(fileID, fileNameView: tableTrash.fileName)))
  215. actionSheet.headerView?.frame.size.height = 150
  216. }
  217. */
  218. actionSheet.present(in: self, from: sender as! UIButton)
  219. } else {
  220. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  221. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  222. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  223. }
  224. }
  225. // MARK: DROP-DOWN-MENU
  226. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  227. if dropdownMenu.token == "tapOrderHeaderMenu" {
  228. switch indexPath.row {
  229. case 0: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(true)
  230. case 1: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(false)
  231. case 2: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(false)
  232. case 3: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(true)
  233. case 4: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(true)
  234. case 5: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(false)
  235. default: print("")
  236. }
  237. datasourceSorted = CCUtility.getOrderSettings()
  238. datasourceAscending = CCUtility.getAscendingSettings()
  239. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending) else {
  240. return
  241. }
  242. self.datasource = datasource
  243. collectionView.reloadData()
  244. }
  245. if dropdownMenu.token == "tapMoreHeaderMenu" {
  246. // Select
  247. if indexPath.row == 0 {
  248. isEditMode = true
  249. collectionView.reloadData()
  250. }
  251. // Restore ALL
  252. if indexPath.row == 1 {
  253. for record: tableTrash in self.datasource {
  254. restoreItem(with: record.fileID)
  255. }
  256. }
  257. // Delete ALL
  258. if indexPath.row == 2 {
  259. var items = [ActionSheetItem]()
  260. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  261. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  262. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  263. let actionSheet = ActionSheet(items: items) { sheet, item in
  264. if item is ActionSheetDangerButton {
  265. for record: tableTrash in self.datasource {
  266. self.deleteItem(with: record.fileID)
  267. }
  268. }
  269. if item is ActionSheetCancelButton { return }
  270. }
  271. actionSheet.present(in: self, from: self.view)
  272. }
  273. }
  274. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  275. // Cancel
  276. if indexPath.row == 0 {
  277. isEditMode = false
  278. selectFileID.removeAll()
  279. collectionView.reloadData()
  280. }
  281. // Restore selected files
  282. if indexPath.row == 1 {
  283. for fileID in selectFileID {
  284. restoreItem(with: fileID)
  285. }
  286. isEditMode = false
  287. selectFileID.removeAll()
  288. collectionView.reloadData()
  289. }
  290. // Delete selected files
  291. if indexPath.row == 2 {
  292. var items = [ActionSheetItem]()
  293. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_selected_", comment: "")))
  294. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  295. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  296. let actionSheet = ActionSheet(items: items) { sheet, item in
  297. if item is ActionSheetDangerButton {
  298. for fileID in self.selectFileID {
  299. self.deleteItem(with: fileID)
  300. }
  301. self.isEditMode = false
  302. self.selectFileID.removeAll()
  303. self.collectionView.reloadData()
  304. }
  305. if item is ActionSheetCancelButton { return }
  306. }
  307. actionSheet.present(in: self, from: self.view)
  308. }
  309. }
  310. }
  311. /*
  312. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  313. if dropdownMenu.token == "tapOrderHeaderMenu" {
  314. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  315. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  316. trashHeader.buttonOrder.setTitle(title, for: .normal)
  317. }
  318. }
  319. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  320. if dropdownMenu.token == "tapOrderHeaderMenu" {
  321. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  322. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  323. trashHeader.buttonOrder.setTitle(title, for: .normal)
  324. }
  325. }
  326. */
  327. // MARK: NC API
  328. @objc func loadListingTrash() {
  329. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  330. ocNetworking?.listingTrash(appDelegate.activeUrl, path:path, account: appDelegate.activeAccount, success: { (item) in
  331. self.refreshControl.endRefreshing()
  332. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path)
  333. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  334. let results = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending)
  335. if (results != nil) {
  336. self.datasource = results!
  337. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  338. self.collectionView.reloadData()
  339. }
  340. }
  341. }, failure: { (message, errorCode) in
  342. self.refreshControl.endRefreshing()
  343. print("error " + message!)
  344. })
  345. }
  346. func restoreItem(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 fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  352. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  353. ocNetworking?.moveFileOrFolder(fileName, fileNameTo: fileNameTo, success: {
  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. }, failure: { (message, errorCode) in
  361. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  362. })
  363. }
  364. func deleteItem(with fileID: String) {
  365. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID) else {
  366. return
  367. }
  368. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  369. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  370. ocNetworking?.deleteFileOrFolder(path, completion: { (message, errorCode) in
  371. if errorCode == 0 {
  372. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID)
  373. guard let datasource = NCManageDatabase.sharedInstance.getTrash(filePath: self.path, sorted: self.datasourceSorted, ascending: self.datasourceAscending) else {
  374. return
  375. }
  376. self.datasource = datasource
  377. self.collectionView.reloadData()
  378. } else {
  379. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  380. }
  381. })
  382. }
  383. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  384. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  385. ocNetworking?.downloadPreviewTrash(withFileID: tableTrash.fileID, fileName: tableTrash.fileName, completion: { (message, errorCode) in
  386. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  387. self.collectionView.reloadItems(at: [indexPath])
  388. }
  389. })
  390. }
  391. // MARK: COLLECTIONVIEW METHODS
  392. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  393. if kind == UICollectionView.elementKindSectionHeader {
  394. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerMenu", for: indexPath) as! NCTrashHeaderMenu
  395. if collectionView.collectionViewLayout == gridLayout {
  396. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  397. } else {
  398. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  399. }
  400. trashHeader.delegate = self
  401. if self.datasource.count == 0 {
  402. trashHeader.buttonSwitch.isEnabled = false
  403. trashHeader.buttonMore.isEnabled = false
  404. } else {
  405. trashHeader.buttonSwitch.isEnabled = true
  406. trashHeader.buttonMore.isEnabled = true
  407. }
  408. // Order (∨∧▽△)
  409. var title = ""
  410. switch datasourceSorted {
  411. case "fileName":
  412. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  413. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  414. case "date":
  415. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  416. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  417. case "size":
  418. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  419. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  420. default:
  421. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  422. }
  423. trashHeader.buttonOrder.setTitle(title + " ▽", for: .normal)
  424. return trashHeader
  425. } else {
  426. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCTrashFooterMenu
  427. trashFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  428. var folders: Int = 0, foldersText = ""
  429. var files: Int = 0, filesText = ""
  430. var size: Double = 0
  431. for record: tableTrash in self.datasource {
  432. if record.directory {
  433. folders += 1
  434. } else {
  435. files += 1
  436. size = size + record.size
  437. }
  438. }
  439. if folders > 1 {
  440. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  441. } else if folders == 1 {
  442. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  443. }
  444. if files > 1 {
  445. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  446. } else if files == 1 {
  447. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  448. }
  449. if foldersText == "" {
  450. trashFooter.labelFooter.text = filesText
  451. } else if filesText == "" {
  452. trashFooter.labelFooter.text = foldersText
  453. } else {
  454. trashFooter.labelFooter.text = foldersText + ", " + filesText
  455. }
  456. return trashFooter
  457. }
  458. }
  459. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  460. return CGSize(width: collectionView.frame.width, height: highHeader)
  461. }
  462. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  463. return CGSize(width: collectionView.frame.width, height: highHeader)
  464. }
  465. func numberOfSections(in collectionView: UICollectionView) -> Int {
  466. return 1
  467. }
  468. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  469. return datasource.count
  470. }
  471. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  472. let tableTrash = datasource[indexPath.item]
  473. var image: UIImage?
  474. if tableTrash.iconName.count > 0 {
  475. image = UIImage.init(named: tableTrash.iconName)
  476. } else {
  477. image = UIImage.init(named: "file")
  478. }
  479. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  480. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  481. } else {
  482. if tableTrash.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  483. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  484. }
  485. }
  486. if collectionView.collectionViewLayout == listLayout {
  487. // LIST
  488. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  489. cell.delegate = self
  490. cell.fileID = tableTrash.fileID
  491. cell.indexPath = indexPath
  492. cell.labelTitle.text = tableTrash.trashbinFileName
  493. if tableTrash.directory {
  494. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  495. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  496. } else {
  497. cell.imageItem.image = image
  498. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  499. }
  500. if isEditMode {
  501. cell.imageItemLeftConstraint.constant = 45
  502. cell.imageSelect.isHidden = false
  503. if selectFileID.contains(tableTrash.fileID) {
  504. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  505. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  506. } else {
  507. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  508. cell.backgroundView = nil
  509. }
  510. } else {
  511. cell.imageItemLeftConstraint.constant = 10
  512. cell.imageSelect.isHidden = true
  513. cell.backgroundView = nil
  514. }
  515. return cell
  516. } else {
  517. // GRID
  518. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  519. cell.delegate = self
  520. cell.fileID = tableTrash.fileID
  521. cell.indexPath = indexPath
  522. cell.labelTitle.text = tableTrash.trashbinFileName
  523. if tableTrash.directory {
  524. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  525. } else {
  526. cell.imageItem.image = image
  527. }
  528. if isEditMode {
  529. cell.imageSelect.isHidden = false
  530. if selectFileID.contains(tableTrash.fileID) {
  531. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  532. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  533. } else {
  534. cell.imageSelect.isHidden = true
  535. cell.backgroundView = nil
  536. }
  537. } else {
  538. cell.imageSelect.isHidden = true
  539. cell.backgroundView = nil
  540. }
  541. return cell
  542. }
  543. }
  544. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  545. let tableTrash = datasource[indexPath.item]
  546. if isEditMode {
  547. if let index = selectFileID.index(of: tableTrash.fileID) {
  548. selectFileID.remove(at: index)
  549. } else {
  550. selectFileID.append(tableTrash.fileID)
  551. }
  552. collectionView.reloadItems(at: [indexPath])
  553. return
  554. }
  555. if tableTrash.directory {
  556. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  557. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  558. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  559. self.navigationController?.pushViewController(ncTrash, animated: true)
  560. }
  561. }
  562. // MARK: UTILITY
  563. private func cellBlurEffect(with frame: CGRect) -> UIView {
  564. let blurEffect = UIBlurEffect(style: .extraLight)
  565. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  566. blurEffectView.frame = frame
  567. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  568. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  569. return blurEffectView
  570. }
  571. }
  572. class ListLayout: UICollectionViewFlowLayout {
  573. let itemHeight: CGFloat = 60
  574. override init() {
  575. super.init()
  576. minimumInteritemSpacing = 0
  577. minimumLineSpacing = 1
  578. self.scrollDirection = .vertical
  579. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  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
  588. return CGSize(width: itemWidth, height: self.itemHeight)
  589. }
  590. // Default fallback
  591. return CGSize(width: 100, height: 100)
  592. }
  593. set {
  594. super.itemSize = newValue
  595. }
  596. }
  597. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  598. return proposedContentOffset
  599. }
  600. }
  601. class GridLayout: UICollectionViewFlowLayout {
  602. let heightLabelPlusButton: CGFloat = 45
  603. override init() {
  604. super.init()
  605. minimumInteritemSpacing = 0
  606. minimumLineSpacing = 1
  607. self.scrollDirection = .vertical
  608. self.sectionInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)
  609. }
  610. required init?(coder aDecoder: NSCoder) {
  611. fatalError("init(coder:) has not been implemented")
  612. }
  613. override var itemSize: CGSize {
  614. get {
  615. if let collectionView = collectionView {
  616. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(collectionView.bounds.width / 90.0))
  617. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  618. return CGSize(width: itemWidth, height: itemHeight)
  619. }
  620. // Default fallback
  621. return CGSize(width: 100, height: 100)
  622. }
  623. set {
  624. super.itemSize = newValue
  625. }
  626. }
  627. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  628. return proposedContentOffset
  629. }
  630. }