Skip to content

Commit

Permalink
show file importer
Browse files Browse the repository at this point in the history
  • Loading branch information
caldrian committed Jan 29, 2025
1 parent 09247f3 commit d299afb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,39 +132,15 @@ private final class BackupProgressViewController: UIViewController {

descriptionLabel = .init()
descriptionLabel.numberOfLines = 0
// descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
// view.addSubview(descriptionLabel)
descriptionLabel.text = progressDescription

progressView = .init(progressViewStyle: .bar)
// progressView.translatesAutoresizingMaskIntoConstraints = false
// view.addSubview(progressView)
progressView.progress = progressValue

exportButton = .init(type: .system)
exportButton.setTitle("save", for: .normal)
// exportButton.translatesAutoresizingMaskIntoConstraints = false
exportButton.addTarget(self, action: #selector(showActivityViewController(_:)), for: .primaryActionTriggered)
exportButton.isEnabled = backupURL != nil
// view.addSubview(exportButton)

/*
NSLayoutConstraint.activate([

descriptionLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: view.leadingAnchor, multiplier: 3),
descriptionLabel.topAnchor.constraint(greaterThanOrEqualToSystemSpacingBelow: view.topAnchor, multiplier: 3),
view.trailingAnchor.constraint(equalToSystemSpacingAfter: descriptionLabel.trailingAnchor, multiplier: 3),

progressView.leadingAnchor.constraint(equalTo: descriptionLabel.leadingAnchor),
progressView.topAnchor.constraint(equalToSystemSpacingBelow: descriptionLabel.bottomAnchor, multiplier: 3),
descriptionLabel.trailingAnchor.constraint(equalTo: progressView.trailingAnchor),

exportButton.leadingAnchor.constraint(equalTo: descriptionLabel.leadingAnchor),
exportButton.topAnchor.constraint(equalToSystemSpacingBelow: progressView.bottomAnchor, multiplier: 3),
descriptionLabel.trailingAnchor.constraint(equalTo: exportButton.trailingAnchor),
view.bottomAnchor.constraint(equalToSystemSpacingBelow: exportButton.bottomAnchor, multiplier: 3)
])
*/

scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -195,10 +171,10 @@ private final class BackupProgressViewController: UIViewController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

// TODO: check calculation (e.g. regarding safe area bottom inset)
let stackViewHeight = stackView.frame.maxY + (navigationController?.navigationBar.frame.height ?? 0)
if let sheetPresentationController = navigationController?.sheetPresentationController {
sheetPresentationController.detents = [.custom { _ in stackViewHeight }]
print("setting detend to \(stackViewHeight)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ struct BackupRestoreMainView: View {
let backupAction: () -> Void
let restoreAction: (_ url: URL) -> Void

@State private var isFileImporterPresented = false

var body: some View {
List {
backupSection
Expand All @@ -42,7 +44,21 @@ struct BackupRestoreMainView: View {
private var restoreSection: some View {
Section(footer: Text(L10n.Localizable.Settings.RestoreFromBackup.description)) {
Button(L10n.Localizable.Settings.RestoreFromBackup.action) {
// restoreAction()
isFileImporterPresented = true
}
.fileImporter(
isPresented: $isFileImporterPresented,
allowedContentTypes: [.json]
) { result in
switch result {
case .success(let file):
let gotAccess = file.startAccessingSecurityScopedResource()
guard gotAccess else { return assertionFailure() } // TODO: also log before every assertionFailure
restoreAction(file)
file.stopAccessingSecurityScopedResource()
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ struct BackupRestoreRootView: View {

@StateObject var viewModel: BackupRestoreRootViewModel

let details = Details(name: "name", error: "error")

var body: some View {

BackupRestoreMainView(
Expand All @@ -44,10 +42,10 @@ struct BackupRestoreRootView: View {
.interactiveDismissDisabled()
.presentationDetents([.medium])
.sheet(isPresented: $viewModel.isSetBackupPasswordPresented) {
SetBackupPasswordView { password in
guard let password else { return viewModel.cancel() }
viewModel.createBackup(password: password)
}
SetBackupPasswordView(
onProceed: { password in viewModel.createBackup(password: password) },
onCancel: { viewModel.cancel() }
)
.interactiveDismissDisabled()
.presentationDetents([.large])
}
Expand Down Expand Up @@ -75,12 +73,6 @@ struct BackupRestoreRootView: View {

typealias BackupState = BackupProgressView.CreateBackupState

struct Details: Identifiable {
let name: String
let error: String
let id = UUID()
}

#Preview {
BackupRestoreRootView(viewModel: BackupRestoreRootViewModel())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,6 @@ final class BackupRestoreRootViewModel: ObservableObject {
}
}

// func getItemForExport() -> URL {
// guard case .backup(.backupReadyAndExported) = state else { fatalError() }
//
// let fileManager = FileManager.default
// let documentsURL = try! fileManager.url(
// for: .documentDirectory,
// in: .userDomainMask,
// appropriateFor: nil,
// create: false
// )
// let backupURL = documentsURL.appendingPathComponent("backup.json")
//
// // Example backup data
// let backupData: [String: Any] = [
// "timestamp": "Date()",
// "data": "Your backup data here"
// ]
//
// let data = try! JSONSerialization.data(withJSONObject: backupData, options: .prettyPrinted)
// try! data.write(to: backupURL)
//
// state = .backup(.backupReadyAndExported)
//
// return backupURL
// }

func cancel() {
switch state {
case .backup(.enterPassword):
Expand Down Expand Up @@ -138,7 +112,7 @@ final class BackupRestoreRootViewModel: ObservableObject {
}

isErrorAlertPresented = switch state {
case .backup(.backupFailed(let error)):
case .backup(.backupFailed):
true
default:
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ import SwiftUI

struct SetBackupPasswordView: View {

/// Password can be an empty string. If `nil` is returned, the user cancelled.
var result: (_ password: String?) -> Void
var onProceed: (_ password: String) -> Void
var onCancel: () -> Void

@Environment(\.dismiss) private var dismiss

var body: some View {
VStack {
Button("action") {
result("")
onProceed("")
}
Button("dismiss") {
result(nil)
onCancel()
}
}
}
}

#Preview {
SetBackupPasswordView { _ in }
SetBackupPasswordView(
onProceed: { _ in },
onCancel: {}
)
}

0 comments on commit d299afb

Please sign in to comment.