Skip to content

Commit e481329

Browse files
committed
style: update styling and formatting
1 parent 8bf794c commit e481329

6 files changed

Lines changed: 53 additions & 100 deletions

File tree

android/src/main/java/com/rivereactnative/RiveReactNativeView.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,7 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
406406

407407
fun setColorPropertyValue(path: String, r: Int, g: Int, b: Int, a: Int) {
408408
try {
409-
val color = Color.argb(
410-
a, r, g, b
411-
)
409+
val color = Color.argb(a, r, g, b)
412410
getViewModelInstance()?.getColorProperty(path)?.value = color
413411
} catch (ex: RiveException) {
414412
handleRiveException(ex)
@@ -437,25 +435,25 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
437435
val propertyTypeEnum = RNPropertyType.mapToRNPropertyType(propertyType);
438436

439437
val property = when (propertyTypeEnum) {
440-
RNPropertyType.String -> getViewModelInstance()?.getStringProperty(path)
441-
RNPropertyType.Boolean -> getViewModelInstance()?.getBooleanProperty(path)
442-
RNPropertyType.Number -> getViewModelInstance()?.getNumberProperty(path)
443-
RNPropertyType.Color -> getViewModelInstance()?.getColorProperty(path)
444-
RNPropertyType.Enum -> getViewModelInstance()?.getEnumProperty(path)
445-
RNPropertyType.Trigger -> getViewModelInstance()?.getTriggerProperty(path)
438+
RNPropertyType.String -> getViewModelInstance()?.getStringProperty(path)
439+
RNPropertyType.Boolean -> getViewModelInstance()?.getBooleanProperty(path)
440+
RNPropertyType.Number -> getViewModelInstance()?.getNumberProperty(path)
441+
RNPropertyType.Color -> getViewModelInstance()?.getColorProperty(path)
442+
RNPropertyType.Enum -> getViewModelInstance()?.getEnumProperty(path)
443+
RNPropertyType.Trigger -> getViewModelInstance()?.getTriggerProperty(path)
446444
} ?: return
447445

448446
// This should not be required, as JavaScript does a check to ensure
449447
// event emitters are unique. Adding this as a safety.
450448
propertyListeners[key]?.cancel()
451449

452450
val job = scope.launch {
453-
property.valueFlow.collect { value ->
454-
sendEvent(key, value)
455-
}
451+
property.valueFlow.collect { value ->
452+
sendEvent(key, value)
453+
}
456454
}
457455
propertyListeners[key] = job
458-
}
456+
}
459457

460458
private fun sendEvent(eventName: String, value: Any) {
461459
context
@@ -997,8 +995,8 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
997995
when (this.getType(key)) {
998996
ReadableType.Null -> result[key] = null
999997
ReadableType.Boolean -> result[key] = this.getBoolean(key)
1000-
ReadableType.Number -> result[key] =
1001-
this.getDouble(key) // React Native treats all numbers as Double
998+
// React Native treats all numbers as Double
999+
ReadableType.Number -> result[key] = this.getDouble(key)
10021000
ReadableType.String -> result[key] = this.getString(key)
10031001
ReadableType.Map -> result[key] = this.getMap(key)?.toMap() // Recursively convert
10041002
ReadableType.Array -> result[key] = this.getArray(key)?.toList() // Convert ReadableArray

android/src/main/java/com/rivereactnative/RiveReactNativeViewManager.kt

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
4848
args?.let {
4949
val stateMachineName = it.getString(0)
5050
val inputName = it.getString(1)
51-
view.run {
52-
fireState(stateMachineName, inputName)
53-
}
51+
view.fireState(stateMachineName, inputName)
5452
}
5553
}
5654

@@ -59,9 +57,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
5957
val stateMachineName = it.getString(0)
6058
val inputName = it.getString(1)
6159
val value = it.getBoolean(2)
62-
view.run {
63-
setBooleanState(stateMachineName, inputName, value)
64-
}
60+
view.setBooleanState(stateMachineName, inputName, value)
6561
}
6662
}
6763

@@ -70,19 +66,15 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
7066
val stateMachineName = it.getString(0)
7167
val inputName = it.getString(1)
7268
val value = it.getDouble(2)
73-
view.run {
74-
setNumberState(stateMachineName, inputName, value.toFloat())
75-
}
69+
view.setNumberState(stateMachineName, inputName, value.toFloat())
7670
}
7771
}
7872

