NCFavorite.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import NCCommunication
  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 = true
  31. emptyImage = UIImage.init(named: "favorite")?.image(color: NCBrandColor.shared.yellowFavorite, size: UIScreen.main.bounds.width)
  32. emptyTitle = "_favorite_no_files_"
  33. emptyDescription = "_tutorial_favorite_view_"
  34. }
  35. // MARK: - DataSource + NC Endpoint
  36. override func reloadDataSource() {
  37. super.reloadDataSource()
  38. DispatchQueue.global().async {
  39. if !self.isSearching {
  40. if self.serverUrl == "" {
  41. self.metadatasSource = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND favorite == true", self.appDelegate.account))
  42. } else {
  43. self.metadatasSource = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.serverUrl))
  44. }
  45. }
  46. self.dataSource = NCDataSource.init(metadatasSource: self.metadatasSource, sort:self.sort, ascending: self.ascending, directoryOnTop: self.directoryOnTop, favoriteOnTop: true, filterLivePhoto: true)
  47. DispatchQueue.main.async {
  48. self.refreshControl.endRefreshing()
  49. self.collectionView.reloadData()
  50. }
  51. }
  52. }
  53. override func reloadDataSourceNetwork(forced: Bool = false) {
  54. super.reloadDataSourceNetwork(forced: forced)
  55. if isSearching {
  56. networkSearch()
  57. return
  58. }
  59. isReloadDataSourceNetworkInProgress = true
  60. collectionView?.reloadData()
  61. if serverUrl == "" {
  62. NCNetworking.shared.listingFavoritescompletion(selector: NCGlobal.shared.selectorListingFavorite) { (account, metadatas, errorCode, errorDescription) in
  63. if errorCode != 0 {
  64. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  65. }
  66. self.refreshControl.endRefreshing()
  67. self.isReloadDataSourceNetworkInProgress = false
  68. self.reloadDataSource()
  69. }
  70. } else {
  71. networkReadFolder(forced: forced) { (metadatas, metadatasUpdate, errorCode, errorDescription) in
  72. if errorCode == 0 {
  73. for metadata in metadatas ?? [] {
  74. if !metadata.directory {
  75. if NCManageDatabase.shared.isDownloadMetadata(metadata, download: false) {
  76. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorDownloadFile)
  77. }
  78. }
  79. }
  80. }
  81. self.refreshControl.endRefreshing()
  82. self.isReloadDataSourceNetworkInProgress = false
  83. if metadatasUpdate?.count ?? 0 > 0 || forced {
  84. self.reloadDataSource()
  85. } else {
  86. self.collectionView?.reloadData()
  87. }
  88. }
  89. }
  90. }
  91. }