Merge pull request #5 from 0111b/feature/doc

Basic documentation
This commit is contained in:
Alexandr Goncharov 2020-10-19 21:46:49 +03:00 committed by GitHub
commit 0acfd29998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 1 deletions

View File

@ -1,3 +1,5 @@
# UIPreview
![Build](https://github.com/0111b/UIPreview/workflows/Build/badge.svg)
[API documentation](https://github.com/0111b/UIPreview/wiki)

View File

@ -3,6 +3,8 @@ import SwiftUI
#endif
//xcrun simctl list devicetypes
/// Default devices used in `View.previewAsScreen(deviceNames:)`
public let defaultDeviceNames: [String] =
[
"iPhone 8",

View File

@ -4,6 +4,7 @@ import SwiftUI
import UIKit
extension UICatalog {
/// Generated preview instance
@available(iOS 14, *)
public struct Preview {
init(_ view: AnyView, title: String) {
@ -18,6 +19,8 @@ extension UICatalog {
}.navigationBarTitle(title)
}
/// Convert preview to the `UIViewController`
/// - Returns: generated UIViewController instance
public func asViewController() -> UIViewController {
preview().asViewController()
}

View File

@ -4,7 +4,13 @@ import SwiftUI
import UIKit
extension UICatalog {
/// Configuration which will be used for preview generation
public struct PreviewConfiguration {
/// Creates new instance
/// - Parameters:
/// - themes: list of themes
/// - contentSize: list of content sizes
/// - size: required size
public init(themes: [Theme] = Theme.allCases,
contentSize: [UIContentSizeCategory] = [.unspecified],
size: CGSize? = nil) {
@ -17,11 +23,15 @@ extension UICatalog {
let contentSize: [UIContentSizeCategory]
let size: CGSize?
/// Adjusts the size of the generated preview
/// - Parameter newSize: required size
/// - Returns: updated configuration
public func with(size newSize: CGSize) -> PreviewConfiguration {
PreviewConfiguration(themes: themes, contentSize: contentSize, size: newSize)
}
}
/// Represents color scheme
public enum Theme: CaseIterable {
case light, dark
}

View File

@ -4,11 +4,15 @@ import SwiftUI
import UIKit
extension UICatalog {
/// Describes the preview that will be generated
@available(iOS 14, *)
public struct PreviewDescriptor: Identifiable, Hashable {
let builder: () -> AnyView
/// Unique identifier
public let id: String // swiftlint:disable:this identifier_name
/// Preview title
public let title: String
/// Returns generated preview
public var preview: Preview { Preview(builder(), title: title) }
public func hash(into hasher: inout Hasher) {
@ -24,6 +28,11 @@ extension UICatalog {
@available(iOS 14, *)
public extension UICatalog.PreviewDescriptor {
/// Creates the instance from suitable `UIView` subtype
/// - Parameters:
/// - content: reference to the type that can be converted to the preview
/// - configuration: preview configuration
/// - title: preview title
init<Content>(_ content: Content.Type,
configuration: UICatalog.PreviewConfiguration = .init(),
title: String? = nil) where Content: UIViewCatalogPresentable {
@ -32,11 +41,19 @@ public extension UICatalog.PreviewDescriptor {
builder = { AnyView(CatalogItem<Content>(configuration: configuration)) }
}
/// Group multiple previews together
/// - Parameters:
/// - content: list of the previews
/// - title: preview title
init(_ content: UICatalog.PreviewDescriptor...,
title: String? = nil) {
self.init(content, title: title)
}
/// Group multiple previews together
/// - Parameters:
/// - content: list of the previews
/// - title: preview title
init(_ content: [UICatalog.PreviewDescriptor],
title: String? = nil) {
id = content.map(\.id).joined()

View File

@ -1 +1,2 @@
/// Global namespace for the catalog
public enum UICatalog {}

View File

@ -3,15 +3,22 @@ import SwiftUI
#endif
import UIKit
/// Item that can be presented in the UICatalog
public protocol UICatalogPresentable {
associatedtype PreviewModel: Hashable
/// Applies data to the instance
/// - Parameter previewModel: data used in the preview
func apply(previewModel: PreviewModel)
/// List of different data configurations used in the preview
static var previewModels: [PreviewModel] { get }
/// Generates new instance for the preview
static func makePreviewInstance() -> Self
/// Applies preview data
/// - Parameter model: preview data
@available(iOS 13, *)
func preview(with model: PreviewModel) -> AnyView
}

View File

@ -3,10 +3,15 @@ import SwiftUI
#endif
import UIKit
/// Generic helper to display `UIViewController` in the Swift UI
@available(iOS 13, *)
public struct UIViewControllerWrapper<ContentView: UIViewController>: UIViewControllerRepresentable {
let contextBuilder: () -> ViewCoordinator<ContentView>
/// Make new instance with given parameters
/// - Parameters:
/// - builder: describes how to build content
/// - update: describes how to update content
public init(_ builder: @autoclosure @escaping () -> ContentView,
update: @escaping (ContentView) -> Void = { _ in }) {
contextBuilder = {

View File

@ -3,10 +3,15 @@ import SwiftUI
#endif
import UIKit
/// Generic helper to display `UIView` in the Swift UI
@available(iOS 13, *)
public struct UIViewWrapper<ContentView: UIView>: UIViewRepresentable {
let contextBuilder: () -> ViewCoordinator<ContentView>
/// Make new instance with given parameters
/// - Parameters:
/// - builder: describes how to build content
/// - update: describes how to update content
public init(_ builder: @autoclosure @escaping () -> ContentView,
update: @escaping (ContentView) -> Void = { _ in }) {
contextBuilder = {

View File

@ -1,3 +1,4 @@
/// Coordinates `UIView` and `UIViewController` updates
public final class ViewCoordinator<ContentView> {
typealias Builder = () -> ContentView
typealias Updater = (ContentView) -> Void