NCTrash.swift 34 KB

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