Skip to content

Commit 054e110

Browse files
committed
refactor: rename list property methods to match rive-react API
1 parent 92f3165 commit 054e110

12 files changed

Lines changed: 110 additions & 99 deletions

android/src/main/java/com/margelo/nitro/rive/HybridViewModelListProperty.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,36 @@ class HybridViewModelListProperty(private val listProperty: ViewModelListPropert
1818
?: throw IllegalArgumentException("Expected HybridViewModelInstance but got ${instance::class.simpleName}")
1919
}
2020

21-
override fun instanceAt(index: Double): HybridViewModelInstanceSpec? {
21+
override fun getInstanceAt(index: Double): HybridViewModelInstanceSpec? {
2222
val idx = index.toInt()
2323
if (idx < 0 || idx >= listProperty.size) return null
2424
return HybridViewModelInstance(listProperty.elementAt(idx))
2525
}
2626

27-
override fun append(instance: HybridViewModelInstanceSpec) {
27+
override fun addInstance(instance: HybridViewModelInstanceSpec) {
2828
val hybridInstance = requireHybridInstance(instance)
2929
listProperty.add(hybridInstance.viewModelInstance)
3030
}
3131

32-
override fun insert(instance: HybridViewModelInstanceSpec, index: Double) {
32+
override fun addInstanceAt(instance: HybridViewModelInstanceSpec, index: Double): Boolean {
3333
val hybridInstance = requireHybridInstance(instance)
34-
listProperty.add(index.toInt(), hybridInstance.viewModelInstance)
34+
val idx = index.toInt()
35+
if (idx < 0 || idx > listProperty.size) return false
36+
listProperty.add(idx, hybridInstance.viewModelInstance)
37+
return true
3538
}
3639

37-
override fun remove(instance: HybridViewModelInstanceSpec) {
40+
override fun removeInstance(instance: HybridViewModelInstanceSpec) {
3841
val hybridInstance = requireHybridInstance(instance)
3942
listProperty.remove(hybridInstance.viewModelInstance)
4043
}
4144

42-
override fun removeAt(index: Double) {
45+
override fun removeInstanceAt(index: Double) {
4346
listProperty.removeAt(index.toInt())
4447
}
4548

46-
override fun swap(index1: Double, index2: Double) {
47-
listProperty.swap(index1.toInt(), index2.toInt())
49+
override fun swap(index1: Double, index2: Double): Boolean {
50+
return listProperty.swap(index1.toInt(), index2.toInt())
4851
}
4952

5053
override fun addListener(onChanged: () -> Unit) {

example/src/pages/DataBindingListExample.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,21 @@ function ListExample({
9191
if (stringProp) {
9292
stringProp.value = 'new btn';
9393
}
94-
listProperty.append(newInstance);
94+
listProperty.addInstance(newInstance);
9595
riveRef.current?.playIfNeeded();
9696
refreshLength();
9797
}, [listProperty, file, refreshLength]);
9898

9999
const handleRemoveFirst = useCallback(() => {
100100
if (!listProperty || listProperty.length === 0) return;
101-
listProperty.removeAt(0);
101+
listProperty.removeInstanceAt(0);
102102
riveRef.current?.playIfNeeded();
103103
refreshLength();
104104
}, [listProperty, refreshLength]);
105105

106106
const handleRemoveLast = useCallback(() => {
107107
if (!listProperty || listProperty.length === 0) return;
108-
listProperty.removeAt(listProperty.length - 1);
108+
listProperty.removeInstanceAt(listProperty.length - 1);
109109
riveRef.current?.playIfNeeded();
110110
refreshLength();
111111
}, [listProperty, refreshLength]);
@@ -121,7 +121,7 @@ function ListExample({
121121
if (!listProperty) return;
122122
console.log(`List has ${listProperty.length} items:`);
123123
for (let i = 0; i < listProperty.length; i++) {
124-
const item = listProperty.instanceAt(i);
124+
const item = listProperty.getInstanceAt(i);
125125
console.log(` [${i}]: ${item?.instanceName ?? 'undefined'}`);
126126
}
127127
}, [listProperty]);

ios/HybridViewModelListProperty.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class HybridViewModelListProperty: HybridViewModelListPropertySpec, ValuedProper
1313
Double(property.count)
1414
}
1515

16-
func instanceAt(index: Double) throws -> (any HybridViewModelInstanceSpec)? {
16+
func getInstanceAt(index: Double) throws -> (any HybridViewModelInstanceSpec)? {
1717
guard let instance = property.instance(at: Int32(index)) else { return nil }
1818
return HybridViewModelInstance(viewModelInstance: instance)
1919
}
@@ -27,27 +27,27 @@ class HybridViewModelListProperty: HybridViewModelListPropertySpec, ValuedProper
2727
return viewModelInstance
2828
}
2929

30-
func append(instance: any HybridViewModelInstanceSpec) throws {
30+
func addInstance(instance: any HybridViewModelInstanceSpec) throws {
3131
let viewModelInstance = try requireViewModelInstance(instance)
3232
property.append(viewModelInstance)
3333
}
3434

35-
func insert(instance: any HybridViewModelInstanceSpec, index: Double) throws {
35+
func addInstanceAt(instance: any HybridViewModelInstanceSpec, index: Double) throws -> Bool {
3636
let viewModelInstance = try requireViewModelInstance(instance)
37-
_ = property.insert(viewModelInstance, at: Int32(index))
37+
return property.insert(viewModelInstance, at: Int32(index))
3838
}
3939

40-
func remove(instance: any HybridViewModelInstanceSpec) throws {
40+
func removeInstance(instance: any HybridViewModelInstanceSpec) throws {
4141
let viewModelInstance = try requireViewModelInstance(instance)
4242
property.remove(viewModelInstance)
4343
}
4444

45-
func removeAt(index: Double) throws {
45+
func removeInstanceAt(index: Double) throws {
4646
property.remove(at: Int32(index))
4747
}
4848

49-
func swap(index1: Double, index2: Double) throws {
50-
property.swap(at: UInt32(index1), with: UInt32(index2))
49+
func swap(index1: Double, index2: Double) throws -> Bool {
50+
return property.swap(at: UInt32(index1), with: UInt32(index2))
5151
}
5252

5353
func addListener(onChanged: @escaping () -> Void) throws {

nitrogen/generated/android/c++/JHybridViewModelListPropertySpec.cpp

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JHybridViewModelListPropertySpec.hpp

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridViewModelListPropertySpec.kt

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/ios/c++/HybridViewModelListPropertySpecSwift.hpp

Lines changed: 15 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/ios/swift/HybridViewModelListPropertySpec.swift

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)