NCTrash.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. //
  9. // Author Henrik Storch <henrik.storch@nextcloud.com>
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import NextcloudKit
  27. import RealmSwift
  28. class NCTrash: UIViewController, NCTrashListCellDelegate, NCTrashGridCellDelegate {
  29. @IBOutlet weak var collectionView: UICollectionView!
  30. var filePath = ""
  31. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  32. var blinkFileId: String?
  33. var dataSourceTask: URLSessionTask?
  34. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  35. let utilityFileSystem = NCUtilityFileSystem()
  36. let utility = NCUtility()
  37. var isEditMode = false
  38. var selectOcId: [String] = []
  39. var tabBarSelect: NCTrashSelectTabBar!
  40. var datasource: [tableTrash] = []
  41. var layoutForView: NCDBLayoutForView?
  42. var listLayout: NCListLayout!
  43. var gridLayout: NCGridLayout!
  44. var layoutKey = NCGlobal.shared.layoutViewTrash
  45. let refreshControl = UIRefreshControl()
  46. var filename: String?
  47. // MARK: - View Life Cycle
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. tabBarSelect = NCTrashSelectTabBar(tabBarController: tabBarController, delegate: self)
  51. view.backgroundColor = .systemBackground
  52. self.navigationController?.navigationBar.prefersLargeTitles = true
  53. collectionView.register(UINib(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  54. collectionView.register(UINib(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  55. collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
  56. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  57. collectionView.alwaysBounceVertical = true
  58. collectionView.backgroundColor = .systemBackground
  59. listLayout = NCListLayout()
  60. gridLayout = NCGridLayout()
  61. // Add Refresh Control
  62. collectionView.refreshControl = refreshControl
  63. refreshControl.tintColor = NCBrandColor.shared.textColor2
  64. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  65. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  66. }
  67. override func viewWillAppear(_ animated: Bool) {
  68. super.viewWillAppear(animated)
  69. navigationController?.setNavigationBarAppearance()
  70. navigationItem.title = titleCurrentFolder
  71. layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  72. if layoutForView?.layout == NCGlobal.shared.layoutList {
  73. collectionView.collectionViewLayout = listLayout
  74. } else {
  75. collectionView.collectionViewLayout = gridLayout
  76. }
  77. isEditMode = false
  78. setNavigationRightItems()
  79. reloadDataSource()
  80. loadListingTrash()
  81. }
  82. override func viewWillDisappear(_ animated: Bool) {
  83. super.viewWillDisappear(animated)
  84. // Cancel Queue & Retrieves Properties
  85. NCNetworking.shared.downloadThumbnailTrashQueue.cancelAll()
  86. dataSourceTask?.cancel()
  87. }
  88. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  89. super.viewWillTransition(to: size, with: coordinator)
  90. coordinator.animate(alongsideTransition: nil) { _ in
  91. self.collectionView?.collectionViewLayout.invalidateLayout()
  92. }
  93. }
  94. override func viewWillLayoutSubviews() {
  95. super.viewWillLayoutSubviews()
  96. if let frame = tabBarController?.tabBar.frame {
  97. tabBarSelect.hostingController?.view.frame = frame
  98. }
  99. }
  100. // MARK: - Layout
  101. func setNavigationRightItems() {
  102. func createMenuActions() -> [UIMenuElement] {
  103. guard let layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: "") else { return [] }
  104. let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: utility.loadImage(named: "checkmark.circle", colors: [NCBrandColor.shared.iconImageColor]), attributes: self.datasource.isEmpty ? .disabled : []) { _ in
  105. self.setEditMode(true)
  106. }
  107. let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: utility.loadImage(named: "list.bullet", colors: [NCBrandColor.shared.iconImageColor]), state: layoutForView.layout == NCGlobal.shared.layoutList ? .on : .off) { _ in
  108. self.onListSelected()
  109. self.setNavigationRightItems()
  110. }
  111. let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: utility.loadImage(named: "square.grid.2x2", colors: [NCBrandColor.shared.iconImageColor]), state: layoutForView.layout == NCGlobal.shared.layoutGrid ? .on : .off) { _ in
  112. self.onGridSelected()
  113. self.setNavigationRightItems()
  114. }
  115. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
  116. return [select, viewStyleSubmenu]
  117. }
  118. if isEditMode {
  119. tabBarSelect.update(selectOcId: selectOcId)
  120. tabBarSelect.show()
  121. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  122. self.setEditMode(false)
  123. }
  124. navigationItem.rightBarButtonItems = [select]
  125. } else if navigationItem.rightBarButtonItems == nil || (!isEditMode && !tabBarSelect.isHidden()) {
  126. tabBarSelect.hide()
  127. let menu = UIBarButtonItem(image: utility.loadImage(named: "ellipsis.circle", colors: [NCBrandColor.shared.iconImageColor]), menu: UIMenu(children: createMenuActions()))
  128. navigationItem.rightBarButtonItems = [menu]
  129. } else {
  130. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  131. }
  132. }
  133. // MARK: TAP EVENT
  134. func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
  135. if !isEditMode {
  136. restoreItem(with: ocId)
  137. } else if let button = sender as? UIView {
  138. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  139. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  140. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  141. } // else: undefined sender
  142. }
  143. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
  144. if !isEditMode {
  145. toggleMenuMore(with: objectId, image: image, isGridCell: false)
  146. } else if let button = sender as? UIView {
  147. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  148. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  149. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  150. } // else: undefined sender
  151. }
  152. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any) {
  153. if !isEditMode {
  154. toggleMenuMore(with: objectId, image: image, isGridCell: true)
  155. } else if let button = sender as? UIView {
  156. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  157. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  158. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  159. }
  160. }
  161. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  162. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  163. // MARK: - DataSource
  164. @objc func reloadDataSource(withQueryDB: Bool = true) {
  165. datasource = NCManageDatabase.shared.getTrash(filePath: getFilePath(), account: appDelegate.account)
  166. collectionView.reloadData()
  167. setNavigationRightItems()
  168. guard let blinkFileId = blinkFileId else { return }
  169. for itemIx in 0..<self.datasource.count where self.datasource[itemIx].fileId.contains(blinkFileId) {
  170. let indexPath = IndexPath(item: itemIx, section: 0)
  171. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  172. UIView.animate(withDuration: 0.3) {
  173. self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
  174. } completion: { _ in
  175. guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  176. cell.backgroundColor = .darkGray
  177. UIView.animate(withDuration: 2) {
  178. cell.backgroundColor = .clear
  179. self.blinkFileId = nil
  180. }
  181. }
  182. }
  183. }
  184. }
  185. func getFilePath() -> String {
  186. if filePath.isEmpty {
  187. guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
  188. let filePath = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + userId + "/trash"
  189. return filePath + "/"
  190. } else {
  191. return filePath + "/"
  192. }
  193. }
  194. }