NCUploadAssets.swift 10 KB

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