NCTrash.swift 35 KB

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