NCCollectionViewCommon.swift 76 KB

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