NCCollectionViewCommon.swift 75 KB

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