Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove lots of force unwrapping #745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Dependencies/SWXMLHash/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,19 @@ class LazyXMLParser: NSObject, SimpleXmlParser, XMLParserDelegate {
}

func startParsing(_ ops: [IndexOp]) {
guard let data = data
else {
return
}

// clear any prior runs of parse... expected that this won't be necessary,
// but you never know
parentStack.removeAll()
root = XMLElement(name: rootElementName, caseInsensitive: options.caseInsensitive)
parentStack.push(root)

self.ops = ops
let parser = Foundation.XMLParser(data: data!)
let parser = Foundation.XMLParser(data: data)
parser.shouldProcessNamespaces = options.shouldProcessNamespaces
parser.delegate = self
_ = parser.parse()
Expand Down
4 changes: 2 additions & 2 deletions MacawTests/Animation/ControlStatesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class ControlStatesTests: XCTestCase {

func testCombineAnimation() {
let animation = [
testNode.placeVar.animation(to: .identity),
testNode.opacityVar.animation(to: 0.0)
testNode.placeVar.animation(to: .identity)!,
testNode.opacityVar.animation(to: 0.0)!
].combine() as! CombineAnimation

animation.play()
Expand Down
10 changes: 7 additions & 3 deletions MacawTests/MacawSVGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ class MacawSVGTests: XCTestCase {
let nodeContent = String(data: getJSONData(node: node), encoding: String.Encoding.utf8)
compareResults(nodeContent: nodeContent, referenceContent: referenceContent)

let nativeImage = getImage(from: referenceFile)

guard let nativeImage = getImage(from: referenceFile)
else {
XCTFail("Failed to create Image from file \(referenceFile)")
return
}

//To save new PNG image for test, uncomment this
//saveImage(image: nativeImage, fileName: referenceFile)
#if os(OSX)
Expand Down Expand Up @@ -826,7 +830,7 @@ class MacawSVGTests: XCTestCase {
validateJSON("masking-mask-02-f-manual")
}

func getImage(from svgName: String) -> MImage {
func getImage(from svgName: String) -> MImage? {
let bundle = Bundle(for: type(of: TestUtils()))
do {
let node = try SVGParser.parse(resource: svgName, fromBundle: bundle)
Expand Down
8 changes: 4 additions & 4 deletions MacawTests/SceneSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ extension SVGLength {
return
}
if string.hasSuffix("%") {
self = SVGLength.percent(Double(string.dropLast())!)
self = SVGLength.percent(Double(string.dropLast()) ?? 0.0)
} else {
self = SVGLength.pixels(Double(string)!)
self = SVGLength.pixels(Double(string) ?? 0.0)
}
}
}
Expand Down Expand Up @@ -479,9 +479,9 @@ extension Stroke: Serializable {
width: parse(dictionary["width"]),
cap: cap,
join: join,
miterLimit: miterLimit != nil ? miterLimit! : 4,
miterLimit: miterLimit ?? 4,
dashes: dashes,
offset: offset != nil ? offset! : 0)
offset: offset ?? 0)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/animation/Animation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Animation {
return self
}

public func reverse() -> Animation {
public func reverse() -> Animation? {
return self
}

Expand Down
2 changes: 1 addition & 1 deletion Source/animation/AnimationImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class BasicAnimation: Animation {
return .running
}

override open func reverse() -> Animation {
override open func reverse() -> Animation? {
return self
}

Expand Down
6 changes: 3 additions & 3 deletions Source/animation/AnimationProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ class AnimationProducer {
return
}

if animation.autoreverses {
animation.autoreverses = false
play([animation, animation.reverse()].sequence() as! BasicAnimation, context)
if contentsAnimation.autoreverses {
contentsAnimation.autoreverses = false
play([contentsAnimation, contentsAnimation.reverse()].sequence() as! BasicAnimation, context)
return
}

Expand Down
36 changes: 27 additions & 9 deletions Source/animation/types/ContentsAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,51 @@ internal class ContentsAnimation: AnimationImpl<[Node]> {

public extension AnimatableVariable where T: ContentsInterpolation {

func animation(_ f: @escaping (Double) -> [Node]) -> Animation {
let group = node! as! Group
func animation(_ f: @escaping (Double) -> [Node]) -> Animation? {
guard let group = node as? Group
else {
return nil
}
return ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: 1.0, delay: 0.0, autostart: false)
}

func animation(_ f: @escaping ((Double) -> [Node]), during: Double = 1.0, delay: Double = 0.0) -> Animation {
let group = node! as! Group
func animation(_ f: @escaping ((Double) -> [Node]), during: Double = 1.0, delay: Double = 0.0) -> Animation? {
guard let group = node as? Group
else {
return nil
}
return ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: during, delay: delay, autostart: false)
}

func animation(during: Double = 1.0, delay: Double = 0.0, _ f: @escaping ((Double) -> [Node])) -> Animation {
let group = node! as! Group
func animation(during: Double = 1.0, delay: Double = 0.0, _ f: @escaping ((Double) -> [Node])) -> Animation? {
guard let group = node as? Group
else {
return nil
}
return ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: during, delay: delay, autostart: false)
}

func animate(_ f: @escaping (Double) -> [Node]) {
let group = node! as! Group
guard let group = node as? Group
else {
return
}
_ = ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: 1.0, delay: 0.0, autostart: true)
}

func animate(_ f: @escaping ((Double) -> [Node]), during: Double = 1.0, delay: Double = 0.0) {
let group = node! as! Group
guard let group = node as? Group
else {
return
}
_ = ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: during, delay: delay, autostart: true)
}

func animate(during: Double = 1.0, delay: Double = 0.0, _ f: @escaping ((Double) -> [Node])) {
let group = node! as! Group
guard let group = node as? Group
else {
return
}
_ = ContentsAnimation(animatedGroup: group, valueFunc: f, animationDuration: during, delay: delay, autostart: true)
}
}
46 changes: 35 additions & 11 deletions Source/animation/types/OpacityAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ internal class OpacityAnimation: AnimationImpl<Double> {
}
}

open override func reverse() -> Animation {
open override func reverse() -> Animation? {
guard let node = node
else {
return nil
}
let factory = { () -> (Double) -> Double in
let original = self.timeFactory()
return { (t: Double) -> Double in
original(1.0 - t)
}
}

let reversedAnimation = OpacityAnimation(animatedNode: node!,
let reversedAnimation = OpacityAnimation(animatedNode: node,
factory: factory, animationDuration: duration, fps: logicalFps)
reversedAnimation.progress = progress
reversedAnimation.completion = completion
Expand All @@ -50,29 +54,49 @@ public typealias OpacityAnimationDescription = AnimationDescription<Double>

public extension AnimatableVariable where T: DoubleInterpolation {
func animate(_ desc: OpacityAnimationDescription) {
_ = OpacityAnimation(animatedNode: node!, valueFunc: desc.valueFunc, animationDuration: desc.duration, delay: desc.delay, autostart: true)
guard let node = node
else {
return
}
_ = OpacityAnimation(animatedNode: node, valueFunc: desc.valueFunc, animationDuration: desc.duration, delay: desc.delay, autostart: true)
}

func animation(_ desc: OpacityAnimationDescription) -> Animation {
return OpacityAnimation(animatedNode: node!, valueFunc: desc.valueFunc, animationDuration: desc.duration, delay: desc.delay, autostart: false)
func animation(_ desc: OpacityAnimationDescription) -> Animation? {
guard let node = node
else {
return .none
}
return OpacityAnimation(animatedNode: node, valueFunc: desc.valueFunc, animationDuration: desc.duration, delay: desc.delay, autostart: false)
}

func animate(from: Double? = nil, to: Double, during: Double = 1.0, delay: Double = 0.0) {
self.animate(((from ?? node!.opacity) >> to).t(during, delay: delay))
guard let node = node
else {
return
}
self.animate(((from ?? node.opacity) >> to).t(during, delay: delay))
}

func animation(from: Double? = nil, to: Double, during: Double = 1.0, delay: Double = 0.0) -> Animation {
func animation(from: Double? = nil, to: Double, during: Double = 1.0, delay: Double = 0.0) -> Animation? {
guard let node = node
else {
return .none
}
if let safeFrom = from {
return self.animation((safeFrom >> to).t(during, delay: delay))
}
let origin = node!.opacity
let origin = node.opacity
let factory = { () -> (Double) -> Double in { (t: Double) in origin.interpolate(to, progress: t) }
}
return OpacityAnimation(animatedNode: self.node!, factory: factory, animationDuration: during, delay: delay)
return OpacityAnimation(animatedNode: node, factory: factory, animationDuration: during, delay: delay)
}

func animation(_ f: @escaping ((Double) -> Double), during: Double = 1.0, delay: Double = 0.0) -> Animation {
return OpacityAnimation(animatedNode: node!, valueFunc: f, animationDuration: during, delay: delay)
func animation(_ f: @escaping ((Double) -> Double), during: Double = 1.0, delay: Double = 0.0) -> Animation? {
guard let node = node
else {
return .none
}
return OpacityAnimation(animatedNode: node, valueFunc: f, animationDuration: during, delay: delay)
}

}
3 changes: 1 addition & 2 deletions Source/animation/types/PathAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ open class StrokeSideVariable {
return self.animation((safeFrom >> to).t(during, delay: delay))
}
let origin = Double(0)
let factory = { () -> (Double) -> Double in
{ (t: Double) in origin.interpolate(to, progress: t) }
let factory = { () -> (Double) -> Double in { (t: Double) in origin.interpolate(to, progress: t) }
}
return PathAnimation(animatedNode: node as! Shape, isEnd: isEnd, factory: factory, animationDuration: during, delay: delay)
}
Expand Down
2 changes: 1 addition & 1 deletion Source/animation/types/ShapeAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public extension AnimatableVariable where T == Fill? {
finalShape.fill = to
_ = ShapeAnimation(animatedNode: shape, finalValue: finalShape, animationDuration: during, delay: delay, autostart: true)
}

func animation(from: Fill? = nil, to: Fill, during: Double = 1.0, delay: Double = 0.0) -> Animation {
let shape = node as! Shape
shape.fill = from ?? (shape.fill ?? Color.clear)
Expand Down
Loading