NCTrash.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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, NCEmptyDataSetDelegate {
  29. @IBOutlet weak var collectionView: UICollectionView!
  30. var filePath = ""
  31. var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
  32. var blinkFileId: String?
  33. var emptyDataSet: NCEmptyDataSet?
  34. var dataSourceTask: URLSessionTask?
  35. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  36. let utilityFileSystem = NCUtilityFileSystem()
  37. let utility = NCUtility()
  38. var isEditMode = false
  39. var selectOcId: [String] = []
  40. var tabBarSelect: NCTrashSelectTabBar!
  41. var datasource: [tableTrash] = []
  42. var layoutForView: NCDBLayoutForView?
  43. var listLayout: NCListLayout!
  44. var gridLayout: NCGridLayout!
  45. var layoutKey = NCGlobal.shared.layoutViewTrash
  46. let refreshControl = UIRefreshControl()
  47. var filename: String?
  48. // MARK: - View Life Cycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. tabBarSelect = NCTrashSelectTabBar(tabBarController: tabBarController, delegate: self)
  52. view.backgroundColor = .systemBackground
  53. self.navigationController?.navigationBar.prefersLargeTitles = true
  54. // Cell
  55. collectionView.register(UINib(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  56. collectionView.register(UINib(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  57. // Footer
  58. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  59. collectionView.alwaysBounceVertical = true
  60. collectionView.backgroundColor = .systemBackground
  61. listLayout = NCListLayout()
  62. gridLayout = NCGridLayout()
  63. // Add Refresh Control
  64. collectionView.refreshControl = refreshControl
  65. refreshControl.tintColor = .gray
  66. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  67. emptyDataSet = NCEmptyDataSet(view: collectionView, offset: NCGlobal.shared.heightButtonsView, delegate: self)
  68. setNavigationRightItems()
  69. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  70. }
  71. override func viewWillAppear(_ animated: Bool) {
  72. super.viewWillAppear(animated)
  73. navigationController?.setNavigationBarAppearance()
  74. navigationItem.title = titleCurrentFolder
  75. layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  76. gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
  77. if layoutForView?.layout == NCGlobal.shared.layoutList {
  78. collectionView.collectionViewLayout = listLayout
  79. } else {
  80. collectionView.collectionViewLayout = gridLayout
  81. }
  82. reloadDataSource()
  83. loadListingTrash()
  84. }
  85. override func viewDidAppear(_ animated: Bool) {
  86. super.viewDidAppear(animated)
  87. appDelegate.activeViewController = self
  88. }
  89. override func viewDidDisappear(_ animated: Bool) {
  90. super.viewDidDisappear(animated)
  91. setEditMode(false)
  92. }
  93. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  94. super.viewWillTransition(to: size, with: coordinator)
  95. coordinator.animate(alongsideTransition: nil) { _ in
  96. self.collectionView?.collectionViewLayout.invalidateLayout()
  97. }
  98. }
  99. override func viewWillLayoutSubviews() {
  100. super.viewWillLayoutSubviews()
  101. if let frame = tabBarController?.tabBar.frame {
  102. tabBarSelect.hostingController?.view.frame = frame
  103. }
  104. }
  105. // MARK: - Layout
  106. func setNavigationRightItems(enableMenu: Bool = false) {
  107. func createMenuActions() -> [UIMenuElement] {
  108. guard let layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: "") else { return [] }
  109. let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: .init(systemName: "checkmark.circle"), attributes: self.datasource.isEmpty ? .disabled : []) { _ in
  110. self.setEditMode(true)
  111. }
  112. let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: .init(systemName: "list.bullet"), state: layoutForView.layout == NCGlobal.shared.layoutList ? .on : .off) { _ in
  113. self.onListSelected()
  114. self.setNavigationRightItems()
  115. }
  116. let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: .init(systemName: "square.grid.2x2"), state: layoutForView.layout == NCGlobal.shared.layoutGrid ? .on : .off) { _ in
  117. self.onGridSelected()
  118. self.setNavigationRightItems()
  119. }
  120. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
  121. return [select, viewStyleSubmenu]
  122. }
  123. if isEditMode {
  124. tabBarSelect.update(selectOcId: selectOcId)
  125. tabBarSelect.show()
  126. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  127. self.setEditMode(false)
  128. }
  129. navigationItem.rightBarButtonItems = [select]
  130. } else {
  131. tabBarSelect.hide()
  132. if navigationItem.rightBarButtonItems == nil || enableMenu {
  133. let menu = UIBarButtonItem(image: .init(systemName: "ellipsis.circle"), menu: UIMenu(children: createMenuActions()))
  134. navigationItem.rightBarButtonItems = [menu]
  135. } else {
  136. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  137. }
  138. }
  139. }
  140. // MARK: - Empty
  141. func emptyDataSetView(_ view: NCEmptyView) {
  142. view.emptyImage.image = UIImage(named: "trash")?.image(color: .gray, size: UIScreen.main.bounds.width)
  143. view.emptyTitle.text = NSLocalizedString("_trash_no_trash_", comment: "")
  144. view.emptyDescription.text = NSLocalizedString("_trash_no_trash_description_", comment: "")
  145. }
  146. // MARK: TAP EVENT
  147. func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
  148. if !isEditMode {
  149. restoreItem(with: ocId)
  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 tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
  157. if !isEditMode {
  158. toggleMenuMore(with: objectId, image: image, isGridCell: false)
  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. } // else: undefined sender
  164. }
  165. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any) {
  166. if !isEditMode {
  167. toggleMenuMore(with: objectId, image: image, isGridCell: true)
  168. } else if let button = sender as? UIView {
  169. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  170. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  171. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  172. }
  173. }
  174. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  175. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  176. // MARK: - DataSource
  177. @objc func reloadDataSource(withQueryDB: Bool = true) {
  178. layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  179. datasource = NCManageDatabase.shared.getTrash(filePath: getFilePath(), sort: layoutForView?.sort, ascending: layoutForView?.ascending, account: appDelegate.account)
  180. collectionView.reloadData()
  181. setNavigationRightItems()
  182. guard let blinkFileId = blinkFileId else { return }
  183. for itemIx in 0..<self.datasource.count where self.datasource[itemIx].fileId.contains(blinkFileId) {
  184. let indexPath = IndexPath(item: itemIx, section: 0)
  185. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  186. UIView.animate(withDuration: 0.3) {
  187. self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
  188. } completion: { _ in
  189. guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  190. cell.backgroundColor = .darkGray
  191. UIView.animate(withDuration: 2) {
  192. cell.backgroundColor = .clear
  193. self.blinkFileId = nil
  194. }
  195. }
  196. }
  197. }
  198. }
  199. func getFilePath() -> String {
  200. if filePath.isEmpty {
  201. guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
  202. let filePath = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + userId + "/trash"
  203. return filePath + "/"
  204. } else {
  205. return filePath + "/"
  206. }
  207. }
  208. }