NCUploadAssets.swift 29 KB

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