7973
"fireStateAtPath" -> {
8074
args?.let {
8175
val inputName = it.getString(0)
8276
val path = it.getString(1)
83-
view.run {
84-
fireStateAtPath(inputName, path)
85-
}
77+
view.fireStateAtPath(inputName, path)
8678
}
8779
}
8880

@@ -91,9 +83,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
9183
val inputName = it.getString(0)
9284
val value = it.getBoolean(1)
9385
val path = it.getString(2)
94-
view.run {
95-
setBooleanStateAtPath(inputName, value, path)
96-
}
86+
view.setBooleanStateAtPath(inputName, value, path)
9787
}
9888
}
9989

@@ -102,9 +92,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
10292
val inputName = it.getString(0)
10393
val value = it.getDouble(1)
10494
val path = it.getString(2)
105-
view.run {
106-
setNumberStateAtPath(inputName, value.toFloat(), path)
107-
}
95+
view.setNumberStateAtPath(inputName, value.toFloat(), path)
10896
}
10997
}
11098

@@ -113,29 +101,23 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
113101
args?.let {
114102
val path = it.getString(0)
115103
val value = it.getBoolean(1)
116-
view.run {
117-
setBooleanPropertyValue(path, value)
118-
}
104+
view.setBooleanPropertyValue(path, value)
119105
}
120106
}
121107

122108
"setStringPropertyValue" -> {
123109
args?.let {
124110
val path = it.getString(0)
125111
val value = it.getString(1)
126-
view.run {
127-
setStringPropertyValue(path, value)
128-
}
112+
view.setStringPropertyValue(path, value)
129113
}
130114
}
131115

132116
"setNumberPropertyValue" -> {
133117
args?.let {
134118
val path = it.getString(0)
135119
val value = it.getDouble(1)
136-
view.run {
137-
setNumberPropertyValue(path, value.toFloat())
138-
}
120+
view.setNumberPropertyValue(path, value.toFloat())
139121
}
140122
}
141123

@@ -146,38 +128,30 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
146128
val g = it.getDouble(2)
147129
val b = it.getDouble(3)
148130
val a = it.getDouble(4)
149-
view.run {
150-
setColorPropertyValue(path, r.toInt(), g.toInt(), b.toInt(), a.toInt())
151-
}
131+
view.setColorPropertyValue(path, r.toInt(), g.toInt(), b.toInt(), a.toInt())
152132
}
153133
}
154134

155135
"setEnumPropertyValue" -> {
156136
args?.let {
157137
val path = it.getString(0)
158138
val value = it.getString(1)
159-
view.run {
160-
setEnumPropertyValue(path, value)
161-
}
139+
view.setEnumPropertyValue(path, value)
162140
}
163141
}
164142

165143
"fireTriggerProperty" -> {
166144
args?.let {
167145
val path = it.getString(0)
168-
view.run {
169-
fireTriggerProperty(path)
170-
}
146+
view.fireTriggerProperty(path)
171147
}
172148
}
173149

174150
"registerPropertyListener" -> {
175151
args?.let {
176152
val path = it.getString(0)
177153
val propertyType = it.getString(1)
178-
view.run {
179-
registerPropertyListener(path, propertyType)
180-
}
154+
view.registerPropertyListener(path, propertyType)
181155
}
182156
}
183157

@@ -187,19 +161,15 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
187161
args?.let {
188162
val x: Double = it.getDouble(0)
189163
val y: Double = it.getDouble(1)
190-
view.run {
191-
this.touchBegan(x.toFloat(), y.toFloat())
192-
}
164+
view.touchBegan(x.toFloat(), y.toFloat())
193165
}
194166
}
195167

196168
"touchEnded" -> {
197169
args?.let {
198170
val x: Double = it.getDouble(0)
199171
val y: Double = it.getDouble(1)
200-
view.run {
201-
this.touchEnded(x.toFloat(), y.toFloat())
202-
}
172+
view.touchEnded(x.toFloat(), y.toFloat())
203173
}
204174
}
205175

@@ -209,9 +179,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
209179
args?.let {
210180
val textRunName: String = it.getString(0)
211181
val textValue: String = it.getString(1)
212-
view.run {
213-
this.setTextRunValue(textRunName, textValue)
214-
}
182+
view.setTextRunValue(textRunName, textValue)
215183
}
216184
}
217185

