NCUploadAssets.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // NCUploadAssets.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/01/23.
  6. // Copyright © 2023 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 SwiftUI
  24. class NCHostingUploadAssetsView: NSObject {
  25. @objc func makeShipDetailsUI(assets: [PHAsset], cryptated: Bool, session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) -> UIViewController {
  26. let uploadAssets = NCUploadAssets(assets: assets, cryptated: cryptated, session: session, userBaseUrl: userBaseUrl, serverUrl: serverUrl)
  27. let details = UploadAssetsView(uploadAssets)
  28. let vc = UIHostingController(rootView: details)
  29. return vc
  30. }
  31. }
  32. class NCUploadAssets: ObservableObject {
  33. internal var assets: [PHAsset]
  34. internal var cryptated: Bool
  35. internal var session: String
  36. internal var userBaseUrl: NCUserBaseUrl
  37. @Published var serverUrl: String
  38. @Published var isMaintainOriginalFilename: Bool = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
  39. @Published var isAddFilenametype: Bool = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
  40. init(assets: [PHAsset], cryptated: Bool, session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) {
  41. self.assets = assets
  42. self.cryptated = cryptated
  43. self.session = session
  44. self.userBaseUrl = userBaseUrl
  45. self.serverUrl = serverUrl
  46. }
  47. func setFileNameMask(fileName: String?) -> String {
  48. guard let asset = assets.first else { return "" }
  49. var preview: String = ""
  50. let creationDate = asset.creationDate ?? Date()
  51. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  52. if let fileName = fileName {
  53. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  54. if !fileName.isEmpty {
  55. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  56. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  57. fileDate: creationDate, fileType: asset.mediaType,
  58. keyFileName: NCGlobal.shared.keyFileNameMask,
  59. keyFileNameType: NCGlobal.shared.keyFileNameType,
  60. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  61. forcedNewFileName: false)
  62. } else {
  63. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  64. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  65. fileDate: creationDate,
  66. fileType: asset.mediaType,
  67. keyFileName: nil,
  68. keyFileNameType: NCGlobal.shared.keyFileNameType,
  69. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  70. forcedNewFileName: false)
  71. }
  72. } else {
  73. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  74. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  75. fileDate: creationDate,
  76. fileType: asset.mediaType,
  77. keyFileName: nil,
  78. keyFileNameType: NCGlobal.shared.keyFileNameType,
  79. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  80. forcedNewFileName: false)
  81. }
  82. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + preview
  83. }
  84. func getOriginalFilename() -> String {
  85. if let asset = assets.first, let name = (asset.value(forKey: "filename") as? String) {
  86. return name
  87. } else {
  88. return ""
  89. }
  90. }
  91. func save() {
  92. }
  93. }
  94. // MARK: - Delegate
  95. extension NCUploadAssets: NCSelectDelegate {
  96. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  97. if let serverUrl = serverUrl {
  98. CCUtility.setDirectoryScanDocument(serverUrl)
  99. self.serverUrl = serverUrl
  100. }
  101. }
  102. }
  103. // MARK: - View
  104. struct UploadAssetsView: View {
  105. @State var fileName: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
  106. @State var isPresentedSelect = false
  107. @ObservedObject var uploadAssets: NCUploadAssets
  108. init(_ uploadAssets: NCUploadAssets) {
  109. self.uploadAssets = uploadAssets
  110. }
  111. var body: some View {
  112. NavigationView {
  113. List {
  114. Section(header: Text(NSLocalizedString("_save_path_", comment: ""))) {
  115. HStack {
  116. Label {
  117. if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == uploadAssets.serverUrl {
  118. Text("/")
  119. .frame(maxWidth: .infinity, alignment: .trailing)
  120. } else {
  121. Text((uploadAssets.serverUrl as NSString).lastPathComponent)
  122. .frame(maxWidth: .infinity, alignment: .trailing)
  123. }
  124. } icon: {
  125. Image("folder")
  126. .renderingMode(.template)
  127. .resizable()
  128. .scaledToFit()
  129. .foregroundColor(Color(NCBrandColor.shared.brand))
  130. }
  131. }
  132. .contentShape(Rectangle())
  133. .onTapGesture {
  134. isPresentedSelect = true
  135. }
  136. }
  137. Section(header: Text(NSLocalizedString("_mode_filename_", comment: ""))) {
  138. Toggle(NSLocalizedString("_maintain_original_filename_", comment: ""), isOn: $uploadAssets.isMaintainOriginalFilename)
  139. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  140. .onChange(of: uploadAssets.isMaintainOriginalFilename) { _ in }
  141. Toggle(NSLocalizedString("_add_filenametype_", comment: ""), isOn: $uploadAssets.isAddFilenametype)
  142. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  143. .onChange(of: uploadAssets.isAddFilenametype) { _ in }
  144. }
  145. Section(header: Text(NSLocalizedString("_filename_", comment: ""))) {
  146. HStack {
  147. Text(NSLocalizedString("_filename_", comment: ""))
  148. if uploadAssets.isMaintainOriginalFilename {
  149. Text(uploadAssets.getOriginalFilename())
  150. .frame(maxWidth: .infinity, alignment: .trailing)
  151. } else {
  152. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
  153. .modifier(TextFieldClearButton(text: $fileName))
  154. .multilineTextAlignment(.trailing)
  155. }
  156. }
  157. if !uploadAssets.isMaintainOriginalFilename {
  158. Text(uploadAssets.setFileNameMask(fileName: fileName))
  159. }
  160. }
  161. .complexModifier { view in
  162. if #available(iOS 15, *) {
  163. view.listRowSeparator(.hidden)
  164. }
  165. }
  166. Button(NSLocalizedString("_save_", comment: "")) {
  167. }
  168. .frame(maxWidth: .infinity)
  169. .buttonStyle(ButtonRounded(disabled: false))
  170. .listRowBackground(Color(UIColor.systemGroupedBackground))
  171. }
  172. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  173. .navigationBarTitleDisplayMode(.inline)
  174. }
  175. .sheet(isPresented: $isPresentedSelect) {
  176. NCSelectViewControllerRepresentable(delegate: uploadAssets)
  177. }
  178. }
  179. }
  180. // MARK: - Preview
  181. struct UploadAssetsView_Previews: PreviewProvider {
  182. static var previews: some View {
  183. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  184. let uploadAssets = NCUploadAssets(assets: [], cryptated: false, session: "", userBaseUrl: appDelegate, serverUrl: "/")
  185. UploadAssetsView(uploadAssets)
  186. }
  187. }
  188. }