Skip to content

Commit

Permalink
weak -> unowned (#3039)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jjatie authored and liuxuan30 committed Jan 21, 2018
1 parent 4285bbc commit f9120d2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 56 deletions.
6 changes: 0 additions & 6 deletions Source/Charts/Jobs/AnimatedMoveViewJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Source/Charts/Jobs/AnimatedViewPortJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 12 additions & 18 deletions Source/Charts/Jobs/AnimatedZoomViewJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -91,6 +85,6 @@ open class AnimatedZoomViewJob: AnimatedViewPortJob
internal override func animationEnd()
{
(view as? BarLineChartViewBase)?.calculateOffsets()
view?.setNeedsDisplay()
view.setNeedsDisplay()
}
}
8 changes: 1 addition & 7 deletions Source/Charts/Jobs/MoveViewJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions Source/Charts/Jobs/ViewPortJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ 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,
yValue: Double,
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()
Expand Down
17 changes: 6 additions & 11 deletions Source/Charts/Jobs/ZoomViewJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down

0 comments on commit f9120d2

Please sign in to comment.