123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872 |
- //
- // NCCollectionViewCommon.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 12/09/2020.
- // Copyright © 2020 Marino Faggiana. All rights reserved.
- //
- // 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 Realm
- import NextcloudKit
- import EasyTipView
- class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, NCSectionFooterDelegate, UIAdaptivePresentationControllerDelegate, NCEmptyDataSetDelegate, UIContextMenuInteractionDelegate, NCAccountRequestDelegate, NCSelectableNavigationView {
- @IBOutlet weak var collectionView: UICollectionView!
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- internal let refreshControl = UIRefreshControl()
- internal var searchController: UISearchController?
- internal var emptyDataSet: NCEmptyDataSet?
- internal var backgroundImageView = UIImageView()
- internal var serverUrl: String = ""
- internal var isDirectoryE2EE = false
- internal var isEditMode = false
- internal var selectOcId: [String] = []
- internal var metadataFolder: tableMetadata?
- internal var dataSource = NCDataSource()
- internal var richWorkspaceText: String?
- internal var headerMenu: NCSectionHeaderMenu?
- internal var isSearchingMode: Bool = false
- internal var layoutForView: NCDBLayoutForView?
- internal var selectableDataSource: [RealmSwiftObject] { dataSource.getMetadataSourceForAllSections() }
- private var autoUploadFileName = ""
- private var autoUploadDirectory = ""
- internal var groupByField = "name"
- internal var providers: [NKSearchProvider]?
- internal var searchResults: [NKSearchResult]?
- internal var listLayout: NCListLayout!
- internal var gridLayout: NCGridLayout!
- internal var literalSearch: String?
- internal var isReloadDataSourceNetworkInProgress: Bool = false
- private var pushed: Bool = false
- private var tipView: EasyTipView?
- // DECLARE
- internal var layoutKey = ""
- internal var titleCurrentFolder = ""
- internal var enableSearchBar: Bool = false
- internal var headerMenuButtonsCommand: Bool = true
- internal var headerMenuButtonsView: Bool = true
- internal var headerRichWorkspaceDisable:Bool = false
- internal var emptyImage: UIImage?
- internal var emptyTitle: String = ""
- internal var emptyDescription: String = ""
- // MARK: - View Life Cycle
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- self.navigationController?.presentationController?.delegate = self
- // CollectionView & layout
- collectionView.alwaysBounceVertical = true
- listLayout = NCListLayout()
- gridLayout = NCGridLayout()
- // Color
- view.backgroundColor = .systemBackground
- collectionView.backgroundColor = .systemBackground
- refreshControl.tintColor = .gray
- if enableSearchBar {
- searchController = UISearchController(searchResultsController: nil)
- searchController?.searchResultsUpdater = self
- searchController?.obscuresBackgroundDuringPresentation = false
- searchController?.delegate = self
- searchController?.searchBar.delegate = self
- searchController?.searchBar.autocapitalizationType = .none
- navigationItem.searchController = searchController
- navigationItem.hidesSearchBarWhenScrolling = true
- }
- // Cell
- collectionView.register(UINib(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
- collectionView.register(UINib(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
- collectionView.register(UINib(nibName: "NCTransferCell", bundle: nil), forCellWithReuseIdentifier: "transferCell")
- // Header
- collectionView.register(UINib(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
- collectionView.register(UINib(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
- // Footer
- collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
- // Refresh Control
- collectionView.addSubview(refreshControl)
- refreshControl.action(for: .valueChanged) { _ in
- self.dataSource.clearDirectory()
- self.reloadDataSourceNetwork(forced: true)
- }
- // Empty
- emptyDataSet = NCEmptyDataSet(view: collectionView, offset: getHeaderHeight(), delegate: self)
- // Long Press on CollectionView
- let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressCollecationView(_:)))
- longPressedGesture.minimumPressDuration = 0.5
- longPressedGesture.delegate = self
- longPressedGesture.delaysTouchesBegan = true
- collectionView.addGestureRecognizer(longPressedGesture)
- // TIP
- var preferences = EasyTipView.Preferences()
- preferences.drawing.foregroundColor = .white
- preferences.drawing.backgroundColor = NCBrandColor.shared.nextcloud
- preferences.drawing.textAlignment = .left
- preferences.drawing.arrowPosition = .top
- preferences.drawing.cornerRadius = 10
- preferences.animating.dismissTransform = CGAffineTransform(translationX: 0, y: 100)
- preferences.animating.showInitialTransform = CGAffineTransform(translationX: 0, y: -100)
- preferences.animating.showInitialAlpha = 0
- preferences.animating.showDuration = 1.5
- preferences.animating.dismissDuration = 1.5
- tipView = EasyTipView(text: NSLocalizedString("_tip_accountrequest_", comment: ""), preferences: preferences, delegate: self)
- // Notification
- NotificationCenter.default.addObserver(self, selector: #selector(initialize), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- appDelegate.activeViewController = self
- layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl)
- gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
- if layoutForView?.layout == NCGlobal.shared.layoutList {
- collectionView?.collectionViewLayout = listLayout
- } else {
- collectionView?.collectionViewLayout = gridLayout
- }
- NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(closeRichWorkspaceWebView), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCloseRichWorkspaceWebView), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(changeStatusFolderE2EE(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(setNavigationItem), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadAvatar), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSourceNetwork), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSourceNetwork), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSourceNetworkForced(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCopyFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
- if serverUrl == "" {
- appDelegate.activeServerUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId)
- } else {
- appDelegate.activeServerUrl = serverUrl
- }
- navigationController?.navigationBar.prefersLargeTitles = true
- navigationController?.setNavigationBarHidden(false, animated: true)
- setNavigationItem()
- reloadDataSource(forced: false)
- if !isSearchingMode {
- reloadDataSourceNetwork()
- }
- //FIXME: iPAD PDF landscape mode iOS 16
- DispatchQueue.main.async {
- self.collectionView?.collectionViewLayout.invalidateLayout()
- }
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- showTip()
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillResignActive), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCloseRichWorkspaceWebView), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadAvatar), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSourceNetwork), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCopyFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object: nil)
- pushed = false
- // REQUEST
- NCNetworking.shared.cancelUnifiedSearchFiles()
- // TIP
- self.tipView?.dismiss()
- }
- func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
- let viewController = presentationController.presentedViewController
- if viewController is NCViewerRichWorkspaceWebView {
- closeRichWorkspaceWebView()
- }
- }
- override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
- super.viewWillTransition(to: size, with: coordinator)
- self.collectionView?.collectionViewLayout.invalidateLayout()
- self.collectionView?.reloadData()
- self.tipView?.dismiss()
-
- coordinator.animate(alongsideTransition: nil) { _ in
- self.showTip()
- }
- }
- override var canBecomeFirstResponder: Bool {
- return true
- }
- // MARK: - NotificationCenter
- @objc func initialize() {
- guard !appDelegate.account.isEmpty else { return }
- // Search
- if searchController?.isActive ?? false || isSearchingMode {
- searchController?.isActive = false
- isSearchingMode = false
- }
- // Select
- if isEditMode {
- isEditMode = !isEditMode
- selectOcId.removeAll()
- }
- if self.view?.window != nil {
- if serverUrl == "" {
- appDelegate.activeServerUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId)
- } else {
- appDelegate.activeServerUrl = serverUrl
- }
- appDelegate.listFilesVC.removeAll()
- appDelegate.listFavoriteVC.removeAll()
- appDelegate.listOfflineVC.removeAll()
- }
- if serverUrl != "" {
- self.navigationController?.popToRootViewController(animated: false)
- }
- layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl)
- gridLayout.itemForLine = CGFloat(layoutForView?.itemForLine ?? 3)
- if layoutForView?.layout == NCGlobal.shared.layoutList {
- collectionView?.collectionViewLayout = listLayout
- } else {
- collectionView?.collectionViewLayout = gridLayout
- }
- setNavigationItem()
- }
- @objc func applicationWillResignActive(_ notification: NSNotification) {
- self.refreshControl.endRefreshing()
- }
- @objc func changeTheming() {
- collectionView.reloadData()
- }
- @objc func reloadDataSource(_ notification: NSNotification) {
- reloadDataSource()
- }
- @objc func reloadDataSourceNetworkForced(_ notification: NSNotification) {
- if !isSearchingMode {
- reloadDataSourceNetwork(forced: true)
- }
- }
- @objc func changeStatusFolderE2EE(_ notification: NSNotification) {
- reloadDataSource()
- }
- @objc func closeRichWorkspaceWebView() {
- reloadDataSourceNetwork()
- }
- @objc func deleteFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let fileNameView = userInfo["fileNameView"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account,
- let onlyLocalCache = userInfo["onlyLocalCache"] as? Bool
- else { return }
- if fileNameView.lowercased() == NCGlobal.shared.fileNameRichWorkspace.lowercased() {
- reloadDataSourceNetwork(forced: true)
- } else if onlyLocalCache {
- self.collectionView?.reloadData()
- } else {
- let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
- if let indexPath = indexPath, dataSource.metadatas.count > 0 {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.performBatchUpdates({
- collectionView?.deleteItems(at: [indexPath])
- }, completion: { _ in
- self.collectionView?.reloadData()
- })
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- }
- @objc func moveFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrlFrom = userInfo["serverUrlFrom"] as? String,
- serverUrlFrom == self.serverUrl
- else { return }
- let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.performBatchUpdates({
- collectionView?.deleteItems(at: [indexPath])
- }, completion: { _ in
- self.collectionView?.reloadData()
- })
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func copyFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let serverUrlTo = userInfo["serverUrlTo"] as? String,
- serverUrlTo == self.serverUrl
- else { return }
- reloadDataSource()
- }
- @objc func renameFile(_ notification: NSNotification) {
- if isSearchingMode {
- reloadDataSourceNetwork()
- } else {
- reloadDataSource()
- }
- }
- @objc func createFolder(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account,
- let e2ee = userInfo["e2ee"] as? Bool
- else { return }
- if e2ee {
- reloadDataSourceNetwork(forced: true)
- } else if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
- reloadDataSource()
- pushMetadata(metadata)
- }
- }
- @objc func favoriteFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl
- else {
- if self is NCFavorite {
- reloadDataSource()
- }
- return
- }
- dataSource.reloadMetadata(ocId: ocId)
- collectionView?.reloadData()
- }
- @objc func downloadStartFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.reloadItems(at: [indexPath])
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func downloadedFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.reloadItems(at: [indexPath])
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func downloadCancelFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: ocId)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.reloadItems(at: [indexPath])
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func uploadStartFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- guard !isSearchingMode, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
- dataSource.addMetadata(metadata)
- self.collectionView?.reloadData()
- }
- @objc func uploadedFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let ocIdTemp = userInfo["ocIdTemp"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- // do not exists metadata ?
- guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else {
- reloadDataSource()
- return
- }
-
- let (indexPath, sameSections) = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.performBatchUpdates({
- collectionView?.reloadItems(at: [indexPath])
- }, completion: { _ in
- self.collectionView?.reloadData()
- })
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func uploadCancelFile(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let ocId = userInfo["ocId"] as? String,
- let serverUrl = userInfo["serverUrl"] as? String,
- serverUrl == self.serverUrl,
- let account = userInfo["account"] as? String,
- account == appDelegate.account
- else { return }
- let (indexPath, sameSections) = dataSource.deleteMetadata(ocId: ocId)
- if let indexPath = indexPath {
- if sameSections && (indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section)) {
- collectionView?.performBatchUpdates({
- collectionView?.deleteItems(at: [indexPath])
- }, completion: { _ in
- self.collectionView?.reloadData()
- })
- } else {
- self.collectionView?.reloadData()
- }
- } else {
- reloadDataSource()
- }
- }
- @objc func triggerProgressTask(_ notification: NSNotification) {
- guard let userInfo = notification.userInfo as NSDictionary?,
- let progressNumber = userInfo["progress"] as? NSNumber,
- let totalBytes = userInfo["totalBytes"] as? Int64,
- let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64,
- let ocId = userInfo["ocId"] as? String,
- let (indexPath, _) = self.dataSource.getIndexPathMetadata(ocId: ocId) as? (IndexPath, NCMetadataForSection?)
- else { return }
- let status = userInfo["status"] as? Int ?? NCGlobal.shared.metadataStatusNormal
- if let cell = collectionView?.cellForItem(at: indexPath) {
- if let cell = cell as? NCCellProtocol {
- if progressNumber.floatValue == 1 && !(cell is NCTransferCell) {
- cell.fileProgressView?.isHidden = true
- cell.fileProgressView?.progress = .zero
- cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
- if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
- cell.writeInfoDateSize(date: metadata.date, size: metadata.size)
- } else {
- cell.fileInfoLabel?.text = ""
- }
- } else {
- cell.fileProgressView?.isHidden = false
- cell.fileProgressView?.progress = progressNumber.floatValue
- cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
- if status == NCGlobal.shared.metadataStatusInDownload {
- cell.fileInfoLabel?.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
- } else if status == NCGlobal.shared.metadataStatusInUpload {
- if totalBytes > 0 {
- cell.fileInfoLabel?.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
- } else {
- cell.fileInfoLabel?.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ …"
- }
- }
- }
- }
- }
- }
- // MARK: - Tip
- func showTip() {
- if self is NCFiles, self.view.window != nil, !NCBrandOptions.shared.disable_multiaccount, !NCBrandOptions.shared.disable_manage_account, self.serverUrl == NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId), let view = self.navigationItem.leftBarButtonItem?.customView {
- if !NCManageDatabase.shared.tipExists(NCGlobal.shared.tipNCCollectionViewCommonAccountRequest), NCManageDatabase.shared.getAllAccountOrderAlias().count > 0 {
- self.tipView?.show(forView: view)
- }
- }
- }
- // MARK: - Layout
- @objc func setNavigationItem() {
- self.setNavigationHeader()
- guard !isEditMode, layoutKey == NCGlobal.shared.layoutViewFiles else { return }
-
- // PROFILE BUTTON
-
- let activeAccount = NCManageDatabase.shared.getActiveAccount()
-
- let image = NCUtility.shared.loadUserImage(
- for: appDelegate.user,
- displayName: activeAccount?.displayName,
- userBaseUrl: appDelegate)
-
- let button = UIButton(type: .custom)
- button.setImage(image, for: .normal)
-
- if serverUrl == NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId) {
-
- var titleButton = " "
-
- if getNavigationTitle() == activeAccount?.alias {
- titleButton = ""
- } else {
- titleButton += activeAccount?.displayName ?? ""
- }
-
- button.setTitle(titleButton, for: .normal)
- button.setTitleColor(.systemBlue, for: .normal)
- }
-
- button.semanticContentAttribute = .forceLeftToRight
- button.sizeToFit()
- button.action(for: .touchUpInside) { _ in
-
- let accounts = NCManageDatabase.shared.getAllAccountOrderAlias()
- if accounts.count > 0 && !NCBrandOptions.shared.disable_multiaccount && !NCBrandOptions.shared.disable_manage_account {
-
- if let vcAccountRequest = UIStoryboard(name: "NCAccountRequest", bundle: nil).instantiateInitialViewController() as? NCAccountRequest {
-
- vcAccountRequest.activeAccount = NCManageDatabase.shared.getActiveAccount()
- vcAccountRequest.accounts = accounts
- vcAccountRequest.enableTimerProgress = false
- vcAccountRequest.enableAddAccount = true
- vcAccountRequest.delegate = self
- vcAccountRequest.dismissDidEnterBackground = true
-
- let screenHeighMax = UIScreen.main.bounds.height - (UIScreen.main.bounds.height/5)
- let numberCell = accounts.count + 1
- let height = min(CGFloat(numberCell * Int(vcAccountRequest.heightCell) + 45), screenHeighMax)
-
- let popup = NCPopupViewController(contentController: vcAccountRequest, popupWidth: 300, popupHeight: height)
-
- self.present(popup, animated: true)
- }
- // TIP
- self.dismissTip()
- }
- }
- navigationItem.setLeftBarButton(UIBarButtonItem(customView: button), animated: true)
- navigationItem.leftItemsSupplementBackButton = true
- }
- func getNavigationTitle() -> String {
- let activeAccount = NCManageDatabase.shared.getActiveAccount()
- guard let userAlias = activeAccount?.alias, !userAlias.isEmpty else {
- return NCBrandOptions.shared.brand
- }
- return userAlias
- }
- // MARK: - Empty
- func emptyDataSetView(_ view: NCEmptyView) {
- self.emptyDataSet?.setOffset(getHeaderHeight())
- if isSearchingMode {
- view.emptyImage.image = UIImage(named: "search")?.image(color: .gray, size: UIScreen.main.bounds.width)
- if isReloadDataSourceNetworkInProgress {
- view.emptyTitle.text = NSLocalizedString("_search_in_progress_", comment: "")
- } else {
- view.emptyTitle.text = NSLocalizedString("_search_no_record_found_", comment: "")
- }
- view.emptyDescription.text = NSLocalizedString("_search_instruction_", comment: "")
- } else if isReloadDataSourceNetworkInProgress {
- view.emptyImage.image = UIImage(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
- view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
- view.emptyDescription.text = ""
- } else {
- if serverUrl == "" {
- view.emptyImage.image = emptyImage
- view.emptyTitle.text = NSLocalizedString(emptyTitle, comment: "")
- view.emptyDescription.text = NSLocalizedString(emptyDescription, comment: "")
- } else {
- view.emptyImage.image = UIImage(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
- view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
- view.emptyDescription.text = NSLocalizedString("_no_file_pull_down_", comment: "")
- }
- }
- }
- // MARK: - SEARCH
- func updateSearchResults(for searchController: UISearchController) {
- self.literalSearch = searchController.searchBar.text
- }
- func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
- isSearchingMode = true
- self.providers?.removeAll()
- self.dataSource.clearDataSource()
- self.collectionView.reloadData()
- // TIP
- self.tipView?.dismiss()
- }
- func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
- if isSearchingMode && self.literalSearch?.count ?? 0 >= 2 {
- reloadDataSourceNetwork()
- }
- }
- func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
- DispatchQueue.global().async {
- NCNetworking.shared.cancelUnifiedSearchFiles()
- self.isSearchingMode = false
- self.literalSearch = ""
- self.providers?.removeAll()
- self.dataSource.clearDataSource()
- self.reloadDataSource()
- }
- }
- // MARK: - TAP EVENT
- func accountRequestChangeAccount(account: String) {
- NCManageDatabase.shared.setAccountActive(account)
- if let activeAccount = NCManageDatabase.shared.getActiveAccount() {
- NCOperationQueue.shared.cancelAllQueue()
- NCNetworking.shared.cancelAllTask()
- appDelegate.settingAccount(activeAccount.account, urlBase: activeAccount.urlBase, user: activeAccount.user, userId: activeAccount.userId, password: CCUtility.getPassword(activeAccount.account))
- }
- }
- func accountRequestAddAccount() {
- appDelegate.openLogin(viewController: self, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
- }
- func tapButtonSwitch(_ sender: Any) {
- if layoutForView?.layout == NCGlobal.shared.layoutGrid {
- // list layout
- headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
- 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)
- } else {
- // grid layout
- headerMenu?.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
- 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)
- }
- }
- func tapButtonOrder(_ sender: Any) {
- let sortMenu = NCSortMenu()
- sortMenu.toggleMenu(viewController: self, account: appDelegate.account, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
- }
- func tapButton1(_ sender: Any) {
- NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: self) { hasPermission in
- if hasPermission {
- NCPhotosPickerViewController.init(viewController: self, maxSelectedAssets: 0, singleSelectedMode: false)
- }
- }
- }
- func tapButton2(_ sender: Any) {
- guard !appDelegate.activeServerUrl.isEmpty else { return }
- let alertController = UIAlertController.createFolder(serverUrl: appDelegate.activeServerUrl, urlBase: appDelegate)
- appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
- }
- func tapButton3(_ sender: Any) {
- if let viewController = appDelegate.window?.rootViewController {
- NCDocumentCamera.shared.openScannerDocument(viewController: viewController)
- }
- }
- func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
- tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: image, sender: sender)
- }
- func tapShareListItem(with objectId: String, sender: Any) {
- if isEditMode { return }
- guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
- NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .sharing)
- }
- func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
- if isEditMode { return }
- guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
- if namedButtonMore == NCGlobal.shared.buttonMoreMore || namedButtonMore == NCGlobal.shared.buttonMoreLock {
- toggleMenu(metadata: metadata, imageIcon: image)
- } else if namedButtonMore == NCGlobal.shared.buttonMoreStop {
- NCNetworking.shared.cancelTransferMetadata(metadata) { }
- }
- }
- func tapRichWorkspace(_ sender: Any) {
- if let navigationController = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateInitialViewController() as? UINavigationController {
- if let viewerRichWorkspace = navigationController.topViewController as? NCViewerRichWorkspace {
- viewerRichWorkspace.richWorkspaceText = richWorkspaceText ?? ""
- viewerRichWorkspace.serverUrl = serverUrl
- viewerRichWorkspace.delegate = self
-
- navigationController.modalPresentationStyle = .fullScreen
- self.present(navigationController, animated: true, completion: nil)
- }
- }
- }
- func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {
- unifiedSearchMore(metadataForSection: metadataForSection)
- }
- func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
- }
- func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
- }
- func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
- }
- func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
- }
- @objc func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) {
- openMenuItems(with: nil, gestureRecognizer: gestureRecognizer)
- }
- func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
- return UIContextMenuConfiguration(identifier: nil, previewProvider: {
- return nil
- }, actionProvider: { _ in
- // let share = UIAction(title: "Share Pupper", image: UIImage(systemName: "square.and.arrow.up")) { action in
- // }
- // return UIMenu(title: "Main Menu", children: [share])
- return nil
- })
- }
- func openMenuItems(with objectId: String?, gestureRecognizer: UILongPressGestureRecognizer) {
- if gestureRecognizer.state != .began { return }
- var listMenuItems: [UIMenuItem] = []
- let touchPoint = gestureRecognizer.location(in: collectionView)
- becomeFirstResponder()
- if serverUrl != "" {
- listMenuItems.append(UIMenuItem(title: NSLocalizedString("_paste_file_", comment: ""), action: #selector(pasteFilesMenu)))
- }
- if listMenuItems.count > 0 {
- UIMenuController.shared.menuItems = listMenuItems
- UIMenuController.shared.showMenu(from: collectionView, rect: CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0))
- }
- }
- // MARK: - Menu Item
- override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
- if #selector(pasteFilesMenu) == action {
- if !UIPasteboard.general.items.isEmpty, !(metadataFolder?.e2eEncrypted ?? false) {
- return true
- }
- }
- return false
- }
- @objc func pasteFilesMenu() {
- NCFunctionCenter.shared.pastePasteboard(serverUrl: serverUrl)
- }
- // MARK: - DataSource + NC Endpoint
- @objc func reloadDataSource(forced: Bool = true) {
- guard !appDelegate.account.isEmpty else { return }
- // E2EE
- isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(serverUrl: serverUrl, userBase: appDelegate)
- // get auto upload folder
- autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
- autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, userId: appDelegate.userId, account: appDelegate.account)
- // get layout for view
- layoutForView = NCManageDatabase.shared.getLayoutForView(account: appDelegate.account, key: layoutKey, serverUrl: serverUrl)
- // set GroupField for Grid
- if !isSearchingMode && layoutForView?.layout == NCGlobal.shared.layoutGrid {
- groupByField = "classFile"
- } else {
- groupByField = "name"
- }
- }
- @objc func reloadDataSourceNetwork(forced: Bool = false) { }
- @objc func networkSearch() {
- guard !appDelegate.account.isEmpty, let literalSearch = literalSearch, !literalSearch.isEmpty
- else {
- self.refreshControl.endRefreshing()
- return
- }
- isReloadDataSourceNetworkInProgress = true
- self.dataSource.clearDataSource()
- self.refreshControl.beginRefreshing()
- self.collectionView.reloadData()
- let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
- if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 {
- NCNetworking.shared.unifiedSearchFiles(userBaseUrl: appDelegate, literal: literalSearch) { account, searchProviders in
- self.providers = searchProviders
- self.searchResults = []
- self.dataSource = NCDataSource(
- metadatas: [] ,
- account: self.appDelegate.account,
- sort: self.layoutForView?.sort,
- ascending: self.layoutForView?.ascending,
- directoryOnTop: self.layoutForView?.directoryOnTop,
- favoriteOnTop: true,
- filterLivePhoto: true,
- providers: self.providers,
- searchResults: self.searchResults)
- } update: { account, id, searchResult, metadatas in
- guard let metadatas = metadatas, metadatas.count > 0, self.isSearchingMode , let searchResult = searchResult else { return }
- NCOperationQueue.shared.unifiedSearchAddSection(collectionViewCommon: self, metadatas: metadatas, searchResult: searchResult)
- } completion: { account, error in
- self.refreshControl.endRefreshing()
- self.isReloadDataSourceNetworkInProgress = false
- self.collectionView.reloadData()
- }
- } else {
- NCNetworking.shared.searchFiles(urlBase: appDelegate, literal: literalSearch) { metadatas, error in
- DispatchQueue.main.async {
- self.refreshControl.endRefreshing()
- self.collectionView.reloadData()
- }
- guard let metadatas = metadatas, error == .success, self.isSearchingMode else { return }
- self.dataSource = NCDataSource(
- metadatas: metadatas,
- account: self.appDelegate.account,
- sort: self.layoutForView?.sort,
- ascending: self.layoutForView?.ascending,
- directoryOnTop: self.layoutForView?.directoryOnTop,
- favoriteOnTop: true,
- filterLivePhoto: true,
- groupByField: self.groupByField,
- providers: self.providers,
- searchResults: self.searchResults)
- self.isReloadDataSourceNetworkInProgress = false
- }
- }
- }
- func unifiedSearchMore(metadataForSection: NCMetadataForSection?) {
- guard let metadataForSection = metadataForSection, let searchResult = metadataForSection.lastSearchResult, let cursor = searchResult.cursor, let term = literalSearch else { return }
- metadataForSection.unifiedSearchInProgress = true
- self.collectionView?.reloadData()
- NCNetworking.shared.unifiedSearchFilesProvider(userBaseUrl: appDelegate, id: searchResult.id, term: term, limit: 5, cursor: cursor) { account, searchResult, metadatas, error in
- if error != .success {
- NCContentPresenter.shared.showError(error: error)
- }
- metadataForSection.unifiedSearchInProgress = false
- guard let searchResult = searchResult, let metadatas = metadatas else { return }
- self.dataSource.appendMetadatasToSection(metadatas, metadataForSection: metadataForSection, lastSearchResult: searchResult)
- DispatchQueue.main.async {
- self.collectionView?.reloadData()
- }
- }
- }
- @objc func networkReadFolder(forced: Bool, completion: @escaping(_ tableDirectory: tableDirectory?, _ metadatas: [tableMetadata]?, _ metadatasUpdate: [tableMetadata]?, _ metadatasDelete: [tableMetadata]?, _ error: NKError) -> Void) {
- var tableDirectory: tableDirectory?
- NCNetworking.shared.readFile(serverUrlFileName: serverUrl) { (account, metadataFolder, error) in
- guard error == .success else {
- completion(nil, nil, nil, nil, error)
- return
- }
- if let metadataFolder = metadataFolder {
- tableDirectory = NCManageDatabase.shared.setDirectory(serverUrl: self.serverUrl, richWorkspace: metadataFolder.richWorkspace, account: account)
- }
- if forced || tableDirectory?.etag != metadataFolder?.etag || metadataFolder?.e2eEncrypted ?? false {
- NCNetworking.shared.readFolder(serverUrl: self.serverUrl, account: self.appDelegate.account) { account, metadataFolder, metadatas, metadatasUpdate, _, metadatasDelete, error in
- guard error == .success else {
- completion(tableDirectory, nil, nil, nil, error)
- return
- }
- self.metadataFolder = metadataFolder
- // E2EE
- if let metadataFolder = metadataFolder, metadataFolder.e2eEncrypted, CCUtility.isEnd(toEndEnabled: self.appDelegate.account) {
- NextcloudKit.shared.getE2EEMetadata(fileId: metadataFolder.ocId, e2eToken: nil) { account, e2eMetadata, data, error in
- if error == .success, let e2eMetadata = e2eMetadata {
- if NCEndToEndMetadata.shared.decoderMetadata(e2eMetadata, privateKey: CCUtility.getEndToEndPrivateKey(account), serverUrl: self.serverUrl, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, userId: self.appDelegate.userId) {
- self.reloadDataSource()
- } else {
- let error = NKError(errorCode: NCGlobal.shared.errorDecodeMetadata, errorDescription: "_e2e_error_decode_metadata_")
- NCContentPresenter.shared.messageNotification("_error_e2ee_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error)
- }
- } else if error.errorCode != NCGlobal.shared.errorResourceNotFound {
- let error = NKError(errorCode: NCGlobal.shared.errorDecodeMetadata, errorDescription: "_e2e_error_decode_metadata_")
- NCContentPresenter.shared.messageNotification("_error_e2ee_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error)
- }
- completion(tableDirectory, metadatas, metadatasUpdate, metadatasDelete, error)
- }
- } else {
- completion(tableDirectory, metadatas, metadatasUpdate, metadatasDelete, error)
- }
- }
- } else {
- completion(tableDirectory, nil, nil, nil, NKError())
- }
- }
- }
- // MARK: - Push metadata
- func pushMetadata(_ metadata: tableMetadata) {
- guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
- appDelegate.activeMetadata = metadata
- // FILES
- if layoutKey == NCGlobal.shared.layoutViewFiles && !pushed {
- if let viewController = appDelegate.listFilesVC[serverUrlPush] {
- if viewController.isViewLoaded {
- pushViewController(viewController: viewController)
- }
- } else {
- if let viewController: NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as? NCFiles {
- viewController.isRoot = false
- viewController.serverUrl = serverUrlPush
- viewController.titleCurrentFolder = metadata.fileNameView
- appDelegate.listFilesVC[serverUrlPush] = viewController
- pushViewController(viewController: viewController)
- }
- }
- }
- // FAVORITE
- if layoutKey == NCGlobal.shared.layoutViewFavorite && !pushed {
- if let viewController = appDelegate.listFavoriteVC[serverUrlPush] {
- if viewController.isViewLoaded {
- pushViewController(viewController: viewController)
- }
- } else {
- if let viewController: NCFavorite = UIStoryboard(name: "NCFavorite", bundle: nil).instantiateInitialViewController() as? NCFavorite {
- viewController.serverUrl = serverUrlPush
- viewController.titleCurrentFolder = metadata.fileNameView
- appDelegate.listFavoriteVC[serverUrlPush] = viewController
- pushViewController(viewController: viewController)
- }
- }
- }
- // OFFLINE
- if layoutKey == NCGlobal.shared.layoutViewOffline && !pushed {
- if let viewController = appDelegate.listOfflineVC[serverUrlPush] {
- if viewController.isViewLoaded {
- pushViewController(viewController: viewController)
- }
- } else {
- if let viewController: NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as? NCOffline {
- viewController.serverUrl = serverUrlPush
- viewController.titleCurrentFolder = metadata.fileNameView
- appDelegate.listOfflineVC[serverUrlPush] = viewController
- pushViewController(viewController: viewController)
- }
- }
- }
- // RECENT ( for push use Files ... he he he )
- if layoutKey == NCGlobal.shared.layoutViewRecent && !pushed {
- if let viewController = appDelegate.listFilesVC[serverUrlPush] {
- if viewController.isViewLoaded {
- pushViewController(viewController: viewController)
- }
- } else {
- if let viewController: NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as? NCFiles {
- viewController.isRoot = false
- viewController.serverUrl = serverUrlPush
- viewController.titleCurrentFolder = metadata.fileNameView
- appDelegate.listFilesVC[serverUrlPush] = viewController
- pushViewController(viewController: viewController)
- }
- }
- }
- // SHARES ( for push use Files ... he he he )
- if layoutKey == NCGlobal.shared.layoutViewShares && !pushed {
- if let viewController = appDelegate.listFilesVC[serverUrlPush] {
- if viewController.isViewLoaded {
- pushViewController(viewController: viewController)
- }
- } else {
- if let viewController: NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as? NCFiles {
- viewController.isRoot = false
- viewController.serverUrl = serverUrlPush
- viewController.titleCurrentFolder = metadata.fileNameView
- appDelegate.listFilesVC[serverUrlPush] = viewController
- pushViewController(viewController: viewController)
- }
- }
- }
- }
- }
- // MARK: - E2EE
- extension NCCollectionViewCommon: NCEndToEndInitializeDelegate {
- func endToEndInitializeSuccess() {
- if let metadata = appDelegate.activeMetadata {
- pushMetadata(metadata)
- }
- }
- }
- // MARK: - Collection View
- extension NCCollectionViewCommon: UICollectionViewDelegate {
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
- appDelegate.activeMetadata = metadata
- let metadataSourceForAllSections = dataSource.getMetadataSourceForAllSections()
- if isEditMode {
- if let index = selectOcId.firstIndex(of: metadata.ocId) {
- selectOcId.remove(at: index)
- } else {
- selectOcId.append(metadata.ocId)
- }
- collectionView.reloadItems(at: [indexPath])
- self.navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(metadataSourceForAllSections.count)"
- return
- }
- if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: appDelegate.account) {
- let e2ee = NCEndToEndInitialize()
- e2ee.delegate = self
- e2ee.initEndToEndEncryption()
- return
- }
- if metadata.directory {
- pushMetadata(metadata)
-
- } else {
-
- let imageIcon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
- if metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue || metadata.classFile == NKCommon.typeClassFile.audio.rawValue {
- var metadatas: [tableMetadata] = []
- for metadata in metadataSourceForAllSections {
- if metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue || metadata.classFile == NKCommon.typeClassFile.audio.rawValue {
- metadatas.append(metadata)
- }
- }
- NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: metadatas, imageIcon: imageIcon)
- return
- }
- if CCUtility.fileProviderStorageExists(metadata) {
- NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon)
- } else if NextcloudKit.shared.isNetworkReachable() {
- NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileView) { _, _ in }
- } else {
- let error = NKError(errorCode: NCGlobal.shared.errorOffline, errorDescription: "_go_online_")
- NCContentPresenter.shared.showInfo(error: error)
- }
- }
- }
- func pushViewController(viewController: UIViewController) {
- if pushed { return }
- pushed = true
- navigationController?.pushViewController(viewController, animated: true)
- }
- func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
- guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
- if isEditMode || metadata.classFile == NKCommon.typeClassFile.url.rawValue { return nil }
- let identifier = indexPath as NSCopying
- var image: UIImage?
- let cell = collectionView.cellForItem(at: indexPath)
- if cell is NCListCell {
- image = (cell as! NCListCell).imageItem.image
- } else if cell is NCGridCell {
- image = (cell as! NCGridCell).imageItem.image
- }
- return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
- return NCViewerProviderContextMenu(metadata: metadata, image: image)
- }, actionProvider: { _ in
- return NCContextMenu().viewMenu(ocId: metadata.ocId, viewController: self, enableDeleteLocal: true, enableViewInFolder: false, image: image)
- })
- }
- func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
- animator.addCompletion {
- if let indexPath = configuration.identifier as? IndexPath {
- self.collectionView(collectionView, didSelectItemAt: indexPath)
- }
- }
- }
- }
- extension NCCollectionViewCommon: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
- guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
- // Thumbnail
- if !metadata.directory {
- if metadata.name == NCGlobal.shared.appName {
- if let image = NCUtility.shared.createFilePreviewImage(ocId: metadata.ocId, etag: metadata.etag, fileNameView: metadata.fileNameView, classFile: metadata.classFile, status: metadata.status, createPreviewMedia: !metadata.hasPreview) {
- (cell as! NCCellProtocol).filePreviewImageView?.image = image
- } else {
- NCOperationQueue.shared.downloadThumbnail(metadata: metadata, placeholder: true, cell: cell, view: collectionView)
- }
- } else {
- // Unified search
- switch metadata.iconName {
- case let str where str.contains("contacts"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconContacts
- case let str where str.contains("conversation"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconTalk
- case let str where str.contains("calendar"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconCalendar
- case let str where str.contains("deck"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconDeck
- case let str where str.contains("mail"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconMail
- case let str where str.contains("talk"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconTalk
- case let str where str.contains("confirm"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconConfirm
- case let str where str.contains("pages"):
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.iconPages
- default:
- (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.file
- }
- if !metadata.iconUrl.isEmpty {
- if let ownerId = NCUtility.shared.getAvatarFromIconUrl(metadata: metadata), let cell = cell as? NCCellProtocol {
- let fileName = metadata.userBaseUrl + "-" + ownerId + ".png"
- NCOperationQueue.shared.downloadAvatar(user: ownerId, dispalyName: nil, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.filePreviewImageView)
- }
- }
- // if metadata.iconName.contains("contacts"), let subline = metadata.subline, !subline.isEmpty, let cell = cell as? NCCellProtocol {
- // let components = subline.components(separatedBy: "@")
- // if let user = components.first {
- // let fileName = metadata.userBaseUrl + "-" + user + ".png"
- // NCOperationQueue.shared.downloadAvatar(user: user, dispalyName: nil, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.filePreviewImageView)
- // }
- // }
- }
- }
- // Avatar
- if metadata.ownerId.count > 0,
- metadata.ownerId != appDelegate.userId,
- appDelegate.account == metadata.account,
- let cell = cell as? NCCellProtocol {
- let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
- NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.fileAvatarImageView)
- }
- }
- func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { }
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return dataSource.numberOfSections()
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-
- let numberItems = dataSource.numberOfItemsInSection(section)
- emptyDataSet?.numberOfItemsInSection(numberItems, section: section)
- return numberItems
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- var cell: NCCellProtocol & UICollectionViewCell
- // LAYOUT LIST
- if layoutForView?.layout == NCGlobal.shared.layoutList {
- guard let listCell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as? NCListCell else { return UICollectionViewCell() }
- listCell.delegate = self
- cell = listCell
- } else {
- // LAYOUT GRID
- guard let gridCell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as? NCGridCell else { return UICollectionViewCell() }
- gridCell.delegate = self
- cell = gridCell
- }
- guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return cell }
- let tableShare = dataSource.metadatasForSection[indexPath.section].metadataShare[metadata.ocId]
- let tableDirectory = dataSource.metadatasForSection[indexPath.section].directories?.filter({ $0.ocId == metadata.ocId }).first
- var isShare = false
- var isMounted = false
- var a11yValues: [String] = []
- if metadataFolder != nil {
- isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionShared)
- isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionMounted)
- }
- cell.fileSelectImage?.image = nil
- cell.fileStatusImage?.image = nil
- cell.fileLocalImage?.image = nil
- cell.fileFavoriteImage?.image = nil
- cell.fileSharedImage?.image = nil
- cell.fileMoreImage?.image = nil
- cell.filePreviewImageView?.image = nil
- cell.filePreviewImageView?.backgroundColor = nil
- cell.fileObjectId = metadata.ocId
- cell.fileUser = metadata.ownerId
- cell.fileProgressView?.isHidden = true
- cell.fileProgressView?.progress = 0.0
- cell.hideButtonShare(false)
- cell.hideButtonMore(false)
- cell.titleInfoTrailingDefault()
- if isSearchingMode {
- cell.fileTitleLabel?.text = metadata.fileName
- cell.fileTitleLabel?.lineBreakMode = .byTruncatingTail
- if metadata.name == NCGlobal.shared.appName {
- cell.fileInfoLabel?.text = NSLocalizedString("_in_", comment: "") + " " + NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user)
- } else {
- cell.fileInfoLabel?.text = metadata.subline
- cell.titleInfoTrailingFull()
- }
- } else {
- cell.fileTitleLabel?.text = metadata.fileNameView
- cell.fileTitleLabel?.lineBreakMode = .byTruncatingMiddle
- cell.writeInfoDateSize(date: metadata.date, size: metadata.size)
- }
- if metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status == NCGlobal.shared.metadataStatusUploading {
- cell.fileProgressView?.isHidden = false
- }
- // Accessibility [shared]
- if metadata.ownerId != appDelegate.userId, appDelegate.account == metadata.account {
- a11yValues.append(NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName)
- }
- if metadata.directory {
- if metadata.e2eEncrypted {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderEncrypted
- } else if isShare {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderSharedWithMe
- } else if tableShare != nil && tableShare?.shareType != 3 {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderSharedWithMe
- } else if tableShare != nil && tableShare?.shareType == 3 {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderPublic
- } else if metadata.mountType == "group" {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderGroup
- } else if isMounted {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderExternal
- } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folderAutomaticUpload
- } else {
- cell.filePreviewImageView?.image = NCBrandColor.cacheImages.folder
- }
- // Local image: offline
- if let tableDirectory = tableDirectory, tableDirectory.offline {
- cell.fileLocalImage?.image = NCBrandColor.cacheImages.offlineFlag
- }
- // color folder
- cell.filePreviewImageView?.image = cell.filePreviewImageView?.image?.colorizeFolder(metadata: metadata, tableDirectory: tableDirectory)
- } else {
- // image local
- if dataSource.metadatasForSection[indexPath.section].metadataOffLine.contains(metadata.ocId) {
- a11yValues.append(NSLocalizedString("_offline_", comment: ""))
- cell.fileLocalImage?.image = NCBrandColor.cacheImages.offlineFlag
- } else if CCUtility.fileProviderStorageExists(metadata) {
- cell.fileLocalImage?.image = NCBrandColor.cacheImages.local
- }
- }
- // image Favorite
- if metadata.favorite {
- cell.fileFavoriteImage?.image = NCBrandColor.cacheImages.favorite
- a11yValues.append(NSLocalizedString("_favorite_", comment: ""))
- }
- // Share image
- if isShare {
- cell.fileSharedImage?.image = NCBrandColor.cacheImages.shared
- } else if tableShare != nil && tableShare?.shareType == 3 {
- cell.fileSharedImage?.image = NCBrandColor.cacheImages.shareByLink
- } else if tableShare != nil && tableShare?.shareType != 3 {
- cell.fileSharedImage?.image = NCBrandColor.cacheImages.shared
- } else {
- cell.fileSharedImage?.image = NCBrandColor.cacheImages.canShare
- }
- if appDelegate.account != metadata.account {
- cell.fileSharedImage?.image = NCBrandColor.cacheImages.shared
- }
- // Button More
- if metadata.isDownloadUpload {
- cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
- } else if metadata.lock == true {
- cell.setButtonMore(named: NCGlobal.shared.buttonMoreLock, image: NCBrandColor.cacheImages.buttonMoreLock)
- a11yValues.append(String(format: NSLocalizedString("_locked_by_", comment: ""), metadata.lockOwnerDisplayName))
- } else {
- cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
- }
- // Write status on Label Info
- switch metadata.status {
- case NCGlobal.shared.metadataStatusWaitDownload:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
- break
- case NCGlobal.shared.metadataStatusInDownload:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
- break
- case NCGlobal.shared.metadataStatusDownloading:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - ↓ …"
- break
- case NCGlobal.shared.metadataStatusWaitUpload:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
- break
- case NCGlobal.shared.metadataStatusInUpload:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
- break
- case NCGlobal.shared.metadataStatusUploading:
- cell.fileInfoLabel?.text = CCUtility.transformedSize(metadata.size) + " - ↑ …"
- break
- case NCGlobal.shared.metadataStatusUploadError:
- if metadata.sessionError != "" {
- cell.fileInfoLabel?.text = NSLocalizedString("_status_wait_upload_", comment: "") + " " + metadata.sessionError
- } else {
- cell.fileInfoLabel?.text = NSLocalizedString("_status_wait_upload_", comment: "")
- }
- break
- default:
- break
- }
- // Live Photo
- if metadata.livePhoto {
- cell.fileStatusImage?.image = NCBrandColor.cacheImages.livePhoto
- a11yValues.append(NSLocalizedString("_upload_mov_livephoto_", comment: ""))
- }
- // URL
- if metadata.classFile == NKCommon.typeClassFile.url.rawValue {
- cell.fileLocalImage?.image = nil
- cell.hideButtonShare(true)
- cell.hideButtonMore(true)
- if let ownerId = NCUtility.shared.getAvatarFromIconUrl(metadata: metadata) {
- cell.fileUser = ownerId
- }
- }
- // Disable Share Button
- if appDelegate.disableSharesView {
- cell.hideButtonShare(true)
- }
- // Separator
- if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 || isSearchingMode {
- cell.cellSeparatorView?.isHidden = true
- } else {
- cell.cellSeparatorView?.isHidden = false
- }
- // Edit mode
- if isEditMode {
- cell.selectMode(true)
- if selectOcId.contains(metadata.ocId) {
- cell.selected(true)
- a11yValues.append(NSLocalizedString("_selected_", comment: ""))
- } else {
- cell.selected(false)
- }
- } else {
- cell.selectMode(false)
- }
- // Accessibility
- cell.setAccessibility(label: metadata.fileNameView + ", " + (cell.fileInfoLabel?.text ?? ""), value: a11yValues.joined(separator: ", "))
- // Color string find in search
- if isSearchingMode, let literalSearch = self.literalSearch, let title = cell.fileTitleLabel?.text {
- let longestWordRange = (title.lowercased() as NSString).range(of: literalSearch)
- let attributedString = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15)])
- attributedString.setAttributes([NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 15), NSAttributedString.Key.foregroundColor : UIColor.systemBlue], range: longestWordRange)
- cell.fileTitleLabel?.attributedText = attributedString
- }
- // ** IMPORT MUST BE AT THE END **
- // if metadata.e2eEncrypted || isDirectoryE2EE {
- //
- // E2EE SECURE FILE DROP (DIR ONLY)
- if !metadata.directory && (metadata.e2eEncrypted || isDirectoryE2EE) {
- cell.hideButtonShare(true)
- }
- return cell
- }
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
- if kind == UICollectionView.elementKindSectionHeader {
- if indexPath.section == 0 {
- let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
- let (_, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: indexPath.section)
- self.headerMenu = header
- if layoutForView?.layout == NCGlobal.shared.layoutGrid {
- header.setImageSwitchList()
- header.buttonSwitch.accessibilityLabel = NSLocalizedString("_list_view_", comment: "")
- } else {
- header.setImageSwitchGrid()
- header.buttonSwitch.accessibilityLabel = NSLocalizedString("_grid_view_", comment: "")
- }
- header.delegate = self
- if headerMenuButtonsCommand && !isSearchingMode {
- let imageButton2 = isDirectoryE2EE ? UIImage(named: "folderEncrypted") : UIImage(named: "folder")
- let titleButton2 = isDirectoryE2EE ? NSLocalizedString("_create_folder_e2ee_", comment: "") : NSLocalizedString("_create_folder_", comment: "")
- header.setButtonsCommand(heigt: NCGlobal.shared.heightButtonsCommand, imageButton1: UIImage(named: "addImage"), titleButton1: NSLocalizedString("_upload_", comment: ""), imageButton2: imageButton2, titleButton2: titleButton2, imageButton3: UIImage(named: "scan"), titleButton3: NSLocalizedString("_scan_", comment: ""))
- } else {
- header.setButtonsCommand(heigt: 0)
- }
- if headerMenuButtonsView {
- header.setStatusButtonsView(enable: !dataSource.getMetadataSourceForAllSections().isEmpty)
- header.setButtonsView(heigt: NCGlobal.shared.heightButtonsView)
- header.setSortedTitle(layoutForView?.titleButtonHeader ?? "")
- } else {
- header.setButtonsView(heigt: 0)
- }
- header.setRichWorkspaceHeight(heightHeaderRichWorkspace)
- header.setRichWorkspaceText(richWorkspaceText)
- header.setSectionHeight(heightHeaderSection)
- if heightHeaderSection == 0 {
- header.labelSection.text = ""
- } else {
- header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
- }
- header.labelSection.textColor = .label
- return header
- } else {
- let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
- header.labelSection.text = self.dataSource.getSectionValueLocalization(indexPath: indexPath)
- header.labelSection.textColor = .label
- return header
- }
- } else {
- let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
- let sections = dataSource.numberOfSections()
- let section = indexPath.section
- let metadataForSection = self.dataSource.getMetadataForSection(indexPath.section)
- let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
- let metadatasCount: Int = metadataForSection?.metadatas.count ?? 0
- let unifiedSearchInProgress = metadataForSection?.unifiedSearchInProgress ?? false
- footer.delegate = self
- footer.metadataForSection = metadataForSection
- footer.setTitleLabel("")
- footer.setButtonText(NSLocalizedString("_show_more_results_", comment: ""))
- footer.separatorIsHidden(true)
- footer.buttonIsHidden(true)
- footer.hideActivityIndicatorSection()
- if isSearchingMode {
- if sections > 1 && section != sections - 1 {
- footer.separatorIsHidden(false)
- }
- if isSearchingMode && isPaginated && metadatasCount > 0 {
- footer.buttonIsHidden(false)
- }
- if unifiedSearchInProgress {
- footer.showActivityIndicatorSection()
- }
- } else {
- if sections == 1 || section == sections - 1 {
- let info = dataSource.getFooterInformationAllMetadatas()
- footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size)
- } else {
- footer.separatorIsHidden(false)
- }
- }
- return footer
- }
- }
- }
- extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
- func getHeaderHeight() -> CGFloat {
- var size: CGFloat = 0
- if headerMenuButtonsCommand && !isSearchingMode {
- size += NCGlobal.shared.heightButtonsCommand
- }
- if headerMenuButtonsView {
- size += NCGlobal.shared.heightButtonsView
- }
- return size
- }
- func getHeaderHeight(section:Int) -> (heightHeaderCommands: CGFloat, heightHeaderRichWorkspace: CGFloat, heightHeaderSection: CGFloat) {
- var headerRichWorkspace: CGFloat = 0
- if let richWorkspaceText = richWorkspaceText, !headerRichWorkspaceDisable {
- let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
- if trimmed.count > 0 && !isSearchingMode {
- headerRichWorkspace = UIScreen.main.bounds.size.height / 6
- }
- }
- if isSearchingMode || layoutForView?.layout == NCGlobal.shared.layoutGrid || dataSource.numberOfSections() > 1 {
- if section == 0 {
- return (getHeaderHeight(), headerRichWorkspace, NCGlobal.shared.heightSection)
- } else {
- return (0, 0, NCGlobal.shared.heightSection)
- }
- } else {
- return (getHeaderHeight(), headerRichWorkspace, 0)
- }
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- let (heightHeaderCommands, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: section)
- let heightHeader = heightHeaderCommands + heightHeaderRichWorkspace + heightHeaderSection
- return CGSize(width: collectionView.frame.width, height: heightHeader)
- }
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
- let sections = dataSource.numberOfSections()
- let metadataForSection = self.dataSource.getMetadataForSection(section)
- let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
- let metadatasCount: Int = metadataForSection?.lastSearchResult?.entries.count ?? 0
- var size = CGSize(width: collectionView.frame.width, height: 0)
- if section == sections - 1 {
- size.height += NCGlobal.shared.endHeightFooter
- } else {
- size.height += NCGlobal.shared.heightFooter
- }
- if isSearchingMode && isPaginated && metadatasCount > 0 {
- size.height += NCGlobal.shared.heightFooterButton
- }
- return size
- }
- }
- extension NCCollectionViewCommon: EasyTipViewDelegate {
- func easyTipViewDidTap(_ tipView: EasyTipView) {
- NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCCollectionViewCommonAccountRequest)
- }
- func easyTipViewDidDismiss(_ tipView: EasyTipView) { }
- func dismissTip() {
- NCManageDatabase.shared.addTip(NCGlobal.shared.tipNCCollectionViewCommonAccountRequest)
- self.tipView?.dismiss()
- }
- }
|