NCFavorite.swift 28 KB

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