Skip to content

Commit

Permalink
- Added upload button action for selected files.
Browse files Browse the repository at this point in the history
- Resolved update number of files bug.
  • Loading branch information
luciancerbu-vsp committed Oct 12, 2023
1 parent 4613991 commit 77efd25
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
32 changes: 19 additions & 13 deletions Permanent/Modules/Main/ViewController/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1519,20 +1519,26 @@ extension MainViewController: PhotoPickerViewControllerDelegate {
func photoTabBarViewControllerDidPickAssets(_ vc: PhotoTabBarViewController?, assets: [PHAsset]) {
let selectedArchive = AuthenticationManager.shared.session?.selectedArchive
let folderNavigationStack = viewModel?.navigationStack
let uploadManagerView = UploadManagerView(viewModel: UploadManagerViewModel(assets: assets, currentArchive: selectedArchive, folderNavigationStack: folderNavigationStack))

let uploadManagerViewModel = UploadManagerViewModel(assets: assets, currentArchive: selectedArchive, folderNavigationStack: folderNavigationStack)
uploadManagerViewModel.completionHandler = { [weak self] assets in
guard let self = self else { return }
if !assets.isEmpty {
let alert = UIAlertController(title: "Preparing Files...".localized(), message: nil, preferredStyle: .alert)
present(alert, animated: true)
viewModel?.didChooseFromPhotoLibrary(assets, completion: { [weak self] urls in
self?.dismiss(animated: true) { [self] in
guard let currentFolder = self?.viewModel?.currentFolder else {
return self?.showErrorAlert(message: .cannotUpload) ?? ()
}
self?.processUpload(toFolder: currentFolder, forURLS: urls)
}
})
}
}

let uploadManagerView = UploadManagerView(viewModel: uploadManagerViewModel)
let hostVC = UIHostingController(rootView: uploadManagerView)
self.present(hostVC, animated: true, completion: nil)
// To do - resolve upload mechanism
// let alert = UIAlertController(title: "Preparing Files...".localized(), message: nil, preferredStyle: .alert)
// present(alert, animated: true)
// viewModel?.didChooseFromPhotoLibrary(assets, completion: { [self] urls in
// dismiss(animated: true) { [self] in
// guard let currentFolder = viewModel?.currentFolder else {
// return showErrorAlert(message: .cannotUpload)
// }
//
// processUpload(toFolder: currentFolder, forURLS: urls)
// }
// })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ import Foundation
import Photos

class UploadManagerViewModel: ObservableObject {
var assets: [PHAsset] = []
@Published var assets: [PHAsset] = []
let currentArchive: ArchiveVOData?
let folderNavigationStack: [FileModel]?
@Published var assetURLs: [AssetDescriptor] = []
@Published var isLoading: Bool = false
@Published var numberOfFiles: String = ""
var numberOfAssets: String {
var filesNumber = "\(assets.count)"
if assets.count == 1 {
filesNumber += " FILE"
} else {
filesNumber += " FILES"
}
return filesNumber
}

var completionHandler: (([PHAsset]) -> Void)?

init(assets: [PHAsset], currentArchive: ArchiveVOData?, folderNavigationStack: [FileModel]?) {
self.assets = assets
Expand Down Expand Up @@ -118,39 +130,10 @@ class UploadManagerViewModel: ObservableObject {
return ""
}

// To do - resolve upload mechanism
// func didChooseFromPhotoLibrary(_ assets: [PHAsset], completion: @escaping ([URL]) -> Void) {
// let dispatchGroup = DispatchGroup()
// var urls: [URL] = []
//
// assets.forEach { photo in
// dispatchGroup.enter()
//
// photo.getURL { descriptor in
// guard let fileDescriptor = descriptor else {
// dispatchGroup.leave()
// return
// }
//
// do {
// let localURL = try FileHelper().copyFile(withURL: fileDescriptor.url, name: fileDescriptor.name)
// urls.append(localURL)
// } catch {
// print(error)
// }
//
// dispatchGroup.leave()
// }
// }
//
// dispatchGroup.notify(queue: .main, execute: {
// completion(urls)
// })
// }

func deleteAsset(at descriptor: AssetDescriptor) {
if let index = assetURLs.firstIndex(where: { $0 == descriptor }) {
assetURLs.remove(at: index)
assets.remove(at: index)
}
}
}
14 changes: 12 additions & 2 deletions Permanent/Modules/UploadManager/Views/UploadManagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct UploadManagerView: View {
.padding(.horizontal, -15)
Spacer(minLength: 6)
HStack {
Text("\(viewModel.getNumberOfAssets())".uppercased())
Text("\(viewModel.numberOfAssets)".uppercased())
.textStyle(SmallXXXRegularTextStyle())
.lineLimit(1)
.multilineTextAlignment(.trailing)
Expand Down Expand Up @@ -215,7 +215,7 @@ struct UploadManagerView: View {

var backButton: some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
dismissView()
}) {
HStack {
Text("Back")
Expand All @@ -226,13 +226,23 @@ struct UploadManagerView: View {

var uploadButton: some View {
Button(action: {
dismissViewWithAssets()
}) {
HStack {
Text("Upload")
.foregroundColor(.white)
}
}
}

func dismissView() {
presentationMode.wrappedValue.dismiss()
}

func dismissViewWithAssets() {
viewModel.completionHandler?(viewModel.assets)
presentationMode.wrappedValue.dismiss()
}
}

#Preview {
Expand Down

0 comments on commit 77efd25

Please sign in to comment.