From c645c6220766b4e8be78393ea50f48b4e089fea4 Mon Sep 17 00:00:00 2001 From: Alexandr Goncharov Date: Sat, 17 Oct 2020 20:52:23 +0300 Subject: [PATCH] Add legend --- Sources/UIPreview/UICatalog/CatalogItem.swift | 23 +++-- Sources/UIPreview/UICatalog/GroupItem.swift | 4 +- Sources/UIPreview/UICatalog/Preview.swift | 3 +- .../UICatalog/PreviewConfiguration.swift | 11 ++- .../UICatalog/PreviewDescriptor.swift | 6 +- .../UIPreview/UICatalog/PreviewLegend.swift | 99 +++++++++++++++++++ 6 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 Sources/UIPreview/UICatalog/PreviewLegend.swift diff --git a/Sources/UIPreview/UICatalog/CatalogItem.swift b/Sources/UIPreview/UICatalog/CatalogItem.swift index 5c19795..e66d808 100644 --- a/Sources/UIPreview/UICatalog/CatalogItem.swift +++ b/Sources/UIPreview/UICatalog/CatalogItem.swift @@ -12,24 +12,32 @@ struct CatalogItem: View { var body: some View { ForEach(values: Content.previewModels) { model in ForEach(values: configuration.colorSchemes) { scheme in - item(model: model, scheme: scheme) + ForEach(values: configuration.contentSizeCategory) { category in + item(model: model, scheme: scheme, category: category) + } } } } func item(model: Content.PreviewModel, - scheme: ColorScheme) -> some View { + scheme: ColorScheme, + category: ContentSizeCategory) -> some View { VStack(alignment: .center, spacing: 0) { - Label(String(describing: model), systemImage: scheme.systemImageName) - .padding() + HStack { + Image(systemName: scheme.systemImageName) + Image(systemName: category.systemImageName) + Text(String(describing: model)) + } + .padding() Content.preview(with: model) .frame(maxWidth: .infinity) .padding() .background(Color(.systemBackground)) .colorScheme(scheme) + .environment(\.sizeCategory, category) } .background(Color(.systemGroupedBackground)) .cornerRadius(6) @@ -43,6 +51,7 @@ struct CatalogItem_Previews: PreviewProvider { static var previews: some View { NavigationView { ScrollView(.vertical) { + PreviewLegend() CatalogItem(configuration: .init()) }.navigationBarTitle("TestView") } @@ -56,12 +65,6 @@ private final class TestView: UILabel, UICatalogPresentable { "Hello2 world", "Hello3 world", "Hello4 world", - "Hello world", - "Hello world", - "Hello world", - "Hello world", - "Hello world", - "Hello world", ] func apply(previewModel: String) { diff --git a/Sources/UIPreview/UICatalog/GroupItem.swift b/Sources/UIPreview/UICatalog/GroupItem.swift index 9f0a59e..d8602a3 100644 --- a/Sources/UIPreview/UICatalog/GroupItem.swift +++ b/Sources/UIPreview/UICatalog/GroupItem.swift @@ -39,7 +39,8 @@ struct GroupItemRow: View { .frame(maxWidth: .infinity) .padding() .background(Color(.systemGroupedBackground)) - .border(Color.secondary) + .cornerRadius(6) + .padding() } if model.isExpanded { @@ -56,6 +57,7 @@ struct GroupItemRow: View { struct GroupItem_Previews: PreviewProvider { static var previews: some View { ScrollView { + PreviewLegend() GroupItem(items: [ .init(title: "Group 1", content: { AnyView(Text("Preview")) } ), diff --git a/Sources/UIPreview/UICatalog/Preview.swift b/Sources/UIPreview/UICatalog/Preview.swift index 01ee785..c073425 100644 --- a/Sources/UIPreview/UICatalog/Preview.swift +++ b/Sources/UIPreview/UICatalog/Preview.swift @@ -4,7 +4,7 @@ import SwiftUI import UIKit extension UICatalog { - @available(iOS 13, *) + @available(iOS 14, *) public struct Preview { init(_ view: AnyView, title: String) { self.title = title @@ -13,6 +13,7 @@ extension UICatalog { func preview() -> some View { ScrollView(.vertical, showsIndicators: true) { + PreviewLegend() view }.navigationBarTitle(title) } diff --git a/Sources/UIPreview/UICatalog/PreviewConfiguration.swift b/Sources/UIPreview/UICatalog/PreviewConfiguration.swift index 6c3ef87..2a5b702 100644 --- a/Sources/UIPreview/UICatalog/PreviewConfiguration.swift +++ b/Sources/UIPreview/UICatalog/PreviewConfiguration.swift @@ -5,11 +5,14 @@ import UIKit extension UICatalog { public struct PreviewConfiguration { - public init(themes: [Theme] = Theme.allCases) { + public init(themes: [Theme] = Theme.allCases, + contentSize: [UIContentSizeCategory] = [.unspecified]) { self.themes = themes + self.contentSize = contentSize } let themes: [Theme] + let contentSize: [UIContentSizeCategory] } public enum Theme: CaseIterable { @@ -27,7 +30,11 @@ extension UICatalog.Theme { } } -@available(iOS 13, *) +@available(iOS 14, *) extension UICatalog.PreviewConfiguration { var colorSchemes: [ColorScheme] { themes.map(\.scheme) } + var contentSizeCategory: [ContentSizeCategory] { + let categories = contentSize.compactMap { ContentSizeCategory($0) } + return categories.isEmpty ? [.medium] : categories + } } diff --git a/Sources/UIPreview/UICatalog/PreviewDescriptor.swift b/Sources/UIPreview/UICatalog/PreviewDescriptor.swift index ee12e60..57bda02 100644 --- a/Sources/UIPreview/UICatalog/PreviewDescriptor.swift +++ b/Sources/UIPreview/UICatalog/PreviewDescriptor.swift @@ -4,7 +4,7 @@ import SwiftUI import UIKit extension UICatalog { - @available(iOS 13, *) + @available(iOS 14, *) public struct PreviewDescriptor: Identifiable, Hashable { let builder: () -> AnyView public let id: String @@ -33,13 +33,11 @@ public extension UICatalog.PreviewDescriptor { } init(_ content: UICatalog.PreviewDescriptor..., - configuration: UICatalog.PreviewConfiguration = .init(), title: String? = nil) { - self.init(content, configuration: configuration, title: title) + self.init(content, title: title) } init(_ content: [UICatalog.PreviewDescriptor], - configuration: UICatalog.PreviewConfiguration = .init(), title: String? = nil) { id = content.map(\.id).joined() self.title = title ?? content.map(\.title).joined(separator: " ") diff --git a/Sources/UIPreview/UICatalog/PreviewLegend.swift b/Sources/UIPreview/UICatalog/PreviewLegend.swift new file mode 100644 index 0000000..737744d --- /dev/null +++ b/Sources/UIPreview/UICatalog/PreviewLegend.swift @@ -0,0 +1,99 @@ +#if canImport(SwiftUI) +import SwiftUI +#endif + +@available(iOS 14, *) +struct PreviewLegend: View { + @State var isExpanded: Bool = false + + var body: some View { + VStack(alignment: .leading) { + Button(action: { + isExpanded.toggle() + }, label: { + Label("Legend", + systemImage: isExpanded + ? "chevron.up" + : "chevron.down") + .foregroundColor(.primary) + }) + if isExpanded { + PreviewLegendBody() + } + } + .frame(maxWidth: .infinity) + .padding() + .background(Color(.systemGroupedBackground)) + .cornerRadius(6) + .padding() + } +} + +@available(iOS 14, *) +private struct PreviewLegendBody: View { + var body: some View { + VStack(alignment: .leading) { + Divider() + .foregroundColor(.accentColor) + Text("Theme: ") + .font(.headline) + .padding() + ForEach(values: UICatalog.Theme.allCases) { theme in + Label { + Text(theme.legend) + } icon: { + Image(systemName: theme.scheme.systemImageName) + } + } + Text("Content size: ") + .font(.headline) + .padding() + ForEach(values: ContentSizeCategory.allCases) { category in + Label { + Text(String(describing: category)) + } icon: { + Image(systemName: category.systemImageName) + } + } + } + } +} + +@available(iOS 14, *) +struct PreviewLegend_Previews: PreviewProvider { + static var previews: some View { + PreviewLegend() + .previewLayout(.sizeThatFits) + } +} + +private extension UICatalog.Theme { + var legend: String { + switch self { + case .light: return "Light theme" + case .dark: return "Dark theme" + } + } +} + +@available(iOS 13, *) +extension ContentSizeCategory { + var systemImageName: String { + switch self { + case .accessibilityExtraExtraExtraLarge: return "7.circle.fill" + case .accessibilityExtraExtraLarge: return "4.circle.fill" + case .extraSmall: return "1.circle" + case .small: return "2.circle" + case .medium: return "3.circle" + case .large: return "4.circle" + case .extraLarge: return "5.circle" + case .extraExtraLarge: return "6.circle" + case .extraExtraExtraLarge: return "7.circle" + case .accessibilityMedium: return "3.circle.fill" + case .accessibilityLarge: return "4.circle.fill" + case .accessibilityExtraLarge: return "5.circle.fill" + @unknown default: + return "questionmark" + } + } +}