// // NCCollectionViewCommon+SelectTabBarDelegate.swift // Nextcloud // // Created by Milen on 01.03.24. // Copyright © 2024 Marino Faggiana. All rights reserved. // // Author Marino Faggiana // // 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 . // import UIKit import Foundation import NextcloudKit extension NCCollectionViewCommon: NCCollectionViewCommonSelectTabBarDelegate { func onListSelected() { if layoutForView?.layout == NCGlobal.shared.layoutGrid { layoutForView?.layout = NCGlobal.shared.layoutList NCManageDatabase.shared.setLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout) self.groupByField = "name" if self.dataSource.groupByField != self.groupByField { self.dataSource.changeGroupByField(self.groupByField) } self.collectionView.reloadData() self.collectionView.collectionViewLayout.invalidateLayout() self.collectionView.setCollectionViewLayout(self.listLayout, animated: true) {_ in self.isTransitioning = false } } } func onGridSelected() { if layoutForView?.layout == NCGlobal.shared.layoutList { layoutForView?.layout = NCGlobal.shared.layoutGrid NCManageDatabase.shared.setLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl, layout: layoutForView?.layout) if isSearchingMode { self.groupByField = "name" } else { self.groupByField = "classFile" } if self.dataSource.groupByField != self.groupByField { self.dataSource.changeGroupByField(self.groupByField) } self.collectionView.reloadData() self.collectionView.collectionViewLayout.invalidateLayout() self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true) {_ in self.isTransitioning = false } } } func selectAll() { if !selectOcId.isEmpty, dataSource.getMetadataSourceForAllSections().count == selectOcId.count { selectOcId = [] } else { selectOcId = dataSource.getMetadataSourceForAllSections().compactMap({ $0.ocId }) } tabBarSelect.update(selectOcId: selectOcId, metadatas: getSelectedMetadatas(), userId: appDelegate.userId) collectionView.reloadData() } func delete() { var alertStyle = UIAlertController.Style.actionSheet if UIDevice.current.userInterfaceIdiom == .pad { alertStyle = .alert } let alertController = UIAlertController(title: NSLocalizedString("_confirm_delete_selected_", comment: ""), message: nil, preferredStyle: alertStyle) let metadatas = getSelectedMetadatas() let canDeleteServer = metadatas.allSatisfy { !$0.lock } if canDeleteServer { let copyMetadatas = metadatas alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_", comment: ""), style: .destructive) { _ in Task { var error = NKError() var ocId: [String] = [] for metadata in copyMetadatas where error == .success { error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) if error == .success { ocId.append(metadata.ocId) } } NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": false, "error": error]) } self.setEditMode(false) }) } alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in let copyMetadatas = metadatas Task { var error = NKError() var ocId: [String] = [] for metadata in copyMetadatas where error == .success { error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) if error == .success { ocId.append(metadata.ocId) } } if error != .success { NCContentPresenter().showError(error: error) } NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "onlyLocalCache": true, "error": error]) self.setEditMode(false) } }) alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { (_: UIAlertAction) in }) self.present(alertController, animated: true, completion: nil) } func move() { let metadatas = getSelectedMetadatas() NCActionCenter.shared.openSelectView(items: metadatas, mainTabBarController: self.tabBarController as? NCMainTabBarController) setEditMode(false) } func share() { let metadatas = getSelectedMetadatas() NCActionCenter.shared.openActivityViewController(selectedMetadata: metadatas, mainTabBarController: self.tabBarController as? NCMainTabBarController) setEditMode(false) } func saveAsAvailableOffline(isAnyOffline: Bool) { let metadatas = getSelectedMetadatas() if !isAnyOffline, metadatas.count > 3 { let alert = UIAlertController( title: NSLocalizedString("_set_available_offline_", comment: ""), message: NSLocalizedString("_select_offline_warning_", comment: ""), preferredStyle: .alert) alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) } self.setEditMode(false) })) alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel)) self.present(alert, animated: true) } else { metadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) } setEditMode(false) } } func lock(isAnyLocked: Bool) { let metadatas = getSelectedMetadatas() for metadata in metadatas where metadata.lock == isAnyLocked { NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !isAnyLocked) } setEditMode(false) } func saveLayout(_ layoutForView: NCDBLayoutForView) { NCManageDatabase.shared.setLayoutForView(layoutForView: layoutForView) NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource) setNavigationRightItems() } func getSelectedMetadatas() -> [tableMetadata] { var selectedMetadatas: [tableMetadata] = [] for ocId in selectOcId { guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { continue } selectedMetadatas.append(metadata) } return selectedMetadatas } func setEditMode(_ editMode: Bool) { isEditMode = editMode selectOcId.removeAll() if editMode { navigationItem.leftBarButtonItems = nil } else { setNavigationLeftItems() } setNavigationRightItems() navigationController?.interactivePopGestureRecognizer?.isEnabled = !editMode navigationItem.hidesBackButton = editMode searchController(enabled: !editMode) collectionView.reloadData() } }