NCFavorite.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // NCFavorite.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/08/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. class NCFavorite: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet weak var collectionView: UICollectionView!
  26. var titleCurrentFolder = NSLocalizedString("_favorites_", comment: "")
  27. @objc var serverUrl = ""
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private var metadataPush: tableMetadata?
  30. private var isEditMode = false
  31. private var selectocId: [String] = []
  32. private var dataSource: NCDataSource?
  33. private var layout = ""
  34. private var groupBy = ""
  35. private var titleButton = ""
  36. private var itemForLine = 0
  37. private var autoUploadFileName = ""
  38. private var autoUploadDirectory = ""
  39. private var listLayout: NCListLayout!
  40. private var gridLayout: NCGridLayout!
  41. private let headerMenuHeight: CGFloat = 50
  42. private let sectionHeaderHeight: CGFloat = 20
  43. private let footerHeight: CGFloat = 50
  44. private let refreshControl = UIRefreshControl()
  45. required init?(coder aDecoder: NSCoder) {
  46. super.init(coder: aDecoder)
  47. appDelegate.activeFavorite = self
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Cell
  52. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  53. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  54. // Header
  55. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  56. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  57. // Footer
  58. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  59. collectionView.alwaysBounceVertical = true
  60. listLayout = NCListLayout()
  61. gridLayout = NCGridLayout()
  62. // Refresh Control
  63. collectionView.addSubview(refreshControl)
  64. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  65. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  66. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetwork), for: .valueChanged)
  67. // empty Data Source
  68. self.collectionView.emptyDataSetDelegate = self
  69. self.collectionView.emptyDataSetSource = self
  70. // 3D Touch peek and pop
  71. if traitCollection.forceTouchCapability == .available {
  72. registerForPreviewing(with: self, sourceView: view)
  73. }
  74. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  75. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  76. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  77. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  78. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  79. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  80. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  81. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  82. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  84. changeTheming()
  85. }
  86. override func viewWillAppear(_ animated: Bool) {
  87. super.viewWillAppear(animated)
  88. self.navigationItem.title = titleCurrentFolder
  89. // get auto upload folder
  90. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  91. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  92. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_favorite)
  93. gridLayout.itemForLine = CGFloat(itemForLine)
  94. if layout == k_layout_list {
  95. collectionView?.collectionViewLayout = listLayout
  96. } else {
  97. collectionView?.collectionViewLayout = gridLayout
  98. }
  99. reloadDataSource()
  100. }
  101. override func viewDidAppear(_ animated: Bool) {
  102. super.viewDidAppear(animated)
  103. reloadDataSourceNetwork()
  104. }
  105. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  106. super.viewWillTransition(to: size, with: coordinator)
  107. coordinator.animate(alongsideTransition: nil) { _ in
  108. self.collectionView?.collectionViewLayout.invalidateLayout()
  109. }
  110. }
  111. // MARK: - NotificationCenter
  112. @objc func changeTheming() {
  113. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  114. }
  115. @objc func deleteFile(_ notification: NSNotification) {
  116. if self.view?.window == nil { return }
  117. if let userInfo = notification.userInfo as NSDictionary? {
  118. if let metadata = userInfo["metadata"] as? tableMetadata, let onlyLocal = userInfo["onlyLocal"] as? Bool, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  119. NCCollectionCommon.shared.notificationDeleteFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, errorCode: errorCode, errorDescription: errorDescription, onlyLocal: onlyLocal)
  120. }
  121. }
  122. }
  123. @objc func downloadStartFile(_ notification: NSNotification) {
  124. if self.view?.window == nil { return }
  125. if let userInfo = notification.userInfo as NSDictionary? {
  126. if let metadata = userInfo["metadata"] as? tableMetadata {
  127. NCCollectionCommon.shared.notificationDownloadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  128. }
  129. }
  130. }
  131. @objc func downloadedFile(_ notification: NSNotification) {
  132. if self.view?.window == nil { return }
  133. if let userInfo = notification.userInfo as NSDictionary? {
  134. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  135. NCCollectionCommon.shared.notificationDownloadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  136. }
  137. }
  138. }
  139. @objc func downloadCancelFile(_ notification: NSNotification) {
  140. if self.view?.window == nil { return }
  141. if let userInfo = notification.userInfo as NSDictionary? {
  142. if let metadata = userInfo["metadata"] as? tableMetadata {
  143. NCCollectionCommon.shared.notificationDownloadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata)
  144. }
  145. }
  146. }
  147. @objc func uploadStartFile(_ notification: NSNotification) {
  148. if self.view?.window == nil { return }
  149. if let userInfo = notification.userInfo as NSDictionary? {
  150. if let metadata = userInfo["metadata"] as? tableMetadata {
  151. NCCollectionCommon.shared.notificationUploadStartFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, serverUrl: serverUrl, account: appDelegate.account)
  152. }
  153. }
  154. }
  155. @objc func uploadedFile(_ notification: NSNotification) {
  156. if self.view?.window == nil { return }
  157. if let userInfo = notification.userInfo as NSDictionary? {
  158. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  159. NCCollectionCommon.shared.notificationUploadedFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, serverUrl: serverUrl, account: appDelegate.account)
  160. }
  161. }
  162. }
  163. @objc func uploadCancelFile(_ notification: NSNotification) {
  164. if self.view?.window == nil { return }
  165. if let userInfo = notification.userInfo as NSDictionary? {
  166. if let metadata = userInfo["metadata"] as? tableMetadata {
  167. NCCollectionCommon.shared.notificationUploadCancelFile(collectionView: collectionView, dataSource: dataSource, metadata: metadata, serverUrl: serverUrl, account: appDelegate.account)
  168. }
  169. }
  170. }
  171. @objc func triggerProgressTask(_ notification: NSNotification) {
  172. if self.view?.window == nil { return }
  173. if let userInfo = notification.userInfo as NSDictionary? {
  174. if let ocId = userInfo["ocId"] as? String {
  175. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  176. let progress = progressNumber.floatValue
  177. NCCollectionCommon.shared.notificationTriggerProgressTask(collectionView: collectionView, dataSource: dataSource, ocId: ocId, progress: progress)
  178. }
  179. }
  180. }
  181. // MARK: DZNEmpty
  182. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  183. return NCBrandColor.sharedInstance.backgroundView
  184. }
  185. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  186. return CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 300, height: 300, color: NCBrandColor.sharedInstance.yellowFavorite)
  187. }
  188. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  189. let text = "\n"+NSLocalizedString("_favorite_no_files_", comment: "")
  190. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  191. return NSAttributedString.init(string: text, attributes: attributes)
  192. }
  193. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  194. let text = "\n"+NSLocalizedString("_tutorial_favorite_view_", comment: "")
  195. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  196. return NSAttributedString.init(string: text, attributes: attributes)
  197. }
  198. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  199. return true
  200. }
  201. // MARK: TAP EVENT
  202. func tapSwitchHeader(sender: Any) {
  203. if collectionView.collectionViewLayout == gridLayout {
  204. // list layout
  205. UIView.animate(withDuration: 0.0, animations: {
  206. self.collectionView.collectionViewLayout.invalidateLayout()
  207. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  208. self.collectionView.reloadData()
  209. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  210. })
  211. })
  212. layout = k_layout_list
  213. NCUtility.shared.setLayoutForView(key: k_layout_view_favorite, layout: layout)
  214. } else {
  215. // grid layout
  216. UIView.animate(withDuration: 0.0, animations: {
  217. self.collectionView.collectionViewLayout.invalidateLayout()
  218. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  219. self.collectionView.reloadData()
  220. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  221. })
  222. })
  223. layout = k_layout_grid
  224. NCUtility.shared.setLayoutForView(key: k_layout_view_favorite, layout: layout)
  225. }
  226. }
  227. func tapOrderHeader(sender: Any) {
  228. let sortMenu = NCSortMenu()
  229. sortMenu.toggleMenu(viewController: self, key: k_layout_view_favorite, sortButton: sender as? UIButton, serverUrl: serverUrl)
  230. }
  231. func tapMoreHeader(sender: Any) {
  232. }
  233. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  234. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  235. }
  236. func tapShareListItem(with objectId: String, sender: Any) {
  237. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  238. return
  239. }
  240. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  241. }
  242. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  243. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else { return }
  244. guard let tabBarController = self.tabBarController else { return }
  245. if namedButtonMore == "more" {
  246. toggleMoreMenu(viewController: tabBarController, metadata: metadata)
  247. } else if namedButtonMore == "stop" {
  248. NCMainCommon.shared.cancelTransferMetadata(metadata, uploadStatusForcedStart: false)
  249. }
  250. }
  251. // MARK: SEGUE
  252. @objc func segue(metadata: tableMetadata) {
  253. self.metadataPush = metadata
  254. performSegue(withIdentifier: "segueDetail", sender: self)
  255. }
  256. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  257. let photoDataSource: NSMutableArray = []
  258. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  259. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  260. photoDataSource.add(metadata)
  261. }
  262. }
  263. if let segueNavigationController = segue.destination as? UINavigationController {
  264. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  265. segueViewController.metadata = metadataPush
  266. }
  267. }
  268. }
  269. }
  270. // MARK: - 3D Touch peek and pop
  271. extension NCFavorite: UIViewControllerPreviewingDelegate {
  272. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  273. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  274. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  275. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  276. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  277. viewController.metadata = metadata
  278. if layout == k_layout_grid {
  279. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  280. previewingContext.sourceRect = cell.frame
  281. viewController.imageFile = cell.imageItem.image
  282. } else {
  283. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  284. previewingContext.sourceRect = cell.frame
  285. viewController.imageFile = cell.imageItem.image
  286. }
  287. viewController.showOpenIn = true
  288. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  289. viewController.showShare = false
  290. return viewController
  291. }
  292. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  293. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  294. collectionView(collectionView, didSelectItemAt: indexPath)
  295. }
  296. }
  297. // MARK: - Collection View
  298. extension NCFavorite: UICollectionViewDelegate {
  299. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  300. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return }
  301. metadataPush = metadata
  302. if isEditMode {
  303. if let index = selectocId.firstIndex(of: metadata.ocId) {
  304. selectocId.remove(at: index)
  305. } else {
  306. selectocId.append(metadata.ocId)
  307. }
  308. collectionView.reloadItems(at: [indexPath])
  309. return
  310. }
  311. if metadata.directory {
  312. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataPush!.serverUrl, addFileName: metadataPush!.fileName) else { return }
  313. let ncFavorite:NCFavorite = UIStoryboard(name: "NCFavorite", bundle: nil).instantiateInitialViewController() as! NCFavorite
  314. ncFavorite.serverUrl = serverUrlPush
  315. ncFavorite.titleCurrentFolder = metadataPush!.fileNameView
  316. self.navigationController?.pushViewController(ncFavorite, animated: true)
  317. } else {
  318. if CCUtility.fileProviderStorageExists(metadataPush?.ocId, fileNameView: metadataPush?.fileNameView) {
  319. performSegue(withIdentifier: "segueDetail", sender: self)
  320. } else {
  321. NCNetworking.shared.download(metadata: metadataPush!, selector: selectorLoadFileViewFavorite) { (_) in }
  322. }
  323. }
  324. }
  325. }
  326. extension NCFavorite: UICollectionViewDataSource {
  327. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  328. if (indexPath.section == 0) {
  329. if kind == UICollectionView.elementKindSectionHeader {
  330. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  331. if collectionView.collectionViewLayout == gridLayout {
  332. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  333. } else {
  334. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  335. }
  336. header.delegate = self
  337. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  338. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  339. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  340. header.setTitleSorted(datasourceTitleButton: titleButton)
  341. if groupBy == "none" {
  342. header.labelSection.isHidden = true
  343. header.labelSectionHeightConstraint.constant = 0
  344. } else {
  345. header.labelSection.isHidden = false
  346. header.setTitleLabel(title: "")
  347. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  348. }
  349. return header
  350. } else {
  351. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  352. let info = dataSource?.getFilesInformation()
  353. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  354. return footer
  355. }
  356. } else {
  357. if kind == UICollectionView.elementKindSectionHeader {
  358. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  359. header.setTitleLabel(title: "")
  360. return header
  361. } else {
  362. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  363. let info = dataSource?.getFilesInformation()
  364. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  365. return footer
  366. }
  367. }
  368. }
  369. func numberOfSections(in collectionView: UICollectionView) -> Int {
  370. return dataSource?.sections ?? 1
  371. }
  372. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  373. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  374. }
  375. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  376. let cell: UICollectionViewCell
  377. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  378. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  379. }
  380. if layout == k_layout_grid {
  381. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  382. } else {
  383. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  384. }
  385. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  386. NCCollectionCommon.shared.cellForItemAt(indexPath: indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectocId: selectocId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, shares: shares, source: self)
  387. return cell
  388. }
  389. }
  390. extension NCFavorite: UICollectionViewDelegateFlowLayout {
  391. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  392. if section == 0 {
  393. if groupBy == "none" {
  394. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  395. } else {
  396. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  397. }
  398. } else {
  399. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  400. }
  401. }
  402. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  403. let sections = dataSource?.sections ?? 1
  404. if (section == sections - 1) {
  405. return CGSize(width: collectionView.frame.width, height: footerHeight)
  406. } else {
  407. return CGSize(width: collectionView.frame.width, height: 0)
  408. }
  409. }
  410. }
  411. // MARK: - NC API & Algorithm
  412. extension NCFavorite {
  413. @objc func reloadDataSource() {
  414. var sort: String
  415. var ascending: Bool
  416. var directoryOnTop: Bool
  417. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_favorite)
  418. if serverUrl == "" {
  419. let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND favorite == true", appDelegate.account))
  420. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
  421. } else {
  422. let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  423. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
  424. }
  425. refreshControl.endRefreshing()
  426. collectionView.reloadData()
  427. }
  428. @objc func reloadDataSourceNetwork() {
  429. if serverUrl == "" {
  430. NCNetworking.shared.listingFavoritescompletion(selector: selectorListingFavorite) { (account, metadatas, errorCode, errorDescription) in
  431. if errorCode == 0 {
  432. for metadata in metadatas ?? [] {
  433. if !metadata.directory && CCUtility.getFavoriteOffline() {
  434. let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  435. if localFile == nil || localFile?.etag != metadata.etag {
  436. NCOperationQueue.shared.download(metadata: metadata, selector: selectorDownloadFile, setFavorite: false)
  437. }
  438. }
  439. }
  440. }
  441. self.reloadDataSource()
  442. }
  443. } else {
  444. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: appDelegate.account) { (account, metadataFolder, metadatas, metadatasUpdate, metadatasLocalUpdate, errorCode, errorDescription) in
  445. if errorCode == 0 {
  446. for metadata in metadatas ?? [] {
  447. if !metadata.directory && CCUtility.getFavoriteOffline() {
  448. let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  449. if localFile == nil || localFile?.etag != metadata.etag {
  450. NCOperationQueue.shared.download(metadata: metadata, selector: selectorDownloadFile, setFavorite: false)
  451. }
  452. }
  453. }
  454. }
  455. self.reloadDataSource()
  456. }
  457. }
  458. }
  459. }