NCTrash.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 Realm
  26. import UIKit
  27. import NextcloudKit
  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: "NCSectionHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderEmptyData")
  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 = .gray
  64. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  65. setNavigationRightItems()
  66. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  67. }
  68. override func viewWillAppear(_ animated: Bool) {
  69. super.viewWillAppear(animated)
  70. navigationController?.setNavigationBarAppearance()
  71. navigationItem.title = titleCurrentFolder
  72. layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  73. gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
  74. if layoutForView?.layout == NCGlobal.shared.layoutList {
  75. collectionView.collectionViewLayout = listLayout
  76. } else {
  77. collectionView.collectionViewLayout = gridLayout
  78. }
  79. reloadDataSource()
  80. loadListingTrash()
  81. }
  82. override func viewDidAppear(_ animated: Bool) {
  83. super.viewDidAppear(animated)
  84. appDelegate.activeViewController = self
  85. }
  86. override func viewDidDisappear(_ animated: Bool) {
  87. super.viewDidDisappear(animated)
  88. setEditMode(false)
  89. }
  90. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  91. super.viewWillTransition(to: size, with: coordinator)
  92. coordinator.animate(alongsideTransition: nil) { _ in
  93. self.collectionView?.collectionViewLayout.invalidateLayout()
  94. }
  95. }
  96. override func viewWillLayoutSubviews() {
  97. super.viewWillLayoutSubviews()
  98. if let frame = tabBarController?.tabBar.frame {
  99. tabBarSelect.hostingController?.view.frame = frame
  100. }
  101. }
  102. // MARK: - Layout
  103. func setNavigationRightItems(enableMenu: Bool = false) {
  104. func createMenuActions() -> [UIMenuElement] {
  105. guard let layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: "") else { return [] }
  106. let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: .init(systemName: "checkmark.circle"), attributes: self.datasource.isEmpty ? .disabled : []) { _ in
  107. self.setEditMode(true)
  108. }
  109. let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: .init(systemName: "list.bullet"), state: layoutForView.layout == NCGlobal.shared.layoutList ? .on : .off) { _ in
  110. self.onListSelected()
  111. self.setNavigationRightItems()
  112. }
  113. let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: .init(systemName: "square.grid.2x2"), state: layoutForView.layout == NCGlobal.shared.layoutGrid ? .on : .off) { _ in
  114. self.onGridSelected()
  115. self.setNavigationRightItems()
  116. }
  117. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
  118. return [select, viewStyleSubmenu]
  119. }
  120. if isEditMode {
  121. tabBarSelect.update(selectOcId: selectOcId)
  122. tabBarSelect.show()
  123. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  124. self.setEditMode(false)
  125. }
  126. navigationItem.rightBarButtonItems = [select]
  127. } else {
  128. tabBarSelect.hide()
  129. if navigationItem.rightBarButtonItems == nil || enableMenu {
  130. let menu = UIBarButtonItem(image: .init(systemName: "ellipsis.circle"), menu: UIMenu(children: createMenuActions()))
  131. navigationItem.rightBarButtonItems = [menu]
  132. } else {
  133. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  134. }
  135. }
  136. }
  137. // MARK: TAP EVENT
  138. func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
  139. if !isEditMode {
  140. restoreItem(with: ocId)
  141. } else if let button = sender as? UIView {
  142. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  143. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  144. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  145. } // else: undefined sender
  146. }
  147. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
  148. if !isEditMode {
  149. toggleMenuMore(with: objectId, image: image, isGridCell: false)
  150. } else if let button = sender as? UIView {
  151. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  152. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  153. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  154. } // else: undefined sender
  155. }
  156. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any) {
  157. if !isEditMode {
  158. toggleMenuMore(with: objectId, image: image, isGridCell: true)
  159. } else if let button = sender as? UIView {
  160. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  161. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  162. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  163. }
  164. }
  165. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  166. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  167. // MARK: - DataSource
  168. @objc func reloadDataSource(withQueryDB: Bool = true) {
  169. layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  170. datasource = NCManageDatabase.shared.getTrash(filePath: getFilePath(), sort: layoutForView?.sort, ascending: layoutForView?.ascending, account: appDelegate.account)
  171. collectionView.reloadData()
  172. setNavigationRightItems()
  173. guard let blinkFileId = blinkFileId else { return }
  174. for itemIx in 0..<self.datasource.count where self.datasource[itemIx].fileId.contains(blinkFileId) {
  175. let indexPath = IndexPath(item: itemIx, section: 0)
  176. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  177. UIView.animate(withDuration: 0.3) {
  178. self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
  179. } completion: { _ in
  180. guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  181. cell.backgroundColor = .darkGray
  182. UIView.animate(withDuration: 2) {
  183. cell.backgroundColor = .clear
  184. self.blinkFileId = nil
  185. }
  186. }
  187. }
  188. }
  189. }
  190. func getFilePath() -> String {
  191. if filePath.isEmpty {
  192. guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
  193. let filePath = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + userId + "/trash"
  194. return filePath + "/"
  195. } else {
  196. return filePath + "/"
  197. }
  198. }
  199. }