[WIP] Tune catalog item
This commit is contained in:
parent
afb7c3a5cc
commit
afaf902502
|
@ -6,7 +6,7 @@ import PackageDescription
|
|||
let package = Package(
|
||||
name: "UIPreview",
|
||||
platforms: [
|
||||
.iOS(.v13)
|
||||
.iOS(.v11)
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
#endif
|
||||
|
||||
@available(iOS 13, *)
|
||||
struct CatalogItem<Content: UIViewCatalogPresentable>: View {
|
||||
let configuration: UICatalog.PreviewConfiguration
|
||||
|
||||
var body: some View {
|
||||
ForEach(values: Content.previewModels) { model in
|
||||
ForEach(values: configuration.colorSchemes) { scheme in
|
||||
item(model: model, scheme: scheme)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func item(model: Content.PreviewModel,
|
||||
scheme: ColorScheme) -> some View {
|
||||
VStack(alignment: .center, spacing: 12.0) {
|
||||
Text("\(scheme.previewName): \(String(describing: model))")
|
||||
.font(.subheadline)
|
||||
.fontWeight(.bold)
|
||||
Content.preview(with: model)
|
||||
.frame(maxWidth: .infinity)
|
||||
// .previewLayout(.sizeThatFits)
|
||||
.background(Color(.systemBackground))
|
||||
.colorScheme(scheme)
|
||||
Divider()
|
||||
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
#if canImport(SwiftUI)
|
||||
import SwiftUI
|
||||
#endif
|
||||
import UIKit
|
||||
|
||||
@available(iOS 13, *)
|
||||
struct CatalogPage<Content: UIViewCatalogPresentable>: View {
|
||||
|
||||
let schemes: [ColorScheme]
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
ForEach(values: Content.previewModels) { model in
|
||||
ForEach(values: schemes) { scheme in
|
||||
item(title: "\(scheme): \(model)") {
|
||||
ColorSchemePreview(scheme: scheme) {
|
||||
Content.preview(with: model)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.navigationBarTitle(String(describing: Content.self))
|
||||
}
|
||||
}
|
||||
|
||||
func item<Content: View>(title: String,
|
||||
@ViewBuilder content: @escaping () -> Content
|
||||
) -> some View {
|
||||
VStack(alignment: .center, spacing: 12.0) {
|
||||
Text(title)
|
||||
.font(.subheadline)
|
||||
.fontWeight(.bold)
|
||||
content()
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .center)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
||||
//@available(iOS 13, *)
|
||||
//struct CatalogPage_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// CatalogPage<UISwitch>(schemes: [.light, .dark, ])
|
||||
// CatalogPage<UILabel>(schemes: [.light, .dark, ])
|
||||
// }
|
||||
//}
|
|
@ -26,7 +26,8 @@ struct ComponentPreview<Component: View>: View {
|
|||
ForEach(values: ColorScheme.allCases) { scheme in
|
||||
ForEach(values: ContentSizeCategory.smallestAndLargest) { category in
|
||||
self.component
|
||||
.previewLayout(.sizeThatFits).padding()
|
||||
.previewLayout(.sizeThatFits)
|
||||
.padding()
|
||||
.background(Color(.systemBackground))
|
||||
.colorScheme(scheme)
|
||||
.environment(\.sizeCategory, category)
|
||||
|
|
|
@ -10,11 +10,14 @@ extension UICatalog {
|
|||
self.view = view
|
||||
}
|
||||
|
||||
public func asViewController() -> UIViewController {
|
||||
func preview() -> some View {
|
||||
ScrollView(.vertical, showsIndicators: true) {
|
||||
view
|
||||
}.navigationBarTitle(title)
|
||||
.asViewController()
|
||||
}
|
||||
|
||||
public func asViewController() -> UIViewController {
|
||||
preview().asViewController()
|
||||
}
|
||||
|
||||
let title: String
|
||||
|
|
|
@ -26,3 +26,8 @@ extension UICatalog.Theme {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 13, *)
|
||||
extension UICatalog.PreviewConfiguration {
|
||||
var colorSchemes: [ColorScheme] { themes.map(\.scheme) }
|
||||
}
|
||||
|
|
|
@ -6,10 +6,10 @@ import UIKit
|
|||
extension UICatalog {
|
||||
|
||||
public struct PreviewDescriptor: Identifiable, Hashable {
|
||||
let view: AnyView
|
||||
let builder: () -> AnyView
|
||||
public let id: String
|
||||
public let title: String
|
||||
public var preview: Preview { Preview(view, title: title) }
|
||||
public var preview: Preview { Preview(builder(), title: title) }
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(id)
|
||||
|
@ -19,7 +19,7 @@ extension UICatalog {
|
|||
lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public extension UICatalog.PreviewDescriptor {
|
||||
|
@ -28,8 +28,6 @@ public extension UICatalog.PreviewDescriptor {
|
|||
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"))
|
||||
self.builder = { AnyView(CatalogItem<Content>(configuration: configuration)) }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue