From f9120d2f591c8443bf40831c4e831fdc79c44c91 Mon Sep 17 00:00:00 2001 From: jjatie Date: Sat, 20 Jan 2018 21:56:52 -0400 Subject: [PATCH] weak -> unowned (#3039) * weak -> unowned `ViewPortJob`s are owned by the Charts that make them. They are guaranteed to only exist while the chart exists. The `Transformer` and `ViewPortHandler` are supplied by the chart, so they will also only exist while the chart exists. Therefor none of them need to be `weak`, but can be `unowned` instead. It's a minor change in the code base (removing some guard statements), but it makes it much easier to discern how the framework is architected. * pulled latest master --- Source/Charts/Jobs/AnimatedMoveViewJob.swift | 6 ---- Source/Charts/Jobs/AnimatedViewPortJob.swift | 10 +++---- Source/Charts/Jobs/AnimatedZoomViewJob.swift | 30 ++++++++------------ Source/Charts/Jobs/MoveViewJob.swift | 8 +----- Source/Charts/Jobs/ViewPortJob.swift | 18 ++++++------ Source/Charts/Jobs/ZoomViewJob.swift | 17 ++++------- 6 files changed, 33 insertions(+), 56 deletions(-) diff --git a/Source/Charts/Jobs/AnimatedMoveViewJob.swift b/Source/Charts/Jobs/AnimatedMoveViewJob.swift index b6ea8c28e5..f91cfbcdff 100644 --- a/Source/Charts/Jobs/AnimatedMoveViewJob.swift +++ b/Source/Charts/Jobs/AnimatedMoveViewJob.swift @@ -20,12 +20,6 @@ open class AnimatedMoveViewJob: AnimatedViewPortJob { internal override func animationUpdate() { - guard - let viewPortHandler = viewPortHandler, - let transformer = transformer, - let view = view - else { return } - var pt = CGPoint( x: xOrigin + (CGFloat(xValue) - xOrigin) * phase, y: yOrigin + (CGFloat(yValue) - yOrigin) * phase diff --git a/Source/Charts/Jobs/AnimatedViewPortJob.swift b/Source/Charts/Jobs/AnimatedViewPortJob.swift index 985358e4b6..20b2440784 100644 --- a/Source/Charts/Jobs/AnimatedViewPortJob.swift +++ b/Source/Charts/Jobs/AnimatedViewPortJob.swift @@ -40,16 +40,16 @@ open class AnimatedViewPortJob: ViewPortJob duration: TimeInterval, easing: ChartEasingFunctionBlock?) { + self.xOrigin = xOrigin + self.yOrigin = yOrigin + self._duration = duration + self._easing = easing + super.init(viewPortHandler: viewPortHandler, xValue: xValue, yValue: yValue, transformer: transformer, view: view) - - self.xOrigin = xOrigin - self.yOrigin = yOrigin - self._duration = duration - self._easing = easing } deinit diff --git a/Source/Charts/Jobs/AnimatedZoomViewJob.swift b/Source/Charts/Jobs/AnimatedZoomViewJob.swift index 4e9d5fd981..4f23911283 100644 --- a/Source/Charts/Jobs/AnimatedZoomViewJob.swift +++ b/Source/Charts/Jobs/AnimatedZoomViewJob.swift @@ -14,7 +14,7 @@ import CoreGraphics open class AnimatedZoomViewJob: AnimatedViewPortJob { - internal var yAxis: YAxis? + internal var yAxis: YAxis internal var xAxisRange: Double = 0.0 internal var scaleX: CGFloat = 0.0 internal var scaleY: CGFloat = 0.0 @@ -40,6 +40,15 @@ open class AnimatedZoomViewJob: AnimatedViewPortJob duration: TimeInterval, easing: ChartEasingFunctionBlock?) { + self.yAxis = yAxis + self.xAxisRange = xAxisRange + self.scaleX = scaleX + self.scaleY = scaleY + self.zoomCenterX = zoomCenterX + self.zoomCenterY = zoomCenterY + self.zoomOriginX = zoomOriginX + self.zoomOriginY = zoomOriginY + super.init(viewPortHandler: viewPortHandler, xValue: 0.0, yValue: 0.0, @@ -49,32 +58,17 @@ open class AnimatedZoomViewJob: AnimatedViewPortJob yOrigin: yOrigin, duration: duration, easing: easing) - - self.yAxis = yAxis - self.xAxisRange = xAxisRange - self.scaleX = scaleX - self.scaleY = scaleY - self.zoomCenterX = zoomCenterX - self.zoomCenterY = zoomCenterY - self.zoomOriginX = zoomOriginX - self.zoomOriginY = zoomOriginY } internal override func animationUpdate() { - guard - let viewPortHandler = viewPortHandler, - let transformer = transformer, - let view = view - else { return } - let scaleX = xOrigin + (self.scaleX - xOrigin) * phase let scaleY = yOrigin + (self.scaleY - yOrigin) * phase var matrix = viewPortHandler.setZoom(scaleX: scaleX, scaleY: scaleY) viewPortHandler.refresh(newMatrix: matrix, chart: view, invalidate: false) - let valsInView = CGFloat(yAxis?.axisRange ?? 0.0) / viewPortHandler.scaleY + let valsInView = CGFloat(yAxis.axisRange) / viewPortHandler.scaleY let xsInView = CGFloat(xAxisRange) / viewPortHandler.scaleX var pt = CGPoint( @@ -91,6 +85,6 @@ open class AnimatedZoomViewJob: AnimatedViewPortJob internal override func animationEnd() { (view as? BarLineChartViewBase)?.calculateOffsets() - view?.setNeedsDisplay() + view.setNeedsDisplay() } } diff --git a/Source/Charts/Jobs/MoveViewJob.swift b/Source/Charts/Jobs/MoveViewJob.swift index acc8165d39..3d150ae1fd 100644 --- a/Source/Charts/Jobs/MoveViewJob.swift +++ b/Source/Charts/Jobs/MoveViewJob.swift @@ -18,15 +18,9 @@ import CoreGraphics @objc(MoveChartViewJob) open class MoveViewJob: ViewPortJob -{ +{ open override func doJob() { - guard - let viewPortHandler = viewPortHandler, - let transformer = transformer, - let view = view - else { return } - var pt = CGPoint( x: xValue, y: yValue diff --git a/Source/Charts/Jobs/ViewPortJob.swift b/Source/Charts/Jobs/ViewPortJob.swift index c52562ed53..c3adcca5c8 100644 --- a/Source/Charts/Jobs/ViewPortJob.swift +++ b/Source/Charts/Jobs/ViewPortJob.swift @@ -16,13 +16,13 @@ import CoreGraphics @objc(ChartViewPortJob) open class ViewPortJob: NSObject { - internal var point: CGPoint = CGPoint() - internal weak var viewPortHandler: ViewPortHandler? - internal var xValue: Double = 0.0 - internal var yValue: Double = 0.0 - internal weak var transformer: Transformer? - internal weak var view: ChartViewBase? - + internal var point: CGPoint = .zero + internal unowned var viewPortHandler: ViewPortHandler + internal var xValue = 0.0 + internal var yValue = 0.0 + internal unowned var transformer: Transformer + internal unowned var view: ChartViewBase + @objc public init( viewPortHandler: ViewPortHandler, xValue: Double, @@ -30,13 +30,13 @@ open class ViewPortJob: NSObject transformer: Transformer, view: ChartViewBase) { - super.init() - self.viewPortHandler = viewPortHandler self.xValue = xValue self.yValue = yValue self.transformer = transformer self.view = view + + super.init() } @objc open func doJob() diff --git a/Source/Charts/Jobs/ZoomViewJob.swift b/Source/Charts/Jobs/ZoomViewJob.swift index 7bd805c425..15225d1f20 100644 --- a/Source/Charts/Jobs/ZoomViewJob.swift +++ b/Source/Charts/Jobs/ZoomViewJob.swift @@ -22,7 +22,7 @@ open class ZoomViewJob: ViewPortJob internal var scaleX: CGFloat = 0.0 internal var scaleY: CGFloat = 0.0 internal var axisDependency: YAxis.AxisDependency = .left - + @objc public init( viewPortHandler: ViewPortHandler, scaleX: CGFloat, @@ -33,26 +33,21 @@ open class ZoomViewJob: ViewPortJob axis: YAxis.AxisDependency, view: ChartViewBase) { + self.scaleX = scaleX + self.scaleY = scaleY + self.axisDependency = axis + super.init( viewPortHandler: viewPortHandler, xValue: xValue, yValue: yValue, transformer: transformer, view: view) - - self.scaleX = scaleX - self.scaleY = scaleY - self.axisDependency = axis + } open override func doJob() { - guard - let viewPortHandler = viewPortHandler, - let transformer = transformer, - let view = view - else { return } - var matrix = viewPortHandler.setZoom(scaleX: scaleX, scaleY: scaleY) viewPortHandler.refresh(newMatrix: matrix, chart: view, invalidate: false)