@momo-kits/native-kits 0.151.2-test.3 → 0.151.2-test.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,7 @@
1
1
 
2
2
  import Foundation
3
3
  import SwiftUI
4
+ import Lottie
4
5
 
5
6
  // MARK: - ButtonType
6
7
 
@@ -27,13 +28,14 @@ public enum ButtonSize {
27
28
  public struct Button: View {
28
29
  // MARK: Lifecycle
29
30
 
30
- public init(title: String = "", action: @escaping () -> Void, type: ButtonType = .primary, size: ButtonSize = .large, iconLeft: AnyView? = nil, iconRight: AnyView? = nil) {
31
+ public init(title: String = "", action: @escaping () -> Void, type: ButtonType = .primary, size: ButtonSize = .large, iconLeft: AnyView? = nil, iconRight: AnyView? = nil, loading: Bool = false) {
31
32
  self.title = title
32
33
  self.action = action
33
34
  self.type = type
34
35
  self.size = size
35
36
  self.iconLeft = iconLeft
36
37
  self.iconRight = iconRight
38
+ self.loading = loading
37
39
  }
38
40
 
39
41
  // MARK: Public
@@ -41,13 +43,26 @@ public struct Button: View {
41
43
  public var body: some View {
42
44
  SwiftUI.Button(action: action) {
43
45
  HStack {
44
- iconLeft
46
+ if (loading) {
47
+ LottieView(
48
+ name: "lottie_circle_loader",
49
+ loopMode: .loop,
50
+ contentMode: .center
51
+ )
52
+ .frame(width: 32, height: 32)
53
+ .foregroundColor(.white)
54
+ .colorMultiply(.white)
55
+ } else {
56
+ iconLeft
57
+ }
45
58
  renderTitle(title: title, type: type, size: size).lineLimit(1).truncationMode(.tail)
46
59
  iconRight
47
60
  }
48
61
  .buttonSize(size)
49
62
  .buttonType(type)
50
- }.disabled(type == .disabled)
63
+ .opacity(loading ? 0.75 : 1.0)
64
+ }
65
+ .disabled(type == .disabled && !loading)
51
66
  .clipShape(RoundedRectangle(cornerRadius: Radius.S))
52
67
  }
53
68
 
@@ -59,6 +74,7 @@ public struct Button: View {
59
74
  var size: ButtonSize = .large
60
75
  var iconLeft: AnyView?
61
76
  var iconRight: AnyView?
77
+ var loading: Bool
62
78
 
63
79
  func renderTitle(title: String, type: ButtonType, size: ButtonSize) -> Text {
64
80
  switch size {
@@ -0,0 +1,86 @@
1
+ //
2
+ // LottieView.swift
3
+ // MoMoPlatform
4
+ //
5
+ // Created by thanhdat on 16/01/2023.
6
+ // Copyright © 2023 Facebook. All rights reserved.
7
+ //
8
+
9
+ import SwiftUI
10
+ import Lottie
11
+
12
+ struct LottieView: UIViewRepresentable {
13
+ var name: String = ""
14
+ var url: String?
15
+ var loopMode: LottieLoopMode = .playOnce
16
+ var contentMode : UIView.ContentMode = .scaleAspectFit
17
+ var onDone: (() -> Void)?
18
+
19
+ // Add static cache for animations
20
+ private static var animationCache = NSCache<NSString, LottieAnimation>()
21
+
22
+ func makeUIView(context: UIViewRepresentableContext<LottieView>) -> UIView {
23
+ let view = UIView(frame: .zero)
24
+
25
+ // Configure Lottie with optimized settings
26
+ let configuration = LottieConfiguration(
27
+ renderingEngine: .coreAnimation,
28
+ decodingStrategy: .dictionaryBased
29
+ )
30
+
31
+ let animationView = LottieAnimationView(configuration: configuration)
32
+
33
+ if let url = url {
34
+ // Use cached animation if available
35
+ if let cachedAnimation = Self.animationCache.object(forKey: url as NSString) {
36
+ animationView.animation = cachedAnimation
37
+ setupAnimationView(animationView, view)
38
+ } else {
39
+ LottieAnimation.loadedFrom(url: URL(string: url)!, closure: { animation in
40
+ if let animation = animation {
41
+ Self.animationCache.setObject(animation, forKey: url as NSString)
42
+ animationView.animation = animation
43
+ setupAnimationView(animationView, view)
44
+ }
45
+ }, animationCache: nil)
46
+ }
47
+ } else {
48
+ // Use cached animation if available
49
+ if let cachedAnimation = Self.animationCache.object(forKey: name as NSString) {
50
+ animationView.animation = cachedAnimation
51
+ setupAnimationView(animationView, view)
52
+ } else if let animation = LottieAnimation.named(name) {
53
+ Self.animationCache.setObject(animation, forKey: name as NSString)
54
+ animationView.animation = animation
55
+ setupAnimationView(animationView, view)
56
+ }
57
+ }
58
+
59
+ return view
60
+ }
61
+
62
+ private func setupAnimationView(_ animationView: LottieAnimationView, _ containerView: UIView) {
63
+ animationView.contentMode = contentMode
64
+ animationView.loopMode = loopMode
65
+ animationView.backgroundBehavior = loopMode == .loop ? .pauseAndRestore : .pause
66
+
67
+ // Optimize rendering
68
+ animationView.shouldRasterizeWhenIdle = true
69
+
70
+ animationView.translatesAutoresizingMaskIntoConstraints = false
71
+ containerView.addSubview(animationView)
72
+
73
+ NSLayoutConstraint.activate([
74
+ animationView.widthAnchor.constraint(equalTo: containerView.widthAnchor),
75
+ animationView.heightAnchor.constraint(equalTo: containerView.heightAnchor)
76
+ ])
77
+
78
+ animationView.play { finished in
79
+ if finished {
80
+ onDone?()
81
+ }
82
+ }
83
+ }
84
+
85
+ func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieView>) {}
86
+ }
@@ -0,0 +1,55 @@
1
+ //
2
+ // LottieViewController.swift
3
+ // MoMoPlatform
4
+ //
5
+ // Created by Đại Trịnh on 26/09/2023.
6
+ // Copyright © 2023 Facebook. All rights reserved.
7
+ //
8
+
9
+ import Foundation
10
+ import Lottie
11
+ import SwiftUI
12
+
13
+ public class LottieViewController: UIViewController {
14
+ var name: String = ""
15
+ var url: String?
16
+ var loopMode: LottieLoopMode = .playOnce
17
+ var frame: CGRect
18
+ var id: String?
19
+
20
+ init(name: String, url: String? = nil, loopMode: LottieLoopMode, frame: CGRect, id: String? = nil) {
21
+ self.name = name
22
+ self.url = url
23
+ self.loopMode = loopMode
24
+ self.frame = frame
25
+ self.id = id
26
+
27
+ super.init(nibName: nil, bundle: nil)
28
+ }
29
+
30
+ required init?(coder: NSCoder) {
31
+ fatalError("init(coder:) has not been implemented")
32
+ }
33
+
34
+ public override func viewDidLoad() {
35
+ super.viewDidLoad()
36
+ setupCustomView()
37
+ }
38
+
39
+ private func setupCustomView() {
40
+ let basicView = UIView()
41
+ basicView.frame = frame
42
+
43
+ let customLottieView = LottieView(name: name, url: url, loopMode: loopMode)
44
+ let hostViewController = UIHostingController(rootView: customLottieView)
45
+ hostViewController.view.frame = basicView.bounds;
46
+ hostViewController.view.backgroundColor = .clear
47
+
48
+ // set accessibility label
49
+ basicView.accessibilityLabel = id ?? "" // Set accessibility label directly to basicView
50
+ basicView.isAccessibilityElement = true // Ensure basicView is treated as an accessible element
51
+
52
+ basicView.addSubview(hostViewController.view)
53
+ view.addSubview(basicView)
54
+ }
55
+ }
@@ -13,5 +13,6 @@ Pod::Spec.new do |spec|
13
13
 
14
14
  spec.framework = 'SwiftUI', 'Combine'
15
15
  spec.dependency 'SDWebImageSwiftUI'
16
+ spec.dependency 'lottie-ios'
16
17
  spec.dependency 'SkeletonUI', '1.0.11'
17
18
  end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/native-kits",
3
- "version": "0.151.2-test.3",
3
+ "version": "0.151.2-test.5",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "@momo-platform/native-max-api": "1.0.18"