NCTrash.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import Sheeeeeeeeet
  25. class NCTrash: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCTrashListCellDelegate, NCGridCellDelegate, NCTrashSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  27. var path = ""
  28. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  29. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. private var isEditMode = false
  31. private var selectFileID = [String]()
  32. private var datasource = [tableTrash]()
  33. private var typeLayout = ""
  34. private var datasourceSorted = ""
  35. private var datasourceAscending = true
  36. private var datasourceGroupBy = "none"
  37. private var datasourceDirectoryOnTop = false
  38. private var listLayout: NCListLayout!
  39. private var gridLayout: NCGridLayout!
  40. private var actionSheet: ActionSheet?
  41. private let highHeader: CGFloat = 50
  42. private let refreshControl = UIRefreshControl()
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. // Cell
  46. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  47. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  48. // Header - Footer
  49. collectionView.register(UINib.init(nibName: "NCTrashSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  50. collectionView.register(UINib.init(nibName: "NCTrashSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  51. collectionView.alwaysBounceVertical = true
  52. listLayout = NCListLayout()
  53. gridLayout = NCGridLayout()
  54. // Add Refresh Control
  55. collectionView.refreshControl = refreshControl
  56. // Configure Refresh Control
  57. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  58. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  59. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  60. // empty Data Source
  61. self.collectionView.emptyDataSetDelegate = self;
  62. self.collectionView.emptyDataSetSource = self;
  63. }
  64. override func viewWillAppear(_ animated: Bool) {
  65. super.viewWillAppear(animated)
  66. // Color
  67. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  68. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  69. self.navigationItem.title = titleCurrentFolder
  70. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_trash)
  71. if typeLayout == k_layout_list {
  72. collectionView.collectionViewLayout = listLayout
  73. } else {
  74. collectionView.collectionViewLayout = gridLayout
  75. }
  76. loadDatasource()
  77. loadListingTrash()
  78. }
  79. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  80. super.viewWillTransition(to: size, with: coordinator)
  81. coordinator.animate(alongsideTransition: nil) { _ in
  82. self.collectionView.collectionViewLayout.invalidateLayout()
  83. self.actionSheet?.viewDidLayoutSubviews()
  84. }
  85. }
  86. // MARK: DZNEmpty
  87. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  88. return NCBrandColor.sharedInstance.backgroundView
  89. }
  90. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  91. return CCGraphics.changeThemingColorImage(UIImage.init(named: "trashNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.graySoft)
  92. }
  93. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  94. let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
  95. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  96. return NSAttributedString.init(string: text, attributes: attributes)
  97. }
  98. func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  99. let text = "\n"+NSLocalizedString("_trash_no_trash_description_", comment: "")
  100. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  101. return NSAttributedString.init(string: text, attributes: attributes)
  102. }
  103. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  104. return true
  105. }
  106. // MARK: TAP EVENT
  107. func tapSwitchHeaderMenu(sender: Any) {
  108. if collectionView.collectionViewLayout == gridLayout {
  109. // list layout
  110. UIView.animate(withDuration: 0.0, animations: {
  111. self.collectionView.collectionViewLayout.invalidateLayout()
  112. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  113. self.collectionView.reloadData()
  114. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  115. })
  116. })
  117. typeLayout = k_layout_list
  118. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  119. } else {
  120. // grid layout
  121. UIView.animate(withDuration: 0.0, animations: {
  122. self.collectionView.collectionViewLayout.invalidateLayout()
  123. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  124. self.collectionView.reloadData()
  125. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  126. })
  127. })
  128. typeLayout = k_layout_grid
  129. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  130. }
  131. }
  132. func tapOrderHeaderMenu(sender: Any) {
  133. var menuView: DropdownMenu?
  134. var selectedRow = 0
  135. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  136. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  137. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  138. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  139. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  140. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  141. switch datasourceSorted {
  142. case "fileName":
  143. if datasourceAscending == true { item1.style = .highlight; selectedRow = 0 }
  144. if datasourceAscending == false { item2.style = .highlight; selectedRow = 1 }
  145. case "date":
  146. if datasourceAscending == false { item3.style = .highlight; selectedRow = 2 }
  147. if datasourceAscending == true { item4.style = .highlight; selectedRow = 3 }
  148. case "size":
  149. if datasourceAscending == true { item5.style = .highlight; selectedRow = 4 }
  150. if datasourceAscending == false { item6.style = .highlight; selectedRow = 5 }
  151. default:
  152. print("")
  153. }
  154. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item1, item2, item3, item4, item5, item6], selectedRow: selectedRow)
  155. menuView?.token = "tapOrderHeaderMenu"
  156. menuView?.delegate = self
  157. menuView?.rowHeight = 45
  158. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  159. menuView?.tableView.alwaysBounceVertical = false
  160. menuView?.tableViewBackgroundColor = UIColor.white
  161. let header = (sender as? UIButton)?.superview as! NCTrashSectionHeaderMenu
  162. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  163. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  164. menuView?.topOffsetY = CGFloat(menuOffsetY)
  165. menuView?.showMenu()
  166. }
  167. func tapMoreHeaderMenu(sender: Any) {
  168. var menuView: DropdownMenu?
  169. if isEditMode {
  170. //let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  171. //let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_selected_", comment: ""))
  172. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_selected_", comment: ""))
  173. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item2], selectedRow: -1)
  174. menuView?.token = "tapMoreHeaderMenuSelect"
  175. } else {
  176. //let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  177. //let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  178. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  179. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item2], selectedRow: -1)
  180. menuView?.token = "tapMoreHeaderMenu"
  181. }
  182. menuView?.delegate = self
  183. menuView?.rowHeight = 45
  184. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  185. menuView?.tableView.alwaysBounceVertical = false
  186. menuView?.tableViewBackgroundColor = UIColor.white
  187. let header = (sender as? UIButton)?.superview as! NCTrashSectionHeaderMenu
  188. let headerRect = self.collectionView.convert(header.bounds, from: self.view)
  189. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  190. menuView?.topOffsetY = CGFloat(menuOffsetY)
  191. menuView?.showMenu()
  192. }
  193. func tapRestoreListItem(with fileID: String, sender: Any) {
  194. if !isEditMode {
  195. restoreItem(with: fileID)
  196. } else {
  197. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  198. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  199. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  200. }
  201. }
  202. func tapMoreListItem(with fileID: String, sender: Any) {
  203. if !isEditMode {
  204. var items = [ActionSheetItem]()
  205. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  206. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  207. actionSheet = ActionSheet(items: items) { sheet, item in
  208. if item is ActionSheetDangerButton { self.deleteItem(with: fileID) }
  209. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  210. }
  211. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  212. return
  213. }
  214. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: tableTrash.directory, iconName: tableTrash.iconName, fileID: tableTrash.fileID, fileNameView: tableTrash.fileName, text: tableTrash.trashbinFileName)
  215. actionSheet?.headerView = headerView
  216. actionSheet?.headerView?.frame.size.height = 50
  217. actionSheet?.present(in: self, from: sender as! UIButton)
  218. } else {
  219. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  220. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  221. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  222. }
  223. }
  224. func tapMoreGridItem(with fileID: String, sender: Any) {
  225. if !isEditMode {
  226. var items = [ActionSheetItem]()
  227. let appearanceDelete = ActionSheetItemAppearance.init()
  228. appearanceDelete.textColor = UIColor.red
  229. items.append(ActionSheetItem(title: NSLocalizedString("_restore_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon)))
  230. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  231. itemDelete.customAppearance = appearanceDelete
  232. items.append(itemDelete)
  233. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  234. actionSheet = ActionSheet(items: items) { sheet, item in
  235. if item.value as? Int == 0 { self.restoreItem(with: fileID) }
  236. if item.value as? Int == 1 { self.deleteItem(with: fileID) }
  237. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  238. }
  239. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  240. return
  241. }
  242. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: tableTrash.directory, iconName: tableTrash.iconName, fileID: tableTrash.fileID, fileNameView: tableTrash.fileName, text: tableTrash.trashbinFileName)
  243. actionSheet?.headerView = headerView
  244. actionSheet?.headerView?.frame.size.height = 50
  245. actionSheet?.present(in: self, from: sender as! UIButton)
  246. } else {
  247. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  248. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  249. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  250. }
  251. }
  252. // MARK: DROP-DOWN-MENU
  253. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  254. if dropdownMenu.token == "tapOrderHeaderMenu" {
  255. switch indexPath.row {
  256. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  257. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  258. case 2: datasourceSorted = "date"; datasourceAscending = false
  259. case 3: datasourceSorted = "date"; datasourceAscending = true
  260. case 4: datasourceSorted = "size"; datasourceAscending = true
  261. case 5: datasourceSorted = "size"; datasourceAscending = false
  262. default: print("")
  263. }
  264. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  265. loadDatasource()
  266. }
  267. if dropdownMenu.token == "tapMoreHeaderMenu" {
  268. /*
  269. // Select
  270. if indexPath.row == 0 {
  271. isEditMode = true
  272. collectionView.reloadData()
  273. }
  274. // Restore ALL
  275. if indexPath.row == 1 {
  276. for record: tableTrash in self.datasource {
  277. restoreItem(with: record.fileID)
  278. }
  279. }
  280. */
  281. // Empty Trash
  282. if indexPath.row == 0 {
  283. var items = [ActionSheetItem]()
  284. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_all_", comment: "")))
  285. items.append(ActionSheetDangerButton(title: NSLocalizedString("_ok_", comment: "")))
  286. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  287. actionSheet = ActionSheet(items: items) { sheet, item in
  288. if item is ActionSheetDangerButton {
  289. self.emptyTrash()
  290. //for record: tableTrash in self.datasource {
  291. // self.deleteItem(with: record.fileID)
  292. //}
  293. }
  294. if item is ActionSheetCancelButton { return }
  295. }
  296. actionSheet?.present(in: self, from: self.view)
  297. }
  298. }
  299. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  300. // Cancel
  301. if indexPath.row == 0 {
  302. isEditMode = false
  303. selectFileID.removeAll()
  304. collectionView.reloadData()
  305. }
  306. // Restore selected files
  307. if indexPath.row == 1 {
  308. for fileID in selectFileID {
  309. restoreItem(with: fileID)
  310. }
  311. isEditMode = false
  312. selectFileID.removeAll()
  313. collectionView.reloadData()
  314. }
  315. // Delete selected files
  316. if indexPath.row == 2 {
  317. var items = [ActionSheetItem]()
  318. items.append(ActionSheetTitle(title: NSLocalizedString("_trash_delete_selected_", comment: "")))
  319. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  320. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  321. actionSheet = ActionSheet(items: items) { sheet, item in
  322. if item is ActionSheetDangerButton {
  323. for fileID in self.selectFileID {
  324. self.deleteItem(with: fileID)
  325. }
  326. self.isEditMode = false
  327. self.selectFileID.removeAll()
  328. self.collectionView.reloadData()
  329. }
  330. if item is ActionSheetCancelButton { return }
  331. }
  332. actionSheet?.present(in: self, from: self.view)
  333. }
  334. }
  335. }
  336. /*
  337. func dropdownMenuWillDismiss(_ dropdownMenu: DropdownMenu) {
  338. if dropdownMenu.token == "tapOrderHeaderMenu" {
  339. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  340. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "▽"
  341. trashHeader.buttonOrder.setTitle(title, for: .normal)
  342. }
  343. }
  344. func dropdownMenuWillShow(_ dropdownMenu: DropdownMenu) {
  345. if dropdownMenu.token == "tapOrderHeaderMenu" {
  346. let trashHeader = collectionView.supplementaryView(forElementKind: UICollectionView.elementKindSectionHeader, at: IndexPath(row: 0, section: 0)) as! NCTrashHeaderMenu
  347. let title = String(trashHeader.buttonOrder.title(for: .normal)!.dropLast()) + "△"
  348. trashHeader.buttonOrder.setTitle(title, for: .normal)
  349. }
  350. }
  351. */
  352. // MARK: NC API
  353. @objc func loadListingTrash() {
  354. OCNetworking.sharedManager().listingTrash(withAccount: appDelegate.activeAccount, path: path, serverUrl: appDelegate.activeUrl, completion: { (account, item, message, errorCode) in
  355. self.refreshControl.endRefreshing()
  356. if errorCode == 0 && account == self.appDelegate.activeAccount {
  357. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path, account: self.appDelegate.activeAccount)
  358. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  359. } else if errorCode != 0 {
  360. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  361. }
  362. self.loadDatasource()
  363. })
  364. }
  365. func restoreItem(with fileID: String) {
  366. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  367. return
  368. }
  369. let fileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  370. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  371. OCNetworking.sharedManager().moveFileOrFolder(withAccount: appDelegate.activeAccount, fileName: fileName, fileNameTo: fileNameTo, completion: { (account, message, errorCode) in
  372. if errorCode == 0 && account == self.appDelegate.activeAccount {
  373. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID, account: account!)
  374. self.loadDatasource()
  375. } else if errorCode != 0 {
  376. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  377. }
  378. })
  379. }
  380. func emptyTrash() {
  381. OCNetworking.sharedManager().emptyTrash(withAccount: appDelegate.activeAccount, completion: { (account, message, errorCode) in
  382. if errorCode == 0 && account == self.appDelegate.activeAccount {
  383. NCManageDatabase.sharedInstance.deleteTrash(fileID: nil, account: self.appDelegate.activeAccount)
  384. } else if errorCode != 0 {
  385. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  386. }
  387. self.loadDatasource()
  388. })
  389. }
  390. func deleteItem(with fileID: String) {
  391. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileID: fileID, account: appDelegate.activeAccount) else {
  392. return
  393. }
  394. let path = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  395. OCNetworking.sharedManager().deleteFileOrFolder(withAccount: appDelegate.activeAccount, path: path, completion: { (account, message, errorCode) in
  396. if errorCode == 0 && account == self.appDelegate.activeAccount {
  397. NCManageDatabase.sharedInstance.deleteTrash(fileID: fileID, account: account!)
  398. self.loadDatasource()
  399. } else if errorCode != 0 {
  400. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  401. }
  402. })
  403. }
  404. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  405. OCNetworking.sharedManager().downloadPreviewTrash(withAccount: appDelegate.activeAccount, fileID: tableTrash.fileID, fileName: tableTrash.fileName, completion: { (account, message, errorCode) in
  406. if errorCode == 0 && account == self.appDelegate.activeAccount && CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  407. self.collectionView.reloadItems(at: [indexPath])
  408. }
  409. })
  410. }
  411. // MARK: DATASOURCE
  412. @objc func loadDatasource() {
  413. datasource.removeAll()
  414. if path == "" {
  415. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  416. path = k_dav + "/trashbin/" + userID! + "/trash/"
  417. }
  418. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending, account: appDelegate.activeAccount) else {
  419. return
  420. }
  421. datasource = tashItems
  422. collectionView.reloadData()
  423. }
  424. // MARK: COLLECTIONVIEW METHODS
  425. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  426. if kind == UICollectionView.elementKindSectionHeader {
  427. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
  428. if collectionView.collectionViewLayout == gridLayout {
  429. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  430. } else {
  431. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  432. }
  433. trashHeader.delegate = self
  434. trashHeader.setStatusButton(datasource: datasource)
  435. trashHeader.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  436. return trashHeader
  437. } else {
  438. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
  439. trashFooter.setTitleLabelFooter(datasource: datasource)
  440. return trashFooter
  441. }
  442. }
  443. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  444. return CGSize(width: collectionView.frame.width, height: highHeader)
  445. }
  446. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  447. return CGSize(width: collectionView.frame.width, height: highHeader)
  448. }
  449. func numberOfSections(in collectionView: UICollectionView) -> Int {
  450. return 1
  451. }
  452. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  453. return datasource.count
  454. }
  455. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  456. let tableTrash = datasource[indexPath.item]
  457. var image: UIImage?
  458. if tableTrash.iconName.count > 0 {
  459. image = UIImage.init(named: tableTrash.iconName)
  460. } else {
  461. image = UIImage.init(named: "file")
  462. }
  463. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName)) {
  464. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(tableTrash.fileID, fileNameView: tableTrash.fileName))
  465. } else {
  466. if tableTrash.hasPreview == 1 && !CCUtility.fileProviderStorageIconExists(tableTrash.fileID, fileNameView: tableTrash.fileName) {
  467. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  468. }
  469. }
  470. if collectionView.collectionViewLayout == listLayout {
  471. // LIST
  472. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  473. cell.delegate = self
  474. cell.fileID = tableTrash.fileID
  475. cell.indexPath = indexPath
  476. cell.labelTitle.text = tableTrash.trashbinFileName
  477. if tableTrash.directory {
  478. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  479. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  480. } else {
  481. cell.imageItem.image = image
  482. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  483. }
  484. if isEditMode {
  485. cell.imageItemLeftConstraint.constant = 45
  486. cell.imageSelect.isHidden = false
  487. if selectFileID.contains(tableTrash.fileID) {
  488. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  489. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  490. } else {
  491. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  492. cell.backgroundView = nil
  493. }
  494. } else {
  495. cell.imageItemLeftConstraint.constant = 10
  496. cell.imageSelect.isHidden = true
  497. cell.backgroundView = nil
  498. }
  499. return cell
  500. } else {
  501. // GRID
  502. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  503. cell.delegate = self
  504. cell.fileID = tableTrash.fileID
  505. cell.indexPath = indexPath
  506. cell.labelTitle.text = tableTrash.trashbinFileName
  507. if tableTrash.directory {
  508. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  509. } else {
  510. cell.imageItem.image = image
  511. }
  512. if isEditMode {
  513. cell.imageSelect.isHidden = false
  514. if selectFileID.contains(tableTrash.fileID) {
  515. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  516. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  517. } else {
  518. cell.imageSelect.isHidden = true
  519. cell.backgroundView = nil
  520. }
  521. } else {
  522. cell.imageSelect.isHidden = true
  523. cell.backgroundView = nil
  524. }
  525. return cell
  526. }
  527. }
  528. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  529. let tableTrash = datasource[indexPath.item]
  530. if isEditMode {
  531. if let index = selectFileID.index(of: tableTrash.fileID) {
  532. selectFileID.remove(at: index)
  533. } else {
  534. selectFileID.append(tableTrash.fileID)
  535. }
  536. collectionView.reloadItems(at: [indexPath])
  537. return
  538. }
  539. if tableTrash.directory {
  540. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  541. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  542. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  543. self.navigationController?.pushViewController(ncTrash, animated: true)
  544. }
  545. }
  546. }