NCTrash.swift 39 KB

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