Skip to content

Commit

Permalink
unwrap optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Stephens committed Aug 9, 2017
1 parent e72f280 commit 7d8ec67
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 133 deletions.
71 changes: 27 additions & 44 deletions Source/Charts/Animation/Animator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,39 +68,27 @@ open class Animator: NSObject

open func stop()
{
if _displayLink != nil
guard let _displayLink = _displayLink else {
return
}
_displayLink.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes)
self._displayLink = nil

_enabledX = false
_enabledY = false

// If we stopped an animation in the middle, we do not want to leave it like this
if phaseX != 1.0 || phaseY != 1.0
{
_displayLink?.remove(from: RunLoop.main, forMode: RunLoopMode.commonModes)
_displayLink = nil

_enabledX = false
_enabledY = false

// If we stopped an animation in the middle, we do not want to leave it like this
if phaseX != 1.0 || phaseY != 1.0
{
phaseX = 1.0
phaseY = 1.0

if delegate != nil
{
delegate!.animatorUpdated(self)
}
if updateBlock != nil
{
updateBlock!()
}
}

if delegate != nil
{
delegate!.animatorStopped(self)
}
if stopBlock != nil
{
stopBlock?()
}
phaseX = 1.0
phaseY = 1.0

delegate?.animatorUpdated(self)
updateBlock?()
}

delegate?.animatorStopped(self)
stopBlock?()
}

fileprivate func updateAnimationPhases(_ currentTime: TimeInterval)
Expand All @@ -115,9 +103,9 @@ open class Animator: NSObject
elapsed = duration
}

if _easingX != nil
if let _easingX = _easingX
{
phaseX = _easingX!(elapsed, duration)
phaseX = _easingX(elapsed, duration)
}
else
{
Expand All @@ -135,9 +123,9 @@ open class Animator: NSObject
elapsed = duration
}

if _easingY != nil
if let _easingY = _easingY
{
phaseY = _easingY!(elapsed, duration)
phaseY = _easingY(elapsed, duration)
}
else
{
Expand All @@ -151,15 +139,10 @@ open class Animator: NSObject
let currentTime: TimeInterval = CACurrentMediaTime()

updateAnimationPhases(currentTime)

if delegate != nil
{
delegate!.animatorUpdated(self)
}
if updateBlock != nil
{
updateBlock!()
}

delegate?.animatorUpdated(self)

updateBlock?()

if currentTime >= _endTime
{
Expand Down
25 changes: 10 additions & 15 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

context.restoreGState()

renderer!.drawExtras(context: context)
renderer?.drawExtras(context: context)

if _xAxis.isEnabled && !_xAxis.isDrawLimitLinesBehindDataEnabled
{
Expand All @@ -258,13 +258,13 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
context.saveGState()
context.clip(to: _viewPortHandler.contentRect)

renderer!.drawValues(context: context)
renderer?.drawValues(context: context)

context.restoreGState()
}
else
{
renderer!.drawValues(context: context)
renderer?.drawValues(context: context)
}

_legendRenderer.renderLegend(context: context)
Expand Down Expand Up @@ -532,7 +532,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

let h = getHighlightByTouchPoint(recognizer.location(in: self))

if h === nil || h!.isEqual(self.lastHighlighted)
if h === nil || h?.isEqual(self.lastHighlighted) ?? false
{
self.highlightValue(nil, callDelegate: true)
self.lastHighlighted = nil
Expand Down Expand Up @@ -735,7 +735,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

if ((h === nil && lastHighlighted !== nil) ||
(h !== nil && lastHighlighted === nil) ||
(h !== nil && lastHighlighted !== nil && !h!.isEqual(lastHighlighted)))
(h !== nil && lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? false)))
{
self.lastHighlighted = h
self.highlightValue(h, callDelegate: true)
Expand Down Expand Up @@ -912,7 +912,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
))
{
var scrollView = self.superview
while (scrollView !== nil && !scrollView!.isKind(of: NSUIScrollView.self))
while (scrollView !== nil && !(scrollView?.isKind(of: NSUIScrollView.self) ?? false))
{
scrollView = scrollView?.superview
}
Expand All @@ -927,22 +927,17 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

var foundScrollView = scrollView as? NSUIScrollView

if foundScrollView !== nil && !foundScrollView!.nsuiIsScrollEnabled
if !(foundScrollView?.nsuiIsScrollEnabled ?? true)
{
foundScrollView = nil
}

var scrollViewPanGestureRecognizer: NSUIGestureRecognizer!

if foundScrollView !== nil
{
for scrollRecognizer in foundScrollView!.nsuiGestureRecognizers!
foundScrollView?.nsuiGestureRecognizers?.forEach { scrollRecognizer in
if let panGestureRecognizer = scrollRecognizer as? NSUIPanGestureRecognizer
{
if scrollRecognizer.isKind(of: NSUIPanGestureRecognizer.self)
{
scrollViewPanGestureRecognizer = scrollRecognizer as! NSUIPanGestureRecognizer
break
}
scrollViewPanGestureRecognizer = panGestureRecognizer
}
}

Expand Down
25 changes: 12 additions & 13 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
_data = newValue
_offsetsCalculated = false

if _data == nil
guard let _data = _data else
{
return
}

// calculate how many digits are needed
setupDefaultFormatter(min: _data!.getYMin(), max: _data!.getYMax())
setupDefaultFormatter(min: _data.getYMin(), max: _data.getYMax())

for set in _data!.dataSets
for set in _data.dataSets
{
if set.needsFormatter || set.valueFormatter === _defaultValueFormatter
{
Expand Down Expand Up @@ -394,11 +394,10 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
descriptionText.characters.count > 0
else { return }

var position = description.position

// if no position specified, draw on default position
if position == nil
{
let position: CGPoint
if let descriptionPosition = description.position {
position = descriptionPosition
} else {
let frame = self.bounds
position = CGPoint(
x: frame.width - _viewPortHandler.offsetRight - description.xOffset,
Expand All @@ -413,7 +412,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
ChartUtils.drawText(
context: context,
text: descriptionText,
point: position!,
point: position,
align: description.textAlign,
attributes: attrs)
}
Expand Down Expand Up @@ -562,12 +561,12 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
{
if h == nil
{
delegate!.chartValueNothingSelected?(self)
delegate?.chartValueNothingSelected?(self)
}
else
{
// notify the listener
delegate!.chartValueSelected?(self, entry: entry!, highlight: h!)
delegate?.chartValueSelected?(self, entry: entry!, highlight: h!)
}
}

Expand Down Expand Up @@ -874,7 +873,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
guard let image = getChartImage(transparent: format != .jpeg)
else { return false }

var imageData: Data!
let imageData: Data?
switch (format)
{
case .png:
Expand All @@ -888,7 +887,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate

do
{
try imageData.write(to: URL(fileURLWithPath: path), options: .atomic)
try imageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
}
catch
{
Expand Down
12 changes: 6 additions & 6 deletions Source/Charts/Charts/CombinedChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider

self.highlighter = CombinedHighlighter(chart: self, barDataProvider: self)

(renderer as! CombinedChartRenderer?)!.createRenderers()
(renderer as? CombinedChartRenderer)?.createRenderers()
renderer?.initBuffers()
}
}
Expand Down Expand Up @@ -120,7 +120,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
{
return nil
}
return (_data as! CombinedChartData!).lineData
return (_data as? CombinedChartData)?.lineData
}
}

Expand All @@ -134,7 +134,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
{
return nil
}
return (_data as! CombinedChartData!).barData
return (_data as? CombinedChartData)?.barData
}
}

Expand All @@ -148,7 +148,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
{
return nil
}
return (_data as! CombinedChartData!).scatterData
return (_data as? CombinedChartData)?.scatterData
}
}

