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

feat: load default acknow list when init AcknowListSwiftUIView #126

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
031D428A2CDA828C00C803B8 /* Package.resolved in Resources */ = {isa = PBXBuildFile; fileRef = 031D42892CDA828C00C803B8 /* Package.resolved */; };
BB29000228230B37002FF5C4 /* Package-version-1.resolved in Resources */ = {isa = PBXBuildFile; fileRef = BB29000128230B37002FF5C4 /* Package-version-1.resolved */; };
D7527E2125CADE4E00378FB8 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7527E2025CADE4E00378FB8 /* AppDelegate.swift */; };
D7527E2325CADE4E00378FB8 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7527E2225CADE4E00378FB8 /* SceneDelegate.swift */; };
Expand All @@ -18,6 +19,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
031D42892CDA828C00C803B8 /* Package.resolved */ = {isa = PBXFileReference; lastKnownFileType = text; name = Package.resolved; path = /Users/tisfeng/Documents/Code/Github/AcknowList/Tests/AcknowListTests/Resources/Package.resolved; sourceTree = "<absolute>"; };
BB29000128230B37002FF5C4 /* Package-version-1.resolved */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = "Package-version-1.resolved"; path = "../../../Tests/AcknowListTests/Resources/Package-version-1.resolved"; sourceTree = "<group>"; };
D7527E1D25CADE4E00378FB8 /* AcknowExampleSPM.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AcknowExampleSPM.app; sourceTree = BUILT_PRODUCTS_DIR; };
D7527E2025CADE4E00378FB8 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -73,6 +75,7 @@
D7527E2B25CADE5000378FB8 /* LaunchScreen.storyboard */,
D7527E2E25CADE5000378FB8 /* Info.plist */,
BB29000128230B37002FF5C4 /* Package-version-1.resolved */,
031D42892CDA828C00C803B8 /* Package.resolved */,
);
path = AcknowExampleSPM;
sourceTree = "<group>";
Expand Down Expand Up @@ -146,6 +149,7 @@
buildActionMask = 2147483647;
files = (
D7527E2D25CADE5000378FB8 /* LaunchScreen.storyboard in Resources */,
031D428A2CDA828C00C803B8 /* Package.resolved in Resources */,
BB29000228230B37002FF5C4 /* Package-version-1.resolved in Resources */,
D7527E2A25CADE5000378FB8 /* Assets.xcassets in Resources */,
D7527E2825CADE4E00378FB8 /* Main.storyboard in Resources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ class ViewController: UIViewController {
}

@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"))
])
// Load default acknowledgements from CocoaPods and Swift Package Manager
let listViewController = AcknowListSwiftUIView()
let viewController = UIHostingController(rootView: listViewController)
let navigationController = UINavigationController(rootViewController: viewController)
present(navigationController, animated: true, completion: nil)
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ let package = Package(
.copy("Resources/RegexTesting-GroundTruth-TPKeyboardAvoiding.txt"),
.copy("Resources/RegexTesting-GroundTruth-pop.txt"),
.copy("Resources/Package-version-1.resolved"),
.copy("Resources/Package-version-2.resolved")]
.copy("Resources/Package-version-2.resolved"),
.copy("Resources/Package.resolved"),
]
),
],
swiftLanguageVersions: [.v5]
Expand Down
2 changes: 1 addition & 1 deletion Sources/AcknowList/AcknowList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct AcknowList {
/**
Footer text to be displayed below the list of the acknowledgements.
*/
public let footerText: String?
public var footerText: String?
}

