-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat/#104] DSKit에서 Lottie 사용을 위한 스텐실파일 추가
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// swiftformat:disable all | ||
// swiftlint:disable all | ||
{% if files %} | ||
{% set accessModifier %}{% if param.publicAccess %}public{% else %}internal{% endif %}{% endset %} | ||
{% set documentPrefix %}{{param.documentName|default:"Document"}}{% endset %} | ||
import Foundation | ||
#if canImport(Lottie) | ||
import Lottie | ||
// MARK: - Animations Assets | ||
{{accessModifier}} extension AnimationAsset { | ||
{% for file in files %} | ||
static let {{file.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = Self(named: "{{file.name}}") | ||
{% endfor %} | ||
} | ||
// MARK: - Animation Helpers | ||
{{accessModifier}} extension AnimationAsset { | ||
/// All the available animation. Can be used to preload them | ||
static let allAnimations: [Self] = [ | ||
{% for file in files %} | ||
Self.{{file.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}, | ||
{% endfor %} | ||
] | ||
} | ||
// MARK: - Structures | ||
{{accessModifier}} struct AnimationAsset: Hashable { | ||
{{accessModifier}} fileprivate(set) var name: String | ||
{{accessModifier}} let animation: LottieAnimation? | ||
{{accessModifier}} init(named name: String) { | ||
self.name = name | ||
if let url = Bundle.module.url(forResource: name, withExtension: "json") { | ||
self.animation = LottieAnimation.filepath(url.path) | ||
} else { | ||
self.animation = nil | ||
} | ||
} | ||
// MARK: Hashable Conformance | ||
{{accessModifier}} static func == (lhs: Self, rhs: Self) -> Bool { | ||
return lhs.name == rhs.name | ||
} | ||
{{accessModifier}} func hash(into hasher: inout Hasher) { | ||
hasher.combine(self.name) | ||
} | ||
} | ||
// MARK: - Preload Helpers | ||
{{accessModifier}} extension AnimationAsset { | ||
/// Preloads all the Lottie Animations to avoid performance issues when loading them | ||
static func preload() -> Void { | ||
for animationAsset in Self.allAnimations { | ||
_ = animationAsset.animation | ||
} | ||
} | ||
} | ||
// swiftformat:enable all | ||
// swiftlint:enable all | ||
#endif | ||
{% else %} | ||
// No files found | ||
{% endif %} |