NCTrash.swift 35 KB

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