NCTrash.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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.buttonOrder.isEnabled = false
  404. trashHeader.buttonMore.isEnabled = false
  405. } else {
  406. trashHeader.buttonSwitch.isEnabled = true
  407. trashHeader.buttonOrder.isEnabled = true
  408. trashHeader.buttonMore.isEnabled = true
  409. }
  410. // Order (∨∧▽△)
  411. var title = ""
  412. switch datasourceSorted {
  413. case "fileName":
  414. if datasourceAscending == true { title = NSLocalizedString("_order_by_name_a_z_", comment: "") }
  415. if datasourceAscending == false { title = NSLocalizedString("_order_by_name_z_a_", comment: "") }
  416. case "date":
  417. if datasourceAscending == false { title = NSLocalizedString("_order_by_date_more_recent_", comment: "") }
  418. if datasourceAscending == true { title = NSLocalizedString("_order_by_date_less_recent_", comment: "") }
  419. case "size":
  420. if datasourceAscending == true { title = NSLocalizedString("_order_by_size_smallest_", comment: "") }
  421. if datasourceAscending == false { title = NSLocalizedString("_order_by_size_largest_", comment: "") }
  422. default:
  423. title = NSLocalizedString("_order_by_", comment: "") + " " + datasourceSorted
  424. }
  425. trashHeader.buttonOrder.setTitle(title + " ▽", for: .normal)
  426. return trashHeader
  427. } else {
  428. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerMenu", for: indexPath) as! NCTrashFooterMenu
  429. trashFooter.labelFooter.textColor = NCBrandColor.sharedInstance.icon
  430. var folders: Int = 0, foldersText = ""
  431. var files: Int = 0, filesText = ""
  432. var size: Double = 0
  433. for record: tableTrash in self.datasource {
  434. if record.directory {
  435. folders += 1
  436. } else {
  437. files += 1
  438. size = size + record.size
  439. }
  440. }
  441. if folders > 1 {
  442. foldersText = "\(folders) " + NSLocalizedString("_folders_", comment: "")
  443. } else if folders == 1 {
  444. foldersText = "1 " + NSLocalizedString("_folder_", comment: "")
  445. }
  446. if files > 1 {
  447. filesText = "\(files) " + NSLocalizedString("_files_", comment: "") + " " + CCUtility.transformedSize(size)
  448. } else if files == 1 {
  449. filesText = "1 " + NSLocalizedString("_file_", comment: "") + " " + CCUtility.transformedSize(size)
  450. }
  451. if foldersText == "" {
  452. trashFooter.labelFooter.text = filesText
  453. } else if filesText == "" {
  454. trashFooter.labelFooter.text = foldersText
  455. } else {
  456. trashFooter.labelFooter.text = foldersText + ", " + filesText
  457. }
  458. return trashFooter
  459. }
  460. }
  461. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  462. return CGSize(width: collectionView.frame.width, height: highHeader)
  463. }
  464. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  465. return CGSize(width: collectionView.frame.width, height: highHeader)
  466. }
  467. func numberOfSections(in collectionView: UICollectionView) -> Int {
  468. return 1
  469. }
  470. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  471. return datasource.count
  472. }
  473. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  474. let tableTrash = datasource[indexPath.item]
  475. var image: UIImage?
  476. if tableTrash.iconName.count > 0 {
  477. image = UIImage.init(named: tableTrash.iconName)
  478. } else {
  479. image = UIImage.init(named: "file")
  480. }
  481. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  482. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  483. } else {
  484. if tableTrash.thumbnailExists && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  485. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  486. }
  487. }
  488. if collectionView.collectionViewLayout == listLayout {
  489. // LIST
  490. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCTrashListCell
  491. cell.delegate = self
  492. cell.fileID = tableTrash.fileID
  493. cell.indexPath = indexPath
  494. cell.labelTitle.text = tableTrash.trashbinFileName
  495. if tableTrash.directory {
  496. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  497. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  498. } else {
  499. cell.imageItem.image = image
  500. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + " " + CCUtility.transformedSize(tableTrash.size)
  501. }
  502. if isEditMode {
  503. cell.imageItemLeftConstraint.constant = 45
  504. cell.imageSelect.isHidden = false
  505. if selectFileID.contains(tableTrash.fileID) {
  506. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  507. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  508. } else {
  509. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  510. cell.backgroundView = nil
  511. }
  512. } else {
  513. cell.imageItemLeftConstraint.constant = 10
  514. cell.imageSelect.isHidden = true
  515. cell.backgroundView = nil
  516. }
  517. return cell
  518. } else {
  519. // GRID
  520. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCTrashGridCell
  521. cell.delegate = self
  522. cell.fileID = tableTrash.fileID
  523. cell.indexPath = indexPath
  524. cell.labelTitle.text = tableTrash.trashbinFileName
  525. if tableTrash.directory {
  526. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  527. } else {
  528. cell.imageItem.image = image
  529. }
  530. if isEditMode {
  531. cell.imageSelect.isHidden = false
  532. if selectFileID.contains(tableTrash.fileID) {
  533. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  534. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  535. } else {
  536. cell.imageSelect.isHidden = true
  537. cell.backgroundView = nil
  538. }
  539. } else {
  540. cell.imageSelect.isHidden = true
  541. cell.backgroundView = nil
  542. }
  543. return cell
  544. }
  545. }
  546. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  547. let tableTrash = datasource[indexPath.item]
  548. if isEditMode {
  549. if let index = selectFileID.index(of: tableTrash.fileID) {
  550. selectFileID.remove(at: index)
  551. } else {
  552. selectFileID.append(tableTrash.fileID)
  553. }
  554. collectionView.reloadItems(at: [indexPath])
  555. return
  556. }
  557. if tableTrash.directory {
  558. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  559. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  560. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  561. self.navigationController?.pushViewController(ncTrash, animated: true)
  562. }
  563. }
  564. // MARK: UTILITY
  565. private func cellBlurEffect(with frame: CGRect) -> UIView {
  566. let blurEffect = UIBlurEffect(style: .extraLight)
  567. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  568. blurEffectView.frame = frame
  569. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  570. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  571. return blurEffectView
  572. }
  573. }
  574. class ListLayout: UICollectionViewFlowLayout {
  575. let itemHeight: CGFloat = 60
  576. override init() {
  577. super.init()
  578. minimumInteritemSpacing = 0
  579. minimumLineSpacing = 1
  580. self.scrollDirection = .vertical
  581. self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
  582. }
  583. required init?(coder aDecoder: NSCoder) {
  584. fatalError("init(coder:) has not been implemented")
  585. }
  586. override var itemSize: CGSize {
  587. get {
  588. if let collectionView = collectionView {
  589. let itemWidth: CGFloat = collectionView.frame.width
  590. return CGSize(width: itemWidth, height: self.itemHeight)
  591. }
  592. // Default fallback
  593. return CGSize(width: 100, height: 100)
  594. }
  595. set {
  596. super.itemSize = newValue
  597. }
  598. }
  599. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  600. return proposedContentOffset
  601. }
  602. }
  603. class GridLayout: UICollectionViewFlowLayout {
  604. let heightLabelPlusButton: CGFloat = 45
  605. override init() {
  606. super.init()
  607. minimumInteritemSpacing = 0
  608. minimumLineSpacing = 1
  609. self.scrollDirection = .vertical
  610. self.sectionInset = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)
  611. }
  612. required init?(coder aDecoder: NSCoder) {
  613. fatalError("init(coder:) has not been implemented")
  614. }
  615. override var itemSize: CGSize {
  616. get {
  617. if let collectionView = collectionView {
  618. let itemWidth: CGFloat = (collectionView.frame.width/CGFloat(collectionView.bounds.width / 90.0))
  619. let itemHeight: CGFloat = itemWidth + heightLabelPlusButton
  620. return CGSize(width: itemWidth, height: itemHeight)
  621. }
  622. // Default fallback
  623. return CGSize(width: 100, height: 100)
  624. }
  625. set {
  626. super.itemSize = newValue
  627. }
  628. }
  629. override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
  630. return proposedContentOffset
  631. }
  632. }