NCTrash.swift 32 KB

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