@@ -220,9 +188,7 @@ class RiveReactNativeViewManager : SimpleViewManager<RiveReactNativeView>() {
220188
val textRunName: String = it.getString(0)
221189
val textValue: String = it.getString(1)
222190
val path: String = it.getString(2)
223-
view.run {
224-
this.setTextRunValueAtPath(textRunName, textValue, path)
225-
}
191+
view.setTextRunValueAtPath(textRunName, textValue, path)
226192
}
227193
}
228194

example/app/(examples)/DataBinding.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export default function DataBinding() {
4545
// SET COLOR VALUE
4646
riveRef.current?.setColorPropertyValue('Energy_Bar/Bar_Color', {
4747
r: 0,
48-
g: 255,
49-
b: 0,
48+
g: 0,
49+
b: 255,
5050
a: 255,
5151
});
5252
// Or set the color using a hex string

ios/RiveReactNativeView.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
268268
}
269269

270270
if hasChanged && viewModel?.isPlaying == false {
271-
// riveView?.advance(delta: 0);
272271
viewModel?.play() // manually calling play to force an update, ideally want to do a single advance
273272
}
274273
}
@@ -526,10 +525,6 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
526525
}
527526

528527
func setColorPropertyValue(path: String, r: Int, g: Int, b: Int, a: Int) {
529-
debugPrint(r)
530-
debugPrint(g)
531-
debugPrint(b)
532-
debugPrint(a)
533528
dataBindingViewModelInstance?.colorProperty(fromPath: path)?.value = UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
534529
}
535530

src/Rive.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ const RiveContainer = React.forwardRef<RiveRef, Props>(
770770

771771
const setColorPropertyValue = useCallback<RiveRef['setColorPropertyValue']>(
772772
(path: string, color: RiveRGBA | string) => {
773-
let parsedColor = parseColor(color);
773+
let parsedColor = typeof color === 'string' ? parseColor(color) : color;
774774
UIManager.dispatchViewManagerCommand(
775775
findNodeHandle(riveRef.current),
776776
ViewManagerMethod.setColorPropertyValue,

src/utils.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,31 @@ function parsePossibleSources(source: RiveAssetPropType): FileAssetSource {
3737
throw new Error('Invalid source provided.');
3838
}
3939

40-
function parseColor(color: RiveRGBA | string): RiveRGBA {
41-
if (typeof color === 'string') {
42-
const hex = color.replace(/^#/, '');
40+
function parseColor(color: string): RiveRGBA {
41+
const hex = color.replace(/^#/, '');
4342

44-
let r = 0,
45-
g = 0,
46-
b = 0,
47-
a = 255;
43+
let r = 0,
44+
g = 0,
45+
b = 0,
46+
a = 255;
4847

49-
if (hex.length === 6) {
50-
// Format: RRGGBB
51-
r = parseInt(hex.slice(0, 2), 16);
52-
g = parseInt(hex.slice(2, 4), 16);
53-
b = parseInt(hex.slice(4, 6), 16);
54-
} else if (hex.length === 8) {
55-
// Format: RRGGBBAA
56-
r = parseInt(hex.slice(0, 2), 16);
57-
g = parseInt(hex.slice(2, 4), 16);
58-
b = parseInt(hex.slice(4, 6), 16);
59-
a = parseInt(hex.slice(6, 8), 16);
60-
} else {
61-
console.warn(`Invalid hex color: ${color}`);
62-
}
63-
64-
return { r, g, b, a };
48+
if (hex.length === 6) {
49+
// Format: RRGGBB
50+
r = parseInt(hex.slice(0, 2), 16);
51+
g = parseInt(hex.slice(2, 4), 16);
52+
b = parseInt(hex.slice(4, 6), 16);
53+
} else if (hex.length === 8) {
54+
// Format: RRGGBBAA
55+
r = parseInt(hex.slice(0, 2), 16);
56+
g = parseInt(hex.slice(2, 4), 16);
57+
b = parseInt(hex.slice(4, 6), 16);
58+
a = parseInt(hex.slice(6, 8), 16);
59+
} else {
60+
console.warn(`Invalid hex color: ${color}`);
6561
}
6662

67-
// Already a RiveRGBA object
68-
return color;
63+
return { r, g, b, a };
6964
}
70-
7165
export { parsePossibleSources, parseColor };
7266

7367
export const getPropertyTypeString = (propertyType: PropertyType): string => {

0 commit comments

Comments
 (0)