Skip to content

Commit

Permalink
Merge pull request #44 from cybozu/documentation
Browse files Browse the repository at this point in the history
Documenting the public API
  • Loading branch information
Kyome22 committed Jul 8, 2024
2 parents 9a1ffff + 4bfd128 commit 1d1bd16
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Sources/LicenseList/Library.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import Foundation

/// A structure that contains information about a library that the project depends on.
public struct Library: Identifiable, Hashable {
/// The unique identifier.
public let id: UUID = .init()
/// The library name.
public let name: String
/// The repository url.
public let url: URL?
/// The license body.
public let licenseBody: String

/// Creates a library with the specified name, repository url, and license body.
/// - Parameters:
/// - name: The library name.
/// - url: The repository url.
/// - licenseBody: The license body.
public init(name: String, url: String, licenseBody: String) {
self.name = name
self.url = URL(string: url)
Expand All @@ -14,6 +24,7 @@ public struct Library: Identifiable, Hashable {
}

extension Library {
/// The libraries automatically collected by the internal plug-in.
public static var libraries: [Library] {
return SPP.libraries.compactMap { info -> Library? in
guard let name = info["name"],
Expand Down
6 changes: 6 additions & 0 deletions Sources/LicenseList/LicenseListView.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import SwiftUI

/// A view that presents license views in a list format.
public struct LicenseListView: View {
@Environment(\.licenseViewStyle) private var licenseViewStyle: LicenseViewStyle

let libraries = Library.libraries
let navigationHandler: ((Library) -> Void)?

/// Creates new license list view.
/// - Parameters:
/// - navigationHandler: The closure to navigate for the given ``Library``.
/// This is used when controlling navigation using a UINavigationController.
public init(navigationHandler: ((Library) -> Void)? = nil) {
self.navigationHandler = navigationHandler
}

/// The content and behavior of the license list view.
public var body: some View {
List {
ForEach(libraries) { library in
Expand Down
11 changes: 11 additions & 0 deletions Sources/LicenseList/LicenseListViewController.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import UIKit
import SwiftUI

/// A view controller that specializes in managing a license list view.
///
/// A view controller implements the following behavior:
/// - Displays a list of libraries that your project depends on via Swift Package Manager.
/// - The list is sorted alphabetically by library name.
/// - Selecting each library will open a details page where you can view the license body.
public class LicenseListViewController: UIViewController {
/// The style that specifies behavior of license views.
public var licenseViewStyle: LicenseViewStyle = .plain

/// Creates a license list view controller.
public init() {
super.init(nibName: nil, bundle: nil)
}
Expand All @@ -12,6 +20,9 @@ public class LicenseListViewController: UIViewController {
fatalError("init(coder:) has not been implemented")
}

/// Called after the controller's view is loaded into memory.
///
/// The view controller embeds ``LicenseListView`` as a child view using UIHostingController.
public override func viewDidLoad() {
super.viewDidLoad()
let licenseListView = LicenseListView() { [weak self] library in
Expand Down
5 changes: 5 additions & 0 deletions Sources/LicenseList/LicenseView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftUI

/// A view that displays license body.
public struct LicenseView: View {
@State private var attributedLicenseBody = AttributedString(stringLiteral: "")

Expand All @@ -8,10 +9,14 @@ public struct LicenseView: View {

private let library: Library

/// Creates new license list view with the specified library.
/// - Parameters:
/// - library: The library to use in this view.
public init(library: Library) {
self.library = library
}

/// The content and behavior of the license view.
public var body: some View {
ScrollView {
Text(attributedLicenseBody)
Expand Down
3 changes: 3 additions & 0 deletions Sources/LicenseList/LicenseViewStyle.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import SwiftUI

/// A style for license views.
public enum LicenseViewStyle {
/// The style used to display just the license body.
case plain
/// The style used to display the license body and repository anchor link.
case withRepositoryAnchorLink
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/LicenseList/View+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension View {
}
}
}

func _licenseViewStyle(_ style: LicenseViewStyle, action: @escaping () -> Void) -> some View {
Group {
switch style {
Expand All @@ -23,7 +23,8 @@ extension View {
}
}
}


/// Sets the style for license views within this view.
public func licenseViewStyle(_ style: LicenseViewStyle) -> some View {
self.environment(\.licenseViewStyle, style)
}
Expand Down

0 comments on commit 1d1bd16

Please sign in to comment.