Skip to content

Commit

Permalink
Swift UI: Refactor OPDS catalog selector (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
FuzzzzyBoy authored Nov 4, 2024
1 parent ef69587 commit ac1f0ed
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 203 deletions.
2 changes: 1 addition & 1 deletion TestApp/Integrations/Carthage/project+lcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/Carthage/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
4 changes: 2 additions & 2 deletions TestApp/Integrations/CocoaPods/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :ios, '14.0'
platform :ios, '18.0'

target 'TestApp' do
# Comment the next line if you don't want to use dynamic frameworks
Expand All @@ -23,7 +23,7 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '18.0'
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
Expand Down
4 changes: 2 additions & 2 deletions TestApp/Integrations/CocoaPods/Podfile+lcp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :ios, '14.0'
platform :ios, '18.0'

target 'TestApp' do
# Comment the next line if you don't want to use dynamic frameworks
Expand Down Expand Up @@ -26,7 +26,7 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '14.0'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '18.0'
config.build_settings['ENABLE_BITCODE'] = 'NO'
end
end
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/CocoaPods/project+lcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/CocoaPods/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/Local/project+lcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/Local/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/SPM/project+lcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
2 changes: 1 addition & 1 deletion TestApp/Integrations/SPM/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ targets:
TestApp:
type: application
platform: iOS
deploymentTarget: "14.0"
deploymentTarget: "18.0"
sources:
- path: Sources
excludes:
Expand Down
4 changes: 1 addition & 3 deletions TestApp/Sources/Library/LibraryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ class LibraryViewController: UIViewController, Loggable {

@objc func addBookFromDevice() {
var types = DocumentTypes.main.supportedUTTypes
if let type = UTType(String(kUTTypeText)) {
types.append(type)
}
types.append(UTType.text)

let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: types)
documentPicker.delegate = self
Expand Down
172 changes: 0 additions & 172 deletions TestApp/Sources/OPDS/OPDSCatalogSelectorViewController.swift

This file was deleted.

93 changes: 93 additions & 0 deletions TestApp/Sources/OPDS/OPDSCatalogs/EditOPDSCatalogView.swift
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 }
)
}
Loading

0 comments on commit ac1f0ed

Please sign in to comment.