react-native-skeleton-native 0.0.0-stage → 0.1.0

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.
Files changed (221) hide show
  1. package/android/build.gradle +89 -0
  2. package/android/src/main/AndroidManifest.xml +3 -0
  3. package/android/src/main/AndroidManifestNew.xml +2 -0
  4. package/android/src/main/java/com/skeletonnative/SkeletonEpoch.java +10 -0
  5. package/android/src/main/java/com/skeletonnative/SkeletonGroupManager.java +22 -0
  6. package/android/src/main/java/com/skeletonnative/SkeletonGroupView.java +19 -0
  7. package/android/src/main/java/com/skeletonnative/SkeletonHighlightView.java +66 -0
  8. package/android/src/main/java/com/skeletonnative/SkeletonMotion.java +81 -0
  9. package/android/src/main/java/com/skeletonnative/SkeletonNativePackage.java +28 -0
  10. package/android/src/main/java/com/skeletonnative/SkeletonPainter.java +418 -0
  11. package/android/src/main/java/com/skeletonnative/SkeletonResources.java +123 -0
  12. package/android/src/main/java/com/skeletonnative/SkeletonShimmerManager.java +72 -0
  13. package/android/src/main/java/com/skeletonnative/SkeletonShimmerView.java +74 -0
  14. package/android/src/main/java/com/skeletonnative/SkeletonView.java +81 -0
  15. package/android/src/main/java/com/skeletonnative/SkeletonViewManager.java +89 -0
  16. package/android/src/main/res/animator/skeleton_native_slide_1000.xml +6 -0
  17. package/android/src/main/res/animator/skeleton_native_slide_1100.xml +6 -0
  18. package/android/src/main/res/animator/skeleton_native_slide_1200.xml +6 -0
  19. package/android/src/main/res/animator/skeleton_native_slide_1300.xml +6 -0
  20. package/android/src/main/res/animator/skeleton_native_slide_1400.xml +6 -0
  21. package/android/src/main/res/animator/skeleton_native_slide_1500.xml +6 -0
  22. package/android/src/main/res/animator/skeleton_native_slide_1600.xml +6 -0
  23. package/android/src/main/res/animator/skeleton_native_slide_1700.xml +6 -0
  24. package/android/src/main/res/animator/skeleton_native_slide_1800.xml +6 -0
  25. package/android/src/main/res/animator/skeleton_native_slide_1900.xml +6 -0
  26. package/android/src/main/res/animator/skeleton_native_slide_2000.xml +6 -0
  27. package/android/src/main/res/animator/skeleton_native_slide_2100.xml +6 -0
  28. package/android/src/main/res/animator/skeleton_native_slide_2200.xml +6 -0
  29. package/android/src/main/res/animator/skeleton_native_slide_2300.xml +6 -0
  30. package/android/src/main/res/animator/skeleton_native_slide_2400.xml +6 -0
  31. package/android/src/main/res/animator/skeleton_native_slide_2500.xml +6 -0
  32. package/android/src/main/res/animator/skeleton_native_slide_2600.xml +6 -0
  33. package/android/src/main/res/animator/skeleton_native_slide_2700.xml +6 -0
  34. package/android/src/main/res/animator/skeleton_native_slide_2800.xml +6 -0
  35. package/android/src/main/res/animator/skeleton_native_slide_2900.xml +6 -0
  36. package/android/src/main/res/animator/skeleton_native_slide_300.xml +6 -0
  37. package/android/src/main/res/animator/skeleton_native_slide_3000.xml +6 -0
  38. package/android/src/main/res/animator/skeleton_native_slide_3100.xml +6 -0
  39. package/android/src/main/res/animator/skeleton_native_slide_3200.xml +6 -0
  40. package/android/src/main/res/animator/skeleton_native_slide_3300.xml +6 -0
  41. package/android/src/main/res/animator/skeleton_native_slide_3400.xml +6 -0
  42. package/android/src/main/res/animator/skeleton_native_slide_3500.xml +6 -0
  43. package/android/src/main/res/animator/skeleton_native_slide_3600.xml +6 -0
  44. package/android/src/main/res/animator/skeleton_native_slide_3700.xml +6 -0
  45. package/android/src/main/res/animator/skeleton_native_slide_3800.xml +6 -0
  46. package/android/src/main/res/animator/skeleton_native_slide_3900.xml +6 -0
  47. package/android/src/main/res/animator/skeleton_native_slide_400.xml +6 -0
  48. package/android/src/main/res/animator/skeleton_native_slide_4000.xml +6 -0
  49. package/android/src/main/res/animator/skeleton_native_slide_4100.xml +6 -0
  50. package/android/src/main/res/animator/skeleton_native_slide_4200.xml +6 -0
  51. package/android/src/main/res/animator/skeleton_native_slide_4300.xml +6 -0
  52. package/android/src/main/res/animator/skeleton_native_slide_4400.xml +6 -0
  53. package/android/src/main/res/animator/skeleton_native_slide_4500.xml +6 -0
  54. package/android/src/main/res/animator/skeleton_native_slide_4600.xml +6 -0
  55. package/android/src/main/res/animator/skeleton_native_slide_4700.xml +6 -0
  56. package/android/src/main/res/animator/skeleton_native_slide_4800.xml +6 -0
  57. package/android/src/main/res/animator/skeleton_native_slide_4900.xml +6 -0
  58. package/android/src/main/res/animator/skeleton_native_slide_500.xml +6 -0
  59. package/android/src/main/res/animator/skeleton_native_slide_5000.xml +6 -0
  60. package/android/src/main/res/animator/skeleton_native_slide_600.xml +6 -0
  61. package/android/src/main/res/animator/skeleton_native_slide_700.xml +6 -0
  62. package/android/src/main/res/animator/skeleton_native_slide_800.xml +6 -0
  63. package/android/src/main/res/animator/skeleton_native_slide_900.xml +6 -0
  64. package/android/src/main/res/drawable/skeleton_native_pulse.xml +22 -0
  65. package/android/src/main/res/drawable/skeleton_native_pulse_1000.xml +5 -0
  66. package/android/src/main/res/drawable/skeleton_native_pulse_1100.xml +5 -0
  67. package/android/src/main/res/drawable/skeleton_native_pulse_1200.xml +5 -0
  68. package/android/src/main/res/drawable/skeleton_native_pulse_1300.xml +5 -0
  69. package/android/src/main/res/drawable/skeleton_native_pulse_1400.xml +5 -0
  70. package/android/src/main/res/drawable/skeleton_native_pulse_1500.xml +5 -0
  71. package/android/src/main/res/drawable/skeleton_native_pulse_1600.xml +5 -0
  72. package/android/src/main/res/drawable/skeleton_native_pulse_1700.xml +5 -0
  73. package/android/src/main/res/drawable/skeleton_native_pulse_1800.xml +5 -0
  74. package/android/src/main/res/drawable/skeleton_native_pulse_1900.xml +5 -0
  75. package/android/src/main/res/drawable/skeleton_native_pulse_2000.xml +5 -0
  76. package/android/src/main/res/drawable/skeleton_native_pulse_2100.xml +5 -0
  77. package/android/src/main/res/drawable/skeleton_native_pulse_2200.xml +5 -0
  78. package/android/src/main/res/drawable/skeleton_native_pulse_2300.xml +5 -0
  79. package/android/src/main/res/drawable/skeleton_native_pulse_2400.xml +5 -0
  80. package/android/src/main/res/drawable/skeleton_native_pulse_2500.xml +5 -0
  81. package/android/src/main/res/drawable/skeleton_native_pulse_2600.xml +5 -0
  82. package/android/src/main/res/drawable/skeleton_native_pulse_2700.xml +5 -0
  83. package/android/src/main/res/drawable/skeleton_native_pulse_2800.xml +5 -0
  84. package/android/src/main/res/drawable/skeleton_native_pulse_2900.xml +5 -0
  85. package/android/src/main/res/drawable/skeleton_native_pulse_300.xml +5 -0
  86. package/android/src/main/res/drawable/skeleton_native_pulse_3000.xml +5 -0
  87. package/android/src/main/res/drawable/skeleton_native_pulse_3100.xml +5 -0
  88. package/android/src/main/res/drawable/skeleton_native_pulse_3200.xml +5 -0
  89. package/android/src/main/res/drawable/skeleton_native_pulse_3300.xml +5 -0
  90. package/android/src/main/res/drawable/skeleton_native_pulse_3400.xml +5 -0
  91. package/android/src/main/res/drawable/skeleton_native_pulse_3500.xml +5 -0
  92. package/android/src/main/res/drawable/skeleton_native_pulse_3600.xml +5 -0
  93. package/android/src/main/res/drawable/skeleton_native_pulse_3700.xml +5 -0
  94. package/android/src/main/res/drawable/skeleton_native_pulse_3800.xml +5 -0
  95. package/android/src/main/res/drawable/skeleton_native_pulse_3900.xml +5 -0
  96. package/android/src/main/res/drawable/skeleton_native_pulse_400.xml +5 -0
  97. package/android/src/main/res/drawable/skeleton_native_pulse_4000.xml +5 -0
  98. package/android/src/main/res/drawable/skeleton_native_pulse_4100.xml +5 -0
  99. package/android/src/main/res/drawable/skeleton_native_pulse_4200.xml +5 -0
  100. package/android/src/main/res/drawable/skeleton_native_pulse_4300.xml +5 -0
  101. package/android/src/main/res/drawable/skeleton_native_pulse_4400.xml +5 -0
  102. package/android/src/main/res/drawable/skeleton_native_pulse_4500.xml +5 -0
  103. package/android/src/main/res/drawable/skeleton_native_pulse_4600.xml +5 -0
  104. package/android/src/main/res/drawable/skeleton_native_pulse_4700.xml +5 -0
  105. package/android/src/main/res/drawable/skeleton_native_pulse_4800.xml +5 -0
  106. package/android/src/main/res/drawable/skeleton_native_pulse_4900.xml +5 -0
  107. package/android/src/main/res/drawable/skeleton_native_pulse_500.xml +5 -0
  108. package/android/src/main/res/drawable/skeleton_native_pulse_5000.xml +5 -0
  109. package/android/src/main/res/drawable/skeleton_native_pulse_600.xml +5 -0
  110. package/android/src/main/res/drawable/skeleton_native_pulse_700.xml +5 -0
  111. package/android/src/main/res/drawable/skeleton_native_pulse_800.xml +5 -0
  112. package/android/src/main/res/drawable/skeleton_native_pulse_900.xml +5 -0
  113. package/android/src/main/res/drawable/skeleton_native_shimmer.xml +22 -0
  114. package/android/src/main/res/drawable/skeleton_native_shimmer_1000.xml +5 -0
  115. package/android/src/main/res/drawable/skeleton_native_shimmer_1100.xml +5 -0
  116. package/android/src/main/res/drawable/skeleton_native_shimmer_1200.xml +5 -0
  117. package/android/src/main/res/drawable/skeleton_native_shimmer_1300.xml +5 -0
  118. package/android/src/main/res/drawable/skeleton_native_shimmer_1400.xml +5 -0
  119. package/android/src/main/res/drawable/skeleton_native_shimmer_1500.xml +5 -0
  120. package/android/src/main/res/drawable/skeleton_native_shimmer_1600.xml +5 -0
  121. package/android/src/main/res/drawable/skeleton_native_shimmer_1700.xml +5 -0
  122. package/android/src/main/res/drawable/skeleton_native_shimmer_1800.xml +5 -0
  123. package/android/src/main/res/drawable/skeleton_native_shimmer_1900.xml +5 -0
  124. package/android/src/main/res/drawable/skeleton_native_shimmer_2000.xml +5 -0
  125. package/android/src/main/res/drawable/skeleton_native_shimmer_2100.xml +5 -0
  126. package/android/src/main/res/drawable/skeleton_native_shimmer_2200.xml +5 -0
  127. package/android/src/main/res/drawable/skeleton_native_shimmer_2300.xml +5 -0
  128. package/android/src/main/res/drawable/skeleton_native_shimmer_2400.xml +5 -0
  129. package/android/src/main/res/drawable/skeleton_native_shimmer_2500.xml +5 -0
  130. package/android/src/main/res/drawable/skeleton_native_shimmer_2600.xml +5 -0
  131. package/android/src/main/res/drawable/skeleton_native_shimmer_2700.xml +5 -0
  132. package/android/src/main/res/drawable/skeleton_native_shimmer_2800.xml +5 -0
  133. package/android/src/main/res/drawable/skeleton_native_shimmer_2900.xml +5 -0
  134. package/android/src/main/res/drawable/skeleton_native_shimmer_300.xml +5 -0
  135. package/android/src/main/res/drawable/skeleton_native_shimmer_3000.xml +5 -0
  136. package/android/src/main/res/drawable/skeleton_native_shimmer_3100.xml +5 -0
  137. package/android/src/main/res/drawable/skeleton_native_shimmer_3200.xml +5 -0
  138. package/android/src/main/res/drawable/skeleton_native_shimmer_3300.xml +5 -0
  139. package/android/src/main/res/drawable/skeleton_native_shimmer_3400.xml +5 -0
  140. package/android/src/main/res/drawable/skeleton_native_shimmer_3500.xml +5 -0
  141. package/android/src/main/res/drawable/skeleton_native_shimmer_3600.xml +5 -0
  142. package/android/src/main/res/drawable/skeleton_native_shimmer_3700.xml +5 -0
  143. package/android/src/main/res/drawable/skeleton_native_shimmer_3800.xml +5 -0
  144. package/android/src/main/res/drawable/skeleton_native_shimmer_3900.xml +5 -0
  145. package/android/src/main/res/drawable/skeleton_native_shimmer_400.xml +5 -0
  146. package/android/src/main/res/drawable/skeleton_native_shimmer_4000.xml +5 -0
  147. package/android/src/main/res/drawable/skeleton_native_shimmer_4100.xml +5 -0
  148. package/android/src/main/res/drawable/skeleton_native_shimmer_4200.xml +5 -0
  149. package/android/src/main/res/drawable/skeleton_native_shimmer_4300.xml +5 -0
  150. package/android/src/main/res/drawable/skeleton_native_shimmer_4400.xml +5 -0
  151. package/android/src/main/res/drawable/skeleton_native_shimmer_4500.xml +5 -0
  152. package/android/src/main/res/drawable/skeleton_native_shimmer_4600.xml +5 -0
  153. package/android/src/main/res/drawable/skeleton_native_shimmer_4700.xml +5 -0
  154. package/android/src/main/res/drawable/skeleton_native_shimmer_4800.xml +5 -0
  155. package/android/src/main/res/drawable/skeleton_native_shimmer_4900.xml +5 -0
  156. package/android/src/main/res/drawable/skeleton_native_shimmer_500.xml +5 -0
  157. package/android/src/main/res/drawable/skeleton_native_shimmer_5000.xml +5 -0
  158. package/android/src/main/res/drawable/skeleton_native_shimmer_600.xml +5 -0
  159. package/android/src/main/res/drawable/skeleton_native_shimmer_700.xml +5 -0
  160. package/android/src/main/res/drawable/skeleton_native_shimmer_800.xml +5 -0
  161. package/android/src/main/res/drawable/skeleton_native_shimmer_900.xml +5 -0
  162. package/android/src/newarch/java/com/skeletonnative/SkeletonGroupManagerSpec.java +19 -0
  163. package/android/src/newarch/java/com/skeletonnative/SkeletonShimmerManagerSpec.java +19 -0
  164. package/android/src/newarch/java/com/skeletonnative/SkeletonViewManagerSpec.java +19 -0
  165. package/android/src/oldarch/java/com/skeletonnative/SkeletonGroupManagerSpec.java +6 -0
  166. package/android/src/oldarch/java/com/skeletonnative/SkeletonShimmerManagerSpec.java +21 -0
  167. package/android/src/oldarch/java/com/skeletonnative/SkeletonViewManagerSpec.java +25 -0
  168. package/ios/RNSkeletonAnimator.h +75 -0
  169. package/ios/RNSkeletonAnimator.mm +406 -0
  170. package/ios/RNSkeletonComponentViews.mm +256 -0
  171. package/ios/RNSkeletonGroupRegistry.h +14 -0
  172. package/ios/RNSkeletonGroupRegistry.mm +37 -0
  173. package/ios/RNSkeletonLegacyViews.h +22 -0
  174. package/ios/RNSkeletonLegacyViews.mm +99 -0
  175. package/ios/RNSkeletonView.h +29 -0
  176. package/ios/RNSkeletonView.mm +77 -0
  177. package/ios/RNSkeletonViewManagers.mm +65 -0
  178. package/lib/commonjs/Shimmer.js +55 -0
  179. package/lib/commonjs/Skeleton.js +113 -0
  180. package/lib/commonjs/SkeletonGroup.js +58 -0
  181. package/lib/commonjs/context.js +10 -0
  182. package/lib/commonjs/index.js +15 -0
  183. package/lib/commonjs/props.js +113 -0
  184. package/lib/commonjs/specs/SkeletonNativeGroupNativeComponent.js +8 -0
  185. package/lib/commonjs/specs/SkeletonNativeShimmerNativeComponent.js +8 -0
  186. package/lib/commonjs/specs/SkeletonNativeViewNativeComponent.js +8 -0
  187. package/lib/commonjs/types.js +3 -0
  188. package/lib/module/Shimmer.js +16 -0
  189. package/lib/module/Skeleton.js +73 -0
  190. package/lib/module/SkeletonGroup.js +19 -0
  191. package/lib/module/context.js +6 -0
  192. package/lib/module/index.js +5 -0
  193. package/lib/module/props.js +103 -0
  194. package/lib/module/specs/SkeletonNativeGroupNativeComponent.js +3 -0
  195. package/lib/module/specs/SkeletonNativeShimmerNativeComponent.js +3 -0
  196. package/lib/module/specs/SkeletonNativeViewNativeComponent.js +3 -0
  197. package/lib/module/types.js +2 -0
  198. package/lib/typescript/Shimmer.d.ts +13 -0
  199. package/lib/typescript/Skeleton.d.ts +17 -0
  200. package/lib/typescript/SkeletonGroup.d.ts +17 -0
  201. package/lib/typescript/context.d.ts +7 -0
  202. package/lib/typescript/index.d.ts +7 -0
  203. package/lib/typescript/props.d.ts +42 -0
  204. package/lib/typescript/specs/SkeletonNativeGroupNativeComponent.d.ts +5 -0
  205. package/lib/typescript/specs/SkeletonNativeShimmerNativeComponent.d.ts +13 -0
  206. package/lib/typescript/specs/SkeletonNativeViewNativeComponent.d.ts +15 -0
  207. package/lib/typescript/types.d.ts +60 -0
  208. package/package.json +83 -4
  209. package/react-native-skeleton-native.podspec +20 -0
  210. package/react-native.config.js +10 -0
  211. package/src/Shimmer.tsx +38 -0
  212. package/src/Skeleton.tsx +125 -0
  213. package/src/SkeletonGroup.tsx +46 -0
  214. package/src/context.ts +13 -0
  215. package/src/index.ts +15 -0
  216. package/src/props.ts +126 -0
  217. package/src/specs/SkeletonNativeGroupNativeComponent.ts +9 -0
  218. package/src/specs/SkeletonNativeShimmerNativeComponent.ts +17 -0
  219. package/src/specs/SkeletonNativeViewNativeComponent.ts +20 -0
  220. package/src/types.ts +66 -0
  221. package/README.md +0 -3
