From 40a036604ce58932f4b780946a8ef17a06c81d67 Mon Sep 17 00:00:00 2001 From: Vincent Tourraine Date: Tue, 9 Jan 2024 13:55:16 +0100 Subject: [PATCH 1/2] Update SPM example project with SwiftUI interface --- .../Base.lproj/Main.storyboard | 19 ++++++++++++++----- .../AcknowExampleSPM/ViewController.swift | 12 ++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Examples/AcknowExampleSPM/AcknowExampleSPM/Base.lproj/Main.storyboard b/Examples/AcknowExampleSPM/AcknowExampleSPM/Base.lproj/Main.storyboard index e204a62..754522d 100644 --- a/Examples/AcknowExampleSPM/AcknowExampleSPM/Base.lproj/Main.storyboard +++ b/Examples/AcknowExampleSPM/AcknowExampleSPM/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -18,25 +18,34 @@ + - + + + diff --git a/Examples/AcknowExampleSPM/AcknowExampleSPM/ViewController.swift b/Examples/AcknowExampleSPM/AcknowExampleSPM/ViewController.swift index c904673..cbc19e0 100644 --- a/Examples/AcknowExampleSPM/AcknowExampleSPM/ViewController.swift +++ b/Examples/AcknowExampleSPM/AcknowExampleSPM/ViewController.swift @@ -6,6 +6,7 @@ // import UIKit +import SwiftUI import AcknowList class ViewController: UIViewController { @@ -31,5 +32,16 @@ class ViewController: UIViewController { let navigationController = UINavigationController(rootViewController: vc) present(navigationController, animated: true, completion: nil) } + + @IBAction func presentAcknowledgementsSwiftUI(_ sender: AnyObject) { + let listViewController = AcknowListSwiftUIView(acknowledgements: [ + Acknow(title: "Test", text: "Bla bla"), + Acknow(title: "Test URL", repository: URL(string: "https://developer.apple.com")), + Acknow(title: "Test GitHub", repository: URL(string: "https://github.com/vtourraine/AcknowList.git")) + ]) + let viewController = UIHostingController(rootView: listViewController) + let navigationController = UINavigationController(rootViewController: viewController) + present(navigationController, animated: true, completion: nil) + } } From 4101afacbec4296e3864c27e3f55ff2e2e6392ba Mon Sep 17 00:00:00 2001 From: Vincent Tourraine Date: Tue, 9 Jan 2024 15:43:10 +0100 Subject: [PATCH 2/2] Update AcknowListSwiftUIView and AcknowSwiftUIView to fetch licenses --- CHANGELOG.md | 2 +- Sources/AcknowList/AcknowListSwiftUI.swift | 15 +++++++++- Sources/AcknowList/AcknowSwiftUI.swift | 32 ++++++++++++++++++---- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4b174..0137c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## 3.1 (work in progress) - Add `GitHubAPI` to get licenses from GitHub API -- Update `AcknowListViewController` to get missing licenses from GitHub API, with new `canFetchLicenseFromGitHub` property to disable this behavior +- Update `AcknowListViewController` and `AcknowListSwiftUIView` to get missing licenses from GitHub API, with new `canFetchLicenseFromGitHub` property to disable this behavior ## 3.0.1 (24 November 2022) diff --git a/Sources/AcknowList/AcknowListSwiftUI.swift b/Sources/AcknowList/AcknowListSwiftUI.swift index 09675f0..d056ee2 100644 --- a/Sources/AcknowList/AcknowListSwiftUI.swift +++ b/Sources/AcknowList/AcknowListSwiftUI.swift @@ -117,8 +117,11 @@ public struct AcknowListRowSwiftUIView: View { /// The represented `Acknow`. public var acknowledgement: Acknow + /// Indicates if the view controller should try to fetch missing licenses from the GitHub API. + public var canFetchLicenseFromGitHub = true + public var body: some View { - if acknowledgement.text != nil { + if acknowledgement.text != nil || canFetchLicenseFromGitHubAndIsGitHubRepository(acknowledgement) { NavigationLink(destination: AcknowSwiftUIView(acknowledgement: acknowledgement)) { Text(acknowledgement.title) } @@ -146,6 +149,16 @@ public struct AcknowListRowSwiftUIView: View { return scheme == "http" || scheme == "https" } + + private func canFetchLicenseFromGitHubAndIsGitHubRepository(_ acknowledgement: Acknow) -> Bool { + if canFetchLicenseFromGitHub, + let repository = acknowledgement.repository { + return GitHubAPI.isGitHubRepository(repository) + } + else { + return false + } + } } @available(iOS 13.0.0, macOS 10.15.0, watchOS 7.0.0, tvOS 13.0.0, *) diff --git a/Sources/AcknowList/AcknowSwiftUI.swift b/Sources/AcknowList/AcknowSwiftUI.swift index 32fa561..c625fe1 100644 --- a/Sources/AcknowList/AcknowSwiftUI.swift +++ b/Sources/AcknowList/AcknowSwiftUI.swift @@ -28,11 +28,7 @@ import SwiftUI public struct AcknowSwiftUIView: View { /// The represented acknowledgement. - public var acknowledgement: Acknow - - public init(acknowledgement: Acknow) { - self.acknowledgement = acknowledgement - } + @State public var acknowledgement: Acknow public var body: some View { #if os(macOS) @@ -44,6 +40,9 @@ public struct AcknowSwiftUIView: View { .font(.body) .padding() } + .onAppear { + fetchLicenseIfNecessary() + } #else ScrollView { Text(acknowledgement.text ?? "") @@ -51,8 +50,31 @@ public struct AcknowSwiftUIView: View { .padding() } .navigationBarTitle(acknowledgement.title) + .onAppear { + fetchLicenseIfNecessary() + } #endif } + + private func fetchLicenseIfNecessary() { + guard acknowledgement.text == nil, + let repository = acknowledgement.repository, + GitHubAPI.isGitHubRepository(repository) else { + return + } + + GitHubAPI.getLicense(for: repository) { result in + switch result { + case .success(let text): + acknowledgement = Acknow(title: acknowledgement.title, text: text, license: acknowledgement.license, repository: acknowledgement.repository) + + case .failure: +#if os(iOS) + UIApplication.shared.open(repository) +#endif + } + } + } } @available(iOS 13.0.0, macOS 10.15.0, watchOS 7.0.0, tvOS 13.0.0, *)