NCCollectionViewCommon.swift 76 KB

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