NCUploadAssets.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. import NextcloudKit
  25. import TLPhotoPicker
  26. import Mantis
  27. import Photos
  28. import QuickLook
  29. class NCHostingUploadAssetsView: NSObject {
  30. func makeShipDetailsUI(assets: [TLPHAsset], serverUrl: String, userBaseUrl: NCUserBaseUrl) -> UIViewController {
  31. let uploadAssets = NCUploadAssets(assets: assets, serverUrl: serverUrl, userBaseUrl: userBaseUrl )
  32. let details = UploadAssetsView(uploadAssets: uploadAssets)
  33. return UIHostingController(rootView: details)
  34. }
  35. }
  36. // MARK: - Class
  37. struct PreviewStore {
  38. var id: String
  39. var asset: TLPHAsset
  40. var assetType: TLPHAsset.AssetType
  41. var data: Data?
  42. var fileName: String
  43. var image: UIImage
  44. }
  45. class NCUploadAssets: NSObject, ObservableObject, NCCreateFormUploadConflictDelegate {
  46. @Published var serverUrl: String
  47. @Published var assets: [TLPHAsset]
  48. @Published var userBaseUrl: NCUserBaseUrl
  49. @Published var dismiss = false
  50. @Published var isUseAutoUploadFolder: Bool = false
  51. @Published var isUseAutoUploadSubFolder: Bool = false
  52. @Published var previewStore: [PreviewStore] = []
  53. @Published var showHUD: Bool = false
  54. @Published var uploadInProgress: Bool = false
  55. var metadatasNOConflict: [tableMetadata] = []
  56. var metadatasUploadInConflict: [tableMetadata] = []
  57. var timer: Timer?
  58. init(assets: [TLPHAsset], serverUrl: String, userBaseUrl: NCUserBaseUrl) {
  59. self.assets = assets
  60. self.serverUrl = serverUrl
  61. self.userBaseUrl = userBaseUrl
  62. }
  63. func loadImages() {
  64. var previewStore: [PreviewStore] = []
  65. DispatchQueue.global().async {
  66. for asset in self.assets {
  67. guard let image = asset.fullResolutionImage?.resizeImage(size: CGSize(width: 300, height: 300), isAspectRation: true), let localIdentifier = asset.phAsset?.localIdentifier else { continue }
  68. previewStore.append(PreviewStore(id: localIdentifier, asset: asset, assetType: asset.type, fileName: "", image: image))
  69. }
  70. DispatchQueue.main.async {
  71. self.previewStore = previewStore
  72. }
  73. }
  74. }
  75. func startTimer(navigationItem: UINavigationItem) {
  76. self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { _ in
  77. guard let buttonDone = navigationItem.leftBarButtonItems?.first, let buttonCrop = navigationItem.leftBarButtonItems?.last else { return }
  78. buttonCrop.isEnabled = true
  79. buttonDone.isEnabled = true
  80. if let markup = navigationItem.rightBarButtonItems?.first(where: { $0.accessibilityIdentifier == "QLOverlayMarkupButtonAccessibilityIdentifier" }) {
  81. if let originalButton = markup.value(forKey: "originalButton") as AnyObject? {
  82. if let symbolImageName = originalButton.value(forKey: "symbolImageName") as? String {
  83. if symbolImageName == "pencil.tip.crop.circle.on" {
  84. buttonCrop.isEnabled = false
  85. buttonDone.isEnabled = false
  86. }
  87. }
  88. }
  89. }
  90. })
  91. }
  92. func stopTimer() {
  93. self.timer?.invalidate()
  94. }
  95. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  96. guard let metadatas = metadatas else {
  97. self.showHUD = false
  98. self.uploadInProgress.toggle()
  99. return
  100. }
  101. func createProcessUploads() {
  102. if !self.dismiss {
  103. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatas, completion: { _ in
  104. self.dismiss = true
  105. })
  106. }
  107. }
  108. if isUseAutoUploadFolder {
  109. DispatchQueue.global().async {
  110. let assets = self.assets.compactMap { $0.phAsset }
  111. let result = NCNetworking.shared.createFolder(assets: assets, selector: NCGlobal.shared.selectorUploadFile, useSubFolder: self.isUseAutoUploadSubFolder, account: self.userBaseUrl.account, urlBase: self.userBaseUrl.urlBase, userId: self.userBaseUrl.userId, withPush: false)
  112. DispatchQueue.main.async {
  113. self.showHUD = false
  114. self.uploadInProgress.toggle()
  115. if result {
  116. createProcessUploads()
  117. } else {
  118. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_createsubfolders_upload_")
  119. NCContentPresenter.shared.showError(error: error)
  120. }
  121. }
  122. }
  123. } else {
  124. createProcessUploads()
  125. }
  126. }
  127. }
  128. // MARK: - View
  129. struct UploadAssetsView: View {
  130. @State private var fileName: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
  131. @State private var isMaintainOriginalFilename: Bool = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
  132. @State private var isAddFilenametype: Bool = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
  133. @State private var isPresentedSelect = false
  134. @State private var isPresentedUploadConflict = false
  135. @State private var isPresentedQuickLook = false
  136. @State private var isPresentedAlert = false
  137. @State private var fileNamePath = NSTemporaryDirectory() + "Photo.jpg"
  138. @State private var renameFileName: String = ""
  139. @State private var renameIndex: Int = 0
  140. @State private var metadata: tableMetadata?
  141. @State private var index: Int = 0
  142. var gridItems: [GridItem] = [GridItem()]
  143. @ObservedObject var uploadAssets: NCUploadAssets
  144. @Environment(\.presentationMode) var presentationMode
  145. init(uploadAssets: NCUploadAssets) {
  146. self.uploadAssets = uploadAssets
  147. uploadAssets.loadImages()
  148. }
  149. func getOriginalFilename() -> String {
  150. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  151. if let asset = uploadAssets.assets.first?.phAsset, let name = (asset.value(forKey: "filename") as? String) {
  152. return (name as NSString).deletingPathExtension
  153. } else {
  154. return ""
  155. }
  156. }
  157. func getTextServerUrl(_ serverUrl: String) -> String {
  158. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", uploadAssets.userBaseUrl.account, serverUrl)), let metadata = NCManageDatabase.shared.getMetadataFromOcId(directory.ocId) {
  159. return (metadata.fileNameView)
  160. } else {
  161. return (serverUrl as NSString).lastPathComponent
  162. }
  163. }
  164. func setFileNameMask(fileName: String?) -> String {
  165. guard let asset = uploadAssets.assets.first?.phAsset else { return "" }
  166. var preview: String = ""
  167. let creationDate = asset.creationDate ?? Date()
  168. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  169. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  170. if let fileName = fileName {
  171. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  172. if !fileName.isEmpty {
  173. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  174. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  175. fileDate: creationDate, fileType: asset.mediaType,
  176. keyFileName: NCGlobal.shared.keyFileNameMask,
  177. keyFileNameType: NCGlobal.shared.keyFileNameType,
  178. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  179. forcedNewFileName: false)
  180. } else {
  181. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  182. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  183. fileDate: creationDate,
  184. fileType: asset.mediaType,
  185. keyFileName: nil,
  186. keyFileNameType: NCGlobal.shared.keyFileNameType,
  187. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  188. forcedNewFileName: false)
  189. }
  190. } else {
  191. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  192. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  193. fileDate: creationDate,
  194. fileType: asset.mediaType,
  195. keyFileName: nil,
  196. keyFileNameType: NCGlobal.shared.keyFileNameType,
  197. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  198. forcedNewFileName: false)
  199. }
  200. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + (preview as NSString).deletingPathExtension
  201. }
  202. func save(completion: @escaping (_ metadatasNOConflict: [tableMetadata], _ metadatasUploadInConflict: [tableMetadata]) -> Void) {
  203. var metadatasNOConflict: [tableMetadata] = []
  204. var metadatasUploadInConflict: [tableMetadata] = []
  205. let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId, account: uploadAssets.userBaseUrl.account)
  206. var serverUrl = uploadAssets.isUseAutoUploadFolder ? autoUploadPath : uploadAssets.serverUrl
  207. for tlAsset in uploadAssets.assets {
  208. guard let asset = tlAsset.phAsset,
  209. let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }),
  210. let assetFileName = asset.value(forKey: "filename") as? NSString else { continue }
  211. var livePhoto: Bool = false
  212. let creationDate = asset.creationDate ?? Date()
  213. let ext = assetFileName.pathExtension.lowercased()
  214. let fileName = previewStore.fileName.isEmpty ?
  215. CCUtility.createFileName(assetFileName as String,
  216. fileDate: creationDate,
  217. fileType: asset.mediaType,
  218. keyFileName: NCGlobal.shared.keyFileNameMask,
  219. keyFileNameType: NCGlobal.shared.keyFileNameType,
  220. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  221. forcedNewFileName: false)!
  222. : (previewStore.fileName + "." + ext)
  223. if previewStore.assetType == .livePhoto && CCUtility.getLivePhoto() && previewStore.data == nil {
  224. livePhoto = true
  225. }
  226. // Auto upload with subfolder
  227. if uploadAssets.isUseAutoUploadSubFolder {
  228. let dateFormatter = DateFormatter()
  229. dateFormatter.dateFormat = "yyyy"
  230. let yearString = dateFormatter.string(from: creationDate)
  231. dateFormatter.dateFormat = "MM"
  232. let monthString = dateFormatter.string(from: creationDate)
  233. serverUrl = autoUploadPath + "/" + yearString + "/" + monthString
  234. }
  235. // Check if is in upload
  236. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", uploadAssets.userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  237. if !isRecordInSessions.isEmpty { continue }
  238. let metadata = NCManageDatabase.shared.createMetadata(account: uploadAssets.userBaseUrl.account, user: uploadAssets.userBaseUrl.user, userId: uploadAssets.userBaseUrl.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: serverUrl, urlBase: uploadAssets.userBaseUrl.urlBase, url: "", contentType: "", isLivePhoto: livePhoto)
  239. metadata.assetLocalIdentifier = asset.localIdentifier
  240. metadata.session = NCNetworking.shared.sessionIdentifierBackground
  241. metadata.sessionSelector = NCGlobal.shared.selectorUploadFile
  242. metadata.status = NCGlobal.shared.metadataStatusWaitUpload
  243. // Modified
  244. if let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }), let data = previewStore.data {
  245. if metadata.contentType == "image/heic" {
  246. let fileNameNoExtension = (fileName as NSString).deletingPathExtension
  247. metadata.contentType = "image/jpeg"
  248. metadata.fileName = fileNameNoExtension + ".jpg"
  249. metadata.fileNameView = fileNameNoExtension + ".jpg"
  250. }
  251. let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  252. do {
  253. try data.write(to: URL(fileURLWithPath: fileNamePath))
  254. metadata.isExtractFile = true
  255. metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
  256. metadata.creationDate = asset.creationDate as? NSDate ?? (Date() as NSDate)
  257. metadata.date = asset.modificationDate as? NSDate ?? (Date() as NSDate)
  258. } catch { }
  259. }
  260. if let result = NCManageDatabase.shared.getMetadataConflict(account: uploadAssets.userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
  261. metadata.fileName = result.fileName
  262. metadatasUploadInConflict.append(metadata)
  263. } else {
  264. metadatasNOConflict.append(metadata)
  265. }
  266. }
  267. completion(metadatasNOConflict, metadatasUploadInConflict)
  268. }
  269. func presentedQuickLook(index: Int) {
  270. var image: UIImage?
  271. if let imageData = uploadAssets.previewStore[index].data {
  272. image = UIImage(data: imageData)
  273. } else if let imageFullResolution = uploadAssets.previewStore[index].asset.fullResolutionImage?.fixedOrientation() {
  274. image = imageFullResolution
  275. }
  276. if let image = image {
  277. if let data = image.jpegData(compressionQuality: 1) {
  278. do {
  279. try data.write(to: URL(fileURLWithPath: fileNamePath))
  280. self.index = index
  281. isPresentedQuickLook = true
  282. } catch {
  283. }
  284. }
  285. }
  286. }
  287. func deleteAsset(index: Int) {
  288. uploadAssets.assets.remove(at: index)
  289. uploadAssets.previewStore.remove(at: index)
  290. if uploadAssets.previewStore.isEmpty {
  291. uploadAssets.dismiss = true
  292. }
  293. }
  294. var body: some View {
  295. NavigationView {
  296. ZStack(alignment: .top) {
  297. List {
  298. Section(footer: Text(NSLocalizedString("_modify_image_desc_", comment: ""))) {
  299. ScrollView(.horizontal) {
  300. LazyHGrid(rows: gridItems, alignment: .center, spacing: 10) {
  301. ForEach(0..<uploadAssets.previewStore.count, id: \.self) { index in
  302. let item = uploadAssets.previewStore[index]
  303. if #available(iOS 16, *) {
  304. Menu {
  305. Button(action: {
  306. renameFileName = uploadAssets.previewStore[index].fileName
  307. renameIndex = index
  308. isPresentedAlert = true
  309. }) {
  310. Label(NSLocalizedString("_rename_", comment: ""), systemImage: "pencil")
  311. }
  312. if item.asset.type == .photo || item.asset.type == .livePhoto {
  313. Button(action: {
  314. presentedQuickLook(index: index)
  315. }) {
  316. Label(NSLocalizedString("_modify_", comment: ""), systemImage: "pencil.tip.crop.circle")
  317. }
  318. }
  319. if item.data != nil {
  320. Button(action: {
  321. if let image = uploadAssets.previewStore[index].asset.fullResolutionImage?.resizeImage(size: CGSize(width: 300, height: 300), isAspectRation: true) {
  322. uploadAssets.previewStore[index].image = image
  323. uploadAssets.previewStore[index].data = nil
  324. uploadAssets.previewStore[index].assetType = uploadAssets.previewStore[index].asset.type
  325. }
  326. }) {
  327. Label(NSLocalizedString("_undo_modify_", comment: ""), systemImage: "arrow.uturn.backward.circle")
  328. }
  329. }
  330. if item.data == nil && item.asset.type == .livePhoto && item.assetType == .livePhoto {
  331. Button(action: {
  332. uploadAssets.previewStore[index].assetType = .photo
  333. }) {
  334. Label(NSLocalizedString("_disable_livephoto_", comment: ""), systemImage: "livephoto.slash")
  335. }
  336. } else if item.data == nil && item.asset.type == .livePhoto && item.assetType == .photo {
  337. Button(action: {
  338. uploadAssets.previewStore[index].assetType = .livePhoto
  339. }) {
  340. Label(NSLocalizedString("_enable_livephoto_", comment: ""), systemImage: "livephoto")
  341. }
  342. }
  343. Button(role: .destructive, action: {
  344. deleteAsset(index: index)
  345. }) {
  346. Label(NSLocalizedString("_remove_", comment: ""), systemImage: "trash")
  347. }
  348. } label: {
  349. ImageAsset(uploadAssets: uploadAssets, index: index)
  350. .alert(NSLocalizedString("_rename_file_", comment: ""), isPresented: $isPresentedAlert) {
  351. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $renameFileName)
  352. .autocapitalization(.none)
  353. .autocorrectionDisabled()
  354. Button(NSLocalizedString("_rename_", comment: ""), action: {
  355. uploadAssets.previewStore[renameIndex].fileName = renameFileName.trimmingCharacters(in: .whitespacesAndNewlines)
  356. })
  357. Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel, action: {})
  358. }
  359. }
  360. } else {
  361. ImageAsset(uploadAssets: uploadAssets, index: index)
  362. .onTapGesture {
  363. if item.asset.type == .photo || item.asset.type == .livePhoto {
  364. presentedQuickLook(index: index)
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. .fullScreenCover(isPresented: $isPresentedQuickLook) {
  373. ViewerQuickLook(url: URL(fileURLWithPath: fileNamePath), index: $index, isPresentedQuickLook: $isPresentedQuickLook, uploadAssets: uploadAssets)
  374. .ignoresSafeArea()
  375. }
  376. .redacted(reason: uploadAssets.previewStore.isEmpty ? .placeholder : [])
  377. Section {
  378. Toggle(isOn: $isMaintainOriginalFilename, label: {
  379. Text(NSLocalizedString("_maintain_original_filename_", comment: ""))
  380. .font(.system(size: 15))
  381. })
  382. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  383. if !isMaintainOriginalFilename {
  384. Toggle(isOn: $isAddFilenametype, label: {
  385. Text(NSLocalizedString("_add_filenametype_", comment: ""))
  386. .font(.system(size: 15))
  387. })
  388. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  389. }
  390. }
  391. Section {
  392. Toggle(isOn: $uploadAssets.isUseAutoUploadFolder, label: {
  393. Text(NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  394. .font(.system(size: 15))
  395. })
  396. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  397. if uploadAssets.isUseAutoUploadFolder {
  398. Toggle(isOn: $uploadAssets.isUseAutoUploadSubFolder, label: {
  399. Text(NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  400. .font(.system(size: 15))
  401. })
  402. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  403. }
  404. if !uploadAssets.isUseAutoUploadFolder {
  405. HStack {
  406. Label {
  407. if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == uploadAssets.serverUrl {
  408. Text("/")
  409. .font(.system(size: 15))
  410. .frame(maxWidth: .infinity, alignment: .trailing)
  411. } else {
  412. Text(self.getTextServerUrl(uploadAssets.serverUrl))
  413. .font(.system(size: 15))
  414. .frame(maxWidth: .infinity, alignment: .trailing)
  415. }
  416. } icon: {
  417. Image("folder")
  418. .renderingMode(.template)
  419. .resizable()
  420. .scaledToFit()
  421. .foregroundColor(Color(NCBrandColor.shared.brand))
  422. }
  423. }
  424. .contentShape(Rectangle())
  425. .onTapGesture {
  426. isPresentedSelect = true
  427. }
  428. }
  429. }
  430. Section {
  431. HStack {
  432. Text(NSLocalizedString("_filename_", comment: ""))
  433. if isMaintainOriginalFilename {
  434. Text(getOriginalFilename())
  435. .font(.system(size: 15))
  436. .frame(maxWidth: .infinity, alignment: .trailing)
  437. .foregroundColor(Color.gray)
  438. } else {
  439. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
  440. .font(.system(size: 15))
  441. .modifier(TextFieldClearButton(text: $fileName))
  442. .multilineTextAlignment(.trailing)
  443. }
  444. }
  445. if !isMaintainOriginalFilename {
  446. Text(setFileNameMask(fileName: fileName))
  447. .font(.system(size: 11))
  448. .foregroundColor(Color.gray)
  449. }
  450. }
  451. .complexModifier { view in
  452. if #available(iOS 15, *) {
  453. view.listRowSeparator(.hidden)
  454. }
  455. }
  456. Button(NSLocalizedString("_save_", comment: "")) {
  457. if uploadAssets.isUseAutoUploadFolder, uploadAssets.isUseAutoUploadSubFolder {
  458. uploadAssets.showHUD = true
  459. }
  460. uploadAssets.uploadInProgress.toggle()
  461. save { metadatasNOConflict, metadatasUploadInConflict in
  462. if metadatasUploadInConflict.isEmpty {
  463. uploadAssets.dismissCreateFormUploadConflict(metadatas: metadatasNOConflict)
  464. } else {
  465. uploadAssets.metadatasNOConflict = metadatasNOConflict
  466. uploadAssets.metadatasUploadInConflict = metadatasUploadInConflict
  467. isPresentedUploadConflict = true
  468. }
  469. }
  470. }
  471. .frame(maxWidth: .infinity)
  472. .buttonStyle(ButtonRounded(disabled: uploadAssets.uploadInProgress))
  473. .listRowBackground(Color(UIColor.systemGroupedBackground))
  474. .disabled(uploadAssets.uploadInProgress)
  475. }
  476. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  477. .navigationBarTitleDisplayMode(.inline)
  478. HUDView(showHUD: $uploadAssets.showHUD, textLabel: NSLocalizedString("_wait_", comment: ""), image: "doc.badge.arrow.up")
  479. .offset(y: uploadAssets.showHUD ? 5 : -200)
  480. .animation(.easeOut)
  481. }
  482. }
  483. .navigationViewStyle(StackNavigationViewStyle())
  484. .sheet(isPresented: $isPresentedSelect) {
  485. SelectView(serverUrl: $uploadAssets.serverUrl)
  486. }
  487. .sheet(isPresented: $isPresentedUploadConflict) {
  488. UploadConflictView(delegate: uploadAssets, serverUrl: uploadAssets.serverUrl, metadatasUploadInConflict: uploadAssets.metadatasUploadInConflict, metadatasNOConflict: uploadAssets.metadatasNOConflict)
  489. }
  490. .onReceive(uploadAssets.$dismiss) { newValue in
  491. if newValue {
  492. presentationMode.wrappedValue.dismiss()
  493. }
  494. }
  495. .onTapGesture {
  496. UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.endEditing(true)
  497. }
  498. .onDisappear {
  499. uploadAssets.dismiss = true
  500. }
  501. }
  502. struct ImageAsset: View {
  503. @ObservedObject var uploadAssets: NCUploadAssets
  504. @State var index: Int
  505. var body: some View {
  506. ZStack(alignment: .bottomTrailing) {
  507. if index < uploadAssets.previewStore.count {
  508. let item = uploadAssets.previewStore[index]
  509. Image(uiImage: item.image)
  510. .resizable()
  511. .aspectRatio(contentMode: .fill)
  512. .frame(width: 80, height: 80, alignment: .center)
  513. .cornerRadius(10)
  514. if item.assetType == .livePhoto && item.data == nil {
  515. Image(systemName: "livephoto")
  516. .resizable()
  517. .scaledToFit()
  518. .frame(width: 15, height: 15)
  519. .foregroundColor(.white)
  520. .padding(.horizontal, 5)
  521. .padding(.vertical, 5)
  522. } else if item.assetType == .video {
  523. Image(systemName: "video.fill")
  524. .resizable()
  525. .scaledToFit()
  526. .frame(width: 15, height: 15)
  527. .foregroundColor(.white)
  528. .padding(.horizontal, 5)
  529. .padding(.vertical, 5)
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. // MARK: - Preview
  537. struct UploadAssetsView_Previews: PreviewProvider {
  538. static var previews: some View {
  539. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  540. let uploadAssets = NCUploadAssets(assets: [], serverUrl: "/", userBaseUrl: appDelegate)
  541. UploadAssetsView(uploadAssets: uploadAssets)
  542. }
  543. }
  544. }