NCTrash.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. //
  2. // NCTrash.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NCCommunication
  25. class NCTrash: UIViewController, UIGestureRecognizerDelegate, NCTrashListCellDelegate, NCGridCellDelegate, NCTrashSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet weak var collectionView: UICollectionView!
  27. var trashPath = ""
  28. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  29. var blinkFileId: 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 sort: String = ""
  35. private var ascending: Bool = true
  36. private var directoryOnTop: Bool = true
  37. private var layout = ""
  38. private var groupBy = "none"
  39. private var titleButton = ""
  40. private var itemForLine = 0
  41. private var listLayout: NCListLayout!
  42. private var gridLayout: NCGridLayout!
  43. private let highHeader: CGFloat = 50
  44. private let refreshControl = UIRefreshControl()
  45. required init?(coder aDecoder: NSCoder) {
  46. super.init(coder: aDecoder)
  47. appDelegate.activeTrash = self
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. self.navigationController?.navigationBar.prefersLargeTitles = true
  52. // Cell
  53. collectionView.register(UINib.init(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  54. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  55. // Header - Footer
  56. collectionView.register(UINib.init(nibName: "NCTrashSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  57. collectionView.register(UINib.init(nibName: "NCTrashSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  58. collectionView.alwaysBounceVertical = true
  59. listLayout = NCListLayout()
  60. gridLayout = NCGridLayout()
  61. // Add Refresh Control
  62. collectionView.addSubview(refreshControl)
  63. refreshControl.tintColor = .gray
  64. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  65. // empty Data Source
  66. self.collectionView.emptyDataSetDelegate = self;
  67. self.collectionView.emptyDataSetSource = self;
  68. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  69. changeTheming()
  70. }
  71. override func viewWillAppear(_ animated: Bool) {
  72. super.viewWillAppear(animated)
  73. self.navigationItem.title = titleCurrentFolder
  74. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_trash, serverUrl: "")
  75. gridLayout.itemForLine = CGFloat(itemForLine)
  76. if layout == k_layout_list {
  77. collectionView.collectionViewLayout = listLayout
  78. } else {
  79. collectionView.collectionViewLayout = gridLayout
  80. }
  81. if trashPath == "" {
  82. guard let userID = (appDelegate.userID as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return }
  83. trashPath = appDelegate.urlBase + "/" + NCUtility.shared.getDAV() + "/trashbin/" + userID + "/trash/"
  84. }
  85. reloadDataSource()
  86. }
  87. override func viewDidAppear(_ animated: Bool) {
  88. super.viewDidAppear(animated)
  89. loadListingTrash()
  90. }
  91. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  92. super.viewWillTransition(to: size, with: coordinator)
  93. coordinator.animate(alongsideTransition: nil) { _ in
  94. self.collectionView?.collectionViewLayout.invalidateLayout()
  95. }
  96. }
  97. @objc func changeTheming() {
  98. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  99. }
  100. // MARK: DZNEmpty
  101. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  102. return NCBrandColor.sharedInstance.backgroundView
  103. }
  104. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  105. return CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), width: 300, height: 300, color: .gray)
  106. }
  107. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  108. let text = "\n"+NSLocalizedString("_trash_no_trash_", comment: "")
  109. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.gray]
  110. return NSAttributedString.init(string: text, attributes: attributes)
  111. }
  112. func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  113. let text = "\n"+NSLocalizedString("_trash_no_trash_description_", comment: "")
  114. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  115. return NSAttributedString.init(string: text, attributes: attributes)
  116. }
  117. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  118. return true
  119. }
  120. // MARK: TAP EVENT
  121. func tapSwitchHeaderMenu(sender: Any) {
  122. if collectionView.collectionViewLayout == gridLayout {
  123. // list layout
  124. UIView.animate(withDuration: 0.0, animations: {
  125. self.collectionView.collectionViewLayout.invalidateLayout()
  126. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  127. self.collectionView.reloadData()
  128. })
  129. })
  130. layout = k_layout_list
  131. NCUtility.shared.setLayoutForView(key: k_layout_view_trash, serverUrl: "", layout: layout)
  132. } else {
  133. // grid layout
  134. UIView.animate(withDuration: 0.0, animations: {
  135. self.collectionView.collectionViewLayout.invalidateLayout()
  136. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  137. self.collectionView.reloadData()
  138. })
  139. })
  140. layout = k_layout_grid
  141. NCUtility.shared.setLayoutForView(key: k_layout_view_trash, serverUrl: "", layout: layout)
  142. }
  143. }
  144. func tapOrderHeaderMenu(sender: Any) {
  145. let sortMenu = NCSortMenu()
  146. sortMenu.toggleMenu(viewController: self, key: k_layout_view_trash, sortButton: sender as? UIButton, serverUrl: "", hideDirectoryOnTop: true)
  147. }
  148. func tapMoreHeaderMenu(sender: Any) {
  149. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  150. var actions: [NCMenuAction] = []
  151. if isEditMode {
  152. actions.append(
  153. NCMenuAction(
  154. title: NSLocalizedString("_trash_delete_selected_", comment: ""),
  155. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  156. action: { menuAction in
  157. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_selected_", comment: ""), message: "", preferredStyle: .alert)
  158. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
  159. for ocId in self.selectOcId {
  160. self.deleteItem(with: ocId)
  161. }
  162. self.isEditMode = false
  163. self.selectOcId.removeAll()
  164. self.collectionView.reloadData()
  165. }))
  166. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  167. }))
  168. self.present(alert, animated: true, completion: nil)
  169. }
  170. )
  171. )
  172. } else {
  173. actions.append(
  174. NCMenuAction(
  175. title: NSLocalizedString("_trash_delete_all_", comment: ""),
  176. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  177. action: { menuAction in
  178. let alert = UIAlertController(title: NSLocalizedString("_trash_delete_all_", comment: ""), message: "", preferredStyle: .alert)
  179. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .destructive, handler: { _ in
  180. self.emptyTrash()
  181. }))
  182. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: { _ in
  183. }))
  184. self.present(alert, animated: true, completion: nil)
  185. }
  186. )
  187. )
  188. }
  189. mainMenuViewController.actions = actions
  190. let menuPanelController = NCMenuPanelController()
  191. menuPanelController.parentPresenter = self
  192. menuPanelController.delegate = mainMenuViewController
  193. menuPanelController.set(contentViewController: mainMenuViewController)
  194. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  195. self.present(menuPanelController, animated: true, completion: nil)
  196. }
  197. func tapRestoreListItem(with ocId: String, sender: Any) {
  198. if !isEditMode {
  199. restoreItem(with: ocId)
  200. } else {
  201. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  202. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  203. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  204. }
  205. }
  206. func tapMoreListItem(with objectId: String, sender: Any) {
  207. if !isEditMode {
  208. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  209. var actions: [NCMenuAction] = []
  210. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.account) else {
  211. return
  212. }
  213. var iconHeader: UIImage!
  214. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  215. iconHeader = icon
  216. } else {
  217. if(tableTrash.directory) {
  218. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  219. } else {
  220. iconHeader = UIImage(named: tableTrash.iconName)
  221. }
  222. }
  223. actions.append(
  224. NCMenuAction(
  225. title: tableTrash.trashbinFileName,
  226. icon: iconHeader,
  227. action: nil
  228. )
  229. )
  230. actions.append(
  231. NCMenuAction(
  232. title: NSLocalizedString("_delete_", comment: ""),
  233. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  234. action: { menuAction in
  235. self.deleteItem(with: objectId)
  236. }
  237. )
  238. )
  239. mainMenuViewController.actions = actions
  240. let menuPanelController = NCMenuPanelController()
  241. menuPanelController.parentPresenter = self
  242. menuPanelController.delegate = mainMenuViewController
  243. menuPanelController.set(contentViewController: mainMenuViewController)
  244. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  245. self.present(menuPanelController, animated: true, completion: nil)
  246. } else {
  247. let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
  248. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  249. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  250. }
  251. }
  252. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  253. if !isEditMode {
  254. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  255. var actions: [NCMenuAction] = []
  256. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: objectId, account: appDelegate.account) else {
  257. return
  258. }
  259. var iconHeader: UIImage!
  260. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  261. iconHeader = icon
  262. } else {
  263. if(tableTrash.directory) {
  264. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  265. } else {
  266. iconHeader = UIImage(named: tableTrash.iconName)
  267. }
  268. }
  269. actions.append(
  270. NCMenuAction(
  271. title: tableTrash.trashbinFileName,
  272. icon: iconHeader,
  273. action: nil
  274. )
  275. )
  276. actions.append(
  277. NCMenuAction(
  278. title: NSLocalizedString("_restore_", comment: ""),
  279. icon: CCGraphics.changeThemingColorImage(UIImage(named: "restore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  280. action: { menuAction in
  281. self.restoreItem(with: objectId)
  282. }
  283. )
  284. )
  285. actions.append(
  286. NCMenuAction(
  287. title: NSLocalizedString("_delete_", comment: ""),
  288. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  289. action: { menuAction in
  290. self.deleteItem(with: objectId)
  291. }
  292. )
  293. )
  294. mainMenuViewController.actions = actions
  295. let menuPanelController = NCMenuPanelController()
  296. menuPanelController.parentPresenter = self
  297. menuPanelController.delegate = mainMenuViewController
  298. menuPanelController.set(contentViewController: mainMenuViewController)
  299. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  300. self.present(menuPanelController, animated: true, completion: nil)
  301. } else {
  302. let buttonPosition: CGPoint = (sender as! UIButton).convert(CGPoint.zero, to: collectionView)
  303. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  304. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  305. }
  306. }
  307. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  308. }
  309. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  310. }
  311. }
  312. // MARK: - Collection View
  313. extension NCTrash: UICollectionViewDelegate {
  314. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  315. let tableTrash = datasource[indexPath.item]
  316. if isEditMode {
  317. if let index = selectOcId.firstIndex(of: tableTrash.fileId) {
  318. selectOcId.remove(at: index)
  319. } else {
  320. selectOcId.append(tableTrash.fileId)
  321. }
  322. collectionView.reloadItems(at: [indexPath])
  323. return
  324. }
  325. if tableTrash.directory {
  326. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  327. ncTrash.trashPath = tableTrash.filePath + tableTrash.fileName
  328. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  329. self.navigationController?.pushViewController(ncTrash, animated: true)
  330. }
  331. }
  332. }
  333. extension NCTrash: UICollectionViewDataSource {
  334. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  335. if kind == UICollectionView.elementKindSectionHeader {
  336. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
  337. if collectionView.collectionViewLayout == gridLayout {
  338. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  339. } else {
  340. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  341. }
  342. trashHeader.delegate = self
  343. trashHeader.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  344. trashHeader.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  345. trashHeader.setStatusButton(datasource: datasource)
  346. trashHeader.setTitleSorted(datasourceTitleButton: titleButton)
  347. return trashHeader
  348. } else {
  349. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
  350. trashFooter.setTitleLabelFooter(datasource: datasource)
  351. return trashFooter
  352. }
  353. }
  354. func numberOfSections(in collectionView: UICollectionView) -> Int {
  355. return 1
  356. }
  357. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  358. return datasource.count
  359. }
  360. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  361. let tableTrash = datasource[indexPath.item]
  362. var image: UIImage?
  363. if tableTrash.iconName.count > 0 {
  364. image = UIImage.init(named: tableTrash.iconName)
  365. } else {
  366. image = UIImage.init(named: "file")
  367. }
  368. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  369. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  370. } else {
  371. if tableTrash.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  372. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  373. }
  374. }
  375. if collectionView.collectionViewLayout == listLayout {
  376. // LIST
  377. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  378. cell.delegate = self
  379. cell.objectId = tableTrash.fileId
  380. cell.indexPath = indexPath
  381. cell.labelTitle.text = tableTrash.trashbinFileName
  382. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  383. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  384. if tableTrash.directory {
  385. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  386. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  387. } else {
  388. cell.imageItem.image = image
  389. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  390. }
  391. if isEditMode {
  392. cell.selectMode(true)
  393. if selectOcId.contains(tableTrash.fileId) {
  394. cell.selected(true)
  395. } else {
  396. cell.selected(false)
  397. }
  398. } else {
  399. cell.selectMode(false)
  400. }
  401. return cell
  402. } else {
  403. // GRID
  404. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  405. cell.delegate = self
  406. cell.objectId = tableTrash.fileId
  407. cell.indexPath = indexPath
  408. cell.labelTitle.text = tableTrash.trashbinFileName
  409. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  410. if tableTrash.directory {
  411. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  412. } else {
  413. cell.imageItem.image = image
  414. }
  415. if isEditMode {
  416. cell.imageSelect.isHidden = false
  417. if selectOcId.contains(tableTrash.fileId) {
  418. cell.selected(true)
  419. } else {
  420. cell.selected(false)
  421. }
  422. } else {
  423. cell.imageSelect.isHidden = true
  424. cell.backgroundView = nil
  425. }
  426. return cell
  427. }
  428. }
  429. }
  430. extension NCTrash: UICollectionViewDelegateFlowLayout {
  431. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  432. return CGSize(width: collectionView.frame.width, height: highHeader)
  433. }
  434. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  435. return CGSize(width: collectionView.frame.width, height: highHeader)
  436. }
  437. }
  438. // MARK: - NC API & Algorithm
  439. extension NCTrash {
  440. @objc func reloadDataSource() {
  441. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_trash, serverUrl: "")
  442. datasource.removeAll()
  443. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: trashPath, sort: sort, ascending: ascending, account: appDelegate.account) else {
  444. return
  445. }
  446. datasource = tashItems
  447. collectionView.reloadData()
  448. if self.blinkFileId != nil {
  449. for item in 0...self.datasource.count-1 {
  450. if self.datasource[item].fileId.contains(self.blinkFileId!) {
  451. let indexPath = IndexPath(item: item, section: 0)
  452. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  453. UIView.animate(withDuration: 0.3) {
  454. self.collectionView.scrollToItem(at:indexPath, at: .centeredVertically, animated: false)
  455. } completion: { (_) in
  456. if let cell = self.collectionView.cellForItem(at: indexPath) {
  457. cell.backgroundColor = .darkGray
  458. UIView.animate(withDuration: 2) {
  459. cell.backgroundColor = .clear
  460. self.blinkFileId = nil
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. }
  468. }
  469. @objc func loadListingTrash() {
  470. NCCommunication.shared.listingTrash(showHiddenFiles: false) { (account, items, errorCode, errorDescription) in
  471. self.refreshControl.endRefreshing()
  472. if errorCode == 0 && account == self.appDelegate.account {
  473. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.trashPath, account: self.appDelegate.account)
  474. NCManageDatabase.sharedInstance.addTrash(account: account, items: items)
  475. } else if errorCode != 0 {
  476. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  477. } else {
  478. print("[LOG] It has been changed user during networking process, error.")
  479. }
  480. self.reloadDataSource()
  481. }
  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. }