NCTrash.swift 31 KB

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