Skip to content

Commit

Permalink
Merge pull request airbnb#1118 from Rapsssito/refactor-array
Browse files Browse the repository at this point in the history
refactor: Keyframe<T> Contiguous Arrays
  • Loading branch information
buba447 authored Apr 23, 2020
2 parents 86d9126 + f580eff commit 05d9696
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lottie-swift/src/Private/Model/Keyframes/Keyframe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CoreGraphics
Keyframe represents a point in time and is the container for datatypes.
Note: This is a parent class and should not be used directly.
*/
struct Keyframe<T: Interpolatable> {
final class Keyframe<T: Interpolatable> {

/// The value of the keyframe
let value: T
Expand Down Expand Up @@ -63,13 +63,13 @@ struct Keyframe<T: Interpolatable> {
}

/**
A generic struct used to parse and remap keyframe json.
A generic class used to parse and remap keyframe json.
Keyframe json has a couple of different variations and formats depending on the
type of keyframea and also the version of the JSON. By parsing the raw data
we can reconfigure it into a constant format.
*/
struct KeyframeData<T: Codable>: Codable {
final class KeyframeData<T: Codable>: Codable {

/// The start value of the keyframe
let startValue: T?
Expand Down
6 changes: 3 additions & 3 deletions lottie-swift/src/Private/Model/Keyframes/KeyframeGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import Foundation

final class KeyframeGroup<T>: Codable where T: Codable, T: Interpolatable {

let keyframes: [Keyframe<T>]
let keyframes: ContiguousArray<Keyframe<T>>

private enum KeyframeWrapperKey: String, CodingKey {
case keyframeData = "k"
}

init(keyframes: [Keyframe<T>]) {
init(keyframes: ContiguousArray<Keyframe<T>>) {
self.keyframes = keyframes
}

Expand Down Expand Up @@ -54,7 +54,7 @@ final class KeyframeGroup<T>: Codable where T: Codable, T: Interpolatable {
*/

var keyframesContainer = try container.nestedUnkeyedContainer(forKey: .keyframeData)
var keyframes = [Keyframe<T>]()
var keyframes = ContiguousArray<Keyframe<T>>()
var previousKeyframeData: KeyframeData<T>?
while(!keyframesContainer.isAtEnd) {
// Ensure that Time and Value are present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,19 @@ final class GroupInterpolator<ValueType>: AnyValueProvider where ValueType: Inte
}

func hasUpdate(frame: CGFloat) -> Bool {
for interpolator in keyframeInterpolators {
if interpolator.hasUpdate(frame: frame) {
return true
}
}
return false
let updated = keyframeInterpolators.first(where: {$0.hasUpdate(frame: frame)})
return updated != nil
}

func value(frame: CGFloat) -> Any {
var output = [ValueType]()
for interpolator in keyframeInterpolators {
output.append(interpolator.value(frame: frame) as! ValueType)
}
let output = keyframeInterpolators.map({$0.value(frame: frame) as! ValueType})
return output
}

/// Initialize with an array of array of keyframes.
init(keyframeGroups: [[Keyframe<ValueType>]]) {
var interpolators = [KeyframeInterpolator<ValueType>]()
for keyframes in keyframeGroups {
interpolators.append(KeyframeInterpolator(keyframes: keyframes))
}
self.keyframeInterpolators = interpolators
init(keyframeGroups: ContiguousArray<ContiguousArray<Keyframe<ValueType>>>) {
self.keyframeInterpolators = ContiguousArray(keyframeGroups.map({KeyframeInterpolator(keyframes: $0)}))
}
let keyframeInterpolators: [KeyframeInterpolator<ValueType>]
let keyframeInterpolators: ContiguousArray<KeyframeInterpolator<ValueType>>

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import CoreGraphics
/// A value provider that produces a value at Time from a group of keyframes
final class KeyframeInterpolator<ValueType>: AnyValueProvider where ValueType: Interpolatable {

init(keyframes: [Keyframe<ValueType>]) {
init(keyframes: ContiguousArray<Keyframe<ValueType>>) {
self.keyframes = keyframes
}
let keyframes: [Keyframe<ValueType>]
let keyframes: ContiguousArray<Keyframe<ValueType>>

var valueType: Any.Type {
return ValueType.self
Expand Down Expand Up @@ -220,3 +220,14 @@ fileprivate extension Array {
}

}

fileprivate extension ContiguousArray {

func validIndex(_ index: Int) -> Int? {
if 0 <= index, index < endIndex {
return index
}
return nil
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ final class GradientStrokeProperties: NodePropertyMap, KeypathSearchable {
self.lineJoin = gradientStroke.lineJoin

if let dashes = gradientStroke.dashPattern {
var dashPatterns = [[Keyframe<Vector1D>]]()
var dashPhase = [Keyframe<Vector1D>]()
var dashPatterns = ContiguousArray<ContiguousArray<Keyframe<Vector1D>>>()
var dashPhase = ContiguousArray<Keyframe<Vector1D>>()
for dash in dashes {
if dash.type == .offset {
dashPhase = dash.value.keyframes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class StrokeNodeProperties: NodePropertyMap, KeypathSearchable {
self.lineJoin = stroke.lineJoin

if let dashes = stroke.dashPattern {
var dashPatterns = [[Keyframe<Vector1D>]]()
var dashPhase = [Keyframe<Vector1D>]()
var dashPatterns = ContiguousArray<ContiguousArray<Keyframe<Vector1D>>>()
var dashPhase = ContiguousArray<Keyframe<Vector1D>>()
for dash in dashes {
if dash.type == .offset {
dashPhase = dash.value.keyframes
Expand Down

0 comments on commit 05d9696

Please sign in to comment.