NCCollectionViewCommon.swift 75 KB

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