NCTrash.swift 30 KB

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