NCCollectionViewCommon.swift 76 KB

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