NCTrash.swift 35 KB

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