Skip to content

Commit

Permalink
Update AcknowListSwiftUIView and AcknowSwiftUIView to fetch licenses
Browse files Browse the repository at this point in the history
  • Loading branch information
vtourraine committed Jan 9, 2024
1 parent 40a0366 commit 4101afa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion Sources/AcknowList/AcknowListSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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, *)
Expand Down
32 changes: 27 additions & 5 deletions Sources/AcknowList/AcknowSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -44,15 +40,41 @@ public struct AcknowSwiftUIView: View {
.font(.body)
.padding()
}
.onAppear {
fetchLicenseIfNecessary()
}
#else
ScrollView {
Text(acknowledgement.text ?? "")
.font(.body)
.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, *)
Expand Down

0 comments on commit 4101afa

Please sign in to comment.