NCFavorite.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 UIKit
  24. import NextcloudKit
  25. class NCFavorite: NCCollectionViewCommon {
  26. required init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. titleCurrentFolder = NSLocalizedString("_favorites_", comment: "")
  29. layoutKey = NCGlobal.shared.layoutViewFavorite
  30. enableSearchBar = false
  31. headerRichWorkspaceDisable = true
  32. emptyImageName = "star.fill"
  33. emptyImageColors = [NCBrandColor.shared.yellowFavorite]
  34. emptyTitle = "_favorite_no_files_"
  35. emptyDescription = "_tutorial_favorite_view_"
  36. }
  37. // MARK: - View Life Cycle
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. reloadDataSource()
  41. }
  42. override func viewDidAppear(_ animated: Bool) {
  43. super.viewDidAppear(animated)
  44. getServerData()
  45. }
  46. // MARK: - DataSource
  47. override func reloadDataSource() {
  48. var predicate = self.defaultPredicate
  49. if self.serverUrl.isEmpty {
  50. predicate = NSPredicate(format: "account == %@ AND favorite == true AND NOT (status IN %@)", session.account, global.metadataStatusHideInView)
  51. }
  52. let metadatas = self.database.getResultsMetadatasPredicate(predicate, layoutForView: layoutForView)
  53. self.dataSource = NCCollectionViewDataSource(metadatas: metadatas, layoutForView: layoutForView)
  54. super.reloadDataSource()
  55. }
  56. override func getServerData() {
  57. NextcloudKit.shared.listingFavorites(showHiddenFiles: NCKeychain().showHiddenFiles, account: session.account) { task in
  58. self.dataSourceTask = task
  59. if self.dataSource.isEmpty() {
  60. self.collectionView.reloadData()
  61. }
  62. } completion: { account, files, _, error in
  63. if error == .success, let files {
  64. self.database.convertFilesToMetadatas(files, useFirstAsMetadataFolder: false) { _, metadatas in
  65. self.database.updateMetadatasFavorite(account: account, metadatas: metadatas)
  66. }
  67. self.reloadDataSource()
  68. }
  69. self.refreshControl.endRefreshing()
  70. }
  71. }
  72. }