@@ -0,0 +1,418 @@
1
+ package com.skeletonnative;
2
+
3
+ import android.content.Context;
4
+ import android.content.res.ColorStateList;
5
+ import android.content.res.Configuration;
6
+ import android.graphics.Color;
7
+ import android.graphics.PorterDuff;
8
+ import android.graphics.Rect;
9
+ import android.graphics.drawable.AnimatedVectorDrawable;
10
+ import android.graphics.drawable.Drawable;
11
+ import android.os.SystemClock;
12
+ import android.view.View;
13
+ import android.view.ViewParent;
14
+ import android.view.ViewTreeObserver;
15
+ import androidx.annotation.Nullable;
16
+
17
+ /**
18
+ * Draws the moving highlight for one host view and decides when it runs.
19
+ *
20
+ * <p>The highlight is an {@link AnimatedVectorDrawable}: a repeating gradient
21
+ * pattern (three periods wide) inside a group whose {@code translateX} is
22
+ * animated by one period per cycle. On API 25+ with hardware rendering the
23
+ * animation is evaluated on RenderThread, so it keeps moving while the UI
24
+ * thread is blocked. On API 24 the same drawable animates on the UI thread.
25
+ *
26
+ * <p>Synchronisation: an AVD cannot be started at a given phase, so the phase
27
+ * is applied in space instead. When the animation starts at {@code t0}, the
28
+ * drawable is offset by {@code ((t0 - epoch) / period) * P} pixels, where
29
+ * {@code P} is the pattern period. Because the pattern repeats every
30
+ * {@code P}, this puts the band exactly where an animation started at the
31
+ * epoch would have it. All views drawn in a group's coordinate space with the
32
+ * group's epoch therefore show one band sweeping across the group.
33
+ */
34
+ final class SkeletonPainter implements SkeletonMotion.Listener {
35
+ static final int MODE_STATIC = 0;
36
+ static final int MODE_SHIMMER = 1;
37
+ static final int MODE_PULSE = 2;
38
+ static final int MODE_GENTLE_PULSE = 3;
39
+
40
+ /** Light and dark defaults; keep in sync with DEFAULT_COLORS in src/props.ts. */
41
+ static final int LIGHT_BASE = 0xFFE1E4E8;
42
+ static final int LIGHT_HIGHLIGHT = 0xFFF5F7F9;
43
+ static final int DARK_BASE = 0xFF2B2E33;
44
+ static final int DARK_HIGHLIGHT = 0xFF3D4148;
45
+
46
+ /** Pattern period relative to the width of the swept area: 1/3 of each cycle has no band in view. */
47
+ private static final float SHIMMER_PERIOD_FACTOR = 1.5f;
48
+ /** For pulse, the triangle wave is so wide that the whole area sees one alpha. */
49
+ private static final float PULSE_PERIOD_FACTOR = 100f;
50
+ private static final float GENTLE_ALPHA = 0.4f;
51
+
52
+ private final android.view.ViewGroup host;
53
+ private final Rect visibleRect = new Rect();
54
+
55
+ @Nullable private Integer baseColor;
56
+ @Nullable private Integer highlightColor;
57
+ private int durationMs = 1400;
58
+ private boolean rtl;
59
+ private float angle = 20f;
60
+ private String variant = "shimmer";
61
+ private boolean animated = true;
62
+ private String reduceMotion = "system";
63
+
64
+ @Nullable private AnimatedVectorDrawable avd;
65
+ private int avdKey = -1;
66
+ private int mode = -1;
67
+ private boolean running;
68
+ private float phaseFraction;
69
+ private boolean dirty = true;
70
+
71
+ private final SkeletonHighlightView highlight;
72
+ private final android.view.ViewGroup hostGroup;
73
+
74
+ private boolean attached;
75
+ private boolean aggregatedVisible = true;
76
+ private boolean onScreen = true;
77
+ @Nullable private ViewTreeObserver observer;
78
+
79
+ private final ViewTreeObserver.OnScrollChangedListener scrollListener =
80
+ new ViewTreeObserver.OnScrollChangedListener() {
81
+ @Override
82
+ public void onScrollChanged() {
83
+ checkOnScreen();
84
+ }
85
+ };
86
+
87
+ // Runs on every UI-thread traversal (not on RenderThread-only animation
88
+ // frames). React Native moves and resizes views outside a layout pass, so a
89
+ // global-layout listener would miss changes to the group's size.
90
+ private final ViewTreeObserver.OnPreDrawListener preDrawListener =
91
+ new ViewTreeObserver.OnPreDrawListener() {
92
+ @Override
93
+ public boolean onPreDraw() {
94
+ checkOnScreen();
95
+ layoutHighlight();
96
+ return true;
97
+ }
98
+ };
99
+
100
+ SkeletonPainter(android.view.ViewGroup host) {
101
+ this.host = host;
102
+ this.hostGroup = host;
103
+ highlight = new SkeletonHighlightView(host.getContext());
104
+ highlight.layout(0, 0, SkeletonHighlightView.WIDTH_PX, SkeletonHighlightView.HEIGHT_PX);
105
+ highlight.setVisibility(View.INVISIBLE);
106
+ host.getOverlay().add(highlight);
107
+ }
108
+
109
+ /** The overlay child that draws the highlight (for SRC_ATOP layers in <Shimmer>). */
110
+ SkeletonHighlightView highlightView() {
111
+ return highlight;
112
+ }
113
+
114
+ // ---- props ---------------------------------------------------------------
115
+
116
+ void setBaseColor(@Nullable Integer color) {
117
+ baseColor = color;
118
+ host.invalidate();
119
+ }
120
+
121
+ void setHighlightColor(@Nullable Integer color) {
122
+ highlightColor = color;
123
+ applyTint();
124
+ host.invalidate();
125
+ }
126
+
127
+ void setDuration(int ms) {
128
+ durationMs = ms > 0 ? ms : 1400;
129
+ dirty = true;
130
+ }
131
+
132
+ void setDirection(@Nullable String direction) {
133
+ rtl = "rtl".equals(direction);
134
+ dirty = true;
135
+ }
136
+
137
+ void setAngle(float degrees) {
138
+ angle = Float.isNaN(degrees) ? 20f : Math.max(-45f, Math.min(45f, degrees));
139
+ dirty = true;
140
+ }
141
+
142
+ void setVariant(@Nullable String value) {
143
+ variant = value == null ? "shimmer" : value;
144
+ dirty = true;
145
+ }
146
+
147
+ void setAnimated(boolean value) {
148
+ animated = value;
149
+ dirty = true;
150
+ }
151
+
152
+ void setReduceMotion(@Nullable String value) {
153
+ reduceMotion = value == null ? "system" : value;
154
+ dirty = true;
155
+ }
156
+
157
+ // ---- colours ---------------------------------------------------------------
158
+
159
+ private boolean isNight() {
160
+ int night = host.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
161
+ return night == Configuration.UI_MODE_NIGHT_YES;
162
+ }
163
+
164
+ int resolvedBaseColor() {
165
+ if (baseColor != null) return baseColor;
166
+ return isNight() ? DARK_BASE : LIGHT_BASE;
167
+ }
168
+
169
+ int resolvedHighlightColor() {
170
+ int color = highlightColor != null ? highlightColor : (isNight() ? DARK_HIGHLIGHT : LIGHT_HIGHLIGHT);
171
+ if (mode == MODE_GENTLE_PULSE) {
172
+ color = Color.argb(Math.round(Color.alpha(color) * GENTLE_ALPHA), Color.red(color), Color.green(color), Color.blue(color));
173
+ }
174
+ return color;
175
+ }
176
+
177
+ void onConfigurationChanged() {
178
+ applyTint();
179
+ host.invalidate();
180
+ }
181
+
182
+ private void applyTint() {
183
+ if (avd == null) return;
184
+ avd.setTintList(ColorStateList.valueOf(resolvedHighlightColor()));
185
+ avd.setTintMode(PorterDuff.Mode.SRC_IN);
186
+ }
187
+
188
+ // ---- mode and drawable ------------------------------------------------------
189
+
190
+ int computeMode() {
191
+ if (!animated || "none".equals(variant)) return MODE_STATIC;
192
+ if ("never".equals(reduceMotion)) {
193
+ // Even when asked to ignore the setting, nothing can animate with animators off.
194
+ return SkeletonMotion.animatorsEnabled(host.getContext()) ? variantMode() : MODE_STATIC;
195
+ }
196
+ if (!SkeletonMotion.animatorsEnabled(host.getContext())) return MODE_STATIC;
197
+ if ("always".equals(reduceMotion)) return MODE_GENTLE_PULSE;
198
+ return variantMode();
199
+ }
200
+
201
+ private int variantMode() {
202
+ return "pulse".equals(variant) ? MODE_PULSE : MODE_SHIMMER;
203
+ }
204
+
205
+ int mode() {
206
+ if (mode < 0) mode = computeMode();
207
+ return mode;
208
+ }
209
+
210
+ /** Apply the props set since the last call; called once per prop update batch. */
211
+ void commit() {
212
+ if (dirty) refresh();
213
+ }
214
+
215
+ /** Re-evaluate the mode and the drawable after a prop or setting change. */
216
+ void refresh() {
217
+ dirty = false;
218
+ int newMode = computeMode();
219
+ boolean pulse = newMode == MODE_PULSE || newMode == MODE_GENTLE_PULSE;
220
+ int key = newMode == MODE_STATIC ? -1 : SkeletonResources.drawable(pulse, effectiveDurationMsFor(newMode));
221
+ if (newMode != mode || key != avdKey) {
222
+ stop();
223
+ if (avd != null) {
224
+ highlight.setDrawable(null);
225
+ avd = null;
226
+ }
227
+
228
+ mode = newMode;
229
+ avdKey = key;
230
+ if (key != -1) {
231
+ Drawable d = host.getContext().getDrawable(key);
232
+ if (d instanceof AnimatedVectorDrawable) {
233
+ avd = (AnimatedVectorDrawable) d.mutate();
234
+ highlight.setDrawable(avd);
235
+ applyTint();
236
+ } else {
237
+ mode = MODE_STATIC;
238
+ }
239
+ }
240
+ } else {
241
+ applyTint();
242
+ }
243
+ updateRunning();
244
+ layoutHighlight();
245
+ host.invalidate();
246
+ }
247
+
248
+ private int effectiveDurationMsFor(int m) {
249
+ return m == MODE_GENTLE_PULSE ? durationMs * 2 : durationMs;
250
+ }
251
+
252
+ boolean isAnimating() {
253
+ return mode() != MODE_STATIC;
254
+ }
255
+
256
+ // ---- lifecycle --------------------------------------------------------------
257
+
258
+ void onAttached() {
259
+ attached = true;
260
+ observer = host.getViewTreeObserver();
261
+ observer.addOnScrollChangedListener(scrollListener);
262
+ observer.addOnPreDrawListener(preDrawListener);
263
+ SkeletonMotion.addListener(host.getContext(), this);
264
+ // The setting may have changed while detached.
265
+ refresh();
266
+ }
267
+
268
+ void onDetached() {
269
+ attached = false;
270
+ if (observer != null && observer.isAlive()) {
271
+ observer.removeOnScrollChangedListener(scrollListener);
272
+ observer.removeOnPreDrawListener(preDrawListener);
273
+ }
274
+ observer = null;
275
+ SkeletonMotion.removeListener(this);
276
+ updateRunning();
277
+ }
278
+
279
+ void onVisibilityAggregated(boolean visible) {
280
+ aggregatedVisible = visible;
281
+ updateRunning();
282
+ }
283
+
284
+ void onSizeChanged() {
285
+ checkOnScreen();
286
+ layoutHighlight();
287
+ }
288
+
289
+ @Override
290
+ public void onMotionSettingsChanged() {
291
+ // A different duration scale changes the real period, so restart from the epoch.
292
+ stop();
293
+ refresh();
294
+ }
295
+
296
+ private void checkOnScreen() {
297
+ boolean now = host.getWidth() > 0 && host.getHeight() > 0 && host.getGlobalVisibleRect(visibleRect);
298
+ if (now != onScreen) {
299
+ onScreen = now;
300
+ updateRunning();
301
+ }
302
+ }
303
+
304
+ private void updateRunning() {
305
+ boolean shouldRun = avd != null && mode != MODE_STATIC && attached && aggregatedVisible && onScreen;
306
+ if (shouldRun && !running) {
307
+ long epoch = epochMs();
308
+ float scale = SkeletonMotion.durationScale(host.getContext());
309
+ float periodMs = SkeletonResources.quantize(effectiveDurationMsFor(mode)) * (scale > 0 ? scale : 1f);
310
+ long now = SystemClock.uptimeMillis();
311
+ double cycles = (now - epoch) / (double) periodMs;
312
+ phaseFraction = (float) (cycles - Math.floor(cycles));
313
+ avd.start();
314
+ running = true;
315
+ layoutHighlight();
316
+ } else if (!shouldRun && running) {
317
+ stop();
318
+ }
319
+ }
320
+
321
+ private void stop() {
322
+ if (running && avd != null) avd.stop();
323
+ running = false;
324
+ }
325
+
326
+ // ---- geometry and drawing ---------------------------------------------------
327
+
328
+ static final class Geometry {
329
+ float ox;
330
+ float oy;
331
+ int gw;
332
+ int gh;
333
+ @Nullable SkeletonGroupView group;
334
+ }
335
+
336
+ private final Geometry geometry = new Geometry();
337
+
338
+ /** Offset of the host inside its group (or itself) and the size of the swept area. */
339
+ Geometry geometry(int w, int h) {
340
+ Geometry g = geometry;
341
+ g.group = null;
342
+ float x = 0;
343
+ float y = 0;
344
+ View v = host;
345
+ ViewParent p = v.getParent();
346
+ while (p instanceof View) {
347
+ View pv = (View) p;
348
+ x += v.getLeft() + v.getTranslationX();
349
+ y += v.getTop() + v.getTranslationY();
350
+ if (pv instanceof SkeletonGroupView) {
351
+ g.group = (SkeletonGroupView) pv;
352
+ break;
353
+ }
354
+ x -= pv.getScrollX();
355
+ y -= pv.getScrollY();
356
+ v = pv;
357
+ p = pv.getParent();
358
+ }
359
+ if (g.group != null) {
360
+ g.ox = x;
361
+ g.oy = y;
362
+ g.gw = Math.max(g.group.getWidth(), 1);
363
+ g.gh = Math.max(g.group.getHeight(), 1);
364
+ } else {
365
+ g.ox = 0;
366
+ g.oy = 0;
367
+ g.gw = Math.max(w, 1);
368
+ g.gh = Math.max(h, 1);
369
+ }
370
+ return g;
371
+ }
372
+
373
+ private long epochMs() {
374
+ Geometry g = geometry(host.getWidth(), host.getHeight());
375
+ return g.group != null ? g.group.epochMs : SkeletonEpoch.GLOBAL_MS;
376
+ }
377
+
378
+ /**
379
+ * Positions the highlight view. In the mirrored, unskewed group space u the
380
+ * pattern repeats every {@code period}; the drawable's left edge sits at
381
+ * {@code left} there. A drawable point (x, y) in the 300 x 2 px view must
382
+ * land at host x = s * (left + tan * (oy - gh / 2) + k * x + tan * (h / 2) * y) + c - ox,
383
+ * host y = (h / 2) * y, where k = period / 100, s = -1 for rtl (c = gw) and
384
+ * s = 1 otherwise (c = 0). View scale/translation give the linear part and
385
+ * a canvas skew inside the view gives the y-dependent shift. Setters are
386
+ * no-ops when values do not change, so this never causes a redraw loop.
387
+ */
388
+ void layoutHighlight() {
389
+ int w = host.getWidth();
390
+ int h = host.getHeight();
391
+ if (avd == null || mode == MODE_STATIC || w <= 0 || h <= 0) {
392
+ highlight.setVisibility(View.INVISIBLE);
393
+ return;
394
+ }
395
+ Geometry g = geometry(w, h);
396
+ boolean pulse = mode == MODE_PULSE || mode == MODE_GENTLE_PULSE;
397
+ float tan = pulse ? 0f : (float) Math.tan(Math.toRadians(angle));
398
+ float period = pulse
399
+ ? PULSE_PERIOD_FACTOR * g.gw
400
+ : Math.max(1f, Math.max(SHIMMER_PERIOD_FACTOR * g.gw, SHIMMER_PERIOD_FACTOR * Math.abs(tan) * g.gh));
401
+ float oxm = rtl ? g.gw - g.ox - w : g.ox;
402
+ // Left edge congruent to the phase shift (mod period), 0.5-1.5 periods left of the host.
403
+ float shift = phaseFraction * period;
404
+ double kp = Math.ceil((oxm - 1.5f * period - shift) / period);
405
+ float left = (float) (shift + kp * period);
406
+
407
+ float k = period / SkeletonHighlightView.PERIOD_PX;
408
+ float sy = h / (float) SkeletonHighlightView.HEIGHT_PX;
409
+ float s = rtl ? -1f : 1f;
410
+ float c = rtl ? g.gw : 0f;
411
+ highlight.setScaleX(s * k);
412
+ highlight.setScaleY(sy);
413
+ highlight.setTranslationX(s * (left + tan * (g.oy - g.gh / 2f)) + c - g.ox);
414
+ highlight.setTranslationY(0f);
415
+ highlight.setSkewX(tan * sy / k);
416
+ highlight.setVisibility(View.VISIBLE);
417
+ }
418
+ }
@@ -0,0 +1,123 @@
1
+ // Generated by scripts/generate-android-res.mjs. Do not edit.
2
+ package com.skeletonnative;
3
+
4
+ final class SkeletonResources {
5
+ static final int MIN_MS = 300;
6
+ static final int MAX_MS = 5000;
7
+ static final int STEP_MS = 100;
8
+
9
+ static final int[] SHIMMER = {
10
+ R.drawable.skeleton_native_shimmer_300,
11
+ R.drawable.skeleton_native_shimmer_400,
12
+ R.drawable.skeleton_native_shimmer_500,
13
+ R.drawable.skeleton_native_shimmer_600,
14
+ R.drawable.skeleton_native_shimmer_700,
15
+ R.drawable.skeleton_native_shimmer_800,
16
+ R.drawable.skeleton_native_shimmer_900,
17
+ R.drawable.skeleton_native_shimmer_1000,
18
+ R.drawable.skeleton_native_shimmer_1100,
19
+ R.drawable.skeleton_native_shimmer_1200,
20
+ R.drawable.skeleton_native_shimmer_1300,
21
+ R.drawable.skeleton_native_shimmer_1400,
22
+ R.drawable.skeleton_native_shimmer_1500,
23
+ R.drawable.skeleton_native_shimmer_1600,
24
+ R.drawable.skeleton_native_shimmer_1700,
25
+ R.drawable.skeleton_native_shimmer_1800,
26
+ R.drawable.skeleton_native_shimmer_1900,
27
+ R.drawable.skeleton_native_shimmer_2000,
28
+ R.drawable.skeleton_native_shimmer_2100,
29
+ R.drawable.skeleton_native_shimmer_2200,
30
+ R.drawable.skeleton_native_shimmer_2300,
31
+ R.drawable.skeleton_native_shimmer_2400,
32
+ R.drawable.skeleton_native_shimmer_2500,
33
+ R.drawable.skeleton_native_shimmer_2600,
34
+ R.drawable.skeleton_native_shimmer_2700,
35
+ R.drawable.skeleton_native_shimmer_2800,
36
+ R.drawable.skeleton_native_shimmer_2900,
37
+ R.drawable.skeleton_native_shimmer_3000,
38
+ R.drawable.skeleton_native_shimmer_3100,
39
+ R.drawable.skeleton_native_shimmer_3200,
40
+ R.drawable.skeleton_native_shimmer_3300,
41
+ R.drawable.skeleton_native_shimmer_3400,
42
+ R.drawable.skeleton_native_shimmer_3500,
43
+ R.drawable.skeleton_native_shimmer_3600,
44
+ R.drawable.skeleton_native_shimmer_3700,
45
+ R.drawable.skeleton_native_shimmer_3800,
46
+ R.drawable.skeleton_native_shimmer_3900,
47
+ R.drawable.skeleton_native_shimmer_4000,
48
+ R.drawable.skeleton_native_shimmer_4100,
49
+ R.drawable.skeleton_native_shimmer_4200,
50
+ R.drawable.skeleton_native_shimmer_4300,
51
+ R.drawable.skeleton_native_shimmer_4400,
52
+ R.drawable.skeleton_native_shimmer_4500,
53
+ R.drawable.skeleton_native_shimmer_4600,
54
+ R.drawable.skeleton_native_shimmer_4700,
55
+ R.drawable.skeleton_native_shimmer_4800,
56
+ R.drawable.skeleton_native_shimmer_4900,
57
+ R.drawable.skeleton_native_shimmer_5000,
58
+ };
59
+
60
+ static final int[] PULSE = {
61
+ R.drawable.skeleton_native_pulse_300,
62
+ R.drawable.skeleton_native_pulse_400,
63
+ R.drawable.skeleton_native_pulse_500,
64
+ R.drawable.skeleton_native_pulse_600,
65
+ R.drawable.skeleton_native_pulse_700,
66
+ R.drawable.skeleton_native_pulse_800,
67
+ R.drawable.skeleton_native_pulse_900,
68
+ R.drawable.skeleton_native_pulse_1000,
69
+ R.drawable.skeleton_native_pulse_1100,
70
+ R.drawable.skeleton_native_pulse_1200,
71
+ R.drawable.skeleton_native_pulse_1300,
72
+ R.drawable.skeleton_native_pulse_1400,
73
+ R.drawable.skeleton_native_pulse_1500,
74
+ R.drawable.skeleton_native_pulse_1600,
75
+ R.drawable.skeleton_native_pulse_1700,
76
+ R.drawable.skeleton_native_pulse_1800,
77
+ R.drawable.skeleton_native_pulse_1900,
78
+ R.drawable.skeleton_native_pulse_2000,
79
+ R.drawable.skeleton_native_pulse_2100,
80
+ R.drawable.skeleton_native_pulse_2200,
81
+ R.drawable.skeleton_native_pulse_2300,
82
+ R.drawable.skeleton_native_pulse_2400,
83
+ R.drawable.skeleton_native_pulse_2500,
84
+ R.drawable.skeleton_native_pulse_2600,
85
+ R.drawable.skeleton_native_pulse_2700,
86
+ R.drawable.skeleton_native_pulse_2800,
87
+ R.drawable.skeleton_native_pulse_2900,
88
+ R.drawable.skeleton_native_pulse_3000,
89
+ R.drawable.skeleton_native_pulse_3100,
90
+ R.drawable.skeleton_native_pulse_3200,
91
+ R.drawable.skeleton_native_pulse_3300,
92
+ R.drawable.skeleton_native_pulse_3400,
93
+ R.drawable.skeleton_native_pulse_3500,
94
+ R.drawable.skeleton_native_pulse_3600,
95
+ R.drawable.skeleton_native_pulse_3700,
96
+ R.drawable.skeleton_native_pulse_3800,
97
+ R.drawable.skeleton_native_pulse_3900,
98
+ R.drawable.skeleton_native_pulse_4000,
99
+ R.drawable.skeleton_native_pulse_4100,
100
+ R.drawable.skeleton_native_pulse_4200,
101
+ R.drawable.skeleton_native_pulse_4300,
102
+ R.drawable.skeleton_native_pulse_4400,
103
+ R.drawable.skeleton_native_pulse_4500,
104
+ R.drawable.skeleton_native_pulse_4600,
105
+ R.drawable.skeleton_native_pulse_4700,
106
+ R.drawable.skeleton_native_pulse_4800,
107
+ R.drawable.skeleton_native_pulse_4900,
108
+ R.drawable.skeleton_native_pulse_5000,
109
+ };
110
+
111
+ private SkeletonResources() {}
112
+
113
+ /** The supported duration closest to {@code ms}. */
114
+ static int quantize(int ms) {
115
+ int clamped = Math.max(MIN_MS, Math.min(MAX_MS, ms));
116
+ return MIN_MS + Math.round((clamped - MIN_MS) / (float) STEP_MS) * STEP_MS;
117
+ }
118
+
119
+ static int drawable(boolean pulse, int ms) {
120
+ int index = (quantize(ms) - MIN_MS) / STEP_MS;
121
+ return pulse ? PULSE[index] : SHIMMER[index];
122
+ }
123
+ }
@@ -0,0 +1,72 @@
1
+ package com.skeletonnative;
2
+
3
+ import androidx.annotation.NonNull;
4
+ import androidx.annotation.Nullable;
5
+ import com.facebook.react.module.annotations.ReactModule;
6
+ import com.facebook.react.uimanager.ThemedReactContext;
7
+ import com.facebook.react.uimanager.annotations.ReactProp;
8
+
9
+ @ReactModule(name = SkeletonShimmerManager.NAME)
10
+ public class SkeletonShimmerManager extends SkeletonShimmerManagerSpec<SkeletonShimmerView> {
11
+ public static final String NAME = "SkeletonNativeShimmer";
12
+
13
+ @NonNull
14
+ @Override
15
+ public String getName() {
16
+ return NAME;
17
+ }
18
+
19
+ @NonNull
20
+ @Override
21
+ protected SkeletonShimmerView createViewInstance(@NonNull ThemedReactContext context) {
22
+ return new SkeletonShimmerView(context);
23
+ }
24
+
25
+ @Override
26
+ protected void onAfterUpdateTransaction(@NonNull SkeletonShimmerView view) {
27
+ super.onAfterUpdateTransaction(view);
28
+ view.painter.commit();
29
+ }
30
+
31
+ @Override
32
+ @ReactProp(name = "highlightColor", customType = "Color")
33
+ public void setHighlightColor(SkeletonShimmerView view, @Nullable Integer value) {
34
+ view.painter.setHighlightColor(value);
35
+ }
36
+
37
+ @Override
38
+ @ReactProp(name = "duration", defaultInt = 1400)
39
+ public void setDuration(SkeletonShimmerView view, int value) {
40
+ view.painter.setDuration(value);
41
+ }
42
+
43
+ @Override
44
+ @ReactProp(name = "direction")
45
+ public void setDirection(SkeletonShimmerView view, @Nullable String value) {
46
+ view.painter.setDirection(value);
47
+ }
48
+
49
+ @Override
50
+ @ReactProp(name = "angle", defaultFloat = 20f)
51
+ public void setAngle(SkeletonShimmerView view, float value) {
52
+ view.painter.setAngle(value);
53
+ }
54
+
55
+ @Override
56
+ @ReactProp(name = "variant")
57
+ public void setVariant(SkeletonShimmerView view, @Nullable String value) {
58
+ view.painter.setVariant(value);
59
+ }
60
+
61
+ @Override
62
+ @ReactProp(name = "animated", defaultBoolean = true)
63
+ public void setAnimated(SkeletonShimmerView view, boolean value) {
64
+ view.painter.setAnimated(value);
65
+ }
66
+
67
+ @Override
68
+ @ReactProp(name = "reduceMotion")
69
+ public void setReduceMotion(SkeletonShimmerView view, @Nullable String value) {
70
+ view.painter.setReduceMotion(value);
71
+ }
72
+ }
@@ -0,0 +1,74 @@
1
+ package com.skeletonnative;
2
+
3
+ import android.content.Context;
4
+ import android.content.res.Configuration;
5
+ import android.graphics.Canvas;
6
+ import android.graphics.Paint;
7
+ import android.graphics.PorterDuff;
8
+ import android.graphics.PorterDuffXfermode;
9
+ import androidx.annotation.NonNull;
10
+ import com.facebook.react.views.view.ReactViewGroup;
11
+
12
+ /**
13
+ * Draws its children, then the highlight (a view in this view's overlay)
14
+ * composited with SRC_ATOP, so the highlight only appears on pixels the
15
+ * children drew. Both are inside an offscreen layer that RenderThread replays
16
+ * on every animation frame, so the masking follows the children live.
17
+ */
18
+ public class SkeletonShimmerView extends ReactViewGroup {
19
+ final SkeletonPainter painter = new SkeletonPainter(this);
20
+ private final Paint atop = new Paint();
21
+
22
+ public SkeletonShimmerView(Context context) {
23
+ super(context);
24
+ atop.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
25
+ // The highlight view composites onto the children with SRC_ATOP through its layer paint.
26
+ painter.highlightView().setLayerType(LAYER_TYPE_HARDWARE, atop);
27
+ setWillNotDraw(false);
28
+ }
29
+
30
+ @Override
31
+ protected void dispatchDraw(@NonNull Canvas canvas) {
32
+ int w = getWidth();
33
+ int h = getHeight();
34
+ if (!painter.isAnimating() || w <= 0 || h <= 0) {
35
+ super.dispatchDraw(canvas);
36
+ return;
37
+ }
38
+ // Children, then the overlay (the highlight), inside one offscreen layer so
39
+ // that SRC_ATOP only sees the children's pixels.
40
+ int layer = canvas.saveLayer(0, 0, w, h, null);
41
+ super.dispatchDraw(canvas);
42
+ canvas.restoreToCount(layer);
43
+ }
44
+
45
+ @Override
46
+ protected void onAttachedToWindow() {
47
+ super.onAttachedToWindow();
48
+ painter.onAttached();
49
+ }
50
+
51
+ @Override
52
+ protected void onDetachedFromWindow() {
53
+ painter.onDetached();
54
+ super.onDetachedFromWindow();
55
+ }
56
+
57
+ @Override
58
+ public void onVisibilityAggregated(boolean isVisible) {
59
+ super.onVisibilityAggregated(isVisible);
60
+ painter.onVisibilityAggregated(isVisible);
61
+ }
62
+
63
+ @Override
64
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
65
+ super.onSizeChanged(w, h, oldw, oldh);
66
+ painter.onSizeChanged();
67
+ }
68
+
69
+ @Override
70
+ protected void onConfigurationChanged(Configuration newConfig) {
71
+ super.onConfigurationChanged(newConfig);
72
+ painter.onConfigurationChanged();
73
+ }
74
+ }