NCViewerImageCommon.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // NCViewerImageCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/03/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 NCViewerImageCommon: NSObject {
  25. @objc static let shared: NCViewerImageCommon = {
  26. let instance = NCViewerImageCommon()
  27. return instance
  28. }()
  29. func getMetadatasDatasource(metadata: tableMetadata?, favoriteDatasorce: Bool, mediaDatasorce: Bool, offLineDatasource: Bool) -> [tableMetadata]? {
  30. guard let metadata = metadata else { return nil }
  31. if favoriteDatasorce {
  32. return NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND favorite == 1 AND typeFile == %@", metadata.account, k_metadataTypeFile_image), sorted: CCUtility.getOrderSettings(), ascending: CCUtility.getAscendingSettings())
  33. } else if mediaDatasorce {
  34. return NCManageDatabase.sharedInstance.getMedias(account: metadata.account, predicate: NSPredicate(format: "account == %@ AND typeFile == %@", metadata.account, k_metadataTypeFile_image))
  35. } else if offLineDatasource {
  36. var datasourceSorted = ""
  37. var datasourceAscending = true
  38. (_, datasourceSorted, datasourceAscending, _, _) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_offline)
  39. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", metadata.account), sorted: datasourceSorted, ascending: datasourceAscending) {
  40. var ocIds = [String]()
  41. for file: tableLocalFile in files {
  42. ocIds.append(file.ocId)
  43. }
  44. return NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", metadata.account, ocIds), sorted: datasourceSorted, ascending: datasourceAscending)
  45. }
  46. } else {
  47. return NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND typeFile == %@", metadata.account, metadata.serverUrl, k_metadataTypeFile_image), sorted: CCUtility.getOrderSettings(), ascending: CCUtility.getAscendingSettings())
  48. }
  49. return nil
  50. }
  51. func getThumbnailImage(metadata: tableMetadata) -> UIImage? {
  52. if CCUtility.fileProviderStorageIconExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  53. let imagePath = CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  54. return UIImage.init(contentsOfFile: imagePath)
  55. }
  56. return nil
  57. }
  58. func getImage(metadata: tableMetadata) -> UIImage? {
  59. var image: UIImage?
  60. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 {
  61. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  62. let ext = CCUtility.getExtension(metadata.fileNameView)
  63. if ext == "GIF" { image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: imagePath)) }
  64. else { image = UIImage.init(contentsOfFile: imagePath) }
  65. }
  66. return image
  67. }
  68. func imageChangeSizeView(viewerImageViewController: NCViewerImageViewController?, size: CGSize, metadata: tableMetadata?) {
  69. guard let viewerImageViewController = viewerImageViewController else { return }
  70. var image: UIImage?
  71. var contentViewSaved : NCViewerImageContentView?
  72. for contentView in viewerImageViewController.contentViews {
  73. if contentView.position == 0 && contentView.isLoading == false {
  74. image = contentView.image
  75. contentViewSaved = contentView
  76. if metadata != nil , let thumbnailImage = self.getThumbnailImage(metadata: metadata!) {
  77. contentView.image = thumbnailImage
  78. } else {
  79. contentView.image = nil
  80. }
  81. }
  82. }
  83. DispatchQueue.main.async {
  84. viewerImageViewController.changeInViewSize(to: size)
  85. if image != nil {
  86. contentViewSaved?.image = image
  87. }
  88. }
  89. }
  90. }