NCCollectionViewCommon.swift 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  26. @IBOutlet weak var collectionView: UICollectionView!
  27. internal let refreshControl = UIRefreshControl()
  28. internal var searchController: UISearchController?
  29. @objc var serverUrl: String = ""
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. internal var isEditMode = false
  32. internal var selectOcId: [String] = []
  33. internal var metadatasSource: [tableMetadata] = []
  34. internal var metadataFolder: tableMetadata?
  35. internal var metadataTouch: tableMetadata?
  36. internal var dataSource: NCDataSource?
  37. internal var richWorkspaceText: String?
  38. internal var layout = ""
  39. internal var groupBy = ""
  40. internal var titleButton = ""
  41. internal var itemForLine = 0
  42. private var autoUploadFileName = ""
  43. private var autoUploadDirectory = ""
  44. private var listLayout: NCListLayout!
  45. private var gridLayout: NCGridLayout!
  46. private let headerHeight: CGFloat = 50
  47. private var headerRichWorkspaceHeight: CGFloat = 0
  48. private let footerHeight: CGFloat = 50
  49. private var timerInputSearch: Timer?
  50. internal var literalSearch: String?
  51. internal var isSearching: Bool = false
  52. internal var isReloadDataSourceNetworkInProgress: Bool = false
  53. // DECLARE
  54. internal var layoutKey = ""
  55. internal var titleCurrentFolder = ""
  56. internal var enableSearchBar: Bool = false
  57. internal var DZNimage: UIImage?
  58. internal var DZNtitle: String = ""
  59. internal var DZNdescription: String = ""
  60. required init?(coder aDecoder: NSCoder) {
  61. super.init(coder: aDecoder)
  62. }
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. self.navigationController?.navigationBar.prefersLargeTitles = true
  66. if enableSearchBar {
  67. searchController = UISearchController(searchResultsController: nil)
  68. searchController?.searchResultsUpdater = self
  69. self.navigationItem.searchController = searchController
  70. searchController?.dimsBackgroundDuringPresentation = false
  71. searchController?.delegate = self
  72. searchController?.searchBar.delegate = self
  73. }
  74. // Cell
  75. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  76. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  77. // Header
  78. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  79. // Footer
  80. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  81. collectionView.alwaysBounceVertical = true
  82. listLayout = NCListLayout()
  83. gridLayout = NCGridLayout()
  84. // Refresh Control
  85. collectionView.addSubview(refreshControl)
  86. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  87. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  88. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetworkRefreshControl), for: .valueChanged)
  89. // empty Data Source
  90. self.collectionView.emptyDataSetDelegate = self
  91. self.collectionView.emptyDataSetSource = self
  92. // 3D Touch peek and pop
  93. if traitCollection.forceTouchCapability == .available {
  94. registerForPreviewing(with: self, sourceView: view)
  95. }
  96. // Long Press on CollectionView
  97. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressCollecationView(gestureRecognizer:metadata:)))
  98. longPressedGesture.minimumPressDuration = 0.5
  99. longPressedGesture.delegate = self
  100. longPressedGesture.delaysTouchesBegan = true
  101. collectionView.addGestureRecognizer(longPressedGesture)
  102. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  103. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  104. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  105. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  106. NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_copyFile), object: nil)
  107. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_createFolder), object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_favoriteFile), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  117. changeTheming()
  118. }
  119. override func viewWillAppear(_ animated: Bool) {
  120. super.viewWillAppear(animated)
  121. appDelegate.activeViewController = self
  122. self.navigationItem.title = titleCurrentFolder
  123. // get auto upload folder
  124. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  125. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  126. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  127. gridLayout.itemForLine = CGFloat(itemForLine)
  128. if layout == k_layout_list {
  129. collectionView?.collectionViewLayout = listLayout
  130. } else {
  131. collectionView?.collectionViewLayout = gridLayout
  132. }
  133. setNavigationItem()
  134. reloadDataSource()
  135. }
  136. override func viewDidAppear(_ animated: Bool) {
  137. super.viewDidAppear(animated)
  138. reloadDataSourceNetwork()
  139. }
  140. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  141. super.viewWillTransition(to: size, with: coordinator)
  142. coordinator.animate(alongsideTransition: nil) { _ in
  143. self.collectionView?.collectionViewLayout.invalidateLayout()
  144. }
  145. }
  146. override var canBecomeFirstResponder: Bool {
  147. return true
  148. }
  149. func setNavigationItem() {
  150. if isEditMode {
  151. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage.init(named: "navigationMore"), style: .plain, target: self, action:#selector(tapSelectMenu(sender:)))
  152. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, target: self, action: #selector(tapSelect(sender:)))
  153. } else {
  154. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(tapSelect(sender:)))
  155. self.navigationItem.leftBarButtonItem = nil
  156. }
  157. }
  158. // MARK: - NotificationCenter
  159. @objc func changeTheming() {
  160. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  161. }
  162. @objc func deleteFile(_ notification: NSNotification) {
  163. if self.view?.window == nil { return }
  164. if let userInfo = notification.userInfo as NSDictionary? {
  165. if let metadata = userInfo["metadata"] as? tableMetadata, let onlyLocal = userInfo["onlyLocal"] as? Bool {
  166. if onlyLocal {
  167. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  168. let indexPath = IndexPath(row: row, section: 0)
  169. collectionView?.reloadItems(at: [indexPath])
  170. }
  171. } else {
  172. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  173. let indexPath = IndexPath(row: row, section: 0)
  174. collectionView?.performBatchUpdates({
  175. collectionView?.deleteItems(at: [indexPath])
  176. }, completion: { (_) in
  177. self.collectionView?.reloadData()
  178. })
  179. }
  180. }
  181. }
  182. }
  183. }
  184. @objc func moveFile(_ notification: NSNotification) {
  185. if self.view?.window == nil { return }
  186. if let userInfo = notification.userInfo as NSDictionary? {
  187. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata {
  188. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  189. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  190. let indexPath = IndexPath(row: row, section: 0)
  191. collectionView?.performBatchUpdates({
  192. collectionView?.deleteItems(at: [indexPath])
  193. }, completion: { (_) in
  194. self.collectionView?.reloadData()
  195. })
  196. }
  197. } else if metadataNew.serverUrl == serverUrl && metadata.account == appDelegate.account {
  198. if let row = dataSource?.addMetadata(metadataNew) {
  199. let indexPath = IndexPath(row: row, section: 0)
  200. collectionView?.performBatchUpdates({
  201. collectionView?.insertItems(at: [indexPath])
  202. }, completion: { (_) in
  203. self.collectionView?.reloadData()
  204. })
  205. }
  206. }
  207. }
  208. }
  209. }
  210. @objc func copyFile(_ notification: NSNotification) {
  211. if self.view?.window == nil { return }
  212. if let userInfo = notification.userInfo as NSDictionary? {
  213. if let serverUrlTo = userInfo["serverUrlTo"] as? String {
  214. if serverUrlTo == self.serverUrl {
  215. self.reloadDataSource()
  216. }
  217. }
  218. }
  219. }
  220. @objc func renameFile(_ notification: NSNotification) {
  221. if self.view?.window == nil { return }
  222. if let userInfo = notification.userInfo as NSDictionary? {
  223. if let metadata = userInfo["metadata"] as? tableMetadata {
  224. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  225. let indexPath = IndexPath(row: row, section: 0)
  226. collectionView?.performBatchUpdates({
  227. collectionView?.reloadItems(at: [indexPath])
  228. }, completion: { (_) in
  229. self.collectionView?.reloadData()
  230. })
  231. }
  232. }
  233. }
  234. }
  235. @objc func createFolder(_ notification: NSNotification) {
  236. if self.view?.window == nil { return }
  237. if let userInfo = notification.userInfo as NSDictionary? {
  238. if let metadata = userInfo["metadata"] as? tableMetadata {
  239. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  240. if let row = dataSource?.addMetadata(metadata) {
  241. let indexPath = IndexPath(row: row, section: 0)
  242. collectionView?.performBatchUpdates({
  243. collectionView?.insertItems(at: [indexPath])
  244. }, completion: { (_) in
  245. self.collectionView?.reloadData()
  246. })
  247. }
  248. }
  249. }
  250. } else {
  251. self.reloadDataSourceNetwork()
  252. }
  253. }
  254. @objc func favoriteFile(_ notification: NSNotification) {
  255. if self.view?.window == nil { return }
  256. if let userInfo = notification.userInfo as NSDictionary? {
  257. if let metadata = userInfo["metadata"] as? tableMetadata {
  258. if dataSource?.getIndexMetadata(ocId: metadata.ocId) != nil {
  259. self.reloadDataSource()
  260. }
  261. }
  262. }
  263. }
  264. @objc func downloadStartFile(_ notification: NSNotification) {
  265. if self.view?.window == nil { return }
  266. if let userInfo = notification.userInfo as NSDictionary? {
  267. if let metadata = userInfo["metadata"] as? tableMetadata {
  268. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  269. let indexPath = IndexPath(row: row, section: 0)
  270. collectionView?.reloadItems(at: [indexPath])
  271. }
  272. }
  273. }
  274. }
  275. @objc func downloadedFile(_ notification: NSNotification) {
  276. if self.view?.window == nil { return }
  277. if let userInfo = notification.userInfo as NSDictionary? {
  278. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  279. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  280. let indexPath = IndexPath(row: row, section: 0)
  281. collectionView?.reloadItems(at: [indexPath])
  282. }
  283. }
  284. }
  285. }
  286. @objc func downloadCancelFile(_ notification: NSNotification) {
  287. if self.view?.window == nil { return }
  288. if let userInfo = notification.userInfo as NSDictionary? {
  289. if let metadata = userInfo["metadata"] as? tableMetadata {
  290. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  291. let indexPath = IndexPath(row: row, section: 0)
  292. collectionView?.reloadItems(at: [indexPath])
  293. }
  294. }
  295. }
  296. }
  297. @objc func uploadStartFile(_ notification: NSNotification) {
  298. if self.view?.window == nil { return }
  299. if let userInfo = notification.userInfo as NSDictionary? {
  300. if let metadata = userInfo["metadata"] as? tableMetadata {
  301. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  302. if let row = dataSource?.addMetadata(metadata) {
  303. let indexPath = IndexPath(row: row, section: 0)
  304. collectionView?.performBatchUpdates({
  305. collectionView?.insertItems(at: [indexPath])
  306. }, completion: { (_) in
  307. self.collectionView?.reloadData()
  308. })
  309. }
  310. }
  311. }
  312. }
  313. }
  314. @objc func uploadedFile(_ notification: NSNotification) {
  315. if self.view?.window == nil { return }
  316. if let userInfo = notification.userInfo as NSDictionary? {
  317. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int {
  318. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  319. dataSource?.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
  320. collectionView?.reloadData()
  321. }
  322. }
  323. }
  324. }
  325. @objc func uploadCancelFile(_ notification: NSNotification) {
  326. if self.view?.window == nil { return }
  327. if let userInfo = notification.userInfo as NSDictionary? {
  328. if let metadata = userInfo["metadata"] as? tableMetadata {
  329. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  330. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  331. let indexPath = IndexPath(row: row, section: 0)
  332. collectionView?.performBatchUpdates({
  333. collectionView?.deleteItems(at: [indexPath])
  334. }, completion: { (_) in
  335. self.collectionView?.reloadData()
  336. })
  337. } else {
  338. self.reloadDataSource()
  339. }
  340. }
  341. }
  342. }
  343. }
  344. @objc func triggerProgressTask(_ notification: NSNotification) {
  345. if self.view?.window == nil { return }
  346. if let userInfo = notification.userInfo as NSDictionary? {
  347. if let ocId = userInfo["ocId"] as? String {
  348. let _ = userInfo["account"] as? String ?? ""
  349. let _ = userInfo["serverUrl"] as? String ?? ""
  350. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  351. let progress = progressNumber.floatValue
  352. let status = userInfo["status"] as? Int ?? Int(k_metadataStatusNormal)
  353. let totalBytes = userInfo["totalBytes"] as? Double ?? 0
  354. let totalBytesExpected = userInfo["totalBytesExpected"] as? Double ?? 0
  355. appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: userInfo["ocId"] as? NSString ?? "")
  356. if let index = dataSource?.getIndexMetadata(ocId: ocId) {
  357. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  358. if cell is NCListCell {
  359. let cell = cell as! NCListCell
  360. if progress > 0 {
  361. cell.progressView?.isHidden = false
  362. cell.progressView?.progress = progress
  363. cell.setButtonMore(named: "stop")
  364. if status == k_metadataStatusInDownload {
  365. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  366. } else if status == k_metadataStatusInUpload {
  367. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  368. }
  369. }
  370. } else if cell is NCGridCell {
  371. let cell = cell as! NCGridCell
  372. if progress > 0 {
  373. cell.progressView.isHidden = false
  374. cell.progressView.progress = progress
  375. cell.setButtonMore(named: "stop")
  376. }
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. // MARK: - DZNEmpty
  384. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  385. return NCBrandColor.sharedInstance.backgroundView
  386. }
  387. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  388. if searchController?.isActive ?? false {
  389. return CCGraphics.changeThemingColorImage(UIImage.init(named: "search"), width: 300, height: 300, color: NCBrandColor.sharedInstance.yellowFavorite)
  390. }
  391. return DZNimage
  392. }
  393. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  394. var text = "\n"+NSLocalizedString(DZNtitle, comment: "")
  395. if searchController?.isActive ?? false {
  396. if isReloadDataSourceNetworkInProgress {
  397. text = "\n"+NSLocalizedString("_search_in_progress_", comment: "")
  398. } else {
  399. text = "\n"+NSLocalizedString("_search_no_record_found_", comment: "")
  400. }
  401. }
  402. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.gray]
  403. return NSAttributedString.init(string: text, attributes: attributes)
  404. }
  405. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  406. var text = "\n"+NSLocalizedString(DZNdescription, comment: "")
  407. if searchController?.isActive ?? false {
  408. text = "\n"+NSLocalizedString("_search_instruction_", comment: "")
  409. }
  410. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  411. return NSAttributedString.init(string: text, attributes: attributes)
  412. }
  413. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  414. return true
  415. }
  416. // MARK: - SEARCH
  417. func updateSearchResults(for searchController: UISearchController) {
  418. timerInputSearch?.invalidate()
  419. timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(reloadDataSourceNetwork), userInfo: nil, repeats: false)
  420. literalSearch = searchController.searchBar.text
  421. collectionView?.reloadData()
  422. }
  423. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  424. isSearching = true
  425. metadatasSource.removeAll()
  426. reloadDataSource()
  427. }
  428. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  429. isSearching = false
  430. literalSearch = ""
  431. reloadDataSource()
  432. }
  433. // MARK: - TAP EVENT
  434. @objc func tapSelect(sender: Any) {
  435. isEditMode = !isEditMode
  436. selectOcId.removeAll()
  437. setNavigationItem()
  438. self.collectionView.reloadData()
  439. }
  440. func tapSwitchHeader(sender: Any) {
  441. if collectionView.collectionViewLayout == gridLayout {
  442. // list layout
  443. UIView.animate(withDuration: 0.0, animations: {
  444. self.collectionView.collectionViewLayout.invalidateLayout()
  445. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  446. self.collectionView.reloadData()
  447. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  448. })
  449. })
  450. layout = k_layout_list
  451. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  452. } else {
  453. // grid layout
  454. UIView.animate(withDuration: 0.0, animations: {
  455. self.collectionView.collectionViewLayout.invalidateLayout()
  456. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  457. self.collectionView.reloadData()
  458. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  459. })
  460. })
  461. layout = k_layout_grid
  462. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  463. }
  464. }
  465. func tapOrderHeader(sender: Any) {
  466. let sortMenu = NCSortMenu()
  467. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  468. }
  469. @objc func tapSelectMenu(sender: Any) {
  470. guard let tabBarController = self.tabBarController else { return }
  471. toggleMoreSelect(viewController: tabBarController, selectOcId: selectOcId)
  472. }
  473. func tapMoreHeader(sender: Any) { }
  474. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  475. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  476. }
  477. func tapShareListItem(with objectId: String, sender: Any) {
  478. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else { return }
  479. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  480. }
  481. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  482. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else { return }
  483. guard let tabBarController = self.tabBarController else { return }
  484. if namedButtonMore == "more" {
  485. toggleMoreMenu(viewController: tabBarController, metadata: metadata)
  486. } else if namedButtonMore == "stop" {
  487. NCNetworking.shared.cancelTransferMetadata(metadata) { }
  488. }
  489. }
  490. func tapRichWorkspace(sender: Any) {
  491. if let navigationController = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateInitialViewController() as? UINavigationController {
  492. if let viewerRichWorkspace = navigationController.topViewController as? NCViewerRichWorkspace {
  493. viewerRichWorkspace.richWorkspaceText = richWorkspaceText ?? ""
  494. viewerRichWorkspace.serverUrl = serverUrl
  495. navigationController.modalPresentationStyle = .fullScreen
  496. self.present(navigationController, animated: true, completion: nil)
  497. }
  498. }
  499. }
  500. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  501. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  502. longPressCollecationView(gestureRecognizer: gestureRecognizer, metadata: metadata)
  503. }
  504. }
  505. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  506. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  507. longPressCollecationView(gestureRecognizer: gestureRecognizer, metadata: metadata)
  508. }
  509. }
  510. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  511. }
  512. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  513. }
  514. @objc func longPressCollecationView(gestureRecognizer: UILongPressGestureRecognizer, metadata: tableMetadata?) {
  515. if gestureRecognizer.state != .began { return }
  516. if serverUrl == "" { return }
  517. var listMenuItems: [UIMenuItem] = []
  518. let touchPoint = gestureRecognizer.location(in: collectionView)
  519. metadataTouch = metadata
  520. becomeFirstResponder()
  521. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_paste_file_", comment: ""), action: #selector(pasteFilesMenu(_:))))
  522. if metadata != nil {
  523. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_copy_file_", comment: ""), action: #selector(copyFileMenu(_:))))
  524. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_open_quicklook_", comment: ""), action: #selector(openQuickLookMenu(_:))))
  525. if !NCBrandOptions.sharedInstance.disable_openin_file {
  526. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_open_in_", comment: ""), action: #selector(openInMenu(_:))))
  527. }
  528. }
  529. if listMenuItems.count > 0 {
  530. UIMenuController.shared.menuItems = listMenuItems
  531. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  532. UIMenuController.shared.setMenuVisible(true, animated: true)
  533. }
  534. }
  535. // MARK: - Menu Item
  536. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  537. if (#selector(pasteFilesMenu(_:)) == action) {
  538. if UIPasteboard.general.items.count > 0 {
  539. return true
  540. }
  541. }
  542. if (#selector(openQuickLookMenu(_:)) == action || #selector(openInMenu(_:)) == action || #selector(copyFileMenu(_:)) == action) {
  543. guard let metadata = metadataTouch else { return false }
  544. if !metadata.directory && metadata.status == k_metadataStatusNormal {
  545. return true
  546. }
  547. }
  548. return false
  549. }
  550. @objc func copyFileMenu(_ notification: Any) {
  551. var metadatas: [tableMetadata] = []
  552. if isEditMode {
  553. for ocId in selectOcId {
  554. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  555. metadatas.append(metadata)
  556. }
  557. }
  558. } else {
  559. guard let metadata = metadataTouch else { return }
  560. metadatas.append(metadata)
  561. }
  562. for metadata in metadatas {
  563. do {
  564. var items = UIPasteboard.general.items
  565. let data = try NSKeyedArchiver.archivedData(withRootObject: metadata.ocId, requiringSecureCoding: false)
  566. items.append([k_metadataKeyedUnarchiver:data])
  567. } catch {
  568. print("error")
  569. }
  570. }
  571. }
  572. @objc func pasteFilesMenu(_ notification: Any) {
  573. var listData: [String] = []
  574. for item in UIPasteboard.general.items {
  575. for object in item {
  576. let contentType = object.key
  577. let data = object.value
  578. if data is String {
  579. if listData.contains(data as! String) {
  580. continue
  581. } else {
  582. listData.append(data as! String)
  583. }
  584. }
  585. let type = NCCommunicationCommon.shared.convertUTItoResultType(fileUTI: contentType as CFString)
  586. if type.resultTypeFile != NCCommunicationCommon.typeFile.unknow.rawValue && type.resultExtension != "" {
  587. uploadPasteFile(fileName: type.resultFilename, ext: type.resultExtension, contentType: contentType, data: data)
  588. }
  589. }
  590. }
  591. }
  592. private func uploadPasteFile(fileName: String, ext: String, contentType: String, data: Any) {
  593. do {
  594. let fileNameView = fileName + "_" + CCUtility.getIncrementalNumber() + "." + ext
  595. let ocId = UUID().uuidString
  596. let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileNameView)!
  597. if data is UIImage {
  598. try (data as? UIImage)?.jpegData(compressionQuality: 1)?.write(to: URL(fileURLWithPath: filePath))
  599. } else if data is Data {
  600. try (data as? Data)?.write(to: URL(fileURLWithPath: filePath))
  601. } else if data is String {
  602. try (data as? String)?.write(to: URL(fileURLWithPath: filePath), atomically: true, encoding: .utf8)
  603. } else {
  604. return
  605. }
  606. let metadataForUpload = NCManageDatabase.sharedInstance.createMetadata(account: appDelegate.account, fileName: fileNameView, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: contentType, livePhoto: false)
  607. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  608. metadataForUpload.sessionSelector = selectorUploadFile
  609. metadataForUpload.size = Double(NCUtilityFileSystem.shared.getFileSize(filePath: filePath))
  610. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  611. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  612. } catch { }
  613. }
  614. @objc func openQuickLookMenu(_ notification: Any) {
  615. guard let metadata = metadataTouch else { return }
  616. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  617. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selectorLoadFileQuickLook, "errorCode": 0, "errorDescription": "" ])
  618. } else {
  619. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadFileQuickLook) { (_) in }
  620. }
  621. }
  622. @objc func openInMenu(_ notification: Any) {
  623. guard let metadata = metadataTouch else { return }
  624. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  625. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selectorOpenIn, "errorCode": 0, "errorDescription": "" ])
  626. } else {
  627. NCNetworking.shared.download(metadata: metadata, selector: selectorOpenIn) { (_) in }
  628. }
  629. }
  630. // MARK: - SEGUE
  631. @objc func segue(metadata: tableMetadata) {
  632. self.metadataTouch = metadata
  633. performSegue(withIdentifier: "segueDetail", sender: self)
  634. }
  635. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  636. let photoDataSource: NSMutableArray = []
  637. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  638. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  639. photoDataSource.add(metadata)
  640. }
  641. }
  642. if let segueNavigationController = segue.destination as? UINavigationController {
  643. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  644. segueViewController.metadata = metadataTouch
  645. }
  646. }
  647. }
  648. // MARK: - NC API & Algorithm
  649. @objc func reloadDataSource() {
  650. let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  651. richWorkspaceText = directory?.richWorkspace
  652. }
  653. @objc func reloadDataSource(_ notification: NSNotification) { }
  654. @objc func reloadDataSourceNetwork(forced: Bool = false) { }
  655. @objc func reloadDataSourceNetworkRefreshControl() {
  656. reloadDataSourceNetwork(forced: true)
  657. }
  658. @objc func networkSearch() {
  659. if literalSearch?.count ?? 0 > 1 {
  660. isReloadDataSourceNetworkInProgress = true
  661. collectionView?.reloadData()
  662. NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch!) { (account, metadatas, errorCode, errorDescription) in
  663. if self.searchController?.isActive ?? false && errorCode == 0 {
  664. self.metadatasSource = metadatas!
  665. }
  666. self.isReloadDataSourceNetworkInProgress = false
  667. self.reloadDataSource()
  668. }
  669. }
  670. }
  671. @objc func networkReadFolder(forced: Bool, completion: @escaping(_ metadatas: [tableMetadata]?, _ errorCode: Int, _ errorDescription: String)->()) {
  672. NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { (account, metadata, errorCode, errorDescription) in
  673. if errorCode == 0 {
  674. let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  675. if forced || directory?.etag != metadata?.etag {
  676. NCNetworking.shared.readFolder(serverUrl: self.serverUrl, account: self.appDelegate.account) { (account, metadataFolder, metadatas, metadatasUpdate, metadatasLocalUpdate, errorCode, errorDescription) in
  677. if errorCode == 0 {
  678. self.metadataFolder = metadataFolder
  679. }
  680. completion(metadatas, errorCode, errorDescription)
  681. }
  682. } else {
  683. completion(nil, 0, "")
  684. }
  685. } else {
  686. completion(nil, errorCode, errorDescription)
  687. }
  688. }
  689. }
  690. }
  691. // MARK: - 3D Touch peek and pop
  692. extension NCCollectionViewCommon: UIViewControllerPreviewingDelegate {
  693. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  694. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  695. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  696. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  697. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  698. viewController.metadata = metadata
  699. if layout == k_layout_grid {
  700. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  701. previewingContext.sourceRect = cell.frame
  702. viewController.imageFile = cell.imageItem.image
  703. } else {
  704. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  705. previewingContext.sourceRect = cell.frame
  706. viewController.imageFile = cell.imageItem.image
  707. }
  708. viewController.showOpenIn = true
  709. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  710. viewController.showShare = false
  711. return viewController
  712. }
  713. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  714. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  715. collectionView(collectionView, didSelectItemAt: indexPath)
  716. }
  717. }
  718. // MARK: - Collection View
  719. extension NCCollectionViewCommon: UICollectionViewDelegate {
  720. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  721. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return }
  722. metadataTouch = metadata
  723. if isEditMode {
  724. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  725. selectOcId.remove(at: index)
  726. } else {
  727. selectOcId.append(metadata.ocId)
  728. }
  729. collectionView.reloadItems(at: [indexPath])
  730. return
  731. }
  732. if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  733. NCContentPresenter.shared.messageNotification("_info_", description: "_e2e_goto_settings_for_enable_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorE2EENotEnabled), forced: true)
  734. return
  735. }
  736. if metadata.directory {
  737. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataTouch!.serverUrl, addFileName: metadataTouch!.fileName) else { return }
  738. if layoutKey == k_layout_view_favorite {
  739. let ncFavorite:NCFavorite = UIStoryboard(name: "NCFavorite", bundle: nil).instantiateInitialViewController() as! NCFavorite
  740. ncFavorite.serverUrl = serverUrlPush
  741. ncFavorite.titleCurrentFolder = metadataTouch!.fileNameView
  742. self.navigationController?.pushViewController(ncFavorite, animated: true)
  743. }
  744. if layoutKey == k_layout_view_offline {
  745. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  746. ncOffline.serverUrl = serverUrlPush
  747. ncOffline.titleCurrentFolder = metadataTouch!.fileNameView
  748. self.navigationController?.pushViewController(ncOffline, animated: true)
  749. }
  750. } else {
  751. if metadata.typeFile == k_metadataTypeFile_document && NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil {
  752. if NCCommunication.shared.isNetworkReachable() {
  753. performSegue(withIdentifier: "segueDetail", sender: self)
  754. } else {
  755. NCContentPresenter.shared.messageNotification("_info_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorOffline), forced: true)
  756. }
  757. return
  758. }
  759. if metadata.typeFile == k_metadataTypeFile_document && NCUtility.shared.isRichDocument(metadata) {
  760. if NCCommunication.shared.isNetworkReachable() {
  761. performSegue(withIdentifier: "segueDetail", sender: self)
  762. } else {
  763. NCContentPresenter.shared.messageNotification("_info_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorOffline), forced: true)
  764. }
  765. return
  766. }
  767. if CCUtility.fileProviderStorageExists(metadataTouch?.ocId, fileNameView: metadataTouch?.fileNameView) {
  768. performSegue(withIdentifier: "segueDetail", sender: self)
  769. } else {
  770. NCNetworking.shared.download(metadata: metadataTouch!, selector: selectorLoadFileView) { (_) in }
  771. }
  772. }
  773. }
  774. func collectionViewSelectAll() {
  775. selectOcId.removeAll()
  776. for metadata in metadatasSource {
  777. selectOcId.append(metadata.ocId)
  778. }
  779. collectionView.reloadData()
  780. }
  781. }
  782. extension NCCollectionViewCommon: UICollectionViewDataSource {
  783. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  784. if kind == UICollectionView.elementKindSectionHeader {
  785. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  786. if collectionView.collectionViewLayout == gridLayout {
  787. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  788. } else {
  789. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  790. }
  791. header.delegate = self
  792. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  793. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  794. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  795. header.setTitleSorted(datasourceTitleButton: titleButton)
  796. header.viewRichWorkspaceHeightConstraint.constant = headerRichWorkspaceHeight
  797. header.setRichWorkspaceText(richWorkspaceText: richWorkspaceText)
  798. return header
  799. } else {
  800. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  801. let info = dataSource?.getFilesInformation()
  802. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  803. return footer
  804. }
  805. }
  806. func numberOfSections(in collectionView: UICollectionView) -> Int {
  807. return 1
  808. }
  809. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  810. return dataSource?.numberOfItems() ?? 0
  811. }
  812. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  813. let cell: UICollectionViewCell
  814. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  815. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  816. }
  817. if layout == k_layout_grid {
  818. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  819. } else {
  820. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  821. }
  822. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  823. NCCollectionCommon.shared.cellForItemAt(indexPath: indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: metadataFolder, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectocId: selectOcId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, shares: shares, source: self, dataSource: dataSource)
  824. return cell
  825. }
  826. }
  827. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  828. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  829. if richWorkspaceText?.count ?? 0 == 0 {
  830. headerRichWorkspaceHeight = 0
  831. } else {
  832. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  833. }
  834. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  835. }
  836. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  837. return CGSize(width: collectionView.frame.width, height: footerHeight)
  838. }
  839. }