Add CardView and CardLabel

This commit is contained in:
Andras Samu
2020-05-30 18:00:18 +02:00
parent d64d0e9d7a
commit 7853476d86
8 changed files with 63 additions and 55 deletions
@@ -0,0 +1,23 @@
import SwiftUI
public struct CardView<Content: View>: View {
@Environment(\.chartStyle) private var chartStyle
let content: () -> Content
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
public var body: some View {
ZStack{
Rectangle()
.fill(self.chartStyle.backgroundColor.linearGradient(from: .bottom, to: .top))
.cornerRadius(20)
.shadow(color: Color.gray, radius: 8)
VStack {
self.content()
}
}
}
}
@@ -6,7 +6,6 @@ import SwiftUI
public struct ChartView: View {
@Environment(\.chartType) private var chartType
@Environment(\.chartStyle) private var chartStyle
@Environment(\.title) private var title
private var configuration: ChartTypeConfiguration
@@ -1,19 +0,0 @@
import SwiftUI
struct AnyChartLabel: ChartLabel {
private let labelMaker: (ChartLabel.Configuration) -> AnyView
init<S: ChartLabel>(_ label: S) {
self.labelMaker = label.makeTypeErasedBody
}
func makeLabel(configuration: ChartLabel.Configuration) -> AnyView {
self.labelMaker(configuration)
}
}
fileprivate extension ChartLabel {
func makeTypeErasedBody(configuration: ChartLabel.Configuration) -> AnyView {
AnyView(makeLabel(configuration: configuration))
}
}
@@ -1,11 +1,43 @@
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public protocol ChartLabel {
associatedtype Body: View
func makeLabel(configuration: Self.Configuration) -> Self.Body
typealias Configuration = ChartLabelConfiguration
public enum ChartLabelSize: CGFloat {
case small = 16.0
case normal = 24.0
case large = 32.0
}
public enum ChartLabelType {
case title
case legend
}
public struct ChartLabel: View {
private let text: String
private let labelSize: ChartLabelSize
private let labelType: ChartLabelType
private var labelColor: Color {
switch labelType {
case .title:
return .black
case .legend:
return .gray
}
}
public init (_ text: String,
type: ChartLabelType = .title,
size: ChartLabelSize = .normal) {
self.text = text
labelType = type
labelSize = size
}
public var body: some View {
Text(self.text)
.font(.system(size: labelSize.rawValue))
.bold()
.foregroundColor(self.labelColor)
.padding([.top, .bottom], 16.0)
}
}
@@ -1,7 +0,0 @@
import SwiftUI
public struct ChartLabelConfiguration {
public let font: Font
public let size: CGFloat
public let color: Color
}
@@ -1,7 +0,0 @@
import SwiftUI
public struct TitleLabel: ChartLabel {
public func makeLabel(configuration: Self.Configuration) -> some View {
return Text("AAA")
}
}
@@ -18,13 +18,4 @@ extension EnvironmentValues {
self[ChartStyleKey.self] = newValue
}
}
var title: AnyChartLabel {
get {
return self[ChartLabelKey.self]
}
set {
self[ChartLabelKey.self] = newValue
}
}
}
@@ -9,7 +9,3 @@ struct ChartStyleKey: EnvironmentKey {
foregroundColor: ColorGradient(ChartColors.orangeDark,
ChartColors.orangeBright))
}
struct ChartLabelKey: EnvironmentKey {
static let defaultValue: AnyChartLabel = AnyChartLabel(TitleLabel())
}