NCCollectionViewCommon.swift 58 KB

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