Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SwiftUI interface to support fetching license from GitHub #116

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- Fix open URL on macOS with SwiftUI


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="20037" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22155" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="20020"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22131"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand All @@ -18,25 +18,34 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="t09-Si-fxa">
<rect key="frame" x="112" y="418" width="191" height="30"/>
<rect key="frame" x="111.5" y="390" width="191" height="30"/>
<state key="normal" title="Present Acknowledgements"/>
<connections>
<action selector="presentAcknowledgements:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QCw-84-8p7"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="7sT-Lf-szL">
<rect key="frame" x="146" y="468" width="123" height="30"/>
<rect key="frame" x="145.5" y="440" width="123" height="30"/>
<state key="normal" title="Present Packages"/>
<connections>
<action selector="presentPackageAcknowledgements:" destination="BYZ-38-t0r" eventType="touchUpInside" id="krd-Cm-GrO"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="PGn-A8-9NI">
<rect key="frame" x="79.5" y="490" width="255" height="30"/>
<state key="normal" title="Present Acknowledgements (SwiftUI)"/>
<connections>
<action selector="presentAcknowledgementsSwiftUI:" destination="BYZ-38-t0r" eventType="touchUpInside" id="s16-jf-2YC"/>
</connections>
</button>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="t09-Si-fxa" firstAttribute="centerY" secondItem="6Tk-OE-BBY" secondAttribute="centerY" constant="-20" id="Yfm-Jq-5xK"/>
<constraint firstItem="PGn-A8-9NI" firstAttribute="top" secondItem="7sT-Lf-szL" secondAttribute="bottom" constant="20" id="7vD-hZ-hRD"/>
<constraint firstItem="PGn-A8-9NI" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="YE6-u4-q40"/>
<constraint firstItem="7sT-Lf-szL" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="bKQ-a5-fxN"/>
<constraint firstItem="7sT-Lf-szL" firstAttribute="centerY" secondItem="6Tk-OE-BBY" secondAttribute="centerY" id="fFv-fa-v3s"/>
<constraint firstItem="t09-Si-fxa" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="tyo-SQ-5Bf"/>
<constraint firstItem="7sT-Lf-szL" firstAttribute="top" secondItem="t09-Si-fxa" secondAttribute="bottom" constant="20" id="uuE-eh-Bor"/>
</constraints>
Expand Down
12 changes: 12 additions & 0 deletions Examples/AcknowExampleSPM/AcknowExampleSPM/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
import SwiftUI
import AcknowList

class ViewController: UIViewController {
Expand All @@ -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)
}
}

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 @@ -148,6 +151,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