NCCollectionViewCommon.swift 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. //
  2. // NCCollectionViewCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/09/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import SwiftUI
  25. import RealmSwift
  26. import NextcloudKit
  27. import EasyTipView
  28. class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, NCListCellDelegate, NCGridCellDelegate, NCPhotoCellDelegate, NCSectionFirstHeaderDelegate, NCSectionFooterDelegate, NCSectionFirstHeaderEmptyDataDelegate, NCAccountSettingsModelDelegate, UIAdaptivePresentationControllerDelegate, UIContextMenuInteractionDelegate {
  29. @IBOutlet weak var collectionView: UICollectionView!
  30. let database = NCManageDatabase.shared
  31. let global = NCGlobal.shared
  32. let utility = NCUtility()
  33. let utilityFileSystem = NCUtilityFileSystem()
  34. let imageCache = NCImageCache.shared
  35. var dataSource = NCCollectionViewDataSource()
  36. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  37. var pinchGesture: UIPinchGestureRecognizer = UIPinchGestureRecognizer()
  38. var autoUploadFileName = ""
  39. var autoUploadDirectory = ""
  40. let refreshControl = UIRefreshControl()
  41. var searchController: UISearchController?
  42. var backgroundImageView = UIImageView()
  43. var serverUrl: String = ""
  44. var isEditMode = false
  45. var isDirectoryEncrypted = false
  46. var fileSelect: [String] = []
  47. var metadataFolder: tableMetadata?
  48. var richWorkspaceText: String?
  49. var sectionFirstHeader: NCSectionFirstHeader?
  50. var sectionFirstHeaderEmptyData: NCSectionFirstHeaderEmptyData?
  51. var isSearchingMode: Bool = false
  52. var layoutForView: NCDBLayoutForView?
  53. var dataSourceTask: URLSessionTask?
  54. var providers: [NKSearchProvider]?
  55. var searchResults: [NKSearchResult]?
  56. var listLayout = NCListLayout()
  57. var gridLayout = NCGridLayout()
  58. var mediaLayout = NCMediaLayout()
  59. var layoutType = NCGlobal.shared.layoutList
  60. var literalSearch: String?
  61. var tabBarSelect: NCCollectionViewCommonSelectTabBar!
  62. var attributesZoomIn: UIMenuElement.Attributes = []
  63. var attributesZoomOut: UIMenuElement.Attributes = []
  64. // DECLARE
  65. var layoutKey = ""
  66. var titleCurrentFolder = ""
  67. var titlePreviusFolder: String?
  68. var enableSearchBar: Bool = false
  69. var headerMenuTransferView = false
  70. var headerRichWorkspaceDisable: Bool = false
  71. var emptyImageName: String?
  72. var emptyImageColors: [UIColor]?
  73. var emptyTitle: String = ""
  74. var emptyDescription: String = ""
  75. var emptyDataPortaitOffset: CGFloat = 0
  76. var emptyDataLandscapeOffset: CGFloat = -20
  77. var lastScale: CGFloat = 1.0
  78. var currentScale: CGFloat = 1.0
  79. var maxColumns: Int {
  80. let screenWidth = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
  81. let column = Int(screenWidth / 44)
  82. return column
  83. }
  84. var transitionColumns = false
  85. var numberOfColumns: Int = 0
  86. var lastNumberOfColumns: Int = 0
  87. var session: NCSession.Session {
  88. NCSession.shared.getSession(controller: tabBarController)
  89. }
  90. var isLayoutPhoto: Bool {
  91. layoutForView?.layout == global.layoutPhotoRatio || layoutForView?.layout == global.layoutPhotoSquare
  92. }
  93. var isLayoutGrid: Bool {
  94. layoutForView?.layout == global.layoutGrid
  95. }
  96. var isLayoutList: Bool {
  97. layoutForView?.layout == global.layoutList
  98. }
  99. var showDescription: Bool {
  100. !headerRichWorkspaceDisable && NCKeychain().showDescription
  101. }
  102. var infoLabelsSeparator: String {
  103. layoutForView?.layout == global.layoutList ? " - " : ""
  104. }
  105. var controller: NCMainTabBarController? {
  106. self.tabBarController as? NCMainTabBarController
  107. }
  108. var defaultPredicate: NSPredicate {
  109. let predicate = NSPredicate(format: "account == %@ AND serverUrl == %@ AND NOT (status IN %@) AND NOT (livePhotoFile != '' AND classFile == %@)", session.account, self.serverUrl, NCGlobal.shared.metadataStatusHideInView, NKCommon.TypeClassFile.video.rawValue)
  110. return predicate
  111. }
  112. var isNumberOfItemsInAllSectionsNull: Bool {
  113. var totalItems = 0
  114. for section in 0..<self.collectionView.numberOfSections {
  115. totalItems += self.collectionView.numberOfItems(inSection: section)
  116. }
  117. return totalItems == 0
  118. }
  119. var numberOfItemsInAllSections: Int {
  120. var totalItems = 0
  121. for section in 0..<self.collectionView.numberOfSections {
  122. totalItems += self.collectionView.numberOfItems(inSection: section)
  123. }
  124. return totalItems
  125. }
  126. var isPinchGestureActive: Bool {
  127. return pinchGesture.state == .began || pinchGesture.state == .changed
  128. }
  129. // MARK: - View Life Cycle
  130. override func viewDidLoad() {
  131. super.viewDidLoad()
  132. tabBarSelect = NCCollectionViewCommonSelectTabBar(controller: self.controller, delegate: self)
  133. self.navigationController?.presentationController?.delegate = self
  134. collectionView.alwaysBounceVertical = true
  135. view.backgroundColor = .systemBackground
  136. collectionView.backgroundColor = .systemBackground
  137. refreshControl.tintColor = NCBrandColor.shared.textColor2
  138. if enableSearchBar {
  139. searchController = UISearchController(searchResultsController: nil)
  140. searchController?.searchResultsUpdater = self
  141. searchController?.obscuresBackgroundDuringPresentation = false
  142. searchController?.delegate = self
  143. searchController?.searchBar.delegate = self
  144. searchController?.searchBar.autocapitalizationType = .none
  145. navigationItem.searchController = searchController
  146. navigationItem.hidesSearchBarWhenScrolling = true
  147. }
  148. // Cell
  149. collectionView.register(UINib(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  150. collectionView.register(UINib(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  151. collectionView.register(UINib(nibName: "NCPhotoCell", bundle: nil), forCellWithReuseIdentifier: "photoCell")
  152. collectionView.register(UINib(nibName: "NCTransferCell", bundle: nil), forCellWithReuseIdentifier: "transferCell")
  153. // Header
  154. collectionView.register(UINib(nibName: "NCSectionFirstHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeader")
  155. collectionView.register(UINib(nibName: "NCSectionFirstHeader", bundle: nil), forSupplementaryViewOfKind: mediaSectionHeader, withReuseIdentifier: "sectionFirstHeader")
  156. collectionView.register(UINib(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  157. collectionView.register(UINib(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: mediaSectionHeader, withReuseIdentifier: "sectionHeader")
  158. collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
  159. collectionView.register(UINib(nibName: "NCSectionFirstHeaderEmptyData", bundle: nil), forSupplementaryViewOfKind: mediaSectionHeader, withReuseIdentifier: "sectionFirstHeaderEmptyData")
  160. // Footer
  161. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  162. collectionView.register(UINib(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: mediaSectionFooter, withReuseIdentifier: "sectionFooter")
  163. collectionView.refreshControl = refreshControl
  164. refreshControl.action(for: .valueChanged) { _ in
  165. self.dataSource.removeAll()
  166. self.getServerData()
  167. }
  168. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressCollecationView(_:)))
  169. longPressedGesture.minimumPressDuration = 0.5
  170. longPressedGesture.delegate = self
  171. longPressedGesture.delaysTouchesBegan = true
  172. collectionView.addGestureRecognizer(longPressedGesture)
  173. collectionView.prefetchDataSource = self
  174. collectionView.dragInteractionEnabled = true
  175. collectionView.dragDelegate = self
  176. collectionView.dropDelegate = self
  177. pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture(_:)))
  178. collectionView.addGestureRecognizer(pinchGesture)
  179. let dropInteraction = UIDropInteraction(delegate: self)
  180. self.navigationController?.navigationItem.leftBarButtonItems?.first?.customView?.addInteraction(dropInteraction)
  181. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeTheming), object: nil)
  182. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource(_:)), name: NSNotification.Name(rawValue: global.notificationCenterReloadDataSource), object: nil)
  183. NotificationCenter.default.addObserver(self, selector: #selector(getServerData(_:)), name: NSNotification.Name(rawValue: global.notificationCenterGetServerData), object: nil)
  184. DispatchQueue.main.async {
  185. self.collectionView?.collectionViewLayout.invalidateLayout()
  186. }
  187. }
  188. override func viewWillAppear(_ animated: Bool) {
  189. super.viewWillAppear(animated)
  190. navigationController?.setNavigationBarAppearance()
  191. navigationController?.navigationBar.prefersLargeTitles = true
  192. navigationController?.setNavigationBarHidden(false, animated: true)
  193. navigationItem.title = titleCurrentFolder
  194. isEditMode = false
  195. setNavigationLeftItems()
  196. setNavigationRightItems()
  197. layoutForView = database.getLayoutForView(account: session.account, key: layoutKey, serverUrl: serverUrl)
  198. if isLayoutList {
  199. collectionView?.collectionViewLayout = listLayout
  200. self.layoutType = global.layoutList
  201. } else if isLayoutGrid {
  202. collectionView?.collectionViewLayout = gridLayout
  203. self.layoutType = global.layoutGrid
  204. } else if layoutForView?.layout == global.layoutPhotoRatio {
  205. collectionView?.collectionViewLayout = mediaLayout
  206. self.layoutType = global.layoutPhotoRatio
  207. } else if layoutForView?.layout == global.layoutPhotoSquare {
  208. collectionView?.collectionViewLayout = mediaLayout
  209. self.layoutType = global.layoutPhotoSquare
  210. }
  211. collectionView.reloadData()
  212. }
  213. override func viewDidAppear(_ animated: Bool) {
  214. super.viewDidAppear(animated)
  215. NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive(_:)), name: UIApplication.willResignActiveNotification, object: nil)
  216. NotificationCenter.default.addObserver(self, selector: #selector(closeRichWorkspaceWebView), name: NSNotification.Name(rawValue: global.notificationCenterCloseRichWorkspaceWebView), object: nil)
  217. NotificationCenter.default.addObserver(self, selector: #selector(changeStatusFolderE2EE(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeStatusFolderE2EE), object: nil)
  218. NotificationCenter.default.addObserver(self, selector: #selector(reloadAvatar(_:)), name: NSNotification.Name(rawValue: global.notificationCenterReloadAvatar), object: nil)
  219. NotificationCenter.default.addObserver(self, selector: #selector(changeLayout(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeLayout), object: nil)
  220. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterDeleteFile), object: nil)
  221. NotificationCenter.default.addObserver(self, selector: #selector(copyMoveFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterCopyMoveFile), object: nil)
  222. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterRenameFile), object: nil)
  223. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: global.notificationCenterCreateFolder), object: nil)
  224. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterFavoriteFile), object: nil)
  225. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterDownloadStartFile), object: nil)
  226. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterDownloadedFile), object: nil)
  227. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterDownloadCancelFile), object: nil)
  228. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterUploadStartFile), object: nil)
  229. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterUploadedFile), object: nil)
  230. NotificationCenter.default.addObserver(self, selector: #selector(uploadedLivePhoto(_:)), name: NSNotification.Name(rawValue: global.notificationCenterUploadedLivePhoto), object: nil)
  231. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: global.notificationCenterUploadCancelFile), object: nil)
  232. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: global.notificationCenterProgressTask), object: nil)
  233. }
  234. override func viewWillDisappear(_ animated: Bool) {
  235. super.viewWillDisappear(animated)
  236. NCNetworking.shared.cancelUnifiedSearchFiles()
  237. dismissTip()
  238. // Cancel Queue & Retrieves Properties
  239. NCNetworking.shared.downloadThumbnailQueue.cancelAll()
  240. NCNetworking.shared.unifiedSearchQueue.cancelAll()
  241. dataSourceTask?.cancel()
  242. }
  243. override func viewDidDisappear(_ animated: Bool) {
  244. super.viewDidDisappear(animated)
  245. NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
  246. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterCloseRichWorkspaceWebView), object: nil)
  247. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterChangeStatusFolderE2EE), object: nil)
  248. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterReloadAvatar), object: nil)
  249. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterChangeLayout), object: nil)
  250. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterDeleteFile), object: nil)
  251. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterCopyMoveFile), object: nil)
  252. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterRenameFile), object: nil)
  253. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterCreateFolder), object: nil)
  254. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterFavoriteFile), object: nil)
  255. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterDownloadStartFile), object: nil)
  256. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterDownloadedFile), object: nil)
  257. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterDownloadCancelFile), object: nil)
  258. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterUploadStartFile), object: nil)
  259. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterUploadedFile), object: nil)
  260. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterUploadedLivePhoto), object: nil)
  261. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterUploadCancelFile), object: nil)
  262. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: global.notificationCenterProgressTask), object: nil)
  263. dataSource.removeImageCache()
  264. }
  265. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  266. let viewController = presentationController.presentedViewController
  267. if viewController is NCViewerRichWorkspaceWebView {
  268. closeRichWorkspaceWebView()
  269. }
  270. }
  271. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  272. super.viewWillTransition(to: size, with: coordinator)
  273. coordinator.animate(alongsideTransition: { _ in
  274. let animator = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) {
  275. self.collectionView?.collectionViewLayout.invalidateLayout()
  276. }
  277. animator.startAnimation()
  278. })
  279. self.dismissTip()
  280. }
  281. override var canBecomeFirstResponder: Bool {
  282. return true
  283. }
  284. override func viewWillLayoutSubviews() {
  285. super.viewWillLayoutSubviews()
  286. if let frame = tabBarController?.tabBar.frame {
  287. tabBarSelect.hostingController?.view.frame = frame
  288. }
  289. }
  290. // MARK: - NotificationCenter
  291. @objc func applicationWillResignActive(_ notification: NSNotification) {
  292. self.refreshControl.endRefreshing()
  293. }
  294. @objc func reloadAvatar(_ notification: NSNotification) {
  295. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  296. self.showTip()
  297. }
  298. guard let userInfo = notification.userInfo as NSDictionary?,
  299. let error = userInfo["error"] as? NKError,
  300. error.errorCode != global.errorNotModified else { return }
  301. setNavigationLeftItems()
  302. }
  303. @objc func changeTheming(_ notification: NSNotification) {
  304. self.reloadDataSource()
  305. }
  306. @objc func changeLayout(_ notification: NSNotification) {
  307. guard let userInfo = notification.userInfo as NSDictionary?,
  308. let account = userInfo["account"] as? String,
  309. let serverUrl = userInfo["serverUrl"] as? String,
  310. let layoutForView = userInfo["layoutForView"] as? NCDBLayoutForView,
  311. account == session.account,
  312. serverUrl == self.serverUrl
  313. else { return }
  314. if self.layoutForView?.layout == layoutForView.layout {
  315. self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
  316. self.reloadDataSource()
  317. return
  318. }
  319. self.layoutForView = self.database.setLayoutForView(layoutForView: layoutForView)
  320. layoutForView.layout = layoutForView.layout
  321. self.layoutType = layoutForView.layout
  322. collectionView.reloadData()
  323. switch layoutForView.layout {
  324. case global.layoutList:
  325. self.collectionView.setCollectionViewLayout(self.listLayout, animated: true)
  326. case global.layoutGrid:
  327. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: true)
  328. case global.layoutPhotoSquare, global.layoutPhotoRatio:
  329. self.collectionView.setCollectionViewLayout(self.mediaLayout, animated: true)
  330. default:
  331. break
  332. }
  333. self.collectionView.collectionViewLayout.invalidateLayout()
  334. self.setNavigationRightItems()
  335. }
  336. @objc func reloadDataSource(_ notification: NSNotification) {
  337. if let userInfo = notification.userInfo as? NSDictionary {
  338. if let serverUrl = userInfo["serverUrl"] as? String {
  339. if serverUrl != self.serverUrl {
  340. return
  341. }
  342. }
  343. if let clearDataSource = userInfo["clearDataSource"] as? Bool, clearDataSource {
  344. self.dataSource.removeAll()
  345. }
  346. }
  347. reloadDataSource()
  348. }
  349. @objc func getServerData(_ notification: NSNotification) {
  350. if let userInfo = notification.userInfo as NSDictionary?,
  351. let serverUrl = userInfo["serverUrl"] as? String {
  352. if serverUrl != self.serverUrl {
  353. return
  354. }
  355. }
  356. getServerData()
  357. }
  358. @objc func changeStatusFolderE2EE(_ notification: NSNotification) {
  359. reloadDataSource()
  360. }
  361. @objc func closeRichWorkspaceWebView() {
  362. reloadDataSource()
  363. }
  364. @objc func deleteFile(_ notification: NSNotification) {
  365. guard let userInfo = notification.userInfo as NSDictionary?,
  366. let error = userInfo["error"] as? NKError else { return }
  367. if error != .success {
  368. NCContentPresenter().showError(error: error)
  369. }
  370. reloadDataSource()
  371. }
  372. @objc func copyMoveFile(_ notification: NSNotification) {
  373. guard let userInfo = notification.userInfo as NSDictionary?,
  374. let serverUrl = userInfo["serverUrl"] as? String,
  375. let account = userInfo["account"] as? String,
  376. account == session.account,
  377. serverUrl == self.serverUrl else { return }
  378. reloadDataSource()
  379. }
  380. @objc func renameFile(_ notification: NSNotification) {
  381. guard let userInfo = notification.userInfo as NSDictionary?,
  382. let account = userInfo["account"] as? String,
  383. let serverUrl = userInfo["serverUrl"] as? String,
  384. let error = userInfo["error"] as? NKError,
  385. account == session.account,
  386. serverUrl == self.serverUrl
  387. else { return }
  388. if error != .success {
  389. NCContentPresenter().showError(error: error)
  390. }
  391. reloadDataSource()
  392. }
  393. @objc func createFolder(_ notification: NSNotification) {
  394. guard let userInfo = notification.userInfo as NSDictionary?,
  395. let ocId = userInfo["ocId"] as? String,
  396. let account = userInfo["account"] as? String,
  397. account == session.account,
  398. let withPush = userInfo["withPush"] as? Bool,
  399. let metadata = database.getMetadataFromOcId(ocId)
  400. else { return }
  401. if metadata.serverUrl + "/" + metadata.fileName == self.serverUrl {
  402. reloadDataSource()
  403. } else if withPush, metadata.serverUrl == self.serverUrl {
  404. reloadDataSource()
  405. if let sceneIdentifier = userInfo["sceneIdentifier"] as? String {
  406. if sceneIdentifier == controller?.sceneIdentifier {
  407. pushMetadata(metadata)
  408. }
  409. } else {
  410. pushMetadata(metadata)
  411. }
  412. }
  413. }
  414. @objc func favoriteFile(_ notification: NSNotification) {
  415. if self is NCFavorite {
  416. return reloadDataSource()
  417. }
  418. guard let userInfo = notification.userInfo as NSDictionary?,
  419. let serverUrl = userInfo["serverUrl"] as? String,
  420. serverUrl == self.serverUrl
  421. else { return }
  422. reloadDataSource()
  423. }
  424. @objc func downloadStartFile(_ notification: NSNotification) {
  425. guard let userInfo = notification.userInfo as NSDictionary?,
  426. let serverUrl = userInfo["serverUrl"] as? String,
  427. let account = userInfo["account"] as? String
  428. else { return }
  429. if account == self.session.account, serverUrl == self.serverUrl {
  430. reloadDataSource()
  431. } else {
  432. collectionView?.reloadData()
  433. }
  434. }
  435. @objc func downloadedFile(_ notification: NSNotification) {
  436. guard let userInfo = notification.userInfo as NSDictionary?,
  437. let serverUrl = userInfo["serverUrl"] as? String,
  438. let account = userInfo["account"] as? String
  439. else { return }
  440. if account == self.session.account, serverUrl == self.serverUrl {
  441. reloadDataSource()
  442. } else {
  443. collectionView?.reloadData()
  444. }
  445. }
  446. @objc func downloadCancelFile(_ notification: NSNotification) {
  447. guard let userInfo = notification.userInfo as NSDictionary?,
  448. let serverUrl = userInfo["serverUrl"] as? String,
  449. let account = userInfo["account"] as? String
  450. else { return }
  451. if account == self.session.account, serverUrl == self.serverUrl {
  452. reloadDataSource()
  453. } else {
  454. collectionView?.reloadData()
  455. }
  456. }
  457. @objc func uploadStartFile(_ notification: NSNotification) {
  458. collectionView?.reloadData()
  459. }
  460. @objc func uploadedFile(_ notification: NSNotification) {
  461. guard let userInfo = notification.userInfo as NSDictionary?,
  462. let serverUrl = userInfo["serverUrl"] as? String,
  463. let account = userInfo["account"] as? String
  464. else { return }
  465. if account == self.session.account, serverUrl == self.serverUrl {
  466. reloadDataSource()
  467. } else {
  468. collectionView?.reloadData()
  469. }
  470. }
  471. @objc func uploadedLivePhoto(_ notification: NSNotification) {
  472. guard let userInfo = notification.userInfo as NSDictionary?,
  473. let serverUrl = userInfo["serverUrl"] as? String,
  474. let account = userInfo["account"] as? String
  475. else { return }
  476. if account == self.session.account, serverUrl == self.serverUrl {
  477. reloadDataSource()
  478. } else {
  479. collectionView?.reloadData()
  480. }
  481. }
  482. @objc func uploadCancelFile(_ notification: NSNotification) {
  483. guard let userInfo = notification.userInfo as NSDictionary?,
  484. let serverUrl = userInfo["serverUrl"] as? String,
  485. let account = userInfo["account"] as? String
  486. else { return }
  487. if account == self.session.account, serverUrl == self.serverUrl {
  488. reloadDataSource()
  489. } else {
  490. collectionView?.reloadData()
  491. }
  492. }
  493. @objc func triggerProgressTask(_ notification: NSNotification) {
  494. guard let userInfo = notification.userInfo as NSDictionary?,
  495. let progressNumber = userInfo["progress"] as? NSNumber,
  496. let totalBytes = userInfo["totalBytes"] as? Int64,
  497. let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64,
  498. let ocId = userInfo["ocId"] as? String,
  499. let ocIdTransfer = userInfo["ocIdTransfer"] as? String,
  500. let session = userInfo["session"] as? String
  501. else { return }
  502. let chunk: Int = userInfo["chunk"] as? Int ?? 0
  503. let e2eEncrypted: Bool = userInfo["e2eEncrypted"] as? Bool ?? false
  504. let transfer = NCTransferProgress.shared.append(NCTransferProgress.Transfer(ocId: ocId, ocIdTransfer: ocIdTransfer, session: session, chunk: chunk, e2eEncrypted: e2eEncrypted, progressNumber: progressNumber, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected))
  505. // HEADER
  506. if self.headerMenuTransferView, transfer.session.contains("upload") {
  507. self.sectionFirstHeader?.setViewTransfer(isHidden: false, progress: transfer.progressNumber.floatValue)
  508. self.sectionFirstHeaderEmptyData?.setViewTransfer(isHidden: false, progress: transfer.progressNumber.floatValue)
  509. }
  510. }
  511. // MARK: - Layout
  512. func setNavigationLeftItems() {
  513. guard layoutKey == global.layoutViewFiles,
  514. let tableAccount = database.getTableAccount(predicate: NSPredicate(format: "account == %@", session.account)) else {
  515. return }
  516. let image = utility.loadUserImage(for: tableAccount.user, displayName: tableAccount.displayName, urlBase: tableAccount.urlBase)
  517. let accountButton = AccountSwitcherButton(type: .custom)
  518. let accounts = database.getAllAccountOrderAlias()
  519. var childrenAccountSubmenu: [UIMenuElement] = []
  520. accountButton.setImage(image, for: .normal)
  521. accountButton.setImage(image, for: .highlighted)
  522. accountButton.semanticContentAttribute = .forceLeftToRight
  523. accountButton.sizeToFit()
  524. if !accounts.isEmpty {
  525. let accountActions: [UIAction] = accounts.map { account in
  526. let image = utility.loadUserImage(for: account.user, displayName: account.displayName, urlBase: account.urlBase)
  527. var name: String = ""
  528. var url: String = ""
  529. if account.alias.isEmpty {
  530. name = account.displayName
  531. url = (URL(string: account.urlBase)?.host ?? "")
  532. } else {
  533. name = account.alias
  534. }
  535. let action = UIAction(title: name, image: image, state: account.active ? .on : .off) { _ in
  536. if !account.active {
  537. NCAccount().changeAccount(account.account, userProfile: nil, controller: self.controller) { }
  538. self.setEditMode(false)
  539. }
  540. }
  541. action.subtitle = url
  542. return action
  543. }
  544. let addAccountAction = UIAction(title: NSLocalizedString("_add_account_", comment: ""), image: utility.loadImage(named: "person.crop.circle.badge.plus", colors: NCBrandColor.shared.iconImageMultiColors)) { _ in
  545. self.appDelegate.openLogin(selector: self.global.introLogin)
  546. }
  547. let settingsAccountAction = UIAction(title: NSLocalizedString("_account_settings_", comment: ""), image: utility.loadImage(named: "gear", colors: [NCBrandColor.shared.iconImageColor])) { _ in
  548. let accountSettingsModel = NCAccountSettingsModel(controller: self.controller, delegate: self)
  549. let accountSettingsView = NCAccountSettingsView(model: accountSettingsModel)
  550. let accountSettingsController = UIHostingController(rootView: accountSettingsView)
  551. self.present(accountSettingsController, animated: true, completion: nil)
  552. }
  553. if !NCBrandOptions.shared.disable_multiaccount {
  554. childrenAccountSubmenu.append(addAccountAction)
  555. }
  556. childrenAccountSubmenu.append(settingsAccountAction)
  557. let addAccountSubmenu = UIMenu(title: "", options: .displayInline, children: childrenAccountSubmenu)
  558. let menu = UIMenu(children: accountActions + [addAccountSubmenu])
  559. accountButton.menu = menu
  560. accountButton.showsMenuAsPrimaryAction = true
  561. accountButton.onMenuOpened = {
  562. self.dismissTip()
  563. }
  564. }
  565. navigationItem.leftItemsSupplementBackButton = true
  566. navigationItem.setLeftBarButtonItems([UIBarButtonItem(customView: accountButton)], animated: true)
  567. if titlePreviusFolder != nil {
  568. navigationController?.navigationBar.topItem?.title = titlePreviusFolder
  569. }
  570. navigationItem.title = titleCurrentFolder
  571. }
  572. func setNavigationRightItems() {
  573. guard layoutKey != global.layoutViewTransfers else { return }
  574. let isTabBarHidden = self.tabBarController?.tabBar.isHidden ?? true
  575. let isTabBarSelectHidden = tabBarSelect.isHidden()
  576. func createMenuActions() -> [UIMenuElement] {
  577. guard let layoutForView = database.getLayoutForView(account: session.account, key: layoutKey, serverUrl: serverUrl) else { return [] }
  578. let select = UIAction(title: NSLocalizedString("_select_", comment: ""),
  579. image: utility.loadImage(named: "checkmark.circle"),
  580. attributes: (self.dataSource.isEmpty() || NCNetworking.shared.isOffline) ? .disabled : []) { _ in
  581. self.setEditMode(true)
  582. self.collectionView.reloadData()
  583. }
  584. let list = UIAction(title: NSLocalizedString("_list_", comment: ""), image: utility.loadImage(named: "list.bullet"), state: layoutForView.layout == global.layoutList ? .on : .off) { _ in
  585. layoutForView.layout = self.global.layoutList
  586. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  587. object: nil,
  588. userInfo: ["account": self.session.account,
  589. "serverUrl": self.serverUrl,
  590. "layoutForView": layoutForView])
  591. }
  592. let grid = UIAction(title: NSLocalizedString("_icons_", comment: ""), image: utility.loadImage(named: "square.grid.2x2"), state: layoutForView.layout == global.layoutGrid ? .on : .off) { _ in
  593. layoutForView.layout = self.global.layoutGrid
  594. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  595. object: nil,
  596. userInfo: ["account": self.session.account,
  597. "serverUrl": self.serverUrl,
  598. "layoutForView": layoutForView])
  599. }
  600. let mediaSquare = UIAction(title: NSLocalizedString("_media_square_", comment: ""), image: utility.loadImage(named: "square.grid.3x3"), state: layoutForView.layout == global.layoutPhotoSquare ? .on : .off) { _ in
  601. layoutForView.layout = self.global.layoutPhotoSquare
  602. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  603. object: nil,
  604. userInfo: ["account": self.session.account,
  605. "serverUrl": self.serverUrl,
  606. "layoutForView": layoutForView])
  607. }
  608. let mediaRatio = UIAction(title: NSLocalizedString("_media_ratio_", comment: ""), image: utility.loadImage(named: "rectangle.grid.3x2"), state: layoutForView.layout == self.global.layoutPhotoRatio ? .on : .off) { _ in
  609. layoutForView.layout = self.global.layoutPhotoRatio
  610. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  611. object: nil,
  612. userInfo: ["account": self.session.account,
  613. "serverUrl": self.serverUrl,
  614. "layoutForView": layoutForView])
  615. }
  616. let viewStyleSubmenu = UIMenu(title: "", options: .displayInline, children: [list, grid, mediaSquare, mediaRatio])
  617. let ascending = layoutForView.ascending
  618. let ascendingChevronImage = utility.loadImage(named: ascending ? "chevron.up" : "chevron.down")
  619. let isName = layoutForView.sort == "fileName"
  620. let isDate = layoutForView.sort == "date"
  621. let isSize = layoutForView.sort == "size"
  622. let byName = UIAction(title: NSLocalizedString("_name_", comment: ""), image: isName ? ascendingChevronImage : nil, state: isName ? .on : .off) { _ in
  623. if isName { // repeated press
  624. layoutForView.ascending = !layoutForView.ascending
  625. }
  626. layoutForView.sort = "fileName"
  627. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  628. object: nil,
  629. userInfo: ["account": self.session.account,
  630. "serverUrl": self.serverUrl,
  631. "layoutForView": layoutForView])
  632. }
  633. let byNewest = UIAction(title: NSLocalizedString("_date_", comment: ""), image: isDate ? ascendingChevronImage : nil, state: isDate ? .on : .off) { _ in
  634. if isDate { // repeated press
  635. layoutForView.ascending = !layoutForView.ascending
  636. }
  637. layoutForView.sort = "date"
  638. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  639. object: nil,
  640. userInfo: ["account": self.session.account,
  641. "serverUrl": self.serverUrl,
  642. "layoutForView": layoutForView])
  643. }
  644. let byLargest = UIAction(title: NSLocalizedString("_size_", comment: ""), image: isSize ? ascendingChevronImage : nil, state: isSize ? .on : .off) { _ in
  645. if isSize { // repeated press
  646. layoutForView.ascending = !layoutForView.ascending
  647. }
  648. layoutForView.sort = "size"
  649. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  650. object: nil,
  651. userInfo: ["account": self.session.account,
  652. "serverUrl": self.serverUrl,
  653. "layoutForView": layoutForView])
  654. }
  655. let sortSubmenu = UIMenu(title: NSLocalizedString("_order_by_", comment: ""), options: .displayInline, children: [byName, byNewest, byLargest])
  656. let foldersOnTop = UIAction(title: NSLocalizedString("_directory_on_top_no_", comment: ""), image: utility.loadImage(named: "folder"), state: layoutForView.directoryOnTop ? .on : .off) { _ in
  657. layoutForView.directoryOnTop = !layoutForView.directoryOnTop
  658. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterChangeLayout,
  659. object: nil,
  660. userInfo: ["account": self.session.account,
  661. "serverUrl": self.serverUrl,
  662. "layoutForView": layoutForView])
  663. }
  664. let personalFilesOnly = NCKeychain().getPersonalFilesOnly(account: session.account)
  665. let personalFilesOnlyAction = UIAction(title: NSLocalizedString("_personal_files_only_", comment: ""), image: utility.loadImage(named: "folder.badge.person.crop", colors: NCBrandColor.shared.iconImageMultiColors), state: personalFilesOnly ? .on : .off) { _ in
  666. NCKeychain().setPersonalFilesOnly(account: self.session.account, value: !personalFilesOnly)
  667. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.serverUrl, "clearDataSource": true])
  668. self.setNavigationRightItems()
  669. }
  670. let showDescriptionKeychain = NCKeychain().showDescription
  671. let showDescription = UIAction(title: NSLocalizedString("_show_description_", comment: ""), image: utility.loadImage(named: "list.dash.header.rectangle"), attributes: richWorkspaceText == nil ? .disabled : [], state: showDescriptionKeychain && richWorkspaceText != nil ? .on : .off) { _ in
  672. NCKeychain().showDescription = !showDescriptionKeychain
  673. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": self.serverUrl, "clearDataSource": true])
  674. self.setNavigationRightItems()
  675. }
  676. showDescription.subtitle = richWorkspaceText == nil ? NSLocalizedString("_no_description_available_", comment: "") : ""
  677. if layoutKey == global.layoutViewRecent {
  678. return [select]
  679. } else {
  680. var additionalSubmenu = UIMenu()
  681. if layoutKey == global.layoutViewFiles {
  682. additionalSubmenu = UIMenu(title: "", options: .displayInline, children: [foldersOnTop, personalFilesOnlyAction, showDescription])
  683. } else {
  684. additionalSubmenu = UIMenu(title: "", options: .displayInline, children: [foldersOnTop, showDescription])
  685. }
  686. return [select, viewStyleSubmenu, sortSubmenu, additionalSubmenu]
  687. }
  688. }
  689. if isEditMode {
  690. tabBarSelect.update(fileSelect: fileSelect, metadatas: getSelectedMetadatas(), userId: session.userId)
  691. tabBarSelect.show()
  692. let select = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done) {
  693. self.setEditMode(false)
  694. self.collectionView.reloadData()
  695. }
  696. navigationItem.rightBarButtonItems = [select]
  697. } else if navigationItem.rightBarButtonItems == nil || (!isEditMode && !tabBarSelect.isHidden()) {
  698. tabBarSelect.hide()
  699. let menuButton = UIBarButtonItem(image: utility.loadImage(named: "ellipsis.circle"), menu: UIMenu(children: createMenuActions()))
  700. menuButton.tintColor = NCBrandColor.shared.iconImageColor
  701. if layoutKey == global.layoutViewFiles {
  702. let notification = UIBarButtonItem(image: utility.loadImage(named: "bell"), style: .plain) {
  703. if let viewController = UIStoryboard(name: "NCNotification", bundle: nil).instantiateInitialViewController() as? NCNotification {
  704. viewController.session = self.session
  705. self.navigationController?.pushViewController(viewController, animated: true)
  706. }
  707. }
  708. notification.tintColor = NCBrandColor.shared.iconImageColor
  709. navigationItem.rightBarButtonItems = [menuButton, notification]
  710. } else {
  711. navigationItem.rightBarButtonItems = [menuButton]
  712. }
  713. } else {
  714. navigationItem.rightBarButtonItems?.first?.menu = navigationItem.rightBarButtonItems?.first?.menu?.replacingChildren(createMenuActions())
  715. }
  716. // fix, if the tabbar was hidden before the update, set it in hidden
  717. if isTabBarHidden, isTabBarSelectHidden {
  718. self.tabBarController?.tabBar.isHidden = true
  719. }
  720. }
  721. func getNavigationTitle() -> String {
  722. let tableAccount = self.database.getTableAccount(predicate: NSPredicate(format: "account == %@", session.account))
  723. if let tableAccount,
  724. !tableAccount.alias.isEmpty {
  725. return tableAccount.alias
  726. }
  727. return NCBrandOptions.shared.brand
  728. }
  729. func accountSettingsDidDismiss(tableAccount: tableAccount?, controller: NCMainTabBarController?) { }
  730. // MARK: - SEARCH
  731. func searchController(enabled: Bool) {
  732. guard enableSearchBar else { return }
  733. searchController?.searchBar.isUserInteractionEnabled = enabled
  734. if enabled {
  735. searchController?.searchBar.alpha = 1
  736. } else {
  737. searchController?.searchBar.alpha = 0.3
  738. }
  739. }
  740. func updateSearchResults(for searchController: UISearchController) {
  741. self.literalSearch = searchController.searchBar.text
  742. }
  743. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  744. isSearchingMode = true
  745. self.providers?.removeAll()
  746. self.dataSource.removeAll()
  747. self.reloadDataSource()
  748. // TIP
  749. dismissTip()
  750. }
  751. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  752. if isSearchingMode && self.literalSearch?.count ?? 0 >= 2 {
  753. networkSearch()
  754. }
  755. }
  756. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  757. NCNetworking.shared.cancelUnifiedSearchFiles()
  758. self.isSearchingMode = false
  759. self.literalSearch = ""
  760. self.providers?.removeAll()
  761. self.dataSource.removeAll()
  762. self.reloadDataSource()
  763. }
  764. // MARK: - TAP EVENT
  765. func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) {
  766. tapMoreGridItem(with: ocId, ocIdTransfer: ocIdTransfer, image: image, sender: sender)
  767. }
  768. func tapMorePhotoItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) {
  769. tapMoreGridItem(with: ocId, ocIdTransfer: ocIdTransfer, image: image, sender: sender)
  770. }
  771. func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any) {
  772. guard let metadata = self.database.getMetadataFromOcId(ocId) else { return }
  773. NCActionCenter.shared.openShare(viewController: self, metadata: metadata, page: .sharing)
  774. }
  775. func tapMoreGridItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any) {
  776. guard let metadata = self.database.getMetadataFromOcId(ocId) else { return }
  777. toggleMenu(metadata: metadata, image: image)
  778. }
  779. func tapRichWorkspace(_ sender: Any) {
  780. if let navigationController = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateInitialViewController() as? UINavigationController {
  781. if let viewerRichWorkspace = navigationController.topViewController as? NCViewerRichWorkspace {
  782. viewerRichWorkspace.richWorkspaceText = richWorkspaceText ?? ""
  783. viewerRichWorkspace.serverUrl = serverUrl
  784. viewerRichWorkspace.delegate = self
  785. navigationController.modalPresentationStyle = .fullScreen
  786. self.present(navigationController, animated: true, completion: nil)
  787. }
  788. }
  789. }
  790. func tapButtonSection(_ sender: Any, metadataForSection: NCMetadataForSection?) {
  791. unifiedSearchMore(metadataForSection: metadataForSection)
  792. }
  793. func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  794. func longPressGridItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  795. func longPressMoreListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  796. func longPressPhotoItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  797. func longPressMoreGridItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer) { }
  798. @objc func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) {
  799. openMenuItems(with: nil, gestureRecognizer: gestureRecognizer)
  800. }
  801. func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
  802. return UIContextMenuConfiguration(identifier: nil, previewProvider: {
  803. return nil
  804. }, actionProvider: { _ in
  805. return nil
  806. })
  807. }
  808. func openMenuItems(with objectId: String?, gestureRecognizer: UILongPressGestureRecognizer) {
  809. if gestureRecognizer.state != .began { return }
  810. var listMenuItems: [UIMenuItem] = []
  811. let touchPoint = gestureRecognizer.location(in: collectionView)
  812. becomeFirstResponder()
  813. if !serverUrl.isEmpty {
  814. listMenuItems.append(UIMenuItem(title: NSLocalizedString("_paste_file_", comment: ""), action: #selector(pasteFilesMenu)))
  815. }
  816. if !listMenuItems.isEmpty {
  817. UIMenuController.shared.menuItems = listMenuItems
  818. UIMenuController.shared.showMenu(from: collectionView, rect: CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0))
  819. }
  820. }
  821. // MARK: - Menu Item
  822. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  823. if #selector(pasteFilesMenu) == action {
  824. if !UIPasteboard.general.items.isEmpty, !(metadataFolder?.e2eEncrypted ?? false) {
  825. return true
  826. }
  827. } else if #selector(copyMenuFile) == action {
  828. return true
  829. } else if #selector(moveMenuFile) == action {
  830. return true
  831. }
  832. return false
  833. }
  834. @objc func pasteFilesMenu() {
  835. NCActionCenter.shared.pastePasteboard(serverUrl: serverUrl, account: session.account, controller: self.controller)
  836. }
  837. // MARK: - DataSource
  838. @objc func reloadDataSource() {
  839. if isSearchingMode {
  840. isDirectoryEncrypted = false
  841. } else {
  842. isDirectoryEncrypted = NCUtilityFileSystem().isDirectoryE2EE(session: session, serverUrl: serverUrl)
  843. }
  844. DispatchQueue.main.async {
  845. UIView.transition(with: self.collectionView,
  846. duration: 0.20,
  847. options: .transitionCrossDissolve,
  848. animations: { self.collectionView.reloadData() },
  849. completion: nil)
  850. self.setNavigationRightItems()
  851. self.refreshControl.endRefreshing()
  852. }
  853. }
  854. func getServerData() {
  855. }
  856. @objc func networkSearch() {
  857. guard !session.account.isEmpty,
  858. let literalSearch = literalSearch,
  859. !literalSearch.isEmpty else {
  860. return self.refreshControl.endRefreshing()
  861. }
  862. self.dataSource.removeAll()
  863. self.refreshControl.beginRefreshing()
  864. self.reloadDataSource()
  865. if NCCapabilities.shared.getCapabilities(account: session.account).capabilityServerVersionMajor >= global.nextcloudVersion20 {
  866. NCNetworking.shared.unifiedSearchFiles(literal: literalSearch, account: session.account) { task in
  867. self.dataSourceTask = task
  868. self.reloadDataSource()
  869. } providers: { _, searchProviders in
  870. self.providers = searchProviders
  871. self.searchResults = []
  872. self.dataSource = NCCollectionViewDataSource(metadatas: [], layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults)
  873. } update: { _, _, searchResult, metadatas in
  874. guard let metadatas, !metadatas.isEmpty, self.isSearchingMode, let searchResult else { return }
  875. NCNetworking.shared.unifiedSearchQueue.addOperation(NCCollectionViewUnifiedSearch(collectionViewCommon: self, metadatas: metadatas, searchResult: searchResult))
  876. } completion: { _, _ in
  877. self.refreshControl.endRefreshing()
  878. self.reloadDataSource()
  879. }
  880. } else {
  881. NCNetworking.shared.searchFiles(literal: literalSearch, account: session.account) { task in
  882. self.dataSourceTask = task
  883. self.reloadDataSource()
  884. } completion: { metadatasSearch, error in
  885. DispatchQueue.main.async {
  886. self.refreshControl.endRefreshing()
  887. self.reloadDataSource()
  888. }
  889. guard let metadatasSearch, error == .success, self.isSearchingMode else { return }
  890. let ocId = metadatasSearch.map { $0.ocId }
  891. let metadatas = self.database.getResultsMetadatasPredicate(NSPredicate(format: "ocId IN %@", ocId), layoutForView: self.layoutForView)
  892. self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: self.layoutForView, providers: self.providers, searchResults: self.searchResults)
  893. }
  894. }
  895. }
  896. func unifiedSearchMore(metadataForSection: NCMetadataForSection?) {
  897. guard let metadataForSection = metadataForSection, let lastSearchResult = metadataForSection.lastSearchResult, let cursor = lastSearchResult.cursor, let term = literalSearch else { return }
  898. metadataForSection.unifiedSearchInProgress = true
  899. self.collectionView?.reloadData()
  900. NCNetworking.shared.unifiedSearchFilesProvider(id: lastSearchResult.id, term: term, limit: 5, cursor: cursor, account: session.account) { task in
  901. self.dataSourceTask = task
  902. self.reloadDataSource()
  903. } completion: { _, searchResult, metadatas, error in
  904. if error != .success {
  905. NCContentPresenter().showError(error: error)
  906. }
  907. metadataForSection.unifiedSearchInProgress = false
  908. guard let searchResult = searchResult, let metadatas = metadatas else { return }
  909. self.dataSource.appendMetadatasToSection(metadatas, metadataForSection: metadataForSection, lastSearchResult: searchResult)
  910. DispatchQueue.main.async {
  911. self.collectionView?.reloadData()
  912. }
  913. }
  914. }
  915. // MARK: - Push metadata
  916. func pushMetadata(_ metadata: tableMetadata) {
  917. guard let navigationCollectionViewCommon = self.controller?.navigationCollectionViewCommon else { return }
  918. let serverUrlPush = utilityFileSystem.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)
  919. if let viewController = navigationCollectionViewCommon.first(where: { $0.navigationController == self.navigationController && $0.serverUrl == serverUrlPush})?.viewController, viewController.isViewLoaded {
  920. navigationController?.pushViewController(viewController, animated: true)
  921. } else {
  922. if let viewController: NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as? NCFiles {
  923. viewController.isRoot = false
  924. viewController.serverUrl = serverUrlPush
  925. viewController.titlePreviusFolder = navigationItem.title
  926. viewController.titleCurrentFolder = metadata.fileNameView
  927. navigationCollectionViewCommon.append(NavigationCollectionViewCommon(serverUrl: serverUrlPush, navigationController: self.navigationController, viewController: viewController))
  928. navigationController?.pushViewController(viewController, animated: true)
  929. }
  930. }
  931. }
  932. // MARK: - Header size
  933. func isHeaderMenuTransferViewEnabled() -> [tableMetadata]? {
  934. if headerMenuTransferView,
  935. NCNetworking.shared.isOnline,
  936. let results = database.getResultsMetadatas(predicate: NSPredicate(format: "status IN %@", [global.metadataStatusWaitUpload, global.metadataStatusUploading])),
  937. !results.isEmpty {
  938. return Array(results)
  939. }
  940. return nil
  941. }
  942. func getHeaderHeight(section: Int) -> (heightHeaderCommands: CGFloat, heightHeaderRichWorkspace: CGFloat, heightHeaderSection: CGFloat) {
  943. var headerRichWorkspace: CGFloat = 0
  944. func getHeaderHeight() -> CGFloat {
  945. var size: CGFloat = 0
  946. if isHeaderMenuTransferViewEnabled() != nil {
  947. if !isSearchingMode {
  948. size += global.heightHeaderTransfer
  949. }
  950. }
  951. return size
  952. }
  953. if let richWorkspaceText = richWorkspaceText, showDescription {
  954. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  955. if !trimmed.isEmpty && !isSearchingMode {
  956. headerRichWorkspace = UIScreen.main.bounds.size.height / 6
  957. }
  958. }
  959. if isSearchingMode || layoutForView?.groupBy != "none" || self.dataSource.numberOfSections() > 1 {
  960. if section == 0 {
  961. return (getHeaderHeight(), headerRichWorkspace, global.heightSection)
  962. } else {
  963. return (0, 0, global.heightSection)
  964. }
  965. } else {
  966. return (getHeaderHeight(), headerRichWorkspace, 0)
  967. }
  968. }
  969. func sizeForHeaderInSection(section: Int) -> CGSize {
  970. var height: CGFloat = 0
  971. if isEditMode {
  972. return CGSize.zero
  973. } else if self.dataSource.isEmpty() {
  974. height = utility.getHeightHeaderEmptyData(view: view, portraitOffset: emptyDataPortaitOffset, landscapeOffset: emptyDataLandscapeOffset, isHeaderMenuTransferViewEnabled: isHeaderMenuTransferViewEnabled() != nil)
  975. } else {
  976. let (heightHeaderCommands, heightHeaderRichWorkspace, heightHeaderSection) = getHeaderHeight(section: section)
  977. height = heightHeaderCommands + heightHeaderRichWorkspace + heightHeaderSection
  978. }
  979. return CGSize(width: collectionView.frame.width, height: height)
  980. }
  981. // MARK: - Footer size
  982. func sizeForFooterInSection(section: Int) -> CGSize {
  983. let sections = dataSource.numberOfSections()
  984. let metadataForSection = self.dataSource.getMetadataForSection(section)
  985. let isPaginated = metadataForSection?.lastSearchResult?.isPaginated ?? false
  986. let metadatasCount: Int = metadataForSection?.lastSearchResult?.entries.count ?? 0
  987. var size = CGSize(width: collectionView.frame.width, height: 0)
  988. if section == sections - 1 {
  989. size.height += global.endHeightFooter
  990. } else {
  991. size.height += global.heightFooter
  992. }
  993. if isSearchingMode && isPaginated && metadatasCount > 0 {
  994. size.height += global.heightFooterButton
  995. }
  996. return size
  997. }
  998. }
  999. // MARK: -
  1000. private class AccountSwitcherButton: UIButton {
  1001. var onMenuOpened: (() -> Void)?
  1002. override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willDisplayMenuFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
  1003. super.contextMenuInteraction(interaction, willDisplayMenuFor: configuration, animator: animator)
  1004. onMenuOpened?()
  1005. }
  1006. }