NCCollectionViewCommon.swift 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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. if pushed { return }
  874. pushed = true
  875. self.navigationController?.pushViewController(viewController, animated: true)
  876. }
  877. func collectionViewSelectAll() {
  878. selectOcId.removeAll()
  879. for metadata in metadatasSource {
  880. selectOcId.append(metadata.ocId)
  881. }
  882. collectionView.reloadData()
  883. }
  884. @available(iOS 13.0, *)
  885. func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
  886. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
  887. metadataTouch = metadata
  888. let identifier = indexPath as NSCopying
  889. return UIContextMenuConfiguration(identifier: identifier, previewProvider: {
  890. return NCViewerProviderContextMenu(metadata: metadata)
  891. }, actionProvider: { suggestedActions in
  892. return NCFunctionCenter.shared.contextMenuConfiguration(metadata: metadata, viewController: self, enableDeleteLocal: true, enableViewInFolder: false)
  893. })
  894. }
  895. @available(iOS 13.0, *)
  896. func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
  897. animator.addCompletion {
  898. if let indexPath = configuration.identifier as? IndexPath {
  899. self.collectionView(collectionView, didSelectItemAt: indexPath)
  900. }
  901. }
  902. }
  903. }
  904. extension NCCollectionViewCommon: UICollectionViewDataSource {
  905. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  906. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  907. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: collectionView, indexPath: indexPath)
  908. }
  909. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  910. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  911. NCOperationQueue.shared.cancelDownloadThumbnail(metadata: metadata)
  912. }
  913. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  914. if kind == UICollectionView.elementKindSectionHeader {
  915. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  916. if collectionView.collectionViewLayout == gridLayout {
  917. header.buttonSwitch.setImage(UIImage.init(named: "switchList")!.image(color: NCBrandColor.shared.icon, size: 50), for: .normal)
  918. } else {
  919. header.buttonSwitch.setImage(UIImage.init(named: "switchGrid")!.image(color: NCBrandColor.shared.icon, size: 50), for: .normal)
  920. }
  921. header.delegate = self
  922. header.setStatusButton(count: dataSource.metadatas.count)
  923. header.setTitleSorted(datasourceTitleButton: titleButton)
  924. header.viewRichWorkspaceHeightConstraint.constant = headerRichWorkspaceHeight
  925. header.setRichWorkspaceText(richWorkspaceText: richWorkspaceText)
  926. return header
  927. } else {
  928. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  929. let info = dataSource.getFilesInformation()
  930. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size )
  931. return footer
  932. }
  933. }
  934. func numberOfSections(in collectionView: UICollectionView) -> Int {
  935. return 1
  936. }
  937. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  938. let numberItems = dataSource.numberOfItems()
  939. emptyDataSet?.numberOfItemsInSection(numberItems, section: section)
  940. return numberItems
  941. }
  942. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  943. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  944. if layout == NCGlobal.shared.layoutList {
  945. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  946. } else {
  947. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  948. }
  949. }
  950. var tableShare: tableShare?
  951. var isShare = false
  952. var isMounted = false
  953. if metadataFolder != nil {
  954. isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionShared)
  955. isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder!.permissions.contains(NCGlobal.shared.permissionMounted)
  956. }
  957. if dataSource.metadataShare[metadata.ocId] != nil {
  958. tableShare = dataSource.metadataShare[metadata.ocId]
  959. }
  960. //
  961. // LAYOUT LIST
  962. //
  963. if layout == NCGlobal.shared.layoutList {
  964. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  965. cell.delegate = self
  966. cell.objectId = metadata.ocId
  967. cell.indexPath = indexPath
  968. cell.labelTitle.text = metadata.fileNameView
  969. cell.labelTitle.textColor = NCBrandColor.shared.textView
  970. cell.separator.backgroundColor = NCBrandColor.shared.separator
  971. cell.imageSelect.image = nil
  972. cell.imageStatus.image = nil
  973. cell.imageLocal.image = nil
  974. cell.imageFavorite.image = nil
  975. cell.imageShared.image = nil
  976. cell.imageMore.image = nil
  977. cell.imageItem.image = nil
  978. cell.imageItem.backgroundColor = nil
  979. cell.progressView.progress = 0.0
  980. if metadata.directory {
  981. if metadata.e2eEncrypted {
  982. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  983. } else if isShare {
  984. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  985. } else if (tableShare != nil && tableShare?.shareType != 3) {
  986. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  987. } else if (tableShare != nil && tableShare?.shareType == 3) {
  988. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  989. } else if metadata.mountType == "group" {
  990. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  991. } else if isMounted {
  992. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  993. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  994. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  995. } else {
  996. cell.imageItem.image = NCBrandColor.cacheImages.folder
  997. }
  998. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  999. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  1000. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  1001. // Local image: offline
  1002. if tableDirectory != nil && tableDirectory!.offline {
  1003. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1004. }
  1005. } else {
  1006. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  1007. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  1008. } else {
  1009. if metadata.hasPreview {
  1010. cell.imageItem.backgroundColor = .lightGray
  1011. } else {
  1012. if metadata.iconName.count > 0 {
  1013. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  1014. } else {
  1015. cell.imageItem.image = NCBrandColor.cacheImages.file
  1016. }
  1017. }
  1018. }
  1019. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  1020. // image local
  1021. if dataSource.metadataOffLine.contains(metadata.ocId) {
  1022. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1023. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  1024. cell.imageLocal.image = NCBrandColor.cacheImages.local
  1025. }
  1026. }
  1027. // image Favorite
  1028. if metadata.favorite {
  1029. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  1030. }
  1031. // Share image
  1032. if (isShare) {
  1033. cell.imageShared.image = NCBrandColor.cacheImages.shared
  1034. } else if (tableShare != nil && tableShare?.shareType == 3) {
  1035. cell.imageShared.image = NCBrandColor.cacheImages.shareByLink
  1036. } else if (tableShare != nil && tableShare?.shareType != 3) {
  1037. cell.imageShared.image = NCBrandColor.cacheImages.shared
  1038. } else {
  1039. cell.imageShared.image = NCBrandColor.cacheImages.canShare
  1040. }
  1041. if metadata.ownerId.count > 0 && metadata.ownerId != appDelegate.userId {
  1042. cell.imageShared.image = UIImage(named: "avatar")
  1043. let fileNameUser = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + metadata.ownerId + ".png"
  1044. if FileManager.default.fileExists(atPath: fileNameUser) {
  1045. if let image = UIImage(contentsOfFile: fileNameUser) {
  1046. cell.imageShared.image = NCUtility.shared.createAvatar(image: image, size: 30)
  1047. }
  1048. } else {
  1049. NCCommunication.shared.downloadAvatar(userId: metadata.ownerId, fileNameLocalPath: fileNameUser, size: NCGlobal.shared.avatarSize) { (account, data, errorCode, errorMessage) in
  1050. if errorCode == 0 && account == self.appDelegate.account {
  1051. if let image = UIImage(contentsOfFile: fileNameUser) {
  1052. cell.imageShared.image = NCUtility.shared.createAvatar(image: image, size: 30)
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. // Transfer
  1059. var progress: Float = 0.0
  1060. var totalBytes: Int64 = 0
  1061. if let progressType = appDelegate.listProgress[metadata.ocId] {
  1062. progress = progressType.progress
  1063. totalBytes = progressType.totalBytes
  1064. }
  1065. if metadata.status == NCGlobal.shared.metadataStatusInDownload || metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status >= NCGlobal.shared.metadataStatusTypeUpload {
  1066. cell.progressView.isHidden = false
  1067. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  1068. } else {
  1069. cell.progressView.isHidden = true
  1070. cell.progressView.progress = progress
  1071. cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  1072. }
  1073. // Write status on Label Info
  1074. switch metadata.status {
  1075. case NCGlobal.shared.metadataStatusWaitDownload:
  1076. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  1077. break
  1078. case NCGlobal.shared.metadataStatusInDownload:
  1079. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  1080. break
  1081. case NCGlobal.shared.metadataStatusDownloading:
  1082. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  1083. break
  1084. case NCGlobal.shared.metadataStatusWaitUpload:
  1085. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  1086. break
  1087. case NCGlobal.shared.metadataStatusInUpload:
  1088. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  1089. break
  1090. case NCGlobal.shared.metadataStatusUploading:
  1091. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  1092. break
  1093. case NCGlobal.shared.metadataStatusUploadError:
  1094. if metadata.sessionError != "" {
  1095. cell.labelInfo.text = NSLocalizedString("_status_wait_upload_", comment: "") + " " + metadata.sessionError
  1096. } else {
  1097. cell.labelInfo.text = NSLocalizedString("_status_wait_upload_", comment: "")
  1098. }
  1099. break
  1100. default:
  1101. break
  1102. }
  1103. // Live Photo
  1104. if metadata.livePhoto {
  1105. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  1106. }
  1107. // E2EE
  1108. if metadata.e2eEncrypted || isEncryptedFolder {
  1109. cell.hideButtonShare(true)
  1110. } else {
  1111. cell.hideButtonShare(false)
  1112. }
  1113. // Remove last separator
  1114. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  1115. cell.separator.isHidden = true
  1116. } else {
  1117. cell.separator.isHidden = false
  1118. }
  1119. // Edit mode
  1120. if isEditMode {
  1121. cell.selectMode(true)
  1122. if selectOcId.contains(metadata.ocId) {
  1123. cell.selected(true)
  1124. } else {
  1125. cell.selected(false)
  1126. }
  1127. } else {
  1128. cell.selectMode(false)
  1129. }
  1130. // Disable Share Button
  1131. if appDelegate.disableSharesView {
  1132. cell.hideButtonShare(true)
  1133. }
  1134. return cell
  1135. }
  1136. //
  1137. // LAYOUT GRID
  1138. //
  1139. if layout == NCGlobal.shared.layoutGrid {
  1140. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  1141. cell.delegate = self
  1142. cell.objectId = metadata.ocId
  1143. cell.indexPath = indexPath
  1144. cell.labelTitle.text = metadata.fileNameView
  1145. cell.labelTitle.textColor = NCBrandColor.shared.textView
  1146. cell.imageSelect.image = nil
  1147. cell.imageStatus.image = nil
  1148. cell.imageLocal.image = nil
  1149. cell.imageFavorite.image = nil
  1150. cell.imageItem.image = nil
  1151. cell.imageItem.backgroundColor = nil
  1152. cell.progressView.progress = 0.0
  1153. if metadata.directory {
  1154. if metadata.e2eEncrypted {
  1155. cell.imageItem.image = NCBrandColor.cacheImages.folderEncrypted
  1156. } else if isShare {
  1157. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  1158. } else if (tableShare != nil && tableShare!.shareType != 3) {
  1159. cell.imageItem.image = NCBrandColor.cacheImages.folderSharedWithMe
  1160. } else if (tableShare != nil && tableShare!.shareType == 3) {
  1161. cell.imageItem.image = NCBrandColor.cacheImages.folderPublic
  1162. } else if metadata.mountType == "group" {
  1163. cell.imageItem.image = NCBrandColor.cacheImages.folderGroup
  1164. } else if isMounted {
  1165. cell.imageItem.image = NCBrandColor.cacheImages.folderExternal
  1166. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  1167. cell.imageItem.image = NCBrandColor.cacheImages.folderAutomaticUpload
  1168. } else {
  1169. cell.imageItem.image = NCBrandColor.cacheImages.folder
  1170. }
  1171. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  1172. let tableDirectory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  1173. // Local image: offline
  1174. if tableDirectory != nil && tableDirectory!.offline {
  1175. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1176. }
  1177. } else {
  1178. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  1179. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  1180. } else {
  1181. if metadata.hasPreview {
  1182. cell.imageItem.backgroundColor = .lightGray
  1183. } else {
  1184. if metadata.iconName.count > 0 {
  1185. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  1186. } else {
  1187. cell.imageItem.image = NCBrandColor.cacheImages.file
  1188. }
  1189. }
  1190. }
  1191. // image Local
  1192. if dataSource.metadataOffLine.contains(metadata.ocId) {
  1193. cell.imageLocal.image = NCBrandColor.cacheImages.offlineFlag
  1194. } else if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  1195. cell.imageLocal.image = NCBrandColor.cacheImages.local
  1196. }
  1197. }
  1198. // image Favorite
  1199. if metadata.favorite {
  1200. cell.imageFavorite.image = NCBrandColor.cacheImages.favorite
  1201. }
  1202. // Transfer
  1203. if metadata.status == NCGlobal.shared.metadataStatusInDownload || metadata.status == NCGlobal.shared.metadataStatusDownloading || metadata.status >= NCGlobal.shared.metadataStatusTypeUpload {
  1204. cell.progressView.isHidden = false
  1205. cell.setButtonMore(named: NCGlobal.shared.buttonMoreStop, image: NCBrandColor.cacheImages.buttonStop)
  1206. } else {
  1207. cell.progressView.isHidden = true
  1208. cell.progressView.progress = 0.0
  1209. cell.setButtonMore(named: NCGlobal.shared.buttonMoreMore, image: NCBrandColor.cacheImages.buttonMore)
  1210. }
  1211. // Live Photo
  1212. if metadata.livePhoto {
  1213. cell.imageStatus.image = NCBrandColor.cacheImages.livePhoto
  1214. }
  1215. // Edit mode
  1216. if isEditMode {
  1217. cell.selectMode(true)
  1218. if selectOcId.contains(metadata.ocId) {
  1219. cell.selected(true)
  1220. } else {
  1221. cell.selected(false)
  1222. }
  1223. } else {
  1224. cell.selectMode(false)
  1225. }
  1226. return cell
  1227. }
  1228. return collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  1229. }
  1230. }
  1231. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  1232. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  1233. headerRichWorkspaceHeight = 0
  1234. if let richWorkspaceText = richWorkspaceText {
  1235. let trimmed = richWorkspaceText.trimmingCharacters(in: .whitespaces)
  1236. if trimmed.count > 0 && !isSearching {
  1237. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  1238. }
  1239. }
  1240. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  1241. }
  1242. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  1243. return CGSize(width: collectionView.frame.width, height: footerHeight)
  1244. }
  1245. }