NCCollectionViewCommon.swift 76 KB

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