NCTrash.swift 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 NCCommunication
  25. class NCTrash: UIViewController, UIGestureRecognizerDelegate, NCTrashListCellDelegate, NCGridCellDelegate, NCTrashSectionHeaderMenuDelegate, 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 let highHeader: CGFloat = 50
  42. private let refreshControl = UIRefreshControl()
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. // Cell
  46. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  47. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  48. // Header - Footer
  49. collectionView.register(UINib.init(nibName: "NCTrashSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  50. collectionView.register(UINib.init(nibName: "NCTrashSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  51. collectionView.alwaysBounceVertical = true
  52. listLayout = NCListLayout()
  53. gridLayout = NCGridLayout()
  54. // Add Refresh Control
  55. collectionView.refreshControl = refreshControl
  56. // Configure Refresh Control
  57. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  58. // empty Data Source
  59. self.collectionView.emptyDataSetDelegate = self;
  60. self.collectionView.emptyDataSetSource = self;
  61. // changeTheming
  62. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  63. changeTheming()
  64. }
  65. override func viewWillAppear(_ animated: Bool) {
  66. super.viewWillAppear(animated)
  67. self.navigationItem.title = titleCurrentFolder
  68. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_trash)
  69. if typeLayout == k_layout_list {
  70. collectionView.collectionViewLayout = listLayout
  71. } else {
  72. collectionView.collectionViewLayout = gridLayout
  73. }
  74. // Datasource & serverUrl
  75. if path == "" {
  76. let userID = (appDelegate.activeUserID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed)
  77. path = k_dav + "/trashbin/" + userID! + "/trash/"
  78. }
  79. if (datasource.count == 0) {
  80. loadDatasource()
  81. }
  82. loadListingTrash()
  83. }
  84. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  85. super.viewWillTransition(to: size, with: coordinator)
  86. coordinator.animate(alongsideTransition: nil) { _ in
  87. self.collectionView.collectionViewLayout.invalidateLayout()
  88. }
  89. }
  90. @objc func changeTheming() {
  91. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  92. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  93. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  94. }
  95. // MARK: DZNEmpty
  96. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  97. return NCBrandColor.sharedInstance.backgroundView
  98. }
  99. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  100. return CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 300, height: 300, color: NCBrandColor.sharedInstance.graySoft)
  101. }
  102. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  103. let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
  104. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  105. return NSAttributedString.init(string: text, attributes: attributes)
  106. }
  107. func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  108. let text = "\n"+NSLocalizedString("_trash_no_trash_description_", comment: "")
  109. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  110. return NSAttributedString.init(string: text, attributes: attributes)
  111. }
  112. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  113. return true
  114. }
  115. func reloadSortOrder(){
  116. NCUtility.sharedInstance.setLayoutForView(
  117. key: k_layout_view_trash,
  118. layout: self.typeLayout,
  119. sort: self.datasourceSorted,
  120. ascending: self.datasourceAscending,
  121. groupBy: self.datasourceGroupBy,
  122. directoryOnTop: self.datasourceDirectoryOnTop)
  123. self.loadDatasource()
  124. }
  125. // MARK: TAP EVENT
  126. func tapSwitchHeaderMenu(sender: Any) {
  127. if collectionView.collectionViewLayout == gridLayout {
  128. // list layout
  129. UIView.animate(withDuration: 0.0, animations: {
  130. self.collectionView.collectionViewLayout.invalidateLayout()
  131. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  132. self.collectionView.reloadData()
  133. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  134. })
  135. })
  136. typeLayout = k_layout_list
  137. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  138. } else {
  139. // grid layout
  140. UIView.animate(withDuration: 0.0, animations: {
  141. self.collectionView.collectionViewLayout.invalidateLayout()
  142. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  143. self.collectionView.reloadData()
  144. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  145. })
  146. })
  147. typeLayout = k_layout_grid
  148. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_trash, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  149. }
  150. }
  151. func tapOrderHeaderMenu(sender: Any) {
  152. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  153. var actions = [NCMenuAction]()
  154. actions.append(
  155. NCMenuAction(
  156. title: NSLocalizedString("_order_by_name_a_z_", comment: ""),
  157. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameAZ"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  158. onTitle: NSLocalizedString("_order_by_name_z_a_", comment: ""),
  159. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortFileNameZA"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  160. selected: datasourceSorted == "fileName",
  161. on: datasourceAscending,
  162. action: { menuAction in
  163. if(self.datasourceSorted == "fileName"){
  164. self.datasourceAscending = !self.datasourceAscending
  165. } else {
  166. self.datasourceSorted = "fileName"
  167. }
  168. self.reloadSortOrder()
  169. }
  170. )
  171. )
  172. actions.append(
  173. NCMenuAction(
  174. title: NSLocalizedString("_order_by_date_more_recent_", comment: ""),
  175. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateMoreRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  176. onTitle: NSLocalizedString("_order_by_date_less_recent_", comment: ""),
  177. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortDateLessRecent"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  178. selected: datasourceSorted == "date",
  179. on: datasourceAscending,
  180. action: { menuAction in
  181. if(self.datasourceSorted == "date"){
  182. self.datasourceAscending = !self.datasourceAscending
  183. } else {
  184. self.datasourceSorted = "date"
  185. }
  186. self.reloadSortOrder()
  187. }
  188. )
  189. )
  190. actions.append(
  191. NCMenuAction(
  192. title: NSLocalizedString("_order_by_size_smallest_", comment: ""),
  193. icon: CCGraphics.changeThemingColorImage(UIImage(named: "sortSmallest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  194. onTitle: NSLocalizedString("_order_by_size_largest_", comment: ""),
  195. onIcon: CCGraphics.changeThemingColorImage(UIImage(named: "sortLargest"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  196. selected: datasourceSorted == "size",
  197. on: datasourceAscending,
  198. action: { menuAction in
  199. if(self.datasourceSorted == "size"){
  200. self.datasourceAscending = !self.datasourceAscending
  201. } else {
  202. self.datasourceSorted = "size"
  203. }
  204. self.reloadSortOrder()
  205. }
  206. )
  207. )
  208. mainMenuViewController.actions = actions
  209. let menuPanelController = NCMenuPanelController()
  210. menuPanelController.parentPresenter = self
  211. menuPanelController.delegate = mainMenuViewController
  212. menuPanelController.set(contentViewController: mainMenuViewController)
  213. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  214. self.present(menuPanelController, animated: true, completion: nil)
  215. }
  216. func tapMoreHeaderMenu(sender: Any) {
  217. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  218. var actions = [NCMenuAction]()
  219. if isEditMode {
  220. actions.append(
  221. NCMenuAction(
  222. title: NSLocalizedString("_trash_delete_selected_", comment: ""),
  223. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  224. action: { menuAction in
  225. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
  226. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
  227. for ocId in self.selectocId {
  228. self.deleteItem(with: ocId)
  229. }
  230. self.isEditMode = false
  231. self.selectocId.removeAll()
  232. self.collectionView.reloadData()
  233. }))
  234. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  235. }))
  236. self.present(alert, animated: true, completion: nil)
  237. }
  238. )
  239. )
  240. } else {
  241. actions.append(
  242. NCMenuAction(
  243. title: NSLocalizedString("_trash_delete_all_", comment: ""),
  244. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  245. action: { menuAction in
  246. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_all_", comment: ""), message: "", preferredStyle: .alert)
  247. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
  248. self.emptyTrash()
  249. }))
  250. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  251. }))
  252. self.present(alert, animated: true, completion: nil)
  253. }
  254. )
  255. )
  256. }
  257. mainMenuViewController.actions = actions
  258. let menuPanelController = NCMenuPanelController()
  259. menuPanelController.parentPresenter = self
  260. menuPanelController.delegate = mainMenuViewController
  261. menuPanelController.set(contentViewController: mainMenuViewController)
  262. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  263. self.present(menuPanelController, animated: true, completion: nil)
  264. }
  265. func tapRestoreListItem(with ocId: String, sender: Any) {
  266. if !isEditMode {
  267. restoreItem(with: ocId)
  268. } else {
  269. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  270. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  271. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  272. }
  273. }
  274. func tapMoreListItem(with objectId: String, sender: Any) {
  275. if !isEditMode {
  276. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  277. var actions = [NCMenuAction]()
  278. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.activeAccount) else {
  279. return
  280. }
  281. var iconHeader: UIImage!
  282. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
  283. iconHeader = icon
  284. } else {
  285. if(tableTrash.directory) {
  286. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  287. } else {
  288. iconHeader = UIImage(named: tableTrash.iconName)
  289. }
  290. }
  291. actions.append(
  292. NCMenuAction(
  293. title: tableTrash.trashbinFileName,
  294. icon: iconHeader,
  295. action: nil
  296. )
  297. )
  298. actions.append(
  299. NCMenuAction(
  300. title: NSLocalizedString("_delete_", comment: ""),
  301. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  302. action: { menuAction in
  303. self.deleteItem(with: objectId)
  304. }
  305. )
  306. )
  307. actions.append(
  308. NCMenuAction(
  309. title: NSLocalizedString("_cancel_", comment: ""),
  310. icon: CCGraphics.changeThemingColorImage(UIImage(named: "cancel"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  311. action: { menuAction in }
  312. )
  313. )
  314. mainMenuViewController.actions = actions
  315. let menuPanelController = NCMenuPanelController()
  316. menuPanelController.parentPresenter = self
  317. menuPanelController.delegate = mainMenuViewController
  318. menuPanelController.set(contentViewController: mainMenuViewController)
  319. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  320. self.present(menuPanelController, animated: true, completion: nil)
  321. } else {
  322. let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
  323. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  324. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  325. }
  326. }
  327. func tapMoreGridItem(with objectId: String, sender: Any) {
  328. if !isEditMode {
  329. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  330. var actions = [NCMenuAction]()
  331. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.activeAccount) else {
  332. return
  333. }
  334. var iconHeader: UIImage!
  335. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
  336. iconHeader = icon
  337. } else {
  338. if(tableTrash.directory) {
  339. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  340. } else {
  341. iconHeader = UIImage(named: tableTrash.iconName)
  342. }
  343. }
  344. actions.append(
  345. NCMenuAction(
  346. title: tableTrash.trashbinFileName,
  347. icon: iconHeader,
  348. action: nil
  349. )
  350. )
  351. actions.append(
  352. NCMenuAction(
  353. title: NSLocalizedString("_restore_", comment: ""),
  354. icon: CCGraphics.changeThemingColorImage(UIImage(named: "restore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  355. action: { menuAction in
  356. self.restoreItem(with: objectId)
  357. }
  358. )
  359. )
  360. actions.append(
  361. NCMenuAction(
  362. title: NSLocalizedString("_delete_", comment: ""),
  363. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  364. action: { menuAction in
  365. self.deleteItem(with: objectId)
  366. }
  367. )
  368. )
  369. actions.append(
  370. NCMenuAction(
  371. title: NSLocalizedString("_cancel_", comment: ""),
  372. icon: CCGraphics.changeThemingColorImage(UIImage(named: "cancel"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  373. action: { menuAction in }
  374. )
  375. )
  376. mainMenuViewController.actions = actions
  377. let menuPanelController = NCMenuPanelController()
  378. menuPanelController.parentPresenter = self
  379. menuPanelController.delegate = mainMenuViewController
  380. menuPanelController.set(contentViewController: mainMenuViewController)
  381. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  382. self.present(menuPanelController, animated: true, completion: nil)
  383. } else {
  384. let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
  385. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  386. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  387. }
  388. }
  389. }
  390. // MARK: - Collection View
  391. extension NCTrash: UICollectionViewDelegate {
  392. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  393. let tableTrash = datasource[indexPath.item]
  394. if isEditMode {
  395. if let index = selectocId.firstIndex(of: tableTrash.fileId) {
  396. selectocId.remove(at: index)
  397. } else {
  398. selectocId.append(tableTrash.fileId)
  399. }
  400. collectionView.reloadItems(at: [indexPath])
  401. return
  402. }
  403. if tableTrash.directory {
  404. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  405. ncTrash.path = tableTrash.filePath + tableTrash.fileName
  406. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  407. self.navigationController?.pushViewController(ncTrash, animated: true)
  408. }
  409. }
  410. }
  411. extension NCTrash: UICollectionViewDataSource {
  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.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  422. trashHeader.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  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 numberOfSections(in collectionView: UICollectionView) -> Int {
  433. return 1
  434. }
  435. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  436. return datasource.count
  437. }
  438. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  439. let tableTrash = datasource[indexPath.item]
  440. var image: UIImage?
  441. if tableTrash.iconName.count > 0 {
  442. image = UIImage.init(named: tableTrash.iconName)
  443. } else {
  444. image = UIImage.init(named: "file")
  445. }
  446. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)) {
  447. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName))
  448. } else {
  449. if tableTrash.hasPreview && !CCUtility.fileProviderStorageIconExists(tableTrash.fileId, fileNameView: tableTrash.fileName) {
  450. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  451. }
  452. }
  453. if collectionView.collectionViewLayout == listLayout {
  454. // LIST
  455. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  456. cell.delegate = self
  457. cell.objectId = tableTrash.fileId
  458. cell.indexPath = indexPath
  459. cell.labelTitle.text = tableTrash.trashbinFileName
  460. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  461. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  462. if tableTrash.directory {
  463. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  464. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  465. } else {
  466. cell.imageItem.image = image
  467. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  468. }
  469. if isEditMode {
  470. cell.imageItemLeftConstraint.constant = 45
  471. cell.imageSelect.isHidden = false
  472. if selectocId.contains(tableTrash.fileId) {
  473. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  474. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  475. } else {
  476. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  477. cell.backgroundView = nil
  478. }
  479. } else {
  480. cell.imageItemLeftConstraint.constant = 10
  481. cell.imageSelect.isHidden = true
  482. cell.backgroundView = nil
  483. }
  484. return cell
  485. } else {
  486. // GRID
  487. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  488. cell.delegate = self
  489. cell.objectId = tableTrash.fileId
  490. cell.indexPath = indexPath
  491. cell.labelTitle.text = tableTrash.trashbinFileName
  492. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  493. if tableTrash.directory {
  494. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  495. } else {
  496. cell.imageItem.image = image
  497. }
  498. if isEditMode {
  499. cell.imageSelect.isHidden = false
  500. if selectocId.contains(tableTrash.fileId) {
  501. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  502. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  503. } else {
  504. cell.imageSelect.isHidden = true
  505. cell.backgroundView = nil
  506. }
  507. } else {
  508. cell.imageSelect.isHidden = true
  509. cell.backgroundView = nil
  510. }
  511. return cell
  512. }
  513. }
  514. }
  515. extension NCTrash: UICollectionViewDelegateFlowLayout {
  516. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  517. return CGSize(width: collectionView.frame.width, height: highHeader)
  518. }
  519. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  520. return CGSize(width: collectionView.frame.width, height: highHeader)
  521. }
  522. }
  523. // MARK: - NC API & Algorithm
  524. extension NCTrash {
  525. @objc func loadDatasource() {
  526. datasource.removeAll()
  527. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: path, sorted: datasourceSorted, ascending: datasourceAscending, account: appDelegate.activeAccount) else {
  528. return
  529. }
  530. datasource = tashItems
  531. reloadDataThenPerform {
  532. // GoTo ocId
  533. if self.blinkocId != nil {
  534. for item in 0...self.datasource.count-1 {
  535. if self.datasource[item].fileId.contains(self.blinkocId!) {
  536. let indexPath = IndexPath(item: item, section: 0)
  537. self.collectionView.scrollToItem(at: indexPath, at: .top, animated: false)
  538. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  539. self.blinkocId = nil
  540. NCUtility.sharedInstance.blink(cell: self.collectionView.cellForItem(at: indexPath))
  541. }
  542. }
  543. }
  544. }
  545. }
  546. }
  547. @objc func loadListingTrash() {
  548. OCNetworking.sharedManager().listingTrash(withAccount: appDelegate.activeAccount, path: path, serverUrl: appDelegate.activeUrl, depth: "1", completion: { (account, item, message, errorCode) in
  549. self.refreshControl.endRefreshing()
  550. if errorCode == 0 && account == self.appDelegate.activeAccount {
  551. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.path, account: self.appDelegate.activeAccount)
  552. NCManageDatabase.sharedInstance.addTrashs(item as! [tableTrash])
  553. } else if errorCode != 0 {
  554. NCContentPresenter.shared.messageNotification("_error_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  555. } else {
  556. print("[LOG] It has been changed user during networking process, error.")
  557. }
  558. self.loadDatasource()
  559. })
  560. }
  561. func reloadDataThenPerform(_ closure: @escaping (() -> Void)) {
  562. CATransaction.begin()
  563. CATransaction.setCompletionBlock(closure)
  564. collectionView.reloadData()
  565. CATransaction.commit()
  566. }
  567. func restoreItem(with fileId: String) {
  568. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
  569. return
  570. }
  571. let fileNameFrom = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  572. let fileNameTo = appDelegate.activeUrl + k_dav + "/trashbin/" + appDelegate.activeUserID + "/restore/" + tableTrash.fileName
  573. NCCommunication.sharedInstance.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, account: appDelegate.activeAccount) { (account, errorCode, errorDescription) in
  574. if errorCode == 0 && account == self.appDelegate.activeAccount {
  575. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
  576. self.loadDatasource()
  577. } else if errorCode != 0 {
  578. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  579. } else {
  580. print("[LOG] It has been changed user during networking process, error.")
  581. }
  582. }
  583. }
  584. func emptyTrash() {
  585. OCNetworking.sharedManager().emptyTrash(withAccount: appDelegate.activeAccount, completion: { (account, message, errorCode) in
  586. if errorCode == 0 && account == self.appDelegate.activeAccount {
  587. NCManageDatabase.sharedInstance.deleteTrash(fileId: nil, account: self.appDelegate.activeAccount)
  588. } else if errorCode != 0 {
  589. NCContentPresenter.shared.messageNotification("_error_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  590. } else {
  591. print("[LOG] It has been changed user during networking process, error.")
  592. }
  593. self.loadDatasource()
  594. })
  595. }
  596. func deleteItem(with fileId: String) {
  597. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.activeAccount) else {
  598. return
  599. }
  600. let serverUrlFileName = appDelegate.activeUrl + tableTrash.filePath + tableTrash.fileName
  601. NCCommunication.sharedInstance.deleteFileOrFolder(serverUrlFileName, account: appDelegate.activeAccount) { (account, errorCode, errorDescription) in
  602. if errorCode == 0 && account == self.appDelegate.activeAccount {
  603. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
  604. self.loadDatasource()
  605. } else if errorCode != 0 {
  606. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  607. } else {
  608. print("[LOG] It has been changed user during networking process, error.")
  609. }
  610. }
  611. }
  612. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  613. let fileNameLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, fileNameView: tableTrash.fileName)!
  614. NCCommunication.sharedInstance.downloadPreviewTrash(serverUrl: appDelegate.activeUrl, fileId: tableTrash.fileId, fileNameLocalPath: fileNameLocalPath, width: CGFloat(k_sizePreview), height: CGFloat(k_sizePreview), account: appDelegate.activeAccount) { (account, data, errorCode, errorDescription) in
  615. if errorCode == 0 && data != nil && account == self.appDelegate.activeAccount {
  616. if let cell = self.collectionView.cellForItem(at: indexPath), let image = UIImage.init(data: data!) {
  617. if cell is NCTrashListCell {
  618. (cell as! NCTrashListCell).imageItem.image = image
  619. } else if cell is NCGridCell {
  620. (cell as! NCGridCell).imageItem.image = image
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }