[WIP] Preview descriptor

This commit is contained in:
Alexandr Goncharov 2020-10-16 14:03:54 +03:00
parent 89f658ea48
commit afb7c3a5cc
9 changed files with 88 additions and 31 deletions

View File

@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "UIPreview",
platforms: [
.iOS(.v11)
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.

View File

@ -1,7 +1,6 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
@available(iOS 13, *)
extension Binding {

View File

@ -0,0 +1,23 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
extension UICatalog {
public struct Preview {
init(_ view: AnyView, title: String) {
self.title = title
self.view = view
}
public func asViewController() -> UIViewController {
ScrollView(.vertical, showsIndicators: true) {
view
}.navigationBarTitle(title)
.asViewController()
}
let title: String
let view: AnyView
}
}

View File

@ -0,0 +1,28 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
extension UICatalog {
public struct PreviewConfiguration {
public init(themes: [Theme] = Theme.allCases) {
self.themes = themes
}
let themes: [Theme]
}
public enum Theme: CaseIterable {
case light, dark
}
}
@available(iOS 13, *)
extension UICatalog.Theme {
var scheme: ColorScheme {
switch self {
case .light: return .light
case .dark: return .dark
}
}
}

View File

@ -0,0 +1,35 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
extension UICatalog {
public struct PreviewDescriptor: Identifiable, Hashable {
let view: AnyView
public let id: String
public let title: String
public var preview: Preview { Preview(view, title: title) }
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
public static func == (lhs: PreviewDescriptor, rhs: PreviewDescriptor) -> Bool {
lhs.id == rhs.id
}
}
}
public extension UICatalog.PreviewDescriptor {
init<Content>(_ content: Content.Type,
configuration: UICatalog.PreviewConfiguration = .init(),
title: String? = nil) where Content: UIViewCatalogPresentable {
id = "\(content)"
self.title = title ?? "\(content)"
// self.view = CatalogPage<Content>(schemes: themes.map(\.scheme))
self.view = AnyView(Text("sdf"))
}
}

View File

@ -1,29 +1 @@
#if canImport(SwiftUI)
import SwiftUI
#endif
import UIKit
public enum UICatalog {
public enum Theme {
case light, dark
}
public static func makePreviewPage<Content>(_ content: Content.Type,
themes: [Theme] = [.light, .dark]
) -> UIViewController where Content: UIViewCatalogPresentable {
guard #available(iOS 13, *) else {
return UIViewController()
}
return CatalogPage<Content>(schemes: themes.map(\.scheme)).asViewController()
}
}
@available(iOS 13, *)
extension UICatalog.Theme {
var scheme: ColorScheme {
switch self {
case .light: return .light
case .dark: return .dark
}
}
}
public enum UICatalog {}