123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- //
- // NCTrash.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 02/10/2018.
- // Copyright © 2018 Marino Faggiana. All rights reserved.
- // Copyright © 2022 Henrik Storch. All rights reserved.
- //
- // Author Henrik Storch <henrik.storch@nextcloud.com>
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import UIKit
- import NextcloudKit
- import RealmSwift
- class NCTrash: UIViewController, NCTrashListCellDelegate, NCTrashGridCellDelegate {
- @IBOutlet weak var collectionView: UICollectionView!
- var filePath = ""
- var titleCurrentFolder = NSLocalizedString("_trash_view_", comment: "")
- var blinkFileId: String?
- var dataSourceTask: URLSessionTask?
- let utilityFileSystem = NCUtilityFileSystem()
- let database = NCManageDatabase.shared
- let utility = NCUtility()
- var isEditMode = false
- var selectOcId: [String] = []
- var tabBarSelect: NCTrashSelectTabBar!
- var datasource: Results<tableTrash>?
- var layoutForView: NCDBLayoutForView?
- var listLayout: NCListLayout!
- var gridLayout: NCGridLayout!
- var layoutKey = NCGlobal.shared.layoutViewTrash
- let refreshControl = UIRefreshControl()
- var filename: String?
- var session: NCSession.Session {
- NCSession.shared.getSession(controller: tabBarController)
- }
- // MARK: - View Life Cycle
- override func viewDidLoad() {
- super.viewDidLoad()
- tabBarSelect = NCTrashSelectTabBar(tabBarController: tabBarController, delegate: self)
- view.backgroundColor = .systemBackground
- self.navigationController?.navigationBar.prefersLargeTitles = true
- collectionView.register(UINib(nibName: "NCTrashListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
- collectionView.register(UINib(nibName: "NCTrashGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
- collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
- collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
- collectionView.alwaysBounceVertical = true
- collectionView.backgroundColor = .systemBackground
- listLayout = NCListLayout()
- gridLayout = NCGridLayout()
- // Add Refresh Control
- collectionView.refreshControl = refreshControl
- refreshControl.tintColor = NCBrandColor.shared.textColor2
- refreshControl.addTarget(self, action: #selector(loadListingTrash), for: .valueChanged)
- NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- navigationController?.setNavigationBarAppearance()
- navigationItem.title = titleCurrentFolder
- layoutForView = self.database.getLayoutForView(account: session.account, key: NCGlobal.shared.layoutViewTrash, serverUrl: "")
- if layoutForView?.layout == NCGlobal.shared.layoutList {
- collectionView.collectionViewLayout = listLayout
- } else {
- collectionView.collectionViewLayout = gridLayout
- }
- isEditMode = false
- setNavigationRightItems()
- reloadDataSource()
- loadListingTrash()
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- // Cancel Queue & Retrieves Properties
- NCNetworking.shared.downloadThumbnailTrashQueue.cancelAll()
- dataSourceTask?.cancel()
- }
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- if let frame = tabBarController?.tabBar.frame {
- tabBarSelect.hostingController?.view.frame = frame
- }
- }
- // MARK: - Layout
- func setNavigationRightItems() {
- func createMenuActions() -> [UIMenuElement] {
- guard let layoutForView = self.database.getLayoutForView(account: session.account, key: layoutKey, serverUrl: ""),
- let datasource else { return [] }
- let select = UIAction(title: NSLocalizedString("_select_", comment: ""), image: utility.loadImage(named: "checkmark.circle", colors: [NCBrandColor.shared.iconImageColor]), attributes: datasource.isEmpty ? .disabled : []) { _ in
- self.setEditMode(true)
- }
- 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
- self.onListSelected()
- self.setNavigationRightItems()
- }
- 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
- self.onGridSelected()
- self.setNavigationRightItems()
- }
- let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid])
- return [select, viewStyleSubmenu]
- }
- if isEditMode {
- tabBarSelect.update(selectOcId: selectOcId)
- tabBarSelect.show()
- let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
- self.setEditMode(false)
- }
- navigationItem.rightBarButtonItems = [select]
- } else if navigationItem.rightBarButtonItems == nil || (!isEditMode && !tabBarSelect.isHidden()) {
- tabBarSelect.hide()
- let menu = UIBarButtonItem(image: utility.loadImage(named: "ellipsis.circle", colors: [NCBrandColor.shared.iconImageColor]), menu: UIMenu(children: createMenuActions()))
- navigationItem.rightBarButtonItems = [menu]
- } else {
- navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
- }
- }
- // MARK: TAP EVENT
- func tapRestoreListItem(with ocId: String, image: UIImage?, sender: Any) {
- if !isEditMode {
- restoreItem(with: ocId)
- } else if let button = sender as? UIView {
- let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- } // else: undefined sender
- }
- func tapMoreListItem(with objectId: String, image: UIImage?, sender: Any) {
- if !isEditMode {
- toggleMenuMore(with: objectId, image: image, isGridCell: false)
- } else if let button = sender as? UIView {
- let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- } // else: undefined sender
- }
- func tapMoreGridItem(with objectId: String, image: UIImage?, sender: Any) {
- if !isEditMode {
- toggleMenuMore(with: objectId, image: image, isGridCell: true)
- } else if let button = sender as? UIView {
- let buttonPosition = button.convert(CGPoint.zero, to: collectionView)
- let indexPath = collectionView.indexPathForItem(at: buttonPosition)
- collectionView(self.collectionView, didSelectItemAt: indexPath!)
- }
- }
- func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
- func longPressMoreGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) { }
- // MARK: - DataSource
- @objc func reloadDataSource(withQueryDB: Bool = true) {
- datasource = self.database.getResultsTrash(filePath: getFilePath(), account: session.account)
- collectionView.reloadData()
- setNavigationRightItems()
- guard let blinkFileId, let datasource else { return }
- for itemIx in 0..<datasource.count where datasource[itemIx].fileId.contains(blinkFileId) {
- let indexPath = IndexPath(item: itemIx, section: 0)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- UIView.animate(withDuration: 0.3) {
- self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: false)
- } completion: { _ in
- guard let cell = self.collectionView.cellForItem(at: indexPath) else { return }
- cell.backgroundColor = .darkGray
- UIView.animate(withDuration: 2) {
- cell.backgroundColor = .clear
- self.blinkFileId = nil
- }
- }
- }
- }
- }
- func getFilePath() -> String {
- if filePath.isEmpty {
- guard let userId = (session.userId as NSString).addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed) else { return "" }
- let filePath = session.urlBase + "/remote.php/dav/trashbin/" + userId + "/trash"
- return filePath + "/"
- } else {
- return filePath + "/"
- }
- }
- }
|