-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift UI: Refactor OPDS catalog selector (#497)
- Loading branch information
1 parent
ef69587
commit ac1f0ed
Showing
20 changed files
with
379 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 0 additions & 172 deletions
172
TestApp/Sources/OPDS/OPDSCatalogSelectorViewController.swift
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
TestApp/Sources/OPDS/OPDSCatalogs/EditOPDSCatalogView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// Copyright 2024 Readium Foundation. All rights reserved. | ||
// Use of this source code is governed by the BSD-style license | ||
// available in the top-level LICENSE file of the project. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct EditOPDSCatalogView: View { | ||
@State var catalog: OPDSCatalog | ||
var onSave: (OPDSCatalog) -> Void | ||
|
||
@Environment(\.presentationMode) var presentationMode | ||
|
||
@State private var showErrorAlert = false | ||
@State private var errorTitle = "" | ||
@State private var errorMessage = "" | ||
@State private var urlString: String | ||
|
||
init( | ||
catalog: OPDSCatalog, | ||
onSave: @escaping (OPDSCatalog) -> Void | ||
) { | ||
self.catalog = catalog | ||
self.onSave = onSave | ||
urlString = catalog.url.absoluteString | ||
} | ||
|
||
var body: some View { | ||
NavigationView { | ||
Form { | ||
Section(header: Text("opds_add_title")) { | ||
TextField("Title", text: $catalog.title) | ||
TextField("URL", text: $urlString) | ||
.keyboardType(.URL) | ||
.autocapitalization(.none) | ||
.disableAutocorrection(true) | ||
} | ||
} | ||
.navigationBarItems( | ||
leading: Button("Cancel") { | ||
presentationMode.wrappedValue.dismiss() | ||
}, | ||
trailing: Button("Save") { | ||
validateAndSave() | ||
} | ||
) | ||
.alert(isPresented: $showErrorAlert) { | ||
Alert( | ||
title: Text(errorTitle), | ||
message: Text(errorMessage), | ||
dismissButton: .default(Text("OK")) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private func validateAndSave() { | ||
let trimmedTitle = catalog.title.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
||
if trimmedTitle.isEmpty { | ||
errorTitle = "Title Required" | ||
errorMessage = "Please enter a title." | ||
showErrorAlert = true | ||
return | ||
} | ||
|
||
if | ||
let url = URL(string: urlString), | ||
url.scheme != nil, | ||
url.host != nil | ||
{ | ||
catalog.url = url | ||
onSave(catalog) | ||
presentationMode.wrappedValue.dismiss() | ||
} else { | ||
errorTitle = "Invalid URL" | ||
errorMessage = "Please enter a valid URL." | ||
showErrorAlert = true | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
EditOPDSCatalogView( | ||
catalog: OPDSCatalog( | ||
id: UUID().uuidString, | ||
title: "OPDS 2.0 Test Catalog", | ||
url: URL(string: "https://test.opds.io/2.0/home.json")! | ||
), | ||
onSave: { _ in } | ||
) | ||
} |
Oops, something went wrong.