Skip to content

Commit

Permalink
226: Fix warning about updates from within a view
Browse files Browse the repository at this point in the history
  • Loading branch information
willdale committed May 6, 2023
1 parent a2fbf55 commit 9853c61
Show file tree
Hide file tree
Showing 18 changed files with 221 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class BarChartData: CTBarChartDataProtocol, GetDataProtocol, Publis
@Published public final var viewData: ChartViewData
@Published public final var infoView: InfoViewData<BarChartDataPoint> = InfoViewData()

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class GroupedBarChartData: CTMultiBarChartDataProtocol, GetDataProt
@Published public final var infoView: InfoViewData<GroupedBarDataPoint> = InfoViewData()
@Published public final var groups: [GroupingData]

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class HorizontalBarChartData: CTHorizontalBarChartDataProtocol, Get
@Published public final var viewData: ChartViewData
@Published public final var infoView: InfoViewData<BarChartDataPoint> = InfoViewData()

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class RangedBarChartData: CTRangedBarChartDataProtocol, GetDataProt
@Published public final var viewData: ChartViewData
@Published public final var infoView: InfoViewData<RangedBarDataPoint> = InfoViewData()

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class StackedBarChartData: CTMultiBarChartDataProtocol, GetDataProt
@Published public final var infoView: InfoViewData<StackedBarDataPoint> = InfoViewData()
@Published public final var groups: [GroupingData]

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class LineChartData: CTLineChartDataProtocol, GetDataProtocol, Publ
public final var noDataText: Text
public final var chartType: (chartType: ChartType, dataSetType: DataSetType)

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class MultiLineChartData: CTLineChartDataProtocol, GetDataProtocol,
public final var noDataText: Text
public final var chartType: (chartType: ChartType, dataSetType: DataSetType)

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class RangedLineChartData: CTLineChartDataProtocol, GetDataProtocol
public final var noDataText: Text
public final var chartType: (chartType: ChartType, dataSetType: DataSetType)

@Published public final var extraLineData: ExtraLineData!
@Published public final var extraLineData: ExtraLineData?

// Publishable
public var subscription = SubscriptionSet().subscription
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftUICharts/PieChart/Views/DoughnutChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct DoughnutChart<ChartData>: View where ChartData: DoughnutChartData
if chartData.disableAnimation {
return 1
} else {
return startAnimation ? 1 : 0
return startAnimation ? 1 : 0.001
}
}
}
4 changes: 3 additions & 1 deletion Sources/SwiftUICharts/Shared/Types/Stroke.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public struct Stroke: Hashable, Identifiable {
self.miterLimit = miterLimit
self.dash = dash
self.dashPhase = dashPhase
}
}

static let `default` = Stroke()
}

extension Stroke {
Expand Down
14 changes: 11 additions & 3 deletions Sources/SwiftUICharts/Shared/ViewModifiers/TouchOverlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal struct TouchOverlay<T>: ViewModifier where T: CTChartData {

@ObservedObject private var chartData: T
let minDistance: CGFloat
private let specifier: String
private let formatter: NumberFormatter?
private let unit: TouchUnit

internal init(
chartData: T,
Expand All @@ -25,9 +28,9 @@ internal struct TouchOverlay<T>: ViewModifier where T: CTChartData {
) {
self.chartData = chartData
self.minDistance = minDistance
self.chartData.infoView.touchSpecifier = specifier
self.chartData.infoView.touchFormatter = formatter
self.chartData.infoView.touchUnit = unit
self.specifier = specifier
self.formatter = formatter
self.unit = unit
}

internal func body(content: Content) -> some View {
Expand Down Expand Up @@ -55,6 +58,11 @@ internal struct TouchOverlay<T>: ViewModifier where T: CTChartData {
}
} else { content }
}
.onAppear {
self.chartData.infoView.touchSpecifier = specifier
self.chartData.infoView.touchFormatter = formatter
self.chartData.infoView.touchUnit = unit
}
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public protocol CTLineBarChartDataProtocol: CTChartData where CTStyle: CTLineBar
/**
A data model for the `ExtraLine` View Modifier
*/
var extraLineData: ExtraLineData! { get set }
var extraLineData: ExtraLineData? { get set }

/**
A type representing a View for displaying second set of labels on the Y axis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ extension CTLineBarChartDataProtocol {

internal var extraLabelsArray: [String] { self.generateExtraYLabels(self.viewData.yAxisSpecifier) }
private func generateExtraYLabels(_ specifier: String) -> [String] {

let dataRange: Double = self.extraLineData.range
let minValue: Double = self.extraLineData.minValue
let range: Double = dataRange / Double(self.extraLineData.style.yAxisNumberOfLabels-1)
guard let extraLineData = extraLineData else { return [] }
let dataRange: Double = extraLineData.range
let minValue: Double = extraLineData.minValue
let range: Double = dataRange / Double(extraLineData.style.yAxisNumberOfLabels-1)
let firstLabel = [String(format: specifier, minValue)]
let otherLabels = (1...self.extraLineData.style.yAxisNumberOfLabels-1).map { String(format: specifier, minValue + range * Double($0)) }
let otherLabels = (1...extraLineData.style.yAxisNumberOfLabels-1).map { String(format: specifier, minValue + range * Double($0)) }
let labels = firstLabel + otherLabels
return labels

Expand Down Expand Up @@ -363,7 +363,7 @@ extension CTLineBarChartDataProtocol {

public func getExtraYAxisTitle(colour: AxisColour) -> some View {
Group {
if let title = self.extraLineData.style.yAxisTitle {
if let title = self.extraLineData?.style.yAxisTitle {
VStack {
if self.chartStyle.xAxisLabelPosition == .top {
Spacer()
Expand Down Expand Up @@ -391,7 +391,7 @@ extension CTLineBarChartDataProtocol {
case .none:
EmptyView()
case .style(let size):
self.getAxisColourAsCircle(customColour: self.extraLineData.style.lineColour, width: size)
self.getAxisColourAsCircle(customColour: self.extraLineData?.style.lineColour ?? ColourStyle(), width: size)
case .custom(let colour, let size):
self.getAxisColourAsCircle(customColour: colour, width: size)
}
Expand Down
Loading

0 comments on commit 9853c61

Please sign in to comment.