NCTrash.swift 30 KB

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