Configurable preview size (#2)

* Configurable preview size

* Fix configurable preview size
This commit is contained in:
Alexandr Goncharov 2020-10-19 19:17:44 +03:00 committed by GitHub
parent def4a7aa48
commit f9a74ce07d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View File

@ -13,7 +13,10 @@ struct CatalogItem<Content: UIViewCatalogPresentable>: View {
ForEach(values: Content.previewModels) { model in
ForEach(values: configuration.colorSchemes) { scheme in
ForEach(values: configuration.contentSizeCategory) { category in
item(model: model, scheme: scheme, category: category)
item(model: model,
scheme: scheme,
category: category,
size: configuration.size)
}
}
}
@ -21,18 +24,21 @@ struct CatalogItem<Content: UIViewCatalogPresentable>: View {
func item(model: Content.PreviewModel,
scheme: ColorScheme,
category: ContentSizeCategory) -> some View {
category: ContentSizeCategory,
size: CGSize?) -> some View {
VStack(alignment: .center, spacing: 0) {
HStack {
Image(systemName: scheme.systemImageName)
Image(systemName: category.systemImageName)
Text(String(describing: model))
.frame(maxWidth: 300)
.lineLimit(4)
.frame(maxWidth: 300, alignment: .leading)
}
.padding()
Content.preview(with: model)
.modifier(SizeModifier(size: size))
.frame(maxWidth: .infinity)
.padding()
.background(Color(.systemBackground))
@ -45,6 +51,19 @@ struct CatalogItem<Content: UIViewCatalogPresentable>: View {
}
}
@available(iOS 13, *)
private struct SizeModifier: ViewModifier {
let size: CGSize?
func body(content: Content) -> some View {
guard let size = size else {
return AnyView(content)
}
return AnyView(content.frame(width: size.width,
height: size.height))
}
}
#if DEBUG
@available(iOS 14, *)
struct CatalogItem_Previews: PreviewProvider {

View File

@ -6,13 +6,20 @@ import UIKit
extension UICatalog {
public struct PreviewConfiguration {
public init(themes: [Theme] = Theme.allCases,
contentSize: [UIContentSizeCategory] = [.unspecified]) {
contentSize: [UIContentSizeCategory] = [.unspecified],
size: CGSize? = nil) {
self.themes = themes
self.contentSize = contentSize
self.size = size
}
let themes: [Theme]
let contentSize: [UIContentSizeCategory]
let size: CGSize?
public func with(size newSize: CGSize) -> PreviewConfiguration {
PreviewConfiguration(themes: themes, contentSize: contentSize, size: newSize)
}
}
public enum Theme: CaseIterable {