NCFileNameModel.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NCFileNameModel.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/06/24.
  6. // Copyright © 2024 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 NextcloudKit
  25. import SwiftUI
  26. import Photos
  27. /// A view model responsible for managing auto-upload file names.
  28. class NCFileNameModel: ObservableObject, ViewOnAppearHandling {
  29. /// A keychain instance for handling authentication.
  30. private var keychain = NCKeychain()
  31. /// A shared global instance for managing application-wide settings.
  32. private let globalKey = NCGlobal.shared
  33. /// A boolean indicating whether to maintain the original file name.
  34. @Published var maintainFilenameOriginal: Bool = NCKeychain().fileNameOriginal
  35. /// A boolean indicating whether to specify a custom file name.
  36. @Published var addFileNameType: Bool = NCKeychain().fileNameType
  37. /// The changed file name.
  38. @Published var changedName: String = ""
  39. /// The complete new file name.
  40. @Published var fileNamePreview: String = ""
  41. /// Initializes the view model with default values.
  42. init() {
  43. onViewAppear()
  44. }
  45. /// Triggered when the view appears.
  46. func onViewAppear() {
  47. changedName = keychain.fileNameMask
  48. getFileName()
  49. }
  50. // MARK: - All functions
  51. func getFileName() {
  52. fileNamePreview = previewFileName()
  53. }
  54. /// Toggles adding filename type.
  55. func toggleAddFilenameType(newValue: Bool) {
  56. keychain.fileNameType = newValue
  57. }
  58. /// Toggles maintain original asset filename.
  59. func toggleMaintainFilenameOriginal(newValue: Bool) {
  60. keychain.fileNameOriginal = newValue
  61. }
  62. /// Submits the changed file name.
  63. func submitChangedName() {
  64. let fileNameWithoutForbiddenChars = NCUtility().removeForbiddenCharacters(changedName)
  65. if changedName != fileNameWithoutForbiddenChars {
  66. changedName = fileNameWithoutForbiddenChars
  67. let errorDescription = String(format: NSLocalizedString("_forbidden_characters_", comment: ""), NCGlobal.shared.forbiddenCharacters.joined(separator: " "))
  68. let error = NKError(errorCode: NCGlobal.shared.errorConflict, errorDescription: errorDescription)
  69. NCContentPresenter().showInfo(error: error)
  70. }
  71. }
  72. /// Generates a preview file name based on current settings and file name mask.
  73. /// - Returns: The preview file name.
  74. func previewFileName() -> String {
  75. // Check if maintaining original file name is enabled
  76. let valueRenameTrimming = changedName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  77. // If the changed name is empty, set the filename mask to empty and generate a new filename
  78. if valueRenameTrimming.isEmpty {
  79. keychain.fileNameMask = ""
  80. } else {
  81. // If there is a changed name, set the filename mask and generate a new filename
  82. keychain.fileNameMask = changedName
  83. }
  84. return NCUtilityFileSystem().createFileName("IMG_0001.JPG", fileDate: Date(), fileType: PHAssetMediaType.image)
  85. }
  86. }