NCTrash.swift 34 KB

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