NCTrash.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 = 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. gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
  73. if layoutForView?.layout == NCGlobal.shared.layoutList {
  74. collectionView.collectionViewLayout = listLayout
  75. } else {
  76. collectionView.collectionViewLayout = gridLayout
  77. }
  78. isEditMode = false
  79. setNavigationRightItems()
  80. reloadDataSource()
  81. loadListingTrash()
  82. }
  83. override func viewWillDisappear(_ animated: Bool) {
  84. super.viewWillDisappear(animated)
  85. // Cancel Queue & Retrieves Properties
  86. NCNetworking.shared.downloadThumbnailTrashQueue.cancelAll()
  87. dataSourceTask?.cancel()
  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. override func viewWillLayoutSubviews() {
  96. super.viewWillLayoutSubviews()
  97. if let frame = tabBarController?.tabBar.frame {
  98. tabBarSelect.hostingController?.view.frame = frame
  99. }
  100. }
  101. // MARK: - Layout
  102. func setNavigationRightItems() {
  103. func createMenuActions() -> [UIMenuElement] {
  104. guard let layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: "") else { return [] }
  105. let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: utility.loadImage(named: "checkmark.circle", colors: [NCBrandColor.shared.iconImageColor]), attributes: self.datasource.isEmpty ? .disabled : []) { _ in
  106. self.setEditMode(true)
  107. }
  108. 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
  109. self.onListSelected()
  110. self.setNavigationRightItems()
  111. }
  112. 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
  113. self.onGridSelected()
  114. self.setNavigationRightItems()
  115. }
  116. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
  117. return [select, viewStyleSubmenu]
  118. }
  119. if isEditMode {
  120. tabBarSelect.update(selectOcId: selectOcId)
  121. tabBarSelect.show()
  122. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  123. self.setEditMode(false)
  124. }
  125. navigationItem.rightBarButtonItems = [select]
  126. } else if navigationItem.rightBarButtonItems == nil || (!isEditMode && !tabBarSelect.isHidden()) {
  127. tabBarSelect.hide()
  128. let menu = UIBarButtonItem(image: utility.loadImage(named: "ellipsis.circle", colors: [NCBrandColor.shared.iconImageColor]), menu: UIMenu(children: createMenuActions()))
  129. navigationItem.rightBarButtonItems = [menu]
  130. } else {
  131. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  132. }
  133. }
  134. // MARK: TAP EVENT
  135. func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
  136. if !isEditMode {
  137. restoreItem(with: ocId)
  138. } else if let button = sender as? UIView {
  139. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  140. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  141. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  142. } // else: undefined sender
  143. }
  144. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
  145. if !isEditMode {
  146. toggleMenuMore(with: objectId, image: image, isGridCell: false)
  147. } else if let button = sender as? UIView {
  148. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  149. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  150. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  151. } // else: undefined sender
  152. }
  153. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any) {
  154. if !isEditMode {
  155. toggleMenuMore(with: objectId, image: image, isGridCell: true)
  156. } else if let button = sender as? UIView {
  157. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  158. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  159. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  160. }
  161. }
  162. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  163. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  164. // MARK: - DataSource
  165. @objc func reloadDataSource(withQueryDB: Bool = true) {
  166. datasource = NCManageDatabase.shared.getTrash(filePath: getFilePath(), account: appDelegate.account)
  167. collectionView.reloadData()
  168. setNavigationRightItems()
  169. guard let blinkFileId = blinkFileId else { return }
  170. for itemIx in 0..<self.datasource.count where self.datasource[itemIx].fileId.contains(blinkFileId) {
  171. let indexPath = IndexPath(item: itemIx, section: 0)
  172. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  173. UIView.animate(withDuration: 0.3) {
  174. self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
  175. } completion: { _ in
  176. guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  177. cell.backgroundColor = .darkGray
  178. UIView.animate(withDuration: 2) {
  179. cell.backgroundColor = .clear
  180. self.blinkFileId = nil
  181. }
  182. }
  183. }
  184. }
  185. }
  186. func getFilePath() -> String {
  187. if filePath.isEmpty {
  188. guard let userId = (appDelegate.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
  189. let filePath = appDelegate.urlBase + "/" + NextcloudKit.shared.nkCommonInstance.dav + "/trashbin/" + userId + "/trash"
  190. return filePath + "/"
  191. } else {
  192. return filePath + "/"
  193. }
  194. }
  195. }