NCTrash.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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, serverUrl: "")
  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: .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. 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, serverUrl: "", 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, serverUrl: "", 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: "", 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. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  310. }
  311. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  312. }
  313. }
  314. // MARK: - Collection View
  315. extension NCTrash: UICollectionViewDelegate {
  316. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  317. let tableTrash = datasource[indexPath.item]
  318. if isEditMode {
  319. if let index = selectocId.firstIndex(of: tableTrash.fileId) {
  320. selectocId.remove(at: index)
  321. } else {
  322. selectocId.append(tableTrash.fileId)
  323. }
  324. collectionView.reloadItems(at: [indexPath])
  325. return
  326. }
  327. if tableTrash.directory {
  328. let ncTrash:NCTrash = UIStoryboard(name: "NCTrash", bundle: nil).instantiateInitialViewController() as! NCTrash
  329. ncTrash.trashPath = tableTrash.filePath + tableTrash.fileName
  330. ncTrash.titleCurrentFolder = tableTrash.trashbinFileName
  331. self.navigationController?.pushViewController(ncTrash, animated: true)
  332. }
  333. }
  334. }
  335. extension NCTrash: UICollectionViewDataSource {
  336. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  337. if kind == UICollectionView.elementKindSectionHeader {
  338. let trashHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCTrashSectionHeaderMenu
  339. if collectionView.collectionViewLayout == gridLayout {
  340. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  341. } else {
  342. trashHeader.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  343. }
  344. trashHeader.delegate = self
  345. trashHeader.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  346. trashHeader.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  347. trashHeader.setStatusButton(datasource: datasource)
  348. trashHeader.setTitleSorted(datasourceTitleButton: titleButton)
  349. return trashHeader
  350. } else {
  351. let trashFooter = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCTrashSectionFooter
  352. trashFooter.setTitleLabelFooter(datasource: datasource)
  353. return trashFooter
  354. }
  355. }
  356. func numberOfSections(in collectionView: UICollectionView) -> Int {
  357. return 1
  358. }
  359. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  360. return datasource.count
  361. }
  362. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  363. let tableTrash = datasource[indexPath.item]
  364. var image: UIImage?
  365. if tableTrash.iconName.count > 0 {
  366. image = UIImage.init(named: tableTrash.iconName)
  367. } else {
  368. image = UIImage.init(named: "file")
  369. }
  370. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)) {
  371. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName))
  372. } else {
  373. if tableTrash.hasPreview && !CCUtility.fileProviderStoragePreviewIconExists(tableTrash.fileId, etag: tableTrash.fileName) {
  374. downloadThumbnail(with: tableTrash, indexPath: indexPath)
  375. }
  376. }
  377. if collectionView.collectionViewLayout == listLayout {
  378. // LIST
  379. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCTrashListCell
  380. cell.delegate = self
  381. cell.objectId = tableTrash.fileId
  382. cell.indexPath = indexPath
  383. cell.labelTitle.text = tableTrash.trashbinFileName
  384. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  385. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  386. if tableTrash.directory {
  387. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  388. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date)
  389. } else {
  390. cell.imageItem.image = image
  391. cell.labelInfo.text = CCUtility.dateDiff(tableTrash.date as Date) + ", " + CCUtility.transformedSize(tableTrash.size)
  392. }
  393. if isEditMode {
  394. cell.imageItemLeftConstraint.constant = 45
  395. cell.imageSelect.isHidden = false
  396. if selectocId.contains(tableTrash.fileId) {
  397. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  398. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  399. } else {
  400. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  401. cell.backgroundView = nil
  402. }
  403. } else {
  404. cell.imageItemLeftConstraint.constant = 10
  405. cell.imageSelect.isHidden = true
  406. cell.backgroundView = nil
  407. }
  408. return cell
  409. } else {
  410. // GRID
  411. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  412. cell.delegate = self
  413. cell.objectId = tableTrash.fileId
  414. cell.indexPath = indexPath
  415. cell.labelTitle.text = tableTrash.trashbinFileName
  416. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  417. if tableTrash.directory {
  418. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  419. } else {
  420. cell.imageItem.image = image
  421. }
  422. if isEditMode {
  423. cell.imageSelect.isHidden = false
  424. if selectocId.contains(tableTrash.fileId) {
  425. cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
  426. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  427. } else {
  428. cell.imageSelect.isHidden = true
  429. cell.backgroundView = nil
  430. }
  431. } else {
  432. cell.imageSelect.isHidden = true
  433. cell.backgroundView = nil
  434. }
  435. return cell
  436. }
  437. }
  438. }
  439. extension NCTrash: UICollectionViewDelegateFlowLayout {
  440. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  441. return CGSize(width: collectionView.frame.width, height: highHeader)
  442. }
  443. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  444. return CGSize(width: collectionView.frame.width, height: highHeader)
  445. }
  446. }
  447. // MARK: - NC API & Algorithm
  448. extension NCTrash {
  449. @objc func reloadDataSource() {
  450. var sort: String
  451. var ascending: Bool
  452. (layout, sort, ascending, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_trash, serverUrl: "")
  453. datasource.removeAll()
  454. guard let tashItems = NCManageDatabase.sharedInstance.getTrash(filePath: trashPath, sort: sort, ascending: ascending, account: appDelegate.account) else {
  455. return
  456. }
  457. datasource = tashItems
  458. CATransaction.begin()
  459. CATransaction.setCompletionBlock {
  460. // GoTo ocId
  461. if self.blinkocId != nil {
  462. for item in 0...self.datasource.count-1 {
  463. if self.datasource[item].fileId.contains(self.blinkocId!) {
  464. let indexPath = IndexPath(item: item, section: 0)
  465. self.collectionView.scrollToItem(at: indexPath, at: .top, animated: false)
  466. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  467. self.blinkocId = nil
  468. NCUtility.shared.blink(cell: self.collectionView.cellForItem(at: indexPath))
  469. }
  470. }
  471. }
  472. }
  473. }
  474. collectionView.reloadData()
  475. CATransaction.commit()
  476. }
  477. @objc func loadListingTrash() {
  478. NCCommunication.shared.listingTrash(showHiddenFiles: false) { (account, items, errorCode, errorDescription) in
  479. self.refreshControl.endRefreshing()
  480. if errorCode == 0 && account == self.appDelegate.account {
  481. NCManageDatabase.sharedInstance.deleteTrash(filePath: self.trashPath, account: self.appDelegate.account)
  482. NCManageDatabase.sharedInstance.addTrash(account: account, items: items)
  483. } else if errorCode != 0 {
  484. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  485. } else {
  486. print("[LOG] It has been changed user during networking process, error.")
  487. }
  488. self.reloadDataSource()
  489. }
  490. }
  491. func restoreItem(with fileId: String) {
  492. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.account) else {
  493. return
  494. }
  495. let fileNameFrom = tableTrash.filePath + tableTrash.fileName
  496. let fileNameTo = appDelegate.urlBase + "/" + NCUtility.shared.getDAV() + "/trashbin/" + appDelegate.userID + "/restore/" + tableTrash.fileName
  497. NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: fileNameFrom, serverUrlFileNameDestination: fileNameTo, overwrite: true) { (account, errorCode, errorDescription) in
  498. if errorCode == 0 && account == self.appDelegate.account {
  499. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
  500. self.reloadDataSource()
  501. } else if errorCode != 0 {
  502. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  503. } else {
  504. print("[LOG] It has been changed user during networking process, error.")
  505. }
  506. }
  507. }
  508. func emptyTrash() {
  509. let serverUrlFileName = appDelegate.urlBase + "/" + NCUtility.shared.getDAV() + "/trashbin/" + appDelegate.userID + "/trash"
  510. NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { (account, errorCode, errorDescription) in
  511. if errorCode == 0 && account == self.appDelegate.account {
  512. NCManageDatabase.sharedInstance.deleteTrash(fileId: nil, account: self.appDelegate.account)
  513. } else if errorCode != 0 {
  514. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  515. } else {
  516. print("[LOG] It has been changed user during networking process, error.")
  517. }
  518. self.reloadDataSource()
  519. }
  520. }
  521. func deleteItem(with fileId: String) {
  522. guard let tableTrash = NCManageDatabase.sharedInstance.getTrashItem(fileId: fileId, account: appDelegate.account) else {
  523. return
  524. }
  525. let serverUrlFileName = tableTrash.filePath + tableTrash.fileName
  526. NCCommunication.shared.deleteFileOrFolder(serverUrlFileName) { (account, errorCode, errorDescription) in
  527. if errorCode == 0 && account == self.appDelegate.account {
  528. NCManageDatabase.sharedInstance.deleteTrash(fileId: fileId, account: account)
  529. self.reloadDataSource()
  530. } else if errorCode != 0 {
  531. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  532. } else {
  533. print("[LOG] It has been changed user during networking process, error.")
  534. }
  535. }
  536. }
  537. func downloadThumbnail(with tableTrash: tableTrash, indexPath: IndexPath) {
  538. let fileNamePreviewLocalPath = CCUtility.getDirectoryProviderStoragePreviewOcId(tableTrash.fileId, etag: tableTrash.fileName)!
  539. let fileNameIconLocalPath = CCUtility.getDirectoryProviderStorageIconOcId(tableTrash.fileId, etag: tableTrash.fileName)!
  540. 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
  541. if errorCode == 0 && imageIcon != nil && account == self.appDelegate.account {
  542. if let cell = self.collectionView.cellForItem(at: indexPath) {
  543. if cell is NCTrashListCell {
  544. (cell as! NCTrashListCell).imageItem.image = imageIcon
  545. } else if cell is NCGridCell {
  546. (cell as! NCGridCell).imageItem.image = imageIcon
  547. }
  548. }
  549. }
  550. }
  551. }
  552. }