NCTrash.swift 31 KB

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