NCCollectionViewCommon.swift 75 KB

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