NCTrash.swift 35 KB

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