Skip to content

Commit

Permalink
Merge pull request #333 from Czajnikowski/preferences-API-improvement
Browse files Browse the repository at this point in the history
Dry up the dance around preferences a bit
  • Loading branch information
Dimillian authored Oct 30, 2020
2 parents 4706015 + f2cf398 commit 83db1f1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
39 changes: 10 additions & 29 deletions ACHNBrowserUI/ACHNBrowserUI/extensions/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,42 +70,23 @@ extension View {
}

extension View {
func propagateHeight<K: PreferenceKey>(_ key: K.Type) -> some View where K.Value == CGFloat? {
func propagate<K>(
_ transform: @escaping (CGRect) -> K.Value,
of source: Anchor<CGRect>.Source = .bounds,
using key: K.Type
) -> some View where K: PreferenceKey {
overlay(
GeometryReader { proxy in
Color.clear
.anchorPreference(key: key, value: .bounds, transform: { proxy[$0].height })
}
)
}

func onHeightPreferenceChange<K: PreferenceKey>(_ key: K.Type = K.self, storeValueIn storage: Binding<CGFloat?>) -> some View where K.Value == CGFloat? {
onPreferenceChange(key, perform: { storage.wrappedValue = $0 })
}

func propagateWidth<K: PreferenceKey>(_ key: K.Type) -> some View where K.Value == CGFloat? {
overlay(
GeometryReader { proxy in
Color.clear
.anchorPreference(key: key, value: .bounds, transform: { proxy[$0].width })
}
)
}

func onWidthPreferenceChange<K: PreferenceKey>(_ key: K.Type = K.self, storeValueIn storage: Binding<CGFloat?>) -> some View where K.Value == CGFloat? {
onPreferenceChange(key, perform: { storage.wrappedValue = $0 })
}

func propagateSize<K: PreferenceKey>(_ key: K.Type = K.self, storeValueIn storage: Binding<CGSize?>) -> some View where K.Value == CGSize? {
overlay(
GeometryReader { proxy in
Color.clear
.anchorPreference(key: key, value: .bounds, transform: { proxy[$0].size })
.anchorPreference(key: key, value: source) { transform(proxy[$0]) }
}
)
}

func onSizePreferenceChange<K: PreferenceKey>(_ key: K.Type = K.self, storeValueIn storage: Binding<CGSize?>) -> some View where K.Value == CGSize? {
func storeValue<K: PreferenceKey>(
from key: K.Type = K.self,
in storage: Binding<K.Value>
) -> some View where K.Value: Equatable {
onPreferenceChange(key, perform: { storage.wrappedValue = $0 })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct TurnipsChartBottomLegendView: View {

var body: some View {
texts(xValues: data.map { $0.minBuyPrice.position.x })
.propagateHeight(HeightPreferenceKey.self)
.propagate({ $0.height }, using: HeightPreferenceKey.self)
}

func texts(xValues: [CGFloat]) -> some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct TurnipsChartVerticalLegend: View {
let steps = CGFloat(TurnipsChart.steps)
let values = Array(stride(from: minY, to: maxY + CGFloat(TurnipsChart.extraMaxSteps), by: steps))
return texts(values: values, ratioY: ratioY)
.propagateWidth(WidthPreferenceKey.self)
.propagate({ $0.width }, using: WidthPreferenceKey.self)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ struct TurnipsChartView: View {
}
}
}
.onSizePreferenceChange(ChartSizePreferenceKey.self, storeValueIn: $chartSize)
.onHeightPreferenceChange(TurnipsChartBottomLegendView.HeightPreferenceKey.self, storeValueIn: $bottomLegendHeight)
.onWidthPreferenceChange(TurnipsChartVerticalLegend.WidthPreferenceKey.self, storeValueIn: $verticalLegendWidth)
.storeValue(from: ChartSizePreferenceKey.self, in: $chartSize)
.storeValue(from: TurnipsChartBottomLegendView.HeightPreferenceKey.self, in: $bottomLegendHeight)
.storeValue(from: TurnipsChartVerticalLegend.WidthPreferenceKey.self, in: $verticalLegendWidth)
}

private var chart: some View {
Expand All @@ -52,7 +52,7 @@ struct TurnipsChartView: View {
TurnipsChartGrid(data: yAxisData)
.stroke()
.opacity(0.5)
.propagateSize(ChartSizePreferenceKey.self, storeValueIn: $chartSize)
.propagate({ $0.size }, using: ChartSizePreferenceKey.self)
TurnipsChartMinBuyPriceCurve(data: yAxisData)
.stroke(style: StrokeStyle(dash: [Self.verticalLinesCount]))
.foregroundColor(.graphMinimum)
Expand Down

0 comments on commit 83db1f1

Please sign in to comment.