NCTrash.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 utilityFileSystem = NCUtilityFileSystem()
  35. let database = NCManageDatabase.shared
  36. let utility = NCUtility()
  37. var isEditMode = false
  38. var selectOcId: [String] = []
  39. var tabBarSelect: NCTrashSelectTabBar!
  40. var datasource: Results<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. var session: NCSession.Session {
  48. NCSession.shared.getSession(controller: tabBarController)
  49. }
  50. // MARK: - View Life Cycle
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. tabBarSelect = NCTrashSelectTabBar(tabBarController: tabBarController, delegate: self)
  54. view.backgroundColor = .systemBackground
  55. self.navigationController?.navigationBar.prefersLargeTitles = true
  56. collectionView.register(UINib(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  57. collectionView.register(UINib(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  58. collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
  59. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  60. collectionView.alwaysBounceVertical = true
  61. collectionView.backgroundColor = .systemBackground
  62. listLayout = NCListLayout()
  63. gridLayout = NCGridLayout()
  64. // Add Refresh Control
  65. collectionView.refreshControl = refreshControl
  66. refreshControl.tintColor = NCBrandColor.shared.textColor2
  67. refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
  68. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  69. }
  70. override func viewWillAppear(_ animated: Bool) {
  71. super.viewWillAppear(animated)
  72. navigationController?.setNavigationBarAppearance()
  73. navigationItem.title = titleCurrentFolder
  74. layoutForView = self.database.getLayoutForView(account: session.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
  75. if layoutForView?.layout == NCGlobal.shared.layoutList {
  76. collectionView.collectionViewLayout = listLayout
  77. } else {
  78. collectionView.collectionViewLayout = gridLayout
  79. }
  80. isEditMode = false
  81. setNavigationRightItems()
  82. reloadDataSource()
  83. loadListingTrash()
  84. }
  85. override func viewWillDisappear(_ animated: Bool) {
  86. super.viewWillDisappear(animated)
  87. // Cancel Queue & Retrieves Properties
  88. NCNetworking.shared.downloadThumbnailTrashQueue.cancelAll()
  89. dataSourceTask?.cancel()
  90. }
  91. override func viewWillLayoutSubviews() {
  92. super.viewWillLayoutSubviews()
  93. if let frame = tabBarController?.tabBar.frame {
  94. tabBarSelect.hostingController?.view.frame = frame
  95. }
  96. }
  97. // MARK: - Layout
  98. func setNavigationRightItems() {
  99. func createMenuActions() -> [UIMenuElement] {
  100. guard let layoutForView = self.database.getLayoutForView(account: session.account, key: layoutKey, serverUrl: ""),
  101. let datasource else { return [] }
  102. let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: utility.loadImage(named: "checkmark.circle", colors: [NCBrandColor.shared.iconImageColor]), attributes: datasource.isEmpty ? .disabled : []) { _ in
  103. self.setEditMode(true)
  104. }
  105. 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
  106. self.onListSelected()
  107. self.setNavigationRightItems()
  108. }
  109. 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
  110. self.onGridSelected()
  111. self.setNavigationRightItems()
  112. }
  113. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
  114. return [select, viewStyleSubmenu]
  115. }
  116. if isEditMode {
  117. tabBarSelect.update(selectOcId: selectOcId)
  118. tabBarSelect.show()
  119. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  120. self.setEditMode(false)
  121. }
  122. navigationItem.rightBarButtonItems = [select]
  123. } else if navigationItem.rightBarButtonItems == nil || (!isEditMode && !tabBarSelect.isHidden()) {
  124. tabBarSelect.hide()
  125. let menu = UIBarButtonItem(image: utility.loadImage(named: "ellipsis.circle", colors: [NCBrandColor.shared.iconImageColor]), menu: UIMenu(children: createMenuActions()))
  126. navigationItem.rightBarButtonItems = [menu]
  127. } else {
  128. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  129. }
  130. }
  131. // MARK: TAP EVENT
  132. func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
  133. if !isEditMode {
  134. restoreItem(with: ocId)
  135. } else if let button = sender as? UIView {
  136. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  137. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  138. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  139. } // else: undefined sender
  140. }
  141. func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
  142. if !isEditMode {
  143. toggleMenuMore(with: objectId, image: image, isGridCell: false)
  144. } else if let button = sender as? UIView {
  145. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  146. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  147. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  148. } // else: undefined sender
  149. }
  150. func tapMoreGridItem(with objectId: String, image: UIImage?, sender: Any) {
  151. if !isEditMode {
  152. toggleMenuMore(with: objectId, image: image, isGridCell: true)
  153. } else if let button = sender as? UIView {
  154. let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
  155. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  156. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  157. }
  158. }
  159. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  160. func longPressMoreGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  161. // MARK: - DataSource
  162. @objc func reloadDataSource(withQueryDB: Bool = true) {
  163. datasource = self.database.getResultsTrash(filePath: getFilePath(), account: session.account)
  164. collectionView.reloadData()
  165. setNavigationRightItems()
  166. guard let blinkFileId, let datasource else { return }
  167. for itemIx in 0..<datasource.count where datasource[itemIx].fileId.contains(blinkFileId) {
  168. let indexPath = IndexPath(item: itemIx, section: 0)
  169. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  170. UIView.animate(withDuration: 0.3) {
  171. self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
  172. } completion: { _ in
  173. guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
  174. cell.backgroundColor = .darkGray
  175. UIView.animate(withDuration: 2) {
  176. cell.backgroundColor = .clear
  177. self.blinkFileId = nil
  178. }
  179. }
  180. }
  181. }
  182. }
  183. func getFilePath() -> String {
  184. if filePath.isEmpty {
  185. guard let userId = (session.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
  186. let filePath = session.urlBase + "/remote.php/dav/trashbin/" + userId + "/trash"
  187. return filePath + "/"
  188. } else {
  189. return filePath + "/"
  190. }
  191. }
  192. }