extension AcknowList {
Expand Down
114 changes: 65 additions & 49 deletions Sources/AcknowList/AcknowListSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import SwiftUI

extension Acknow: Identifiable {
public var id: String {
get {
title
}
title
}
}

Expand All @@ -44,34 +42,42 @@ public struct AcknowListSwiftUIView: View {
/// Footer text to be displayed below the list of the acknowledgements.
public var footerText: String?

public init(acknowList: AcknowList) {
acknowledgements = acknowList.acknowledgements
headerText = acknowList.headerText
footerText = acknowList.footerText
}

public init(acknowledgements: [Acknow], headerText: String? = nil, footerText: String? = nil) {
public init(acknowledgements: [Acknow],
headerText: String? = nil,
footerText: String? = nil) {
self.acknowledgements = acknowledgements
self.headerText = headerText
self.footerText = footerText
}

public init(acknowList: AcknowList? = AcknowParser.defaultAcknowList()) {
self.init(
acknowledgements: acknowList?.acknowledgements ?? [],
headerText: acknowList?.headerText,
footerText: acknowList?.footerText)
}

public init(plistFileURL: URL) {
guard let data = try? Data(contentsOf: plistFileURL),
let acknowList = try? AcknowPodDecoder().decode(from: data) else {
self.init(acknowledgements: [], headerText: nil, footerText: nil)
let acknowList = try? AcknowPodDecoder().decode(from: data)
else {
self.init(acknowledgements: [],
headerText: nil,
footerText: nil)
return
}

let header: String?
if acknowList.headerText != AcknowPodDecoder.K.DefaultHeaderText {
header = acknowList.headerText
}
else {
} else {
header = nil
}

self.init(acknowledgements: acknowList.acknowledgements, headerText: header, footerText: acknowList.footerText)
self.init(
acknowledgements: acknowList.acknowledgements,
headerText: header,
footerText: acknowList.footerText)
}

struct HeaderFooter: View {
Expand All @@ -80,33 +86,36 @@ public struct AcknowListSwiftUIView: View {
var body: some View {
if let text = text {
Text(text)
}
else {
} else {
EmptyView()
}
}
}

public var body: some View {
#if os(iOS) || os(tvOS)
#if os(iOS) || os(tvOS)
List {
Section(header: HeaderFooter(text: headerText), footer: HeaderFooter(text: footerText)) {
ForEach (acknowledgements) { acknowledgement in
Section(
header: HeaderFooter(text: headerText), footer: HeaderFooter(text: footerText)
) {
ForEach(acknowledgements) { acknowledgement in
AcknowListRowSwiftUIView(acknowledgement: acknowledgement)
}
}
}
.listStyle(GroupedListStyle())
.navigationBarTitle(Text(AcknowLocalization.localizedTitle()))
#else
#else
List {
Section(header: HeaderFooter(text: headerText), footer: HeaderFooter(text: footerText)) {
ForEach (acknowledgements) { acknowledgement in
Section(
header: HeaderFooter(text: headerText), footer: HeaderFooter(text: footerText)
) {
ForEach(acknowledgements) { acknowledgement in
AcknowListRowSwiftUIView(acknowledgement: acknowledgement)
}
}
}
#endif
#endif
}
}

Expand All @@ -121,21 +130,22 @@ public struct AcknowListRowSwiftUIView: View {
public var canFetchLicenseFromGitHub = true

public var body: some View {
if acknowledgement.text != nil || canFetchLicenseFromGitHubAndIsGitHubRepository(acknowledgement) {
if acknowledgement.text != nil
|| canFetchLicenseFromGitHubAndIsGitHubRepository(acknowledgement)
{
NavigationLink(destination: AcknowSwiftUIView(acknowledgement: acknowledgement)) {
Text(acknowledgement.title)
}
}
else if let repository = acknowledgement.repository,
canOpenRepository(for: repository) {
} else if let repository = acknowledgement.repository,
canOpenRepository(for: repository)
{
Button(action: {
repository.openWithDefaultBrowser()
}) {
Text(acknowledgement.title)
.foregroundColor(.primary)
}
}
else {
} else {
Text(acknowledgement.title)
}
}
Expand All @@ -150,10 +160,10 @@ public struct AcknowListRowSwiftUIView: View {

private func canFetchLicenseFromGitHubAndIsGitHubRepository(_ acknowledgement: Acknow) -> Bool {
if canFetchLicenseFromGitHub,
let repository = acknowledgement.repository {
let repository = acknowledgement.repository
{
return GitHubAPI.isGitHubRepository(repository)
}
else {
} else {
return false
}
}
Expand All @@ -162,17 +172,19 @@ public struct AcknowListRowSwiftUIView: View {
@available(iOS 13.0.0, macOS 10.15.0, watchOS 7.0.0, tvOS 13.0.0, visionOS 1.0.0, *)
struct AcknowListSwiftUI_Previews: PreviewProvider {
static let license = """
Copyright (c) 2015-2024 Vincent Tourraine (https://www.vtourraine.net)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
static let acks = [Acknow(title: "Title 1", text: license),
Acknow(title: "Title 2", text: license),
Acknow(title: "Title 3", text: license)]
Copyright (c) 2015-2024 Vincent Tourraine (https://www.vtourraine.net)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
static let acks = [
Acknow(title: "Title 1", text: license),
Acknow(title: "Title 2", text: license),
Acknow(title: "Title 3", text: license),
]

static var previews: some View {
NavigationView {
Expand All @@ -181,22 +193,26 @@ struct AcknowListSwiftUI_Previews: PreviewProvider {
.previewDevice(PreviewDevice(rawValue: "iPhone 12"))

NavigationView {
AcknowListSwiftUIView(acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
AcknowListSwiftUIView(
acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
}
.previewDevice(PreviewDevice(rawValue: "iPhone 12"))

NavigationView {
AcknowListSwiftUIView(acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
AcknowListSwiftUIView(
acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
}
.previewDevice(PreviewDevice(rawValue: "Apple TV 4K"))

NavigationView {
AcknowListSwiftUIView(acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
AcknowListSwiftUIView(
acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
}
.previewDevice(PreviewDevice(rawValue: "Apple Watch Series 6 - 44mm"))

NavigationView {
AcknowListSwiftUIView(acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
AcknowListSwiftUIView(
acknowledgements: acks, headerText: "Test Header", footerText: "Test Footer")
}
.previewDevice(PreviewDevice(rawValue: "Mac"))
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/AcknowList/AcknowParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ open class AcknowParser {
let pods = defaultPods()
let packages = defaultPackages()
if let pods = pods, let packages = packages {
return pods + packages
var acknowList = pods + packages
// If have both pods and packages libraries, remove the footer text from the pods.
acknowList.footerText = nil
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removed pods footer text, because here have both sources.

image

return acknowList
}
else {
return pods ?? packages
Expand Down
61 changes: 61 additions & 0 deletions Tests/AcknowListTests/Resources/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"object": {
"pins": [
{
"package": "AcknowList",
"repositoryURL": "https://github.com/vtourraine/AcknowList.git",
"state": {
"branch": null,
"revision": "e66b9541c1902ced4979b3c214a5def2cf96f015",
"version": "2.1.0"
}
},
{
"package": "AMGAppButton",
"repositoryURL": "https://github.com/studioamanga/AMGAppButton.git",
"state": {
"branch": null,
"revision": "6044b277a3a4281afb1fdf6f651fa4829109d7d9",
"version": "1.1.0"
}
},
{
"package": "SVProgressHUD",
"repositoryURL": "https://github.com/epitonium/SVProgressHUD.git",
"state": {
"branch": null,
"revision": "3580cb55f351d83f861f52362aa38305f663f88d",
"version": "1.0.0"
}
},
{
"package": "TrackupCore",
"repositoryURL": "https://github.com/vtourraine/Trackup.git",
"state": {
"branch": null,
"revision": "513e0bf40b6d005354c844446e8e2334c450a863",
"version": "0.1.0"
}
},
{
"package": "TrackupVersionHistory",
"repositoryURL": "https://github.com/vtourraine/TrackupVersionHistory.git",
"state": {
"branch": null,
"revision": "7916b78a3cd5392b5807220277f3aa1d561c1658",
"version": "0.2.0"
}
},
{
"package": "VTAppButton",
"repositoryURL": "https://github.com/vtourraine/VTAppButton.git",
"state": {
"branch": null,
"revision": "3eeb59199d563d7e0ac35ebb0bf967defa878391",
"version": "1.1.0"
}
}
]
},
"version": 1
}