NCCollectionViewCommon.swift 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  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, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  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 serverUrl: String = ""
  31. internal var isEncryptedFolder = false
  32. internal var isEditMode = false
  33. internal var selectOcId: [String] = []
  34. internal var metadatasSource: [tableMetadata] = []
  35. internal var metadataFolder: tableMetadata?
  36. internal var metadataTouch: tableMetadata?
  37. internal var dataSource = NCDataSource()
  38. internal var richWorkspaceText: String?
  39. internal var layout = ""
  40. internal var groupBy = ""
  41. internal var titleButton = ""
  42. internal var itemForLine = 0
  43. private var autoUploadFileName = ""
  44. private var autoUploadDirectory = ""
  45. internal var listLayout: NCListLayout!
  46. internal var gridLayout: NCGridLayout!
  47. private let headerHeight: CGFloat = 50
  48. private var headerRichWorkspaceHeight: CGFloat = 0
  49. private let footerHeight: CGFloat = 50
  50. private var timerInputSearch: Timer?
  51. internal var literalSearch: String?
  52. internal var isSearching: Bool = false
  53. internal var isReloadDataSourceNetworkInProgress: Bool = false
  54. // DECLARE
  55. internal var layoutKey = ""
  56. internal var titleCurrentFolder = ""
  57. internal var enableSearchBar: Bool = false
  58. internal var DZNimage: UIImage?
  59. internal var DZNtitle: String = ""
  60. internal var DZNdescription: String = ""
  61. required init?(coder aDecoder: NSCoder) {
  62. super.init(coder: aDecoder)
  63. }
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. self.navigationController?.navigationBar.prefersLargeTitles = true
  67. if enableSearchBar {
  68. searchController = UISearchController(searchResultsController: nil)
  69. searchController?.searchResultsUpdater = self
  70. searchController?.dimsBackgroundDuringPresentation = false
  71. searchController?.delegate = self
  72. searchController?.searchBar.delegate = self
  73. navigationItem.searchController = searchController
  74. navigationItem.hidesSearchBarWhenScrolling = false
  75. }
  76. // Cell
  77. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  78. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  79. // Header
  80. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  81. // Footer
  82. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  83. collectionView.alwaysBounceVertical = true
  84. listLayout = NCListLayout()
  85. gridLayout = NCGridLayout()
  86. // Refresh Control
  87. collectionView.addSubview(refreshControl)
  88. refreshControl.tintColor = .gray
  89. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetworkRefreshControl), for: .valueChanged)
  90. // empty Data Source
  91. self.collectionView.emptyDataSetDelegate = self
  92. self.collectionView.emptyDataSetSource = self
  93. // 3D Touch peek and pop
  94. if traitCollection.forceTouchCapability == .available {
  95. registerForPreviewing(with: self, sourceView: view)
  96. }
  97. // Long Press on CollectionView
  98. let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressCollecationView(_:)))
  99. longPressedGesture.minimumPressDuration = 0.5
  100. longPressedGesture.delegate = self
  101. longPressedGesture.delaysTouchesBegan = true
  102. collectionView.addGestureRecognizer(longPressedGesture)
  103. // Notification
  104. NotificationCenter.default.addObserver(self, selector: #selector(initializeMain), name: NSNotification.Name(rawValue: k_notificationCenter_initializeMain), object: nil)
  105. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  106. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  107. NotificationCenter.default.addObserver(self, selector: #selector(changeStatusFolderE2EE(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_changeStatusFolderE2EE), object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_copyFile), object: nil)
  111. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  112. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_createFolder), object: nil)
  113. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_favoriteFile), object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  115. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  117. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  118. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  119. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  120. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  121. changeTheming()
  122. }
  123. override func viewWillAppear(_ animated: Bool) {
  124. super.viewWillAppear(animated)
  125. if serverUrl == "" {
  126. appDelegate.activeServerUrl = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  127. } else {
  128. appDelegate.activeServerUrl = serverUrl
  129. }
  130. appDelegate.activeViewController = self
  131. self.navigationItem.title = titleCurrentFolder
  132. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey, serverUrl: serverUrl)
  133. gridLayout.itemForLine = CGFloat(itemForLine)
  134. if layout == k_layout_list {
  135. collectionView?.collectionViewLayout = listLayout
  136. } else {
  137. collectionView?.collectionViewLayout = gridLayout
  138. }
  139. setNavigationItem()
  140. reloadDataSource()
  141. }
  142. override func viewDidAppear(_ animated: Bool) {
  143. super.viewDidAppear(animated)
  144. reloadDataSourceNetwork()
  145. }
  146. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  147. super.viewWillTransition(to: size, with: coordinator)
  148. coordinator.animate(alongsideTransition: nil) { _ in
  149. self.collectionView?.collectionViewLayout.invalidateLayout()
  150. }
  151. }
  152. override var canBecomeFirstResponder: Bool {
  153. return true
  154. }
  155. func setNavigationItem() {
  156. if isEditMode {
  157. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage.init(named: "navigationMore"), style: .plain, target: self, action:#selector(tapSelectMenu(sender:)))
  158. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .plain, target: self, action: #selector(tapSelect(sender:)))
  159. } else {
  160. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_select_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(tapSelect(sender:)))
  161. self.navigationItem.leftBarButtonItem = nil
  162. }
  163. }
  164. // MARK: - NotificationCenter
  165. @objc func initializeMain() {
  166. if appDelegate.account.count == 0 { return }
  167. if searchController?.isActive ?? false {
  168. searchController?.isActive = false
  169. }
  170. // set active serverUrl
  171. if self.view?.window != nil {
  172. if serverUrl == "" {
  173. appDelegate.activeServerUrl = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  174. } else {
  175. appDelegate.activeServerUrl = serverUrl
  176. }
  177. }
  178. self.navigationController?.popToRootViewController(animated: false)
  179. appDelegate.listFilesVC.removeAllObjects()
  180. appDelegate.listFavoriteVC.removeAllObjects()
  181. appDelegate.listOfflineVC.removeAllObjects()
  182. reloadDataSource()
  183. }
  184. @objc func changeTheming() {
  185. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  186. }
  187. @objc func changeStatusFolderE2EE(_ notification: NSNotification) {
  188. if self.view?.window == nil { return }
  189. reloadDataSource()
  190. }
  191. @objc func deleteFile(_ notification: NSNotification) {
  192. if self.view?.window == nil { return }
  193. if let userInfo = notification.userInfo as NSDictionary? {
  194. if let metadata = userInfo["metadata"] as? tableMetadata, let onlyLocal = userInfo["onlyLocal"] as? Bool {
  195. if onlyLocal {
  196. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  197. let indexPath = IndexPath(row: row, section: 0)
  198. collectionView?.reloadItems(at: [indexPath])
  199. }
  200. } else {
  201. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  202. let indexPath = IndexPath(row: row, section: 0)
  203. collectionView?.performBatchUpdates({
  204. collectionView?.deleteItems(at: [indexPath])
  205. }, completion: { (_) in
  206. self.collectionView?.reloadData()
  207. })
  208. }
  209. }
  210. }
  211. }
  212. }
  213. @objc func moveFile(_ notification: NSNotification) {
  214. if self.view?.window == nil { return }
  215. if let userInfo = notification.userInfo as NSDictionary? {
  216. if let metadata = userInfo["metadata"] as? tableMetadata, let metadataNew = userInfo["metadataNew"] as? tableMetadata {
  217. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  218. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  219. let indexPath = IndexPath(row: row, section: 0)
  220. collectionView?.performBatchUpdates({
  221. collectionView?.deleteItems(at: [indexPath])
  222. }, completion: { (_) in
  223. self.collectionView?.reloadData()
  224. })
  225. }
  226. } else if metadataNew.serverUrl == serverUrl && metadata.account == appDelegate.account {
  227. if let row = dataSource.addMetadata(metadataNew) {
  228. let indexPath = IndexPath(row: row, section: 0)
  229. collectionView?.performBatchUpdates({
  230. collectionView?.insertItems(at: [indexPath])
  231. }, completion: { (_) in
  232. self.collectionView?.reloadData()
  233. })
  234. }
  235. }
  236. }
  237. }
  238. }
  239. @objc func copyFile(_ notification: NSNotification) {
  240. if self.view?.window == nil { return }
  241. if let userInfo = notification.userInfo as NSDictionary? {
  242. if let serverUrlTo = userInfo["serverUrlTo"] as? String {
  243. if serverUrlTo == self.serverUrl {
  244. self.reloadDataSource()
  245. }
  246. }
  247. }
  248. }
  249. @objc func renameFile(_ notification: NSNotification) {
  250. if self.view?.window == nil { return }
  251. if let userInfo = notification.userInfo as NSDictionary? {
  252. if let metadata = userInfo["metadata"] as? tableMetadata {
  253. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  254. let indexPath = IndexPath(row: row, section: 0)
  255. collectionView?.performBatchUpdates({
  256. collectionView?.reloadItems(at: [indexPath])
  257. }, completion: { (_) in
  258. self.collectionView?.reloadData()
  259. })
  260. }
  261. }
  262. }
  263. }
  264. @objc func createFolder(_ notification: NSNotification) {
  265. if self.view?.window == nil { return }
  266. if let userInfo = notification.userInfo as NSDictionary? {
  267. if let metadata = userInfo["metadata"] as? tableMetadata {
  268. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  269. if let row = dataSource.addMetadata(metadata) {
  270. let indexPath = IndexPath(row: row, section: 0)
  271. collectionView?.performBatchUpdates({
  272. collectionView?.insertItems(at: [indexPath])
  273. }, completion: { (_) in
  274. self.collectionView?.reloadData()
  275. })
  276. }
  277. }
  278. }
  279. } else {
  280. self.reloadDataSourceNetwork()
  281. }
  282. }
  283. @objc func favoriteFile(_ notification: NSNotification) {
  284. if self.view?.window == nil { return }
  285. if let userInfo = notification.userInfo as NSDictionary? {
  286. if let metadata = userInfo["metadata"] as? tableMetadata {
  287. if dataSource.getIndexMetadata(ocId: metadata.ocId) != nil {
  288. self.reloadDataSource()
  289. }
  290. }
  291. }
  292. }
  293. @objc func downloadStartFile(_ notification: NSNotification) {
  294. if self.view?.window == nil { return }
  295. if let userInfo = notification.userInfo as NSDictionary? {
  296. if let metadata = userInfo["metadata"] as? tableMetadata {
  297. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  298. let indexPath = IndexPath(row: row, section: 0)
  299. collectionView?.reloadItems(at: [indexPath])
  300. }
  301. }
  302. }
  303. }
  304. @objc func downloadedFile(_ notification: NSNotification) {
  305. if self.view?.window == nil { return }
  306. if let userInfo = notification.userInfo as NSDictionary? {
  307. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  308. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  309. let indexPath = IndexPath(row: row, section: 0)
  310. collectionView?.reloadItems(at: [indexPath])
  311. }
  312. }
  313. }
  314. }
  315. @objc func downloadCancelFile(_ notification: NSNotification) {
  316. if self.view?.window == nil { return }
  317. if let userInfo = notification.userInfo as NSDictionary? {
  318. if let metadata = userInfo["metadata"] as? tableMetadata {
  319. if let row = dataSource.reloadMetadata(ocId: metadata.ocId) {
  320. let indexPath = IndexPath(row: row, section: 0)
  321. collectionView?.reloadItems(at: [indexPath])
  322. }
  323. }
  324. }
  325. }
  326. @objc func uploadStartFile(_ notification: NSNotification) {
  327. if self.view?.window == nil { return }
  328. if let userInfo = notification.userInfo as NSDictionary? {
  329. if let metadata = userInfo["metadata"] as? tableMetadata {
  330. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  331. if let row = dataSource.addMetadata(metadata) {
  332. let indexPath = IndexPath(row: row, section: 0)
  333. collectionView?.performBatchUpdates({
  334. collectionView?.insertItems(at: [indexPath])
  335. }, completion: { (_) in
  336. self.collectionView?.reloadData()
  337. })
  338. }
  339. }
  340. }
  341. }
  342. }
  343. @objc func uploadedFile(_ notification: NSNotification) {
  344. if self.view?.window == nil { return }
  345. if let userInfo = notification.userInfo as NSDictionary? {
  346. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int {
  347. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  348. dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
  349. collectionView?.reloadData()
  350. }
  351. }
  352. }
  353. }
  354. @objc func uploadCancelFile(_ notification: NSNotification) {
  355. if self.view?.window == nil { return }
  356. if let userInfo = notification.userInfo as NSDictionary? {
  357. if let metadata = userInfo["metadata"] as? tableMetadata {
  358. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  359. if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
  360. let indexPath = IndexPath(row: row, section: 0)
  361. collectionView?.performBatchUpdates({
  362. collectionView?.deleteItems(at: [indexPath])
  363. }, completion: { (_) in
  364. self.collectionView?.reloadData()
  365. })
  366. } else {
  367. self.reloadDataSource()
  368. }
  369. }
  370. }
  371. }
  372. }
  373. @objc func triggerProgressTask(_ notification: NSNotification) {
  374. if self.view?.window == nil { return }
  375. if let userInfo = notification.userInfo as NSDictionary? {
  376. if let ocId = userInfo["ocId"] as? String {
  377. let _ = userInfo["account"] as? String ?? ""
  378. let _ = userInfo["serverUrl"] as? String ?? ""
  379. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  380. let progress = progressNumber.floatValue
  381. let status = userInfo["status"] as? Int ?? Int(k_metadataStatusNormal)
  382. let totalBytes = userInfo["totalBytes"] as? Double ?? 0
  383. let totalBytesExpected = userInfo["totalBytesExpected"] as? Double ?? 0
  384. appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: userInfo["ocId"] as? NSString ?? "")
  385. if let index = dataSource.getIndexMetadata(ocId: ocId) {
  386. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  387. if cell is NCListCell {
  388. let cell = cell as! NCListCell
  389. if progress > 0 {
  390. cell.progressView?.isHidden = false
  391. cell.progressView?.progress = progress
  392. cell.setButtonMore(named: k_buttonMoreStop, image: NCCollectionCommon.images.cellButtonStop)
  393. if status == k_metadataStatusInDownload {
  394. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  395. } else if status == k_metadataStatusInUpload {
  396. cell.labelInfo.text = CCUtility.transformedSize(totalBytesExpected) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  397. }
  398. }
  399. } else if cell is NCGridCell {
  400. let cell = cell as! NCGridCell
  401. if progress > 0 {
  402. cell.progressView.isHidden = false
  403. cell.progressView.progress = progress
  404. cell.setButtonMore(named: k_buttonMoreStop, image: NCCollectionCommon.images.cellButtonStop)
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. }
  412. // MARK: - DZNEmpty
  413. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  414. return NCBrandColor.sharedInstance.backgroundView
  415. }
  416. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  417. if searchController?.isActive ?? false {
  418. return CCGraphics.changeThemingColorImage(UIImage.init(named: "search"), width: 300, height: 300, color: .gray)
  419. }
  420. if isReloadDataSourceNetworkInProgress {
  421. return CCGraphics.changeThemingColorImage(UIImage.init(named: "networkInProgress"), width: 300, height: 300, color: .gray)
  422. }
  423. return DZNimage
  424. }
  425. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  426. var text = "\n"+NSLocalizedString(DZNtitle, comment: "")
  427. if isReloadDataSourceNetworkInProgress {
  428. text = "\n"+NSLocalizedString("_request_in_progress_", comment: "")
  429. }
  430. if searchController?.isActive ?? false {
  431. if isReloadDataSourceNetworkInProgress {
  432. text = "\n"+NSLocalizedString("_search_in_progress_", comment: "")
  433. } else {
  434. text = "\n"+NSLocalizedString("_search_no_record_found_", comment: "")
  435. }
  436. }
  437. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.gray]
  438. return NSAttributedString.init(string: text, attributes: attributes)
  439. }
  440. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  441. var text = "\n"+NSLocalizedString(DZNdescription, comment: "")
  442. if isReloadDataSourceNetworkInProgress {
  443. text = ""
  444. }
  445. if searchController?.isActive ?? false {
  446. text = "\n"+NSLocalizedString("_search_instruction_", comment: "")
  447. }
  448. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  449. return NSAttributedString.init(string: text, attributes: attributes)
  450. }
  451. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  452. return true
  453. }
  454. // MARK: - SEARCH
  455. func updateSearchResults(for searchController: UISearchController) {
  456. timerInputSearch?.invalidate()
  457. timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(reloadDataSourceNetwork), userInfo: nil, repeats: false)
  458. literalSearch = searchController.searchBar.text
  459. collectionView?.reloadData()
  460. }
  461. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  462. isSearching = true
  463. metadatasSource.removeAll()
  464. reloadDataSource()
  465. }
  466. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  467. isSearching = false
  468. literalSearch = ""
  469. reloadDataSource()
  470. }
  471. // MARK: - TAP EVENT
  472. @objc func tapSelect(sender: Any) {
  473. isEditMode = !isEditMode
  474. selectOcId.removeAll()
  475. setNavigationItem()
  476. self.collectionView.reloadData()
  477. }
  478. func tapSwitchHeader(sender: Any) {
  479. if collectionView.collectionViewLayout == gridLayout {
  480. // list layout
  481. UIView.animate(withDuration: 0.0, animations: {
  482. self.collectionView.collectionViewLayout.invalidateLayout()
  483. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  484. self.collectionView.reloadData()
  485. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  486. })
  487. })
  488. layout = k_layout_list
  489. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layout)
  490. } else {
  491. // grid layout
  492. UIView.animate(withDuration: 0.0, animations: {
  493. self.collectionView.collectionViewLayout.invalidateLayout()
  494. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  495. self.collectionView.reloadData()
  496. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  497. })
  498. })
  499. layout = k_layout_grid
  500. NCUtility.shared.setLayoutForView(key: layoutKey, serverUrl: serverUrl, layout: layout)
  501. }
  502. }
  503. func tapOrderHeader(sender: Any) {
  504. let sortMenu = NCSortMenu()
  505. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  506. }
  507. @objc func tapSelectMenu(sender: Any) {
  508. guard let tabBarController = self.tabBarController else { return }
  509. toggleMoreSelect(viewController: tabBarController, selectOcId: selectOcId)
  510. }
  511. func tapMoreHeader(sender: Any) { }
  512. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  513. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  514. }
  515. func tapShareListItem(with objectId: String, sender: Any) {
  516. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else { return }
  517. NCNetworkingNotificationCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  518. }
  519. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  520. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else { return }
  521. if namedButtonMore == k_buttonMoreMore {
  522. toggleMoreMenu(viewController: self, metadata: metadata)
  523. } else if namedButtonMore == k_buttonMoreStop {
  524. NCNetworking.shared.cancelTransferMetadata(metadata) { }
  525. }
  526. }
  527. func tapRichWorkspace(sender: Any) {
  528. if let navigationController = UIStoryboard(name: "NCViewerRichWorkspace", bundle: nil).instantiateInitialViewController() as? UINavigationController {
  529. if let viewerRichWorkspace = navigationController.topViewController as? NCViewerRichWorkspace {
  530. viewerRichWorkspace.richWorkspaceText = richWorkspaceText ?? ""
  531. viewerRichWorkspace.serverUrl = serverUrl
  532. navigationController.modalPresentationStyle = .fullScreen
  533. self.present(navigationController, animated: true, completion: nil)
  534. }
  535. }
  536. }
  537. func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  538. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  539. metadataTouch = metadata
  540. longPressCollecationView(gestureRecognizer)
  541. }
  542. }
  543. func longPressGridItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {
  544. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) {
  545. metadataTouch = metadata
  546. longPressCollecationView(gestureRecognizer)
  547. }
  548. }
  549. func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  550. }
  551. func longPressMoreGridItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {
  552. }
  553. @objc func longPressCollecationView(_ gestureRecognizer: UILongPressGestureRecognizer) {
  554. if gestureRecognizer.state != .began { return }
  555. if serverUrl == "" { return }
  556. var listMenuItems: [UIMenuItem] = []
  557. let touchPoint = gestureRecognizer.location(in: collectionView)
  558. becomeFirstResponder()
  559. if metadataTouch != nil {
  560. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_copy_file_", comment: ""), action: #selector(copyFileMenu(_:))))
  561. }
  562. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_paste_file_", comment: ""), action: #selector(pasteFilesMenu(_:))))
  563. if metadataTouch != nil {
  564. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_open_quicklook_", comment: ""), action: #selector(openQuickLookMenu(_:))))
  565. if !NCBrandOptions.sharedInstance.disable_openin_file {
  566. listMenuItems.append(UIMenuItem.init(title: NSLocalizedString("_open_in_", comment: ""), action: #selector(openInMenu(_:))))
  567. }
  568. }
  569. if listMenuItems.count > 0 {
  570. UIMenuController.shared.menuItems = listMenuItems
  571. UIMenuController.shared.setTargetRect(CGRect(x: touchPoint.x, y: touchPoint.y, width: 0, height: 0), in: collectionView)
  572. UIMenuController.shared.setMenuVisible(true, animated: true)
  573. }
  574. }
  575. // MARK: - Menu Item
  576. override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
  577. if (#selector(pasteFilesMenu(_:)) == action) {
  578. if UIPasteboard.general.items.count > 0 {
  579. return true
  580. }
  581. }
  582. if (#selector(openQuickLookMenu(_:)) == action || #selector(openInMenu(_:)) == action || #selector(copyFileMenu(_:)) == action) {
  583. guard let metadata = metadataTouch else { return false }
  584. if !metadata.directory && metadata.status == k_metadataStatusNormal {
  585. return true
  586. }
  587. }
  588. return false
  589. }
  590. @objc func copyFileMenu(_ notification: Any) {
  591. var metadatas: [tableMetadata] = []
  592. var items = [[String : Any]]()
  593. if isEditMode {
  594. for ocId in selectOcId {
  595. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  596. metadatas.append(metadata)
  597. }
  598. }
  599. } else {
  600. guard let metadata = metadataTouch else { return }
  601. metadatas.append(metadata)
  602. }
  603. for metadata in metadatas {
  604. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  605. do {
  606. let etagPasteboard = try NSKeyedArchiver.archivedData(withRootObject: metadata.ocId, requiringSecureCoding: false)
  607. items.append([k_metadataKeyedUnarchiver:etagPasteboard])
  608. } catch {
  609. print("error")
  610. }
  611. } else {
  612. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadCopy, setFavorite: false) { (_) in }
  613. }
  614. }
  615. UIPasteboard.general.setItems(items, options: [:])
  616. if isEditMode {
  617. tapSelect(sender: self)
  618. }
  619. }
  620. @objc func pasteFilesMenu(_ notification: Any) {
  621. var listData: [String] = []
  622. for item in UIPasteboard.general.items {
  623. for object in item {
  624. let contentType = object.key
  625. let data = object.value
  626. if contentType == k_metadataKeyedUnarchiver {
  627. do {
  628. if let ocId = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as! Data) as? String{
  629. uploadPasteOcId(ocId)
  630. }
  631. } catch {
  632. print("error")
  633. }
  634. continue
  635. }
  636. if data is String {
  637. if listData.contains(data as! String) {
  638. continue
  639. } else {
  640. listData.append(data as! String)
  641. }
  642. }
  643. let type = NCCommunicationCommon.shared.convertUTItoResultType(fileUTI: contentType as CFString)
  644. if type.resultTypeFile != NCCommunicationCommon.typeFile.unknow.rawValue && type.resultExtension != "" {
  645. uploadPasteFile(fileName: type.resultFilename, ext: type.resultExtension, contentType: contentType, data: data)
  646. }
  647. }
  648. }
  649. }
  650. private func uploadPasteFile(fileName: String, ext: String, contentType: String, data: Any) {
  651. do {
  652. let fileNameView = fileName + "_" + CCUtility.getIncrementalNumber() + "." + ext
  653. let ocId = UUID().uuidString
  654. let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileNameView)!
  655. if data is UIImage {
  656. try (data as? UIImage)?.jpegData(compressionQuality: 1)?.write(to: URL(fileURLWithPath: filePath))
  657. } else if data is Data {
  658. try (data as? Data)?.write(to: URL(fileURLWithPath: filePath))
  659. } else if data is String {
  660. try (data as? String)?.write(to: URL(fileURLWithPath: filePath), atomically: true, encoding: .utf8)
  661. } else {
  662. return
  663. }
  664. let metadataForUpload = NCManageDatabase.sharedInstance.createMetadata(account: appDelegate.account, fileName: fileNameView, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: contentType, livePhoto: false)
  665. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  666. metadataForUpload.sessionSelector = selectorUploadFile
  667. metadataForUpload.size = Double(NCUtilityFileSystem.shared.getFileSize(filePath: filePath))
  668. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  669. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  670. } catch { }
  671. }
  672. private func uploadPasteOcId(_ ocId: String) {
  673. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  674. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  675. let fileNameView = NCUtility.shared.createFileName(metadata.fileNameView, serverUrl: serverUrl, account: appDelegate.account)
  676. let ocId = NSUUID().uuidString
  677. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileNameView))
  678. let metadataForUpload = NCManageDatabase.sharedInstance.createMetadata(account: appDelegate.account, fileName: fileNameView, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: "", livePhoto: false)
  679. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  680. metadataForUpload.sessionSelector = selectorUploadFile
  681. metadataForUpload.size = metadata.size
  682. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  683. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  684. }
  685. }
  686. }
  687. @objc func openQuickLookMenu(_ notification: Any) {
  688. guard let metadata = metadataTouch else { return }
  689. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  690. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selectorLoadFileQuickLook, "errorCode": 0, "errorDescription": "" ])
  691. } else {
  692. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadFileQuickLook) { (_) in }
  693. }
  694. }
  695. @objc func openInMenu(_ notification: Any) {
  696. guard let metadata = metadataTouch else { return }
  697. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  698. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selectorOpenIn, "errorCode": 0, "errorDescription": "" ])
  699. } else {
  700. NCNetworking.shared.download(metadata: metadata, selector: selectorOpenIn) { (_) in }
  701. }
  702. }
  703. // MARK: - SEGUE
  704. @objc func segue(metadata: tableMetadata) {
  705. self.metadataTouch = metadata
  706. performSegue(withIdentifier: "segueDetail", sender: self)
  707. }
  708. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  709. let photoDataSource: NSMutableArray = []
  710. for metadata in (dataSource.metadatas) {
  711. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  712. photoDataSource.add(metadata)
  713. }
  714. }
  715. if let segueNavigationController = segue.destination as? UINavigationController {
  716. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  717. segueViewController.metadata = metadataTouch
  718. segueViewController.layoutKey = layoutKey
  719. }
  720. }
  721. }
  722. // MARK: - DataSource + NC Endpoint
  723. @objc func reloadDataSource() {
  724. if appDelegate.account.count == 0 { return }
  725. let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  726. richWorkspaceText = directory?.richWorkspace
  727. // E2EE
  728. isEncryptedFolder = CCUtility.isFolderEncrypted(serverUrl, e2eEncrypted: metadataFolder?.e2eEncrypted ?? false, account: appDelegate.account, urlBase: appDelegate.urlBase)
  729. // get auto upload folder
  730. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  731. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  732. }
  733. @objc func reloadDataSource(_ notification: NSNotification) { }
  734. @objc func reloadDataSourceNetwork(forced: Bool = false) { }
  735. @objc func reloadDataSourceNetworkRefreshControl() {
  736. reloadDataSourceNetwork(forced: true)
  737. }
  738. @objc func networkSearch() {
  739. if appDelegate.account.count == 0 { return }
  740. if literalSearch?.count ?? 0 > 1 {
  741. isReloadDataSourceNetworkInProgress = true
  742. collectionView?.reloadData()
  743. NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch!) { (account, metadatas, errorCode, errorDescription) in
  744. if self.searchController?.isActive ?? false && errorCode == 0 {
  745. self.metadatasSource = metadatas!
  746. }
  747. self.refreshControl.endRefreshing()
  748. self.isReloadDataSourceNetworkInProgress = false
  749. self.reloadDataSource()
  750. }
  751. } else {
  752. self.refreshControl.endRefreshing()
  753. }
  754. }
  755. @objc func networkReadFolder(forced: Bool, completion: @escaping(_ metadatas: [tableMetadata]?, _ metadatasUpdate: [tableMetadata]?, _ errorCode: Int, _ errorDescription: String)->()) {
  756. NCNetworking.shared.readFile(serverUrlFileName: serverUrl, account: appDelegate.account) { (account, metadata, errorCode, errorDescription) in
  757. if errorCode == 0 {
  758. let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  759. if forced || directory?.etag != metadata?.etag || directory?.e2eEncrypted ?? false {
  760. NCNetworking.shared.readFolder(serverUrl: self.serverUrl, account: self.appDelegate.account) { (account, metadataFolder, metadatas, metadatasUpdate, metadatasLocalUpdate, errorCode, errorDescription) in
  761. if errorCode == 0 {
  762. self.metadataFolder = metadataFolder
  763. // E2EE
  764. if let metadataFolder = metadataFolder {
  765. if metadataFolder.e2eEncrypted && CCUtility.isEnd(toEndEnabled: self.appDelegate.account) {
  766. NCCommunication.shared.getE2EEMetadata(fileId: metadataFolder.ocId, e2eToken: nil) { (account, e2eMetadata, errorCode, errorDescription) in
  767. if errorCode == 0 && e2eMetadata != nil {
  768. if !NCEndToEndMetadata.sharedInstance.decoderMetadata(e2eMetadata!, privateKey: CCUtility.getEndToEndPrivateKey(account), serverUrl: self.serverUrl, account: account, urlBase: self.appDelegate.urlBase) {
  769. NCContentPresenter.shared.messageNotification("_error_e2ee_", description: "_e2e_error_decode_metadata_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorDecodeMetadata), forced: true)
  770. } else {
  771. self.reloadDataSource()
  772. }
  773. } else if errorCode != 404 {
  774. NCContentPresenter.shared.messageNotification("_error_e2ee_", description: "_e2e_error_decode_metadata_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorDecodeMetadata), forced: true)
  775. }
  776. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  777. }
  778. } else {
  779. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  780. }
  781. } else {
  782. completion(metadatas, metadatasUpdate, errorCode, errorDescription)
  783. }
  784. } else {
  785. completion(nil, nil, errorCode, errorDescription)
  786. }
  787. }
  788. } else {
  789. completion(nil, nil, 0, "")
  790. }
  791. } else {
  792. completion(nil, nil, errorCode, errorDescription)
  793. }
  794. }
  795. }
  796. }
  797. // MARK: - 3D Touch peek and pop
  798. extension NCCollectionViewCommon: UIViewControllerPreviewingDelegate {
  799. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  800. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  801. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  802. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return nil }
  803. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  804. viewController.metadata = metadata
  805. if layout == k_layout_grid {
  806. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  807. previewingContext.sourceRect = cell.frame
  808. viewController.imageFile = cell.imageItem.image
  809. } else {
  810. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  811. previewingContext.sourceRect = cell.frame
  812. viewController.imageFile = cell.imageItem.image
  813. }
  814. viewController.showOpenIn = true
  815. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  816. viewController.showShare = false
  817. return viewController
  818. }
  819. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  820. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  821. collectionView(collectionView, didSelectItemAt: indexPath)
  822. }
  823. }
  824. // MARK: - Collection View
  825. extension NCCollectionViewCommon: UICollectionViewDelegate {
  826. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  827. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else { return }
  828. metadataTouch = metadata
  829. if isEditMode {
  830. if let index = selectOcId.firstIndex(of: metadata.ocId) {
  831. selectOcId.remove(at: index)
  832. } else {
  833. selectOcId.append(metadata.ocId)
  834. }
  835. collectionView.reloadItems(at: [indexPath])
  836. return
  837. }
  838. if metadata.e2eEncrypted && !CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  839. NCContentPresenter.shared.messageNotification("_info_", description: "_e2e_goto_settings_for_enable_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorE2EENotEnabled), forced: true)
  840. return
  841. }
  842. if metadata.directory {
  843. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataTouch!.serverUrl, addFileName: metadataTouch!.fileName) else { return }
  844. // FILES
  845. if layoutKey == k_layout_view_files {
  846. if let viewController = appDelegate.listFilesVC.value(forKey: serverUrlPush) {
  847. guard let vcFiles = (viewController as? NCFiles) else { return }
  848. if vcFiles.isViewLoaded {
  849. self.navigationController?.pushViewController(vcFiles, animated: true)
  850. }
  851. } else {
  852. let vcFiles:NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as! NCFiles
  853. vcFiles.isRoot = false
  854. vcFiles.serverUrl = serverUrlPush
  855. vcFiles.titleCurrentFolder = metadataTouch!.fileNameView
  856. appDelegate.listFilesVC.setValue(vcFiles, forKey: serverUrlPush)
  857. self.navigationController?.pushViewController(vcFiles, animated: true)
  858. }
  859. }
  860. // FAVORITE
  861. if layoutKey == k_layout_view_favorite {
  862. if let viewController = appDelegate.listFavoriteVC.value(forKey: serverUrlPush) {
  863. guard let vcFavorite = (viewController as? NCFavorite) else { return }
  864. if vcFavorite.isViewLoaded {
  865. self.navigationController?.pushViewController(vcFavorite, animated: true)
  866. }
  867. } else {
  868. let vcFavorite:NCFavorite = UIStoryboard(name: "NCFavorite", bundle: nil).instantiateInitialViewController() as! NCFavorite
  869. vcFavorite.serverUrl = serverUrlPush
  870. vcFavorite.titleCurrentFolder = metadataTouch!.fileNameView
  871. appDelegate.listFavoriteVC.setValue(vcFavorite, forKey: serverUrlPush)
  872. self.navigationController?.pushViewController(vcFavorite, animated: true)
  873. }
  874. }
  875. // OFFLINE
  876. if layoutKey == k_layout_view_offline {
  877. if let viewController = appDelegate.listOfflineVC.value(forKey: serverUrlPush) {
  878. guard let vcOffline = (viewController as? NCOffline) else { return }
  879. if vcOffline.isViewLoaded {
  880. self.navigationController?.pushViewController(vcOffline, animated: true)
  881. }
  882. } else {
  883. let vcOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  884. vcOffline.serverUrl = serverUrlPush
  885. vcOffline.titleCurrentFolder = metadataTouch!.fileNameView
  886. appDelegate.listOfflineVC.setValue(vcOffline, forKey: serverUrlPush)
  887. self.navigationController?.pushViewController(vcOffline, animated: true)
  888. }
  889. }
  890. // RECENT ( for push use Files ... he he he )
  891. if layoutKey == k_layout_view_recent {
  892. if let viewController = appDelegate.listFilesVC.value(forKey: serverUrlPush) {
  893. guard let vcFiles = (viewController as? NCFiles) else { return }
  894. if vcFiles.isViewLoaded {
  895. self.navigationController?.pushViewController(vcFiles, animated: true)
  896. }
  897. } else {
  898. let vcFiles:NCFiles = UIStoryboard(name: "NCFiles", bundle: nil).instantiateInitialViewController() as! NCFiles
  899. vcFiles.isRoot = false
  900. vcFiles.serverUrl = serverUrlPush
  901. vcFiles.titleCurrentFolder = metadataTouch!.fileNameView
  902. appDelegate.listFilesVC.setValue(vcFiles, forKey: serverUrlPush)
  903. self.navigationController?.pushViewController(vcFiles, animated: true)
  904. }
  905. }
  906. } else {
  907. if metadata.typeFile == k_metadataTypeFile_document && NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil {
  908. if NCCommunication.shared.isNetworkReachable() {
  909. performSegue(withIdentifier: "segueDetail", sender: self)
  910. } else {
  911. NCContentPresenter.shared.messageNotification("_info_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorOffline), forced: true)
  912. }
  913. return
  914. }
  915. if metadata.typeFile == k_metadataTypeFile_document && NCUtility.shared.isRichDocument(metadata) {
  916. if NCCommunication.shared.isNetworkReachable() {
  917. performSegue(withIdentifier: "segueDetail", sender: self)
  918. } else {
  919. NCContentPresenter.shared.messageNotification("_info_", description: "_go_online_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorOffline), forced: true)
  920. }
  921. return
  922. }
  923. if CCUtility.fileProviderStorageExists(metadataTouch?.ocId, fileNameView: metadataTouch?.fileNameView) {
  924. performSegue(withIdentifier: "segueDetail", sender: self)
  925. } else {
  926. NCNetworking.shared.download(metadata: metadataTouch!, selector: selectorLoadFileView) { (_) in }
  927. }
  928. }
  929. }
  930. func collectionViewSelectAll() {
  931. selectOcId.removeAll()
  932. for metadata in metadatasSource {
  933. selectOcId.append(metadata.ocId)
  934. }
  935. collectionView.reloadData()
  936. }
  937. }
  938. extension NCCollectionViewCommon: UICollectionViewDataSource {
  939. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  940. if kind == UICollectionView.elementKindSectionHeader {
  941. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  942. if collectionView.collectionViewLayout == gridLayout {
  943. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), width: 100, height: 100, color: NCBrandColor.sharedInstance.icon), for: .normal)
  944. } else {
  945. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), width: 100, height: 100, color: NCBrandColor.sharedInstance.icon), for: .normal)
  946. }
  947. header.delegate = self
  948. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  949. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  950. header.setStatusButton(count: dataSource.metadatas.count)
  951. header.setTitleSorted(datasourceTitleButton: titleButton)
  952. header.viewRichWorkspaceHeightConstraint.constant = headerRichWorkspaceHeight
  953. header.setRichWorkspaceText(richWorkspaceText: richWorkspaceText)
  954. return header
  955. } else {
  956. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  957. let info = dataSource.getFilesInformation()
  958. footer.setTitleLabel(directories: info.directories, files: info.files, size: info.size )
  959. return footer
  960. }
  961. }
  962. func numberOfSections(in collectionView: UICollectionView) -> Int {
  963. return 1
  964. }
  965. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  966. return dataSource.numberOfItems()
  967. }
  968. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  969. guard let metadata = dataSource.cellForItemAt(indexPath: indexPath) else {
  970. return UICollectionViewCell()
  971. }
  972. var tableShare: tableShare?
  973. var isShare = false
  974. var isMounted = false
  975. // Download preview
  976. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: collectionView, indexPath: indexPath)
  977. if metadataFolder != nil {
  978. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  979. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  980. }
  981. if dataSource.metadataShare[metadata.ocId] != nil {
  982. tableShare = dataSource.metadataShare[metadata.ocId]
  983. }
  984. // LAYOUT LIST
  985. if layout == k_layout_list {
  986. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  987. cell.delegate = self
  988. cell.objectId = metadata.ocId
  989. cell.indexPath = indexPath
  990. cell.labelTitle.text = metadata.fileNameView
  991. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  992. cell.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  993. cell.imageSelect.image = nil
  994. cell.imageStatus.image = nil
  995. cell.imageLocal.image = nil
  996. cell.imageFavorite.image = nil
  997. cell.imageShared.image = nil
  998. cell.imageMore.image = nil
  999. cell.imageItem.image = nil
  1000. cell.imageItem.backgroundColor = nil
  1001. cell.progressView.progress = 0.0
  1002. if metadata.directory {
  1003. if metadata.e2eEncrypted {
  1004. cell.imageItem.image = NCCollectionCommon.images.cellFolderEncryptedImage
  1005. } else if isShare {
  1006. cell.imageItem.image = NCCollectionCommon.images.cellFolderSharedWithMeImage
  1007. } else if (tableShare != nil && tableShare?.shareType != 3) {
  1008. cell.imageItem.image = NCCollectionCommon.images.cellFolderSharedWithMeImage
  1009. } else if (tableShare != nil && tableShare?.shareType == 3) {
  1010. cell.imageItem.image = NCCollectionCommon.images.cellFolderPublicImage
  1011. } else if metadata.mountType == "group" {
  1012. cell.imageItem.image = NCCollectionCommon.images.cellFolderGroupImage
  1013. } else if isMounted {
  1014. cell.imageItem.image = NCCollectionCommon.images.cellFolderExternalImage
  1015. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  1016. cell.imageItem.image = NCCollectionCommon.images.cellFolderAutomaticUploadImage
  1017. } else {
  1018. cell.imageItem.image = NCCollectionCommon.images.cellFolderImage
  1019. }
  1020. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  1021. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  1022. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  1023. // Local image: offline
  1024. if tableDirectory != nil && tableDirectory!.offline {
  1025. cell.imageLocal.image = NCCollectionCommon.images.cellOfflineFlag
  1026. }
  1027. } else {
  1028. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  1029. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  1030. } else {
  1031. if metadata.hasPreview {
  1032. cell.imageItem.backgroundColor = .lightGray
  1033. } else {
  1034. if metadata.iconName.count > 0 {
  1035. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  1036. } else {
  1037. cell.imageItem.image = NCCollectionCommon.images.cellFileImage
  1038. }
  1039. }
  1040. }
  1041. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  1042. // image local
  1043. if dataSource.metadataLocalImage[metadata.ocId] == "offlineFlag" {
  1044. cell.imageLocal.image = NCCollectionCommon.images.cellOfflineFlag
  1045. } else if dataSource.metadataLocalImage[metadata.ocId] == "local" {
  1046. cell.imageLocal.image = NCCollectionCommon.images.cellLocal
  1047. }
  1048. }
  1049. // image Favorite
  1050. if metadata.favorite {
  1051. cell.imageFavorite.image = NCCollectionCommon.images.cellFavouriteImage
  1052. }
  1053. // Share image
  1054. if (isShare) {
  1055. cell.imageShared.image = NCCollectionCommon.images.cellSharedImage
  1056. } else if (tableShare != nil && tableShare?.shareType == 3) {
  1057. cell.imageShared.image = NCCollectionCommon.images.cellShareByLinkImage
  1058. } else if (tableShare != nil && tableShare?.shareType != 3) {
  1059. cell.imageShared.image = NCCollectionCommon.images.cellSharedImage
  1060. } else {
  1061. cell.imageShared.image = NCCollectionCommon.images.cellCanShareImage
  1062. }
  1063. if metadata.ownerId.count > 0 && metadata.ownerId != appDelegate.userID {
  1064. // Load avatar
  1065. let fileNameSource = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + metadata.ownerId + ".png"
  1066. let fileNameSourceAvatar = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-avatar-" + metadata.ownerId + ".png"
  1067. if FileManager.default.fileExists(atPath: fileNameSourceAvatar) {
  1068. cell.imageShared.image = UIImage(contentsOfFile: fileNameSourceAvatar)
  1069. } else if FileManager.default.fileExists(atPath: fileNameSource) {
  1070. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  1071. } else {
  1072. NCCommunication.shared.downloadAvatar(userID: metadata.ownerId, fileNameLocalPath: fileNameSource, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  1073. if errorCode == 0 && account == self.appDelegate.account {
  1074. cell.imageShared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  1075. }
  1076. }
  1077. }
  1078. }
  1079. if isEditMode {
  1080. cell.imageItemLeftConstraint.constant = 45
  1081. cell.imageSelect.isHidden = false
  1082. if selectOcId.contains(metadata.ocId) {
  1083. cell.imageSelect.image = NCCollectionCommon.images.cellCheckedYes
  1084. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  1085. } else {
  1086. cell.imageSelect.image = NCCollectionCommon.images.cellCheckedNo
  1087. cell.backgroundView = nil
  1088. }
  1089. } else {
  1090. cell.imageItemLeftConstraint.constant = 10
  1091. cell.imageSelect.isHidden = true
  1092. cell.backgroundView = nil
  1093. }
  1094. // Transfer
  1095. var progress: Float = 0.0
  1096. var totalBytes: Double = 0.0
  1097. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.ocId) as? NSArray
  1098. if progressArray != nil && progressArray?.count == 3 {
  1099. progress = progressArray?.object(at: 0) as? Float ?? 0
  1100. totalBytes = progressArray?.object(at: 1) as? Double ?? 0
  1101. }
  1102. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  1103. cell.progressView.isHidden = false
  1104. cell.setButtonMore(named: k_buttonMoreStop, image: NCCollectionCommon.images.cellButtonStop)
  1105. } else {
  1106. cell.progressView.isHidden = true
  1107. cell.progressView.progress = progress
  1108. cell.setButtonMore(named: k_buttonMoreMore, image: NCCollectionCommon.images.cellButtonMore)
  1109. }
  1110. // Write status on Label Info
  1111. switch metadata.status {
  1112. case Int(k_metadataStatusWaitDownload):
  1113. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  1114. break
  1115. case Int(k_metadataStatusInDownload):
  1116. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  1117. break
  1118. case Int(k_metadataStatusDownloading):
  1119. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↓ " + CCUtility.transformedSize(totalBytes)
  1120. break
  1121. case Int(k_metadataStatusWaitUpload):
  1122. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  1123. break
  1124. case Int(k_metadataStatusInUpload):
  1125. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  1126. break
  1127. case Int(k_metadataStatusUploading):
  1128. cell.labelInfo.text = CCUtility.transformedSize(metadata.size) + " - ↑ " + CCUtility.transformedSize(totalBytes)
  1129. break
  1130. default:
  1131. break
  1132. }
  1133. // Live Photo
  1134. if metadata.livePhoto {
  1135. cell.imageStatus.image = NCCollectionCommon.images.cellLivePhotoImage
  1136. }
  1137. // E2EE
  1138. if metadata.e2eEncrypted || isEncryptedFolder {
  1139. cell.hideButtonShare(true)
  1140. } else {
  1141. cell.hideButtonShare(false)
  1142. }
  1143. // Remove last separator
  1144. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  1145. cell.separator.isHidden = true
  1146. } else {
  1147. cell.separator.isHidden = false
  1148. }
  1149. return cell
  1150. }
  1151. // LAYOUT GRID
  1152. if layout == k_layout_grid {
  1153. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  1154. cell.delegate = self
  1155. cell.objectId = metadata.ocId
  1156. cell.indexPath = indexPath
  1157. cell.labelTitle.text = metadata.fileNameView
  1158. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  1159. cell.imageSelect.image = nil
  1160. cell.imageStatus.image = nil
  1161. cell.imageLocal.image = nil
  1162. cell.imageFavorite.image = nil
  1163. cell.imageItem.image = nil
  1164. cell.imageItem.backgroundColor = nil
  1165. cell.progressView.progress = 0.0
  1166. if metadata.directory {
  1167. if metadata.e2eEncrypted {
  1168. cell.imageItem.image = NCCollectionCommon.images.cellFolderEncryptedImage
  1169. } else if isShare {
  1170. cell.imageItem.image = NCCollectionCommon.images.cellFolderSharedWithMeImage
  1171. } else if (tableShare != nil && tableShare!.shareType != 3) {
  1172. cell.imageItem.image = NCCollectionCommon.images.cellFolderSharedWithMeImage
  1173. } else if (tableShare != nil && tableShare!.shareType == 3) {
  1174. cell.imageItem.image = NCCollectionCommon.images.cellFolderPublicImage
  1175. } else if metadata.mountType == "group" {
  1176. cell.imageItem.image = NCCollectionCommon.images.cellFolderGroupImage
  1177. } else if isMounted {
  1178. cell.imageItem.image = NCCollectionCommon.images.cellFolderExternalImage
  1179. } else if metadata.fileName == autoUploadFileName && metadata.serverUrl == autoUploadDirectory {
  1180. cell.imageItem.image = NCCollectionCommon.images.cellFolderAutomaticUploadImage
  1181. } else {
  1182. cell.imageItem.image = NCCollectionCommon.images.cellFolderImage
  1183. }
  1184. let lockServerUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  1185. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  1186. // Local image: offline
  1187. if tableDirectory != nil && tableDirectory!.offline {
  1188. cell.imageLocal.image = NCCollectionCommon.images.cellOfflineFlag
  1189. }
  1190. } else {
  1191. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  1192. cell.imageItem.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  1193. } else {
  1194. if metadata.hasPreview {
  1195. cell.imageItem.backgroundColor = .lightGray
  1196. } else {
  1197. if metadata.iconName.count > 0 {
  1198. cell.imageItem.image = UIImage.init(named: metadata.iconName)
  1199. } else {
  1200. cell.imageItem.image = NCCollectionCommon.images.cellFileImage
  1201. }
  1202. }
  1203. }
  1204. // image Local
  1205. if dataSource.metadataLocalImage[metadata.ocId] == "offlineFlag" {
  1206. cell.imageLocal.image = NCCollectionCommon.images.cellOfflineFlag
  1207. } else if dataSource.metadataLocalImage[metadata.ocId] == "local" {
  1208. cell.imageLocal.image = NCCollectionCommon.images.cellLocal
  1209. }
  1210. }
  1211. // image Favorite
  1212. if metadata.favorite {
  1213. cell.imageFavorite.image = NCCollectionCommon.images.cellFavouriteImage
  1214. }
  1215. if isEditMode {
  1216. cell.imageSelect.isHidden = false
  1217. if selectOcId.contains(metadata.ocId) {
  1218. cell.imageSelect.image = NCCollectionCommon.images.cellCheckedYes
  1219. cell.backgroundView = NCUtility.shared.cellBlurEffect(with: cell.bounds)
  1220. } else {
  1221. cell.imageSelect.image = NCCollectionCommon.images.cellCheckedNo
  1222. cell.backgroundView = nil
  1223. }
  1224. } else {
  1225. cell.imageSelect.isHidden = true
  1226. cell.backgroundView = nil
  1227. }
  1228. // Transfer
  1229. if metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status >= k_metadataStatusTypeUpload {
  1230. cell.progressView.isHidden = false
  1231. cell.setButtonMore(named: k_buttonMoreStop, image: NCCollectionCommon.images.cellButtonStop)
  1232. } else {
  1233. cell.progressView.isHidden = true
  1234. cell.progressView.progress = 0.0
  1235. cell.setButtonMore(named: k_buttonMoreMore, image: NCCollectionCommon.images.cellButtonMore)
  1236. }
  1237. // Live Photo
  1238. if metadata.livePhoto {
  1239. cell.imageStatus.image = NCCollectionCommon.images.cellLivePhotoImage
  1240. }
  1241. return cell
  1242. }
  1243. return UICollectionViewCell()
  1244. }
  1245. }
  1246. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  1247. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  1248. if richWorkspaceText?.count ?? 0 == 0 {
  1249. headerRichWorkspaceHeight = 0
  1250. } else {
  1251. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  1252. }
  1253. return CGSize(width: collectionView.frame.width, height: headerHeight + headerRichWorkspaceHeight)
  1254. }
  1255. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  1256. return CGSize(width: collectionView.frame.width, height: footerHeight)
  1257. }
  1258. }