NCTrash.swift 31 KB

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