NCTrash.swift 35 KB

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