NCCollectionViewCommon.swift 76 KB

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