Skip to content

Commit

Permalink
unwrap optionals (#2698)
Browse files Browse the repository at this point in the history
* unwrap optionals

* unwrap delegate

* unwrap h

* remove underscore from local constant

* updates from code review

* more cleanup

* add guard
  • Loading branch information
russellbstephens authored and liuxuan30 committed Nov 15, 2017
1 parent a1811c7 commit fc22a27
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 210 deletions.
76 changes: 26 additions & 50 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

@objc 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,14 +103,7 @@ open class Animator: NSObject
elapsed = duration
}

if _easingX != nil
{
phaseX = _easingX!(elapsed, duration)
}
else
{
phaseX = Double(elapsed / duration)
}
phaseX = _easingX?(elapsed, duration) ?? Double(elapsed / duration)
}

if _enabledY
Expand All @@ -135,9 +116,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 +132,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
51 changes: 19 additions & 32 deletions Source/Charts/Charts/BarLineChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
open override func draw(_ rect: CGRect)
{
super.draw(rect)

if _data === nil
{
return
}

guard data != nil, let renderer = renderer else { return }

let optionalContext = NSUIGraphicsGetCurrentContext()
guard let context = optionalContext else { return }
Expand Down Expand Up @@ -223,17 +220,17 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
// make sure the data cannot be drawn outside the content-rect
context.saveGState()
context.clip(to: _viewPortHandler.contentRect)
renderer?.drawData(context: context)
renderer.drawData(context: context)

// if highlighting is enabled
if (valuesToHighlight())
{
renderer?.drawHighlighted(context: context, indices: _indicesToHighlight)
renderer.drawHighlighted(context: context, indices: _indicesToHighlight)
}

context.restoreGState()

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

if _xAxis.isEnabled && !_xAxis.isDrawLimitLinesBehindDataEnabled
{
Expand All @@ -259,13 +256,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 @@ -529,19 +526,19 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

if recognizer.state == NSUIGestureRecognizerState.ended
{
if !self.isHighLightPerTapEnabled { return }
if !isHighLightPerTapEnabled { return }

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

if h === nil || h!.isEqual(self.lastHighlighted)
if h?.isEqual(lastHighlighted) ?? true
{
self.highlightValue(nil, callDelegate: true)
self.lastHighlighted = nil
highlightValue(nil, callDelegate: true)
lastHighlighted = nil
}
else
{
self.highlightValue(h, callDelegate: true)
self.lastHighlighted = h
highlightValue(h, callDelegate: true)
lastHighlighted = h
}
}
}
Expand Down Expand Up @@ -751,9 +748,9 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD

let lastHighlighted = self.lastHighlighted

if ((h === nil && lastHighlighted !== nil) ||
if (h === nil && lastHighlighted !== nil) ||
(h !== nil && lastHighlighted === nil) ||
(h !== nil && lastHighlighted !== nil && !h!.isEqual(lastHighlighted)))
(lastHighlighted !== nil && !(h?.isEqual(lastHighlighted) ?? true))
{
self.lastHighlighted = h
self.highlightValue(h, callDelegate: true)
Expand Down Expand Up @@ -931,7 +928,7 @@ open class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChartD
))
{
var scrollView = self.superview
while (scrollView !== nil && !scrollView!.isKind(of: NSUIScrollView.self))
while !(scrollView?.isKind(of: NSUIScrollView.self) ?? true)
{
scrollView = scrollView?.superview
}
Expand All @@ -946,23 +943,13 @@ 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!
{
if scrollRecognizer.isKind(of: NSUIPanGestureRecognizer.self)
{
scrollViewPanGestureRecognizer = scrollRecognizer as! NSUIPanGestureRecognizer
break
}
}
let scrollViewPanGestureRecognizer = foundScrollView?.nsuiGestureRecognizers?.first {
$0 is NSUIPanGestureRecognizer
}

if otherGestureRecognizer === scrollViewPanGestureRecognizer
Expand Down
48 changes: 18 additions & 30 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,16 +394,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
descriptionText.count > 0
else { return }

var position = description.position

// if no position specified, draw on default position
if position == nil
{
let frame = self.bounds
position = CGPoint(
x: frame.width - _viewPortHandler.offsetRight - description.xOffset,
y: frame.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight)
}
let position = description.position ?? CGPoint(x: bounds.width - _viewPortHandler.offsetRight - description.xOffset,
y: bounds.height - _viewPortHandler.offsetBottom - description.yOffset - description.font.lineHeight)

var attrs = [NSAttributedStringKey : Any]()

Expand All @@ -413,7 +405,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 @@ -558,16 +550,16 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
}
}

if callDelegate && delegate != nil
if callDelegate, let delegate = delegate
{
if h == nil
if let h = h
{
delegate!.chartValueNothingSelected?(self)
// notify the listener
delegate.chartValueSelected?(self, entry: entry!, highlight: h)
}
else
{
// notify the listener
delegate!.chartValueSelected?(self, entry: entry!, highlight: h!)
delegate.chartValueNothingSelected?(self)
}
}

Expand Down Expand Up @@ -871,24 +863,20 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
/// - returns: `true` if the image was saved successfully
open func save(to path: String, format: ImageFormat, compressionQuality: Double) -> Bool
{
guard let image = getChartImage(transparent: format != .jpeg)
else { return false }
guard let image = getChartImage(transparent: format != .jpeg) else { return false }

var imageData: Data!
let imageData: Data?
switch (format)
{
case .png:
imageData = NSUIImagePNGRepresentation(image)
break

case .jpeg:
imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality))
break
case .png: imageData = NSUIImagePNGRepresentation(image)
case .jpeg: imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality))
}

guard let data = imageData else { return false }

do
{
try imageData.write(to: URL(fileURLWithPath: path), options: .atomic)
try data.write(to: URL(fileURLWithPath: path), options: .atomic)
}
catch
{
Expand Down
Loading

0 comments on commit fc22a27

Please sign in to comment.