NCCollectionViewCommon.swift 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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 Foundation
  24. import NCCommunication
  25. class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, UIAdaptivePresentationControllerDelegate, NCEmptyDataSetDelegate, UIContextMenuInteractionDelegate {
  26. @IBOutlet weak var collectionView: UICollectionView!
  27. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. internal let refreshControl = UIRefreshControl()
  29. internal var searchController: UISearchController?
  30. internal var emptyDataSet: NCEmptyDataSet?
  31. internal var serverUrl: String = ""
  32. internal var isEncryptedFolder = false
  33. internal var isEditMode = false
  34. internal var selectOcId: [String] = []
  35. internal var metadatasSource: [tableMetadata] = []
  36. internal var metadataFolder: tableMetadata?
  37. internal var metadataTouch: tableMetadata?
  38. internal var dataSource = NCDataSource()
  39. internal var richWorkspaceText: String?
  40. internal var layout = ""
  41. internal var sort: String = ""
  42. internal var ascending: Bool = true
  43. internal var directoryOnTop: Bool = true
  44. internal var groupBy = ""
  45. internal var titleButton = ""
  46. internal var itemForLine = 0
  47. private var autoUploadFileName = ""
  48. private var autoUploadDirectory = ""
  49. internal var listLayout: NCListLayout!
  50. internal var gridLayout: NCGridLayout!
  51. private let headerHeight: CGFloat = 50
  52. private var headerRichWorkspaceHeight: CGFloat = 0
  53. private let footerHeight: CGFloat = 100
  54. private var timerInputSearch: Timer?
  55. internal var literalSearch: String?
  56. internal var isSearching: Bool = false
  57. internal var isReloadDataSourceNetworkInProgress: Bool = false
  58. var selectedIndexPath: IndexPath!
  59. // DECLARE
  60. internal var layoutKey = ""
  61. internal var titleCurrentFolder = ""
  62. internal var enableSearchBar: Bool = false
  63. internal var emptyImage: UIImage?
  64. internal var emptyTitle: String = ""
  65. internal var emptyDescription: String = ""
  66. required init?(coder aDecoder: NSCoder) {
  67. super.init(coder: aDecoder)
  68. }
  69. override func viewDidLoad() {
  70. super.viewDidLoad()
  71. self.navigationController?.presentationController?.delegate = self
  72. if enableSearchBar {
  73. searchController = UISearchController(searchResultsController: nil)
  74. searchController?.searchResultsUpdater = self
  75. searchController?.obscuresBackgroundDuringPresentation = false
  76. searchController?.delegate = self
  77. searchController?.searchBar.delegate = self
  78. navigationItem.searchController = searchController
  79. navigationItem.hidesSearchBarWhenScrolling = false
  80. }
  81. // Cell
  82. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  83. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  84. collectionView.register(UINib.init(nibName: "NCTransferCell", bundle: nil), forCellWithReuseIdentifier: "transferCell")
  85. // Header
  86. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  87. // Footer
  88. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  89. collectionView.alwaysBounceVertical = true
  90. listLayout = NCListLayout()
  91. gridLayout = NCGridLayout()
  92. // Refresh Control
  93. collectionView.addSubview(refreshControl)
  94. refreshControl.tintColor = .gray
  95. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetworkRefreshControl), for: .valueChanged)
  96. // Empty
  97. emptyDataSet = NCEmptyDataSet.init(view: collectionView, offset: 0, delegate: self)
  98. // Long Press on CollectionView
  99. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressCollecationView(_:)))
  100. longPressedGesture.minimumPressDuration = 0.5
  101. longPressedGesture.delegate = self
  102. longPressedGesture.delaysTouchesBegan = true
  103. collectionView.addGestureRecognizer(longPressedGesture)
  104. // Notification
  105. NotificationCenter.default.addObserver(self, selector: #selector(initializeMain), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitializeMain), object: nil)
  106. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  107. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSource), object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSourceNetworkForced(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced), object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(changeStatusFolderE2EE(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(closeRichWorkspaceWebView), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCloseRichWorkspaceWebView), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDeleteFile), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterMoveFile), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCopyFile), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterRenameFile), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCreateFolder), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterFavoriteFile), object: nil)
  117. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  118. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  119. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  120. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  121. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  122. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile), object: nil)
  123. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterProgressTask), object:nil)
  124. changeTheming()
  125. }
  126. override func viewWillAppear(_ animated: Bool) {
  127. super.viewWillAppear(animated)
  128. appDelegate.activeViewController = self
  129. if serverUrl == "" {
  130. appDelegate.activeServerUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  131. } else {
  132. appDelegate.activeServerUrl = serverUrl
  133. }
  134. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: serverUrl)
  135. gridLayout.itemForLine = CGFloat(itemForLine)
  136. if layout == NCGlobal.shared.layoutList {
  137. collectionView?.collectionViewLayout = listLayout
  138. } else {
  139. collectionView?.collectionViewLayout = gridLayout
  140. }
  141. navigationController?.navigationBar.prefersLargeTitles = true
  142. navigationController?.setNavigationBarHidden(false, animated: true)
  143. setNavigationItem()
  144. reloadDataSource()
  145. }
  146. override func viewDidAppear(_ animated: Bool) {
  147. super.viewDidAppear(animated)
  148. reloadDataSourceNetwork()
  149. }
  150. func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
  151. let viewController = presentationController.presentedViewController
  152. if viewController is NCViewerRichWorkspaceWebView {
  153. closeRichWorkspaceWebView()
  154. } else if viewController is UINavigationController {
  155. if (viewController as! UINavigationController).topViewController is NCFileViewInFolder {
  156. appDelegate.activeFileViewInFolder = nil
  157. }
  158. }
  159. }
  160. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  161. super.viewWillTransition(to: size, with: coordinator)
  162. coordinator.animate(alongsideTransition: nil) { _ in
  163. self.collectionView?.collectionViewLayout.invalidateLayout()
  164. }
  165. }
  166. override var canBecomeFirstResponder: Bool {
  167. return true
  168. }
  169. func setNavigationItem() {
  170. if isEditMode {
  171. navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage.init(named: "navigationMore"), style: .plain, target: self, action:#selector(tapSelectMenu(sender:)))
  172. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, target: self, action: #selector(tapSelect(sender:)))
  173. navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(dataSource.metadatas.count)"
  174. } else {
  175. navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(tapSelect(sender:)))
  176. navigationItem.leftBarButtonItem = nil
  177. navigationItem.title = titleCurrentFolder
  178. // PROFILE BUTTON
  179. if layoutKey == NCGlobal.shared.layoutViewFiles {
  180. let profileButton = UIButton.init(type: .custom)
  181. profileButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
  182. profileButton.addTarget(self, action: #selector(profileButtonTapped(sender:)), for: .touchUpInside)
  183. var image = NCUtility.shared.loadImage(named: "person.crop.circle")
  184. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + appDelegate.user + ".png"
  185. if let userImage = UIImage(contentsOfFile: fileNamePath) {
  186. image = userImage
  187. }
  188. UIGraphicsBeginImageContextWithOptions(profileButton.bounds.size, false, 3.0)
  189. UIBezierPath.init(roundedRect: profileButton.bounds, cornerRadius: profileButton.bounds.size.height).addClip()
  190. image.draw(in: profileButton.bounds)
  191. image = UIGraphicsGetImageFromCurrentImageContext() ?? NCUtility.shared.loadImage(named: "person.crop.circle")
  192. UIGraphicsEndImageContext()
  193. profileButton.setImage(image, for: .normal)
  194. profileButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
  195. profileButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
  196. navigationItem.setLeftBarButton(UIBarButtonItem(customView: profileButton), animated: true)
  197. }
  198. }
  199. }
  200. // MARK: - NotificationCenter
  201. @objc func initializeMain() {
  202. if appDelegate.account == "" { return }
  203. if searchController?.isActive ?? false {
  204. searchController?.isActive = false
  205. }
  206. // set active serverUrl
  207. if self.view?.window != nil {
  208. if serverUrl == "" {
  209. appDelegate.activeServerUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  210. } else {
  211. appDelegate.activeServerUrl = serverUrl
  212. }
  213. }
  214. if self is NCFiles || self is NCFavorite || self is NCOffline {
  215. self.navigationController?.popToRootViewController(animated: false)
  216. }
  217. appDelegate.listFilesVC.removeAll()
  218. appDelegate.listFavoriteVC.removeAll()
  219. appDelegate.listOfflineVC.removeAll()
  220. selectOcId.removeAll()
  221. setNavigationItem()
  222. reloadDataSource()
  223. }
  224. @objc func changeTheming() {
  225. view.backgroundColor = NCBrandColor.shared.backgroundView
  226. collectionView.backgroundColor = NCBrandColor.shared.backgroundView
  227. collectionView.reloadData()
  228. }
  229. @objc func reloadDataSource(_ notification: NSNotification) {
  230. if self.view?.window == nil { return }
  231. reloadDataSource()
  232. }
  233. @objc func reloadDataSourceNetworkForced(_ notification: NSNotification) {
  234. if self.view?.window == nil { return }
  235. if let userInfo = notification.userInfo as NSDictionary? {
  236. if let serverUrl = userInfo["serverUrl"] as? String {
  237. if serverUrl == self.serverUrl {
  238. reloadDataSourceNetwork(forced: true)
  239. }
  240. }
  241. } else {
  242. reloadDataSourceNetwork(forced: true)
  243. }
  244. }
  245. @objc func changeStatusFolderE2EE(_ notification: NSNotification) {
  246. if self.view?.window == nil { return }
  247. reloadDataSource()
  248. }
  249. @objc func closeRichWorkspaceWebView() {
  250. if self.view?.window == nil { return }
  251. reloadDataSourceNetwork()
  252. }
  253. @objc func deleteFile(_ notification: NSNotification) {
  254. if self.view?.window == nil { return }
  255. if let userInfo = notification.userInfo as NSDictionary? {
  256. if let ocId = userInfo["ocId"] as? String, let fileNameView = userInfo["fileNameView"] as? String, let onlyLocal = userInfo["onlyLocal"] as? Bool {
  257. if onlyLocal {
  258. reloadDataSource()
  259. } else if fileNameView.lowercased() == NCGlobal.shared.fileNameRichWorkspace.lowercased() {
  260. reloadDataSourceNetwork(forced: true)
  261. } else {
  262. if let row = dataSource.deleteMetadata(ocId: ocId) {
  263. let indexPath = IndexPath(row: row, section: 0)
  264. collectionView?.performBatchUpdates({
  265. collectionView?.deleteItems(at: [indexPath])
  266. }, completion: { (_) in
  267. self.collectionView?.reloadData()
  268. })
  269. }
  270. }
  271. }
  272. }
  273. }
  274. @objc func moveFile(_ notification: NSNotification) {
  275. if self.view?.window == nil { return }
  276. if let userInfo = notification.userInfo as NSDictionary? {
  277. if let ocId = userInfo["ocId"] as? String, let serverUrlFrom = userInfo["serverUrlFrom"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  278. // DEL
  279. if serverUrlFrom == serverUrl && metadata.account == appDelegate.account {
  280. if let row = dataSource.deleteMetadata(ocId: ocId) {
  281. let indexPath = IndexPath(row: row, section: 0)
  282. collectionView?.performBatchUpdates({
  283. collectionView?.deleteItems(at: [indexPath])
  284. }, completion: { (_) in
  285. self.collectionView?.reloadData()
  286. })
  287. }
  288. // ADD
  289. } else if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  290. if let row = dataSource.addMetadata(metadata) {
  291. let indexPath = IndexPath(row: row, section: 0)
  292. collectionView?.performBatchUpdates({
  293. collectionView?.insertItems(at: [indexPath])
  294. }, completion: { (_) in
  295. self.collectionView?.reloadData()
  296. })
  297. }
  298. }
  299. }
  300. }
  301. }
  302. @objc func copyFile(_ notification: NSNotification) {
  303. if self.view?.window == nil { return }
  304. if let userInfo = notification.userInfo as NSDictionary? {
  305. if let serverUrlTo = userInfo["serverUrlTo"] as? String {
  306. if serverUrlTo == self.serverUrl {
  307. reloadDataSource()
  308. }
  309. }
  310. }
  311. }
  312. @objc func renameFile(_ notification: NSNotification) {
  313. if self.view?.window == nil { return }
  314. reloadDataSource()
  315. }
  316. @objc func createFolder(_ notification: NSNotification) {
  317. if self.view?.window == nil { return }
  318. if let userInfo = notification.userInfo as NSDictionary? {
  319. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  320. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  321. if let row = dataSource.addMetadata(metadata) {
  322. let indexPath = IndexPath(row: row, section: 0)
  323. collectionView?.performBatchUpdates({
  324. collectionView?.insertItems(at: [indexPath])
  325. }, completion: { (_) in
  326. self.collectionView?.reloadData()
  327. })
  328. }
  329. }
  330. }
  331. } else {
  332. reloadDataSourceNetwork()
  333. }
  334. }
  335. @objc func favoriteFile(_ notification: NSNotification) {
  336. if self.view?.window == nil { return }
  337. if let userInfo = notification.userInfo as NSDictionary? {
  338. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  339. if dataSource.getIndexMetadata(ocId: metadata.ocId) != nil {
  340. reloadDataSource()
  341. }
  342. }
  343. }
  344. }
  345. @objc func downloadStartFile(_ notification: NSNotification) {
  346. if self.view?.window == nil { return }
  347. if let userInfo = notification.userInfo as NSDictionary? {
  348. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  349. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  350. let indexPath = IndexPath(row: row, section: 0)
  351. if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
  352. collectionView?.reloadItems(at: [indexPath])
  353. }
  354. }
  355. }
  356. }
  357. }
  358. @objc func downloadedFile(_ notification: NSNotification) {
  359. if self.view?.window == nil { return }
  360. if let userInfo = notification.userInfo as NSDictionary? {
  361. if let ocId = userInfo["ocId"] as? String, let _ = userInfo["errorCode"] as? Int, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  362. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  363. let indexPath = IndexPath(row: row, section: 0)
  364. if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
  365. collectionView?.reloadItems(at: [indexPath])
  366. }
  367. }
  368. }
  369. }
  370. }
  371. @objc func downloadCancelFile(_ notification: NSNotification) {
  372. if self.view?.window == nil { return }
  373. if let userInfo = notification.userInfo as NSDictionary? {
  374. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  375. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  376. let indexPath = IndexPath(row: row, section: 0)
  377. if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
  378. collectionView?.reloadItems(at: [indexPath])
  379. }
  380. }
  381. }
  382. }
  383. }
  384. @objc func uploadStartFile(_ notification: NSNotification) {
  385. if self.view?.window == nil { return }
  386. if let userInfo = notification.userInfo as NSDictionary? {
  387. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  388. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  389. dataSource.addMetadata(metadata)
  390. self.collectionView?.reloadData()
  391. }
  392. }
  393. }
  394. }
  395. @objc func uploadedFile(_ notification: NSNotification) {
  396. if self.view?.window == nil { return }
  397. if let userInfo = notification.userInfo as NSDictionary? {
  398. if let ocId = userInfo["ocId"] as? String, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  399. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  400. dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
  401. collectionView?.reloadData()
  402. }
  403. }
  404. }
  405. }
  406. @objc func uploadCancelFile(_ notification: NSNotification) {
  407. if self.view?.window == nil { return }
  408. if let userInfo = notification.userInfo as NSDictionary? {
  409. if let ocId = userInfo["ocId"] as? String, let serverUrl = userInfo["serverUrl"] as? String, let account = userInfo["account"] as? String {
  410. if serverUrl == self.serverUrl && account == appDelegate.account {
  411. if let row = dataSource.deleteMetadata(ocId: ocId) {
  412. let indexPath = IndexPath(row: row, section: 0)
  413. collectionView?.performBatchUpdates({
  414. collectionView?.deleteItems(at: [indexPath])
  415. }, completion: { (_) in
  416. self.collectionView?.reloadData()
  417. })
  418. } else {
  419. self.reloadDataSource()
  420. }
  421. }
  422. }
  423. }
  424. }
  425. @objc func triggerProgressTask(_ notification: NSNotification) {
  426. if self.view?.window == nil { return }
  427. if let userInfo = notification.userInfo as NSDictionary? {
  428. if let ocId = userInfo["ocId"] as? String {
  429. let _ = userInfo["account"] as? String ?? ""
  430. let _ = userInfo["serverUrl"] as? String ?? ""
  431. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  432. let progress = progressNumber.floatValue
  433. let status = userInfo["status"] as? Int ?? NCGlobal.shared.metadataStatusNormal
  434. let totalBytes = userInfo["totalBytes"] as? Int64 ?? 0
  435. let totalBytesExpected = userInfo["totalBytesExpected"] as? Int64 ?? 0
  436. let progressType = NCGlobal.progressType(progress: progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected)
  437. appDelegate.listProgress[ocId] = progressType
  438. if let index = dataSource.getIndexMetadata(ocId: ocId) {
  439. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  440. if cell is NCListCell {
  441. let cell = cell as! NCListCell
  442. if progress > 0 {
  443. cell.progressView?.isHidden = false
  444. cell.progressView?.progress = progress
  445. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  446. if status == NCGlobal.shared.metadataStatusInDownload {
  447. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  448. } else if status == NCGlobal.shared.metadataStatusInUpload {
  449. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  450. }
  451. }
  452. } else if cell is NCTransferCell {
  453. let cell = cell as! NCTransferCell
  454. if progress > 0 {
  455. cell.progressView?.isHidden = false
  456. cell.progressView?.progress = progress
  457. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  458. if status == NCGlobal.shared.metadataStatusInDownload {
  459. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  460. } else if status == NCGlobal.shared.metadataStatusInUpload {
  461. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  462. }
  463. }
  464. } else if cell is NCGridCell {
  465. let cell = cell as! NCGridCell
  466. if progress > 0 {
  467. cell.progressView.isHidden = false
  468. cell.progressView.progress = progress
  469. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. // MARK: - Empty
  478. func emptyDataSetView(_ view: NCEmptyView) {
  479. if searchController?.isActive ?? false {
  480. view.emptyImage.image = UIImage.init(named: "search")?.image(color: .gray, size: UIScreen.main.bounds.width)
  481. if isReloadDataSourceNetworkInProgress {
  482. view.emptyTitle.text = NSLocalizedString("_search_in_progress_", comment: "")
  483. } else {
  484. view.emptyTitle.text = NSLocalizedString("_search_no_record_found_", comment: "")
  485. }
  486. view.emptyDescription.text = NSLocalizedString("_search_instruction_", comment: "")
  487. } else if isReloadDataSourceNetworkInProgress {
  488. view.emptyImage.image = UIImage.init(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  489. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  490. view.emptyDescription.text = ""
  491. } else {
  492. if serverUrl == "" {
  493. view.emptyImage.image = emptyImage
  494. view.emptyTitle.text = NSLocalizedString(emptyTitle, comment: "")
  495. view.emptyDescription.text = NSLocalizedString(emptyDescription, comment: "")
  496. } else {
  497. view.emptyImage.image = UIImage.init(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  498. view.emptyTitle.text = NSLocalizedString("_files_no_files_", comment: "")
  499. view.emptyDescription.text = NSLocalizedString("_no_file_pull_down_", comment: "")
  500. }
  501. }
  502. }
  503. // MARK: - SEARCH
  504. func updateSearchResults(for searchController: UISearchController) {
  505. timerInputSearch?.invalidate()
  506. timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(reloadDataSourceNetwork), userInfo: nil, repeats: false)
  507. literalSearch = searchController.searchBar.text
  508. collectionView?.reloadData()
  509. }
  510. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  511. isSearching = true
  512. metadatasSource.removeAll()
  513. reloadDataSource()
  514. }
  515. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  516. isSearching = false
  517. literalSearch = ""
  518. reloadDataSource()
  519. }
  520. // MARK: - TAP EVENT
  521. @objc func tapSelect(sender: Any) {
  522. isEditMode = !isEditMode
  523. selectOcId.removeAll()
  524. setNavigationItem()
  525. self.collectionView.reloadData()
  526. }
  527. @objc func profileButtonTapped(sender: Any) {
  528. let accounts = NCManageDatabase.shared.getAllAccount()
  529. if accounts.count > 1 {
  530. if let vcAccountRequest = UIStoryboard(name: "NCAccountRequest", bundle: nil).instantiateInitialViewController() as? NCAccountRequest {
  531. vcAccountRequest.accounts = accounts
  532. vcAccountRequest.enableTimerProgress = false
  533. vcAccountRequest.enableAddAccount = true
  534. let screenHeighMax = UIScreen.main.bounds.height - (UIScreen.main.bounds.height/5)
  535. let height = min(CGFloat(accounts.count * Int(vcAccountRequest.heightCell) + 65), screenHeighMax)
  536. let popup = NCPopupViewController(contentController: vcAccountRequest, popupWidth: 300, popupHeight: height)
  537. popup.backgroundAlpha = 0.8
  538. UIApplication.shared.keyWindow?.rootViewController?.present(popup, animated: true)
  539. }
  540. }
  541. }
  542. func tapSwitchHeader(sender: Any) {
  543. if collectionView.collectionViewLayout == gridLayout {
  544. // list layout
  545. UIView.animate(withDuration: 0.0, animations: {
  546. self.collectionView.collectionViewLayout.invalidateLayout()
  547. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  548. self.collectionView.reloadData()
  549. })
  550. })
  551. layout = NCGlobal.shared.layoutList
  552. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layout)
  553. } else {
  554. // grid layout
  555. UIView.animate(withDuration: 0.0, animations: {
  556. self.collectionView.collectionViewLayout.invalidateLayout()
  557. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  558. self.collectionView.reloadData()
  559. })
  560. })
  561. layout = NCGlobal.shared.layoutGrid
  562. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layout)
  563. }
  564. }
  565. func tapOrderHeader(sender: Any) {
  566. let sortMenu = NCSortMenu()
  567. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  568. }
  569. @objc func tapSelectMenu(sender: Any) {
  570. guard let tabBarController = self.tabBarController else { return }
  571. toggleMenuSelect(viewController: tabBarController, selectOcId: selectOcId)
  572. }
  573. func tapMoreHeader(sender: Any) { }
  574. func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
  575. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, image: image, sender: sender)
  576. }
  577. func tapShareListItem(with objectId: String, sender: Any) {
  578. if isEditMode { return }
  579. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
  580. NCFunctionCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  581. }
  582. func tapMoreGridItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {
  583. if isEditMode { return }
  584. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) else { return }
  585. if namedButtonMore == NCGlobal.shared.buttonMoreMore {
  586. toggleMenu(viewController: self, metadata: metadata, image: image)
  587. } else if namedButtonMore == NCGlobal.shared.buttonMoreStop {
  588. NCNetworking.shared.cancelTransferMetadata(metadata) { }
  589. }
  590. }
  591. func tapRichWorkspace(sender: Any) {
  592. if let navigationController = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateInitialViewController() as? UINavigationController {
  593. if let viewerRichWorkspace = navigationController.topViewController as? NCViewerRichWorkspace {
  594. viewerRichWorkspace.richWorkspaceText = richWorkspaceText ?? ""
  595. viewerRichWorkspace.serverUrl = serverUrl
  596. navigationController.modalPresentationStyle = .fullScreen
  597. self.present(navigationController, animated: true, completion: nil)
  598. }
  599. }
  600. }
  601. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  602. }
  603. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  604. }
  605. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  606. }
  607. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  608. }
  609. @objc func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) {
  610. openMenuItems(with: nil, gestureRecognizer: gestureRecognizer)
  611. /*
  612. if #available(iOS 13.0, *) {
  613. let interaction = UIContextMenuInteraction(delegate: self)
  614. self.view.addInteraction(interaction)
  615. }
  616. */
  617. }
  618. @available(iOS 13.0, *)
  619. func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
  620. return UIContextMenuConfiguration(identifier: nil, previewProvider: {
  621. return nil
  622. }, actionProvider: { suggestedActions in
  623. //let share = UIAction(title: "Share Pupper", image: UIImage(systemName: "square.and.arrow.up")) { action in
  624. //}
  625. //return UIMenu(title: "Main Menu", children: [share])
  626. return nil
  627. })
  628. }
  629. func openMenuItems(with objectId: String?, gestureRecognizer: UILongPressGestureRecognizer) {
  630. if gestureRecognizer.state != .began { return }
  631. if serverUrl == "" { return }
  632. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(objectId) {
  633. metadataTouch = metadata
  634. } else {
  635. metadataTouch = nil
  636. }
  637. var listMenuItems: [UIMenuItem] = []
  638. let touchPoint = gestureRecognizer.location(in: collectionView)
  639. becomeFirstResponder()
  640. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_paste_file_", comment: ""), action: #selector(pasteFilesMenu)))
  641. if listMenuItems.count > 0 {
  642. UIMenuController.shared.menuItems = listMenuItems
  643. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  644. UIMenuController.shared.setMenuVisible(true, animated: true)
  645. }
  646. }
  647. // MARK: - Menu Item
  648. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  649. if (#selector(pasteFilesMenu) == action) {
  650. if UIPasteboard.general.items.count > 0 {
  651. return true
  652. }
  653. }
  654. return false
  655. }
  656. @objc func pasteFilesMenu() {
  657. NCFunctionCenter.shared.pastePasteboard(serverUrl: serverUrl)
  658. }
  659. // MARK: - DataSource + NC Endpoint
  660. @objc func reloadDataSource() {
  661. if appDelegate.account == "" { return }
  662. // Get richWorkspace Text
  663. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  664. richWorkspaceText = directory?.richWorkspace
  665. // E2EE
  666. isEncryptedFolder = CCUtility.isFolderEncrypted(serverUrl, e2eEncrypted: metadataFolder?.e2eEncrypted ?? false, account: appDelegate.account, urlBase: appDelegate.urlBase)
  667. // get auto upload folder
  668. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  669. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  670. // get layout for view
  671. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: serverUrl)
  672. }
  673. @objc func reloadDataSourceNetwork(forced: Bool = false) { }
  674. @objc func reloadDataSourceNetworkRefreshControl() {
  675. reloadDataSourceNetwork(forced: true)
  676. }
  677. @objc func networkSearch() {
  678. if appDelegate.account == "" { return }
  679. if literalSearch?.count ?? 0 > 1 {
  680. isReloadDataSourceNetworkInProgress = true
  681. collectionView?.reloadData()
  682. NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch!) { (account, metadatas, errorCode, errorDescription) in
  683. if self.searchController?.isActive ?? false && errorCode == 0 {
  684. self.metadatasSource = metadatas!
  685. }
  686. self.refreshControl.endRefreshing()
  687. self.isReloadDataSourceNetworkInProgress = false
  688. self.reloadDataSource()
  689. }
  690. } else {
  691. self.refreshControl.endRefreshing()
  692. }
  693. }
  694. @objc func networkReadFolder(forced: Bool, completion: @escaping(_ metadatas: [tableMetadata]?, _ metadatasUpdate: [tableMetadata]?, _ errorCode: Int, _ errorDescription: String)->()) {
  695. NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { (account, metadata, errorCode, errorDescription) in
  696. if errorCode == 0 {
  697. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  698. if forced || directory?.etag != metadata?.etag || directory?.e2eEncrypted ?? false {
  699. NCNetworking.shared.readFolder(serverUrl: self.serverUrl, account: self.appDelegate.account) { (account, metadataFolder, metadatas, metadatasUpdate, metadatasLocalUpdate, errorCode, errorDescription) in
  700. if errorCode == 0 {
  701. self.metadataFolder = metadataFolder
  702. // E2EE
  703. if let metadataFolder = metadataFolder {
  704. if metadataFolder.e2eEncrypted && CCUtility.isEnd(toEndEnabled: self.appDelegate.account) {
  705. NCCommunication.shared.getE2EEMetadata(fileId: metadataFolder.ocId, e2eToken: nil) { (account, e2eMetadata, errorCode, errorDescription) in
  706. if errorCode == 0 && e2eMetadata != nil {
  707. if !NCEndToEndMetadata.shared.decoderMetadata(e2eMetadata!, privateKey: CCUtility.getEndToEndPrivateKey(account), serverUrl: self.serverUrl, account: account, urlBase: self.appDelegate.urlBase) {
  708. NCContentPresenter.shared.messageNotification("_error_e2ee_", description: "_e2e_error_decode_metadata_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorDecodeMetadata, forced: true)
  709. } else {
  710. self.reloadDataSource()
  711. }
  712. } else if errorCode != NCGlobal.shared.ErrorResourceNotFound {
  713. NCContentPresenter.shared.messageNotification("_error_e2ee_", description: "_e2e_error_decode_metadata_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.ErrorDecodeMetadata, forced: true)
  714. }
  715. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  716. }
  717. } else {
  718. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  719. }
  720. } else {
  721. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  722. }
  723. } else {
  724. completion(nil, nil, errorCode, errorDescription)
  725. }
  726. }
  727. } else {
  728. completion(nil, nil, 0, "")
  729. }
  730. } else {
  731. completion(nil, nil, errorCode, errorDescription)
  732. }
  733. }
  734. }
  735. }
  736. // MARK: - Collection View
  737. extension NCCollectionViewCommon: UICollectionViewDelegate {
  738. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  739. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  740. metadataTouch = metadata
  741. selectedIndexPath = indexPath
  742. if isEditMode {
  743. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  744. selectOcId.remove(at: index)
  745. } else {
  746. selectOcId.append(metadata.ocId)
  747. }
  748. collectionView.reloadItems(at: [indexPath])
  749. self.navigationItem.title = NSLocalizedString("_selected_", comment: "") + " : \(selectOcId.count)" + " / \(dataSource.metadatas.count)"
  750. return
  751. }
  752. if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  753. NCContentPresenter.shared.messageNotification("_info_", description: "_e2e_goto_settings_for_enable_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.ErrorE2EENotEnabled, forced: true)
  754. return
  755. }
  756. if metadata.directory {
  757. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataTouch!.serverUrl, addFileName: metadataTouch!.fileName) else { return }
  758. // FILES
  759. if layoutKey == NCGlobal.shared.layoutViewFiles {
  760. if let viewController = appDelegate.listFilesVC[serverUrlPush] {
  761. if viewController.isViewLoaded {
  762. self.navigationController?.pushViewController(viewController, animated: true)
  763. }
  764. } else {
  765. let vcFiles:NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as! NCFiles
  766. vcFiles.isRoot = false
  767. vcFiles.serverUrl = serverUrlPush
  768. vcFiles.titleCurrentFolder = metadataTouch!.fileNameView
  769. appDelegate.listFilesVC[serverUrlPush] = vcFiles
  770. self.navigationController?.pushViewController(vcFiles, animated: true)
  771. }
  772. }
  773. // FAVORITE
  774. if layoutKey == NCGlobal.shared.layoutViewFavorite {
  775. if let viewController = appDelegate.listFavoriteVC[serverUrlPush] {
  776. if viewController.isViewLoaded {
  777. self.navigationController?.pushViewController(viewController, animated: true)
  778. }
  779. } else {
  780. let vcFavorite:NCFavorite = UIStoryboard(name: "NCFavorite", bundle: nil).instantiateInitialViewController() as! NCFavorite
  781. vcFavorite.serverUrl = serverUrlPush
  782. vcFavorite.titleCurrentFolder = metadataTouch!.fileNameView
  783. appDelegate.listFavoriteVC[serverUrlPush] = vcFavorite
  784. self.navigationController?.pushViewController(vcFavorite, animated: true)
  785. }
  786. }
  787. // OFFLINE
  788. if layoutKey == NCGlobal.shared.layoutViewOffline {
  789. if let viewController = appDelegate.listOfflineVC[serverUrlPush] {
  790. if viewController.isViewLoaded {
  791. self.navigationController?.pushViewController(viewController, animated: true)
  792. }
  793. } else {
  794. let vcOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  795. vcOffline.serverUrl = serverUrlPush
  796. vcOffline.titleCurrentFolder = metadataTouch!.fileNameView
  797. appDelegate.listOfflineVC[serverUrlPush] = vcOffline
  798. self.navigationController?.pushViewController(vcOffline, animated: true)
  799. }
  800. }
  801. // RECENT ( for push use Files ... he he he )
  802. if layoutKey == NCGlobal.shared.layoutViewRecent {
  803. if let viewController = appDelegate.listFilesVC[serverUrlPush] {
  804. if viewController.isViewLoaded {
  805. self.navigationController?.pushViewController(viewController, animated: true)
  806. }
  807. } else {
  808. let vcFiles:NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as! NCFiles
  809. vcFiles.isRoot = false
  810. vcFiles.serverUrl = serverUrlPush
  811. vcFiles.titleCurrentFolder = metadataTouch!.fileNameView
  812. appDelegate.listFilesVC[serverUrlPush] = vcFiles
  813. self.navigationController?.pushViewController(vcFiles, animated: true)
  814. }
  815. }
  816. //VIEW IN FOLDER
  817. if layoutKey == NCGlobal.shared.layoutViewViewInFolder {
  818. let vcFileViewInFolder:NCFileViewInFolder = UIStoryboard(name: "NCFileViewInFolder", bundle: nil).instantiateInitialViewController() as! NCFileViewInFolder
  819. vcFileViewInFolder.serverUrl = serverUrlPush
  820. vcFileViewInFolder.titleCurrentFolder = metadataTouch!.fileNameView
  821. self.navigationController?.pushViewController(vcFileViewInFolder, animated: true)
  822. }
  823. // SHARES ( for push use Files ... he he he )
  824. if layoutKey == NCGlobal.shared.layoutViewShares {
  825. if let viewController = appDelegate.listFilesVC[serverUrlPush] {
  826. if viewController.isViewLoaded {
  827. self.navigationController?.pushViewController(viewController, animated: true)
  828. }
  829. } else {
  830. let vcFiles:NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as! NCFiles
  831. vcFiles.isRoot = false
  832. vcFiles.serverUrl = serverUrlPush
  833. vcFiles.titleCurrentFolder = metadataTouch!.fileNameView
  834. appDelegate.listFilesVC[serverUrlPush] = vcFiles
  835. self.navigationController?.pushViewController(vcFiles, animated: true)
  836. }
  837. }
  838. } else {
  839. guard let metadataTouch = metadataTouch else { return }
  840. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo || metadata.typeFile == NCGlobal.shared.metadataTypeFileAudio {
  841. var metadatas: [tableMetadata] = []
  842. for metadata in dataSource.metadatas {
  843. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo || metadata.typeFile == NCGlobal.shared.metadataTypeFileAudio {
  844. metadatas.append(metadata)
  845. }
  846. }
  847. NCViewer.shared.view(viewController: self, metadata: metadataTouch, metadatas: metadatas)
  848. return
  849. }
  850. if CCUtility.fileProviderStorageExists(metadataTouch.ocId, fileNameView: metadataTouch.fileNameView) {
  851. NCViewer.shared.view(viewController: self, metadata: metadataTouch, metadatas: [metadataTouch])
  852. } else if NCCommunication.shared.isNetworkReachable() {
  853. NCNetworking.shared.download(metadata: metadataTouch, activityIndicator: false, selector: NCGlobal.shared.selectorLoadFileView) { (_) in }
  854. } else {
  855. NCContentPresenter.shared.messageNotification("_info_", description: "_go_online_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.ErrorOffline, forced: true)
  856. }
  857. }
  858. }
  859. func collectionViewSelectAll() {
  860. selectOcId.removeAll()
  861. for metadata in metadatasSource {
  862. selectOcId.append(metadata.ocId)
  863. }
  864. collectionView.reloadData()
  865. }
  866. @available(iOS 13.0, *)
  867. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  868. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
  869. metadataTouch = metadata
  870. let identifier = indexPath as NSCopying
  871. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  872. return NCViewerProviderContextMenu(metadata: metadata)
  873. }, actionProvider: { suggestedActions in
  874. return NCFunctionCenter.shared.contextMenuConfiguration(metadata: metadata, viewController: self, enableDeleteLocal: true, enableViewInFolder: false)
  875. })
  876. }
  877. @available(iOS 13.0, *)
  878. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  879. animator.addCompletion {
  880. if let indexPath = configuration.identifier as? IndexPath {
  881. self.collectionView(collectionView, didSelectItemAt: indexPath)
  882. }
  883. }
  884. }
  885. }
  886. extension NCCollectionViewCommon: UICollectionViewDataSource {
  887. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  888. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  889. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: collectionView, indexPath: indexPath)
  890. }
  891. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  892. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  893. NCOperationQueue.shared.cancelDownloadThumbnail(metadata: metadata)
  894. }
  895. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  896. if kind == UICollectionView.elementKindSectionHeader {
  897. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  898. if collectionView.collectionViewLayout == gridLayout {
  899. header.buttonSwitch.setImage(UIImage.init(named: "switchList")!.image(color: NCBrandColor.shared.icon, size: 50), for: .normal)
  900. } else {
  901. header.buttonSwitch.setImage(UIImage.init(named: "switchGrid")!.image(color: NCBrandColor.shared.icon, size: 50), for: .normal)
  902. }
  903. header.delegate = self
  904. header.setStatusButton(count: dataSource.metadatas.count)
  905. header.setTitleSorted(datasourceTitleButton: titleButton)
  906. header.viewRichWorkspaceHeightConstraint.constant = headerRichWorkspaceHeight
  907. header.setRichWorkspaceText(richWorkspaceText: richWorkspaceText)
  908. return header
  909. } else {
  910. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  911. let info = dataSource.getFilesInformation()
  912. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size )
  913. return footer
  914. }
  915. }
  916. func numberOfSections(in collectionView: UICollectionView) -> Int {
  917. return 1
  918. }
  919. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  920. let numberItems = dataSource.numberOfItems()
  921. emptyDataSet?.numberOfItemsInSection(numberItems, section: section)
  922. return numberItems
  923. }
  924. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  925. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  926. if layout == NCGlobal.shared.layoutList {
  927. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  928. } else {
  929. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  930. }
  931. }
  932. var tableShare: tableShare?
  933. var isShare = false
  934. var isMounted = false
  935. if metadataFolder != nil {
  936. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionShared)
  937. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionMounted)
  938. }
  939. if dataSource.metadataShare[metadata.ocId] != nil {
  940. tableShare = dataSource.metadataShare[metadata.ocId]
  941. }
  942. //
  943. // LAYOUT LIST
  944. //
  945. if layout == NCGlobal.shared.layoutList {
  946. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  947. cell.delegate = self
  948. cell.objectId = metadata.ocId
  949. cell.indexPath = indexPath
  950. cell.labelTitle.text = metadata.fileNameView
  951. cell.labelTitle.textColor = NCBrandColor.shared.textView
  952. cell.separator.backgroundColor = NCBrandColor.shared.separator
  953. cell.imageSelect.image = nil
  954. cell.imageStatus.image = nil
  955. cell.imageLocal.image = nil
  956. cell.imageFavorite.image = nil
  957. cell.imageShared.image = nil
  958. cell.imageShared.clearLayerMask()
  959. cell.imageMore.image = nil
  960. cell.imageItem.image = nil
  961. cell.imageItem.backgroundColor = nil
  962. cell.progressView.progress = 0.0
  963. if metadata.directory {
  964. if metadata.e2eEncrypted {
  965. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  966. } else if isShare {
  967. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  968. } else if (tableShare != nil && tableShare?.shareType != 3) {
  969. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  970. } else if (tableShare != nil && tableShare?.shareType == 3) {
  971. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  972. } else if metadata.mountType == "group" {
  973. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  974. } else if isMounted {
  975. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  976. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  977. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  978. } else {
  979. cell.imageItem.image = NCBrandColor.cacheImages.folder
  980. }
  981. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  982. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  983. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  984. // Local image: offline
  985. if tableDirectory != nil && tableDirectory!.offline {
  986. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  987. }
  988. } else {
  989. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  990. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  991. } else {
  992. if metadata.hasPreview {
  993. cell.imageItem.backgroundColor = .lightGray
  994. } else {
  995. if metadata.iconName.count > 0 {
  996. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  997. } else {
  998. cell.imageItem.image = NCBrandColor.cacheImages.file
  999. }
  1000. }
  1001. }
  1002. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  1003. // image local
  1004. if dataSource.metadataOffLine.contains(metadata.ocId) {
  1005. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1006. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  1007. cell.imageLocal.image = NCBrandColor.cacheImages.local
  1008. }
  1009. }
  1010. // image Favorite
  1011. if metadata.favorite {
  1012. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  1013. }
  1014. // Share image
  1015. if (isShare) {
  1016. cell.imageShared.image = NCBrandColor.cacheImages.shared
  1017. } else if (tableShare != nil && tableShare?.shareType == 3) {
  1018. cell.imageShared.image = NCBrandColor.cacheImages.shareByLink
  1019. } else if (tableShare != nil && tableShare?.shareType != 3) {
  1020. cell.imageShared.image = NCBrandColor.cacheImages.shared
  1021. } else {
  1022. cell.imageShared.image = NCBrandColor.cacheImages.canShare
  1023. }
  1024. if metadata.ownerId.count > 0 && metadata.ownerId != appDelegate.userId {
  1025. var fileNameUser = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + metadata.ownerId
  1026. fileNameUser = fileNameUser + ".png"
  1027. if FileManager.default.fileExists(atPath: fileNameUser) {
  1028. cell.imageShared.avatar()
  1029. cell.imageShared.image = UIImage(contentsOfFile: fileNameUser)
  1030. } else {
  1031. NCCommunication.shared.downloadAvatar(userId: metadata.ownerId, fileNameLocalPath: fileNameUser, size: NCGlobal.shared.avatarSize) { (account, data, errorCode, errorMessage) in
  1032. if errorCode == 0 && account == self.appDelegate.account {
  1033. cell.imageShared.avatar()
  1034. cell.imageShared.image = UIImage(contentsOfFile: fileNameUser)
  1035. }
  1036. }
  1037. }
  1038. }
  1039. // Transfer
  1040. var progress: Float = 0.0
  1041. var totalBytes: Int64 = 0
  1042. if let progressType = appDelegate.listProgress[metadata.ocId] {
  1043. progress = progressType.progress
  1044. totalBytes = progressType.totalBytes
  1045. }
  1046. if metadata.status == NCGlobal.shared.metadataStatusInDownload || metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status >= NCGlobal.shared.metadataStatusTypeUpload {
  1047. cell.progressView.isHidden = false
  1048. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  1049. } else {
  1050. cell.progressView.isHidden = true
  1051. cell.progressView.progress = progress
  1052. cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  1053. }
  1054. // Write status on Label Info
  1055. switch metadata.status {
  1056. case NCGlobal.shared.metadataStatusWaitDownload:
  1057. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  1058. break
  1059. case NCGlobal.shared.metadataStatusInDownload:
  1060. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  1061. break
  1062. case NCGlobal.shared.metadataStatusDownloading:
  1063. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  1064. break
  1065. case NCGlobal.shared.metadataStatusWaitUpload:
  1066. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  1067. break
  1068. case NCGlobal.shared.metadataStatusInUpload:
  1069. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  1070. break
  1071. case NCGlobal.shared.metadataStatusUploading:
  1072. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  1073. break
  1074. default:
  1075. break
  1076. }
  1077. // Live Photo
  1078. if metadata.livePhoto {
  1079. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  1080. }
  1081. // E2EE
  1082. if metadata.e2eEncrypted || isEncryptedFolder {
  1083. cell.hideButtonShare(true)
  1084. } else {
  1085. cell.hideButtonShare(false)
  1086. }
  1087. // Remove last separator
  1088. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  1089. cell.separator.isHidden = true
  1090. } else {
  1091. cell.separator.isHidden = false
  1092. }
  1093. // Edit mode
  1094. if isEditMode {
  1095. cell.selectMode(true)
  1096. if selectOcId.contains(metadata.ocId) {
  1097. cell.selected(true)
  1098. } else {
  1099. cell.selected(false)
  1100. }
  1101. } else {
  1102. cell.selectMode(false)
  1103. }
  1104. // Disable Share Button
  1105. if appDelegate.disableSharesView {
  1106. cell.hideButtonShare(true)
  1107. }
  1108. return cell
  1109. }
  1110. //
  1111. // LAYOUT GRID
  1112. //
  1113. if layout == NCGlobal.shared.layoutGrid {
  1114. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  1115. cell.delegate = self
  1116. cell.objectId = metadata.ocId
  1117. cell.indexPath = indexPath
  1118. cell.labelTitle.text = metadata.fileNameView
  1119. cell.labelTitle.textColor = NCBrandColor.shared.textView
  1120. cell.imageSelect.image = nil
  1121. cell.imageStatus.image = nil
  1122. cell.imageLocal.image = nil
  1123. cell.imageFavorite.image = nil
  1124. cell.imageItem.image = nil
  1125. cell.imageItem.backgroundColor = nil
  1126. cell.progressView.progress = 0.0
  1127. if metadata.directory {
  1128. if metadata.e2eEncrypted {
  1129. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  1130. } else if isShare {
  1131. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  1132. } else if (tableShare != nil && tableShare!.shareType != 3) {
  1133. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  1134. } else if (tableShare != nil && tableShare!.shareType == 3) {
  1135. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  1136. } else if metadata.mountType == "group" {
  1137. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  1138. } else if isMounted {
  1139. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  1140. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  1141. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  1142. } else {
  1143. cell.imageItem.image = NCBrandColor.cacheImages.folder
  1144. }
  1145. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  1146. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  1147. // Local image: offline
  1148. if tableDirectory != nil && tableDirectory!.offline {
  1149. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1150. }
  1151. } else {
  1152. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  1153. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  1154. } else {
  1155. if metadata.hasPreview {
  1156. cell.imageItem.backgroundColor = .lightGray
  1157. } else {
  1158. if metadata.iconName.count > 0 {
  1159. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  1160. } else {
  1161. cell.imageItem.image = NCBrandColor.cacheImages.file
  1162. }
  1163. }
  1164. }
  1165. // image Local
  1166. if dataSource.metadataOffLine.contains(metadata.ocId) {
  1167. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1168. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  1169. cell.imageLocal.image = NCBrandColor.cacheImages.local
  1170. }
  1171. }
  1172. // image Favorite
  1173. if metadata.favorite {
  1174. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  1175. }
  1176. // Transfer
  1177. if metadata.status == NCGlobal.shared.metadataStatusInDownload || metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status >= NCGlobal.shared.metadataStatusTypeUpload {
  1178. cell.progressView.isHidden = false
  1179. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  1180. } else {
  1181. cell.progressView.isHidden = true
  1182. cell.progressView.progress = 0.0
  1183. cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  1184. }
  1185. // Live Photo
  1186. if metadata.livePhoto {
  1187. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  1188. }
  1189. // Edit mode
  1190. if isEditMode {
  1191. cell.selectMode(true)
  1192. if selectOcId.contains(metadata.ocId) {
  1193. cell.selected(true)
  1194. } else {
  1195. cell.selected(false)
  1196. }
  1197. } else {
  1198. cell.selectMode(false)
  1199. }
  1200. return cell
  1201. }
  1202. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  1203. }
  1204. }
  1205. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  1206. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  1207. headerRichWorkspaceHeight = 0
  1208. if let richWorkspaceText = richWorkspaceText {
  1209. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  1210. if trimmed.count > 0 && !isSearching {
  1211. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  1212. }
  1213. }
  1214. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  1215. }
  1216. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  1217. return CGSize(width: collectionView.frame.width, height: footerHeight)
  1218. }
  1219. }