NCUploadAssets.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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, 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 fileNamePath = NSTemporaryDirectory() + "Photo.jpg"
  136. @State private var metadata: tableMetadata?
  137. @State private var index: Int = 0
  138. var gridItems: [GridItem] = [GridItem()]
  139. @ObservedObject var uploadAssets: NCUploadAssets
  140. @Environment(\.presentationMode) var presentationMode
  141. init(uploadAssets: NCUploadAssets) {
  142. self.uploadAssets = uploadAssets
  143. uploadAssets.loadImages()
  144. }
  145. func getOriginalFilename() -> String {
  146. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  147. if let asset = uploadAssets.assets.first?.phAsset, let name = (asset.value(forKey: "filename") as? String) {
  148. return (name as NSString).deletingPathExtension
  149. } else {
  150. return ""
  151. }
  152. }
  153. func getTextServerUrl(_ serverUrl: String) -> String {
  154. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", uploadAssets.userBaseUrl.account, serverUrl)), let metadata = NCManageDatabase.shared.getMetadataFromOcId(directory.ocId) {
  155. return (metadata.fileNameView)
  156. } else {
  157. return (serverUrl as NSString).lastPathComponent
  158. }
  159. }
  160. func setFileNameMask(fileName: String?) -> String {
  161. guard let asset = uploadAssets.assets.first?.phAsset else { return "" }
  162. var preview: String = ""
  163. let creationDate = asset.creationDate ?? Date()
  164. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  165. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  166. if let fileName = fileName {
  167. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  168. if !fileName.isEmpty {
  169. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  170. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  171. fileDate: creationDate, fileType: asset.mediaType,
  172. keyFileName: NCGlobal.shared.keyFileNameMask,
  173. keyFileNameType: NCGlobal.shared.keyFileNameType,
  174. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  175. forcedNewFileName: false)
  176. } else {
  177. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  178. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  179. fileDate: creationDate,
  180. fileType: asset.mediaType,
  181. keyFileName: nil,
  182. keyFileNameType: NCGlobal.shared.keyFileNameType,
  183. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  184. forcedNewFileName: false)
  185. }
  186. } else {
  187. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  188. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  189. fileDate: creationDate,
  190. fileType: asset.mediaType,
  191. keyFileName: nil,
  192. keyFileNameType: NCGlobal.shared.keyFileNameType,
  193. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  194. forcedNewFileName: false)
  195. }
  196. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + (preview as NSString).deletingPathExtension
  197. }
  198. func save(completion: @escaping (_ metadatasNOConflict: [tableMetadata], _ metadatasUploadInConflict: [tableMetadata]) -> Void) {
  199. var metadatasNOConflict: [tableMetadata] = []
  200. var metadatasUploadInConflict: [tableMetadata] = []
  201. let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId, account: uploadAssets.userBaseUrl.account)
  202. var serverUrl = uploadAssets.isUseAutoUploadFolder ? autoUploadPath : uploadAssets.serverUrl
  203. for asset in uploadAssets.assets {
  204. guard let asset = asset.phAsset, let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }) else { continue }
  205. var livePhoto: Bool = false
  206. let creationDate = asset.creationDate ?? Date()
  207. let fileName = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  208. fileDate: creationDate,
  209. fileType: asset.mediaType,
  210. keyFileName: NCGlobal.shared.keyFileNameMask,
  211. keyFileNameType: NCGlobal.shared.keyFileNameType,
  212. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  213. forcedNewFileName: false)!
  214. if asset.mediaSubtypes.contains(.photoLive) && CCUtility.getLivePhoto() && previewStore.data == nil {
  215. livePhoto = true
  216. }
  217. // Auto upload with subfolder
  218. if uploadAssets.isUseAutoUploadSubFolder {
  219. let dateFormatter = DateFormatter()
  220. dateFormatter.dateFormat = "yyyy"
  221. let yearString = dateFormatter.string(from: creationDate)
  222. dateFormatter.dateFormat = "MM"
  223. let monthString = dateFormatter.string(from: creationDate)
  224. serverUrl = autoUploadPath + "/" + yearString + "/" + monthString
  225. }
  226. // Check if is in upload
  227. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", uploadAssets.userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  228. if !isRecordInSessions.isEmpty { continue }
  229. 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)
  230. metadata.assetLocalIdentifier = asset.localIdentifier
  231. metadata.session = NCNetworking.shared.sessionIdentifierBackground
  232. metadata.sessionSelector = NCGlobal.shared.selectorUploadFile
  233. metadata.status = NCGlobal.shared.metadataStatusWaitUpload
  234. // Modified
  235. if let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }), let data = previewStore.data {
  236. if metadata.contentType == "image/heic" {
  237. let fileNameNoExtension = (fileName as NSString).deletingPathExtension
  238. metadata.contentType = "image/jpeg"
  239. metadata.fileName = fileNameNoExtension + ".jpg"
  240. metadata.fileNameView = fileNameNoExtension + ".jpg"
  241. }
  242. let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  243. do {
  244. try data.write(to: URL(fileURLWithPath: fileNamePath))
  245. metadata.isExtractFile = true
  246. metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
  247. metadata.creationDate = asset.creationDate as? NSDate ?? (Date() as NSDate)
  248. metadata.date = asset.modificationDate as? NSDate ?? (Date() as NSDate)
  249. } catch { }
  250. }
  251. if let result = NCManageDatabase.shared.getMetadataConflict(account: uploadAssets.userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
  252. metadata.fileName = result.fileName
  253. metadatasUploadInConflict.append(metadata)
  254. } else {
  255. metadatasNOConflict.append(metadata)
  256. }
  257. }
  258. completion(metadatasNOConflict, metadatasUploadInConflict)
  259. }
  260. func presentedQuickLook(index: Int) {
  261. var image: UIImage?
  262. if let imageData = uploadAssets.previewStore[index].data {
  263. image = UIImage(data: imageData)
  264. } else if let imageFullResolution = uploadAssets.previewStore[index].asset.fullResolutionImage?.fixedOrientation() {
  265. image = imageFullResolution
  266. }
  267. if let image = image {
  268. if let data = image.pngData() {
  269. do {
  270. try data.write(to: URL(fileURLWithPath: fileNamePath))
  271. self.index = index
  272. isPresentedQuickLook = true
  273. } catch {
  274. }
  275. }
  276. }
  277. }
  278. func deleteAsset(index: Int) {
  279. uploadAssets.assets.remove(at: index)
  280. uploadAssets.previewStore.remove(at: index)
  281. if uploadAssets.previewStore.isEmpty {
  282. uploadAssets.dismiss = true
  283. }
  284. }
  285. var body: some View {
  286. NavigationView {
  287. ZStack(alignment: .top) {
  288. List {
  289. Section(header: Text(NSLocalizedString("_modify_photo_", comment: "")), footer: Text(NSLocalizedString("_modify_photo_desc_", comment: ""))) {
  290. ScrollView(.horizontal) {
  291. LazyHGrid(rows: gridItems, alignment: .center, spacing: 10) {
  292. ForEach(0..<uploadAssets.previewStore.count, id: \.self) { index in
  293. let item = uploadAssets.previewStore[index]
  294. if #available(iOS 15, *) {
  295. Menu {
  296. Button(action: {
  297. }) {
  298. Label(NSLocalizedString("_rename_", comment: ""), systemImage: "pencil")
  299. }
  300. if item.asset.type == .photo || item.asset.type == .livePhoto {
  301. Button(action: {
  302. presentedQuickLook(index: index)
  303. }) {
  304. Label(NSLocalizedString("_modify_", comment: ""), systemImage: "pencil.tip.crop.circle")
  305. }
  306. }
  307. Button(action: {
  308. deleteAsset(index: index)
  309. }) {
  310. Label(NSLocalizedString("_remove_", comment: ""), systemImage: "trash")
  311. }
  312. } label: {
  313. ImageAsset(uploadAssets: uploadAssets, index: index)
  314. }
  315. } else {
  316. ImageAsset(uploadAssets: uploadAssets, index: index)
  317. .onTapGesture {
  318. if item.asset.type == .photo || item.asset.type == .livePhoto {
  319. presentedQuickLook(index: index)
  320. }
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. .fullScreenCover(isPresented: $isPresentedQuickLook) {
  328. ViewerQuickLook(url: URL(fileURLWithPath: fileNamePath), index: $index, isPresentedQuickLook: $isPresentedQuickLook, uploadAssets: uploadAssets)
  329. .ignoresSafeArea()
  330. }
  331. .redacted(reason: uploadAssets.previewStore.isEmpty ? .placeholder : [])
  332. Section {
  333. Toggle(isOn: $isMaintainOriginalFilename, label: {
  334. Text(NSLocalizedString("_maintain_original_filename_", comment: ""))
  335. .font(.system(size: 15))
  336. })
  337. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  338. if !isMaintainOriginalFilename {
  339. Toggle(isOn: $isAddFilenametype, label: {
  340. Text(NSLocalizedString("_add_filenametype_", comment: ""))
  341. .font(.system(size: 15))
  342. })
  343. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  344. }
  345. }
  346. Section {
  347. Toggle(isOn: $uploadAssets.isUseAutoUploadFolder, label: {
  348. Text(NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  349. .font(.system(size: 15))
  350. })
  351. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  352. if uploadAssets.isUseAutoUploadFolder {
  353. Toggle(isOn: $uploadAssets.isUseAutoUploadSubFolder, label: {
  354. Text(NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  355. .font(.system(size: 15))
  356. })
  357. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  358. }
  359. if !uploadAssets.isUseAutoUploadFolder {
  360. HStack {
  361. Label {
  362. if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == uploadAssets.serverUrl {
  363. Text("/")
  364. .font(.system(size: 15))
  365. .frame(maxWidth: .infinity, alignment: .trailing)
  366. } else {
  367. Text(self.getTextServerUrl(uploadAssets.serverUrl))
  368. .font(.system(size: 15))
  369. .frame(maxWidth: .infinity, alignment: .trailing)
  370. }
  371. } icon: {
  372. Image("folder")
  373. .renderingMode(.template)
  374. .resizable()
  375. .scaledToFit()
  376. .foregroundColor(Color(NCBrandColor.shared.brand))
  377. }
  378. }
  379. .contentShape(Rectangle())
  380. .onTapGesture {
  381. isPresentedSelect = true
  382. }
  383. }
  384. }
  385. Section {
  386. HStack {
  387. Text(NSLocalizedString("_filename_", comment: ""))
  388. if isMaintainOriginalFilename {
  389. Text(getOriginalFilename())
  390. .font(.system(size: 15))
  391. .frame(maxWidth: .infinity, alignment: .trailing)
  392. .foregroundColor(Color.gray)
  393. } else {
  394. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
  395. .font(.system(size: 15))
  396. .modifier(TextFieldClearButton(text: $fileName))
  397. .multilineTextAlignment(.trailing)
  398. }
  399. }
  400. if !isMaintainOriginalFilename {
  401. Text(setFileNameMask(fileName: fileName))
  402. .font(.system(size: 11))
  403. .foregroundColor(Color.gray)
  404. }
  405. }
  406. .complexModifier { view in
  407. if #available(iOS 15, *) {
  408. view.listRowSeparator(.hidden)
  409. }
  410. }
  411. Button(NSLocalizedString("_save_", comment: "")) {
  412. if uploadAssets.isUseAutoUploadFolder, uploadAssets.isUseAutoUploadSubFolder {
  413. uploadAssets.showHUD = true
  414. }
  415. uploadAssets.uploadInProgress.toggle()
  416. save { metadatasNOConflict, metadatasUploadInConflict in
  417. if metadatasUploadInConflict.isEmpty {
  418. uploadAssets.dismissCreateFormUploadConflict(metadatas: metadatasNOConflict)
  419. } else {
  420. uploadAssets.metadatasNOConflict = metadatasNOConflict
  421. uploadAssets.metadatasUploadInConflict = metadatasUploadInConflict
  422. isPresentedUploadConflict = true
  423. }
  424. }
  425. }
  426. .frame(maxWidth: .infinity)
  427. .buttonStyle(ButtonRounded(disabled: uploadAssets.uploadInProgress))
  428. .listRowBackground(Color(UIColor.systemGroupedBackground))
  429. .disabled(uploadAssets.uploadInProgress)
  430. }
  431. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  432. .navigationBarTitleDisplayMode(.inline)
  433. HUDView(showHUD: $uploadAssets.showHUD, textLabel: NSLocalizedString("_wait_", comment: ""), image: "doc.badge.arrow.up")
  434. .offset(y: uploadAssets.showHUD ? 5 : -200)
  435. .animation(.easeOut)
  436. }
  437. }
  438. .navigationViewStyle(StackNavigationViewStyle())
  439. .sheet(isPresented: $isPresentedSelect) {
  440. SelectView(serverUrl: $uploadAssets.serverUrl)
  441. }
  442. .sheet(isPresented: $isPresentedUploadConflict) {
  443. UploadConflictView(delegate: uploadAssets, serverUrl: uploadAssets.serverUrl, metadatasUploadInConflict: uploadAssets.metadatasUploadInConflict, metadatasNOConflict: uploadAssets.metadatasNOConflict)
  444. }
  445. .onReceive(uploadAssets.$dismiss) { newValue in
  446. if newValue {
  447. presentationMode.wrappedValue.dismiss()
  448. }
  449. }
  450. .onTapGesture {
  451. UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.endEditing(true)
  452. }
  453. .onDisappear {
  454. uploadAssets.dismiss = true
  455. }
  456. }
  457. struct ImageAsset: View {
  458. @ObservedObject var uploadAssets: NCUploadAssets
  459. @State var index: Int
  460. var body: some View {
  461. ZStack(alignment: .bottomTrailing) {
  462. if index < uploadAssets.previewStore.count {
  463. let item = uploadAssets.previewStore[index]
  464. Image(uiImage: item.image)
  465. .resizable()
  466. .aspectRatio(contentMode: .fill)
  467. .frame(width: 80, height: 80, alignment: .center)
  468. .cornerRadius(10)
  469. if item.asset.type == .livePhoto && item.data == nil {
  470. Image(systemName: "livephoto")
  471. .resizable()
  472. .scaledToFit()
  473. .frame(width: 15, height: 15)
  474. .foregroundColor(.white)
  475. .padding(.horizontal, 5)
  476. .padding(.vertical, 5)
  477. } else if item.asset.type == .video {
  478. Image(systemName: "video.fill")
  479. .resizable()
  480. .scaledToFit()
  481. .frame(width: 15, height: 15)
  482. .foregroundColor(.white)
  483. .padding(.horizontal, 5)
  484. .padding(.vertical, 5)
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. // MARK: - Preview
  492. struct UploadAssetsView_Previews: PreviewProvider {
  493. static var previews: some View {
  494. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  495. let uploadAssets = NCUploadAssets(assets: [], serverUrl: "/", userBaseUrl: appDelegate)
  496. UploadAssetsView(uploadAssets: uploadAssets)
  497. }
  498. }
  499. }