Expand All @@ -162,7 +162,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
{
return nil
}
return (_data as! CombinedChartData!).candleData
return (_data as? CombinedChartData)?.candleData
}
}

Expand All @@ -176,7 +176,7 @@ open class CombinedChartView: BarLineChartViewBase, CombinedChartDataProvider
{
return nil
}
return (_data as! CombinedChartData!).bubbleData
return (_data as? CombinedChartData)?.bubbleData
}
}

Expand Down
8 changes: 4 additions & 4 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ open class PieChartView: PieRadarChartViewBase
let optionalContext = NSUIGraphicsGetCurrentContext()
guard let context = optionalContext else { return }

renderer!.drawData(context: context)
renderer?.drawData(context: context)

if (valuesToHighlight())
{
renderer!.drawHighlighted(context: context, indices: _indicesToHighlight)
renderer?.drawHighlighted(context: context, indices: _indicesToHighlight)
}

renderer!.drawExtras(context: context)
renderer?.drawExtras(context: context)

renderer!.drawValues(context: context)
renderer?.drawValues(context: context)

_legendRenderer.renderLegend(context: context)

Expand Down
27 changes: 12 additions & 15 deletions Source/Charts/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,10 @@ open class PieRadarChartViewBase: ChartViewBase

if !rotationWithTwoFingers
{
let touch = touches.first as NSUITouch!

let touchLocation = touch?.location(in: self)

processRotationGestureBegan(location: touchLocation!)
if let touch = touches.first {
let touchLocation = touch.location(in: self)
processRotationGestureBegan(location: touchLocation)
}
}
}

Expand All @@ -573,11 +572,10 @@ open class PieRadarChartViewBase: ChartViewBase
{
if rotationEnabled && !rotationWithTwoFingers
{
let touch = touches.first as NSUITouch!

let touchLocation = touch?.location(in: self)

processRotationGestureMoved(location: touchLocation!)
if let touch = touches.first {
let touchLocation = touch.location(in: self)
processRotationGestureMoved(location: touchLocation)
}
}

if !_isRotating
Expand All @@ -595,11 +593,10 @@ open class PieRadarChartViewBase: ChartViewBase

if rotationEnabled && !rotationWithTwoFingers
{
let touch = touches.first as NSUITouch!

let touchLocation = touch?.location(in: self)

processRotationGestureEnded(location: touchLocation!)
if let touch = touches.first {
let touchLocation = touch.location(in: self)
processRotationGestureEnded(location: touchLocation)
}
}

if _isRotating
Expand Down
Loading

0 comments on commit 7d8ec67

Please sign in to comment.