NCTrash.swift 31 KB

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