NCFileNameModel.swift 4.0 KB

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