1- import UIKit
2- import RiveRuntime
31import NitroModules
2+ import RiveRuntime
3+ import UIKit
44
55protocol RiveViewSource : AnyObject {
66 func registerView( _ view: RiveReactNativeView )
@@ -38,10 +38,10 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
3838
3939 // MARK: Public Config Properties
4040 var autoPlay : Bool = true
41-
41+
4242 // MARK: - Public Methods
43-
44- func awaitViewReady( ) async -> Bool {
43+
44+ func awaitViewReady( ) async -> Bool {
4545 if !isViewReady {
4646 await withCheckedContinuation { continuation in
4747 viewReadyContinuation = continuation
@@ -50,7 +50,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
5050 }
5151 return true
5252 }
53-
53+
5454 func configure( _ config: ViewConfiguration , reload: Bool = false ) {
5555 if reload {
5656 cleanup ( )
@@ -77,7 +77,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
7777
7878 applyDataBinding ( config. bindData, refresh: false )
7979 }
80-
80+
8181 func bindViewModelInstance( viewModelInstance: RiveDataBindingViewModel . Instance ) {
8282 baseViewModel? . riveModel? . stateMachine? . bind ( viewModelInstance: viewModelInstance)
8383 }
@@ -93,36 +93,37 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
9393 switch bindData {
9494 case . none:
9595 baseViewModel? . riveModel? . disableAutoBind ( )
96-
9796
9897 case . auto:
99- baseViewModel? . riveModel? . enableAutoBind { [ weak self ] instance in
98+ baseViewModel? . riveModel? . enableAutoBind { instance in
10099 // Auto-bind callback
101100 }
102101
103102 case . byName( let name) :
104103 guard let artboard = artboard,
105- let riveFile = baseViewModel? . riveModel? . riveFile,
106- let viewModel = riveFile. defaultViewModel ( for: artboard) ,
107- let instance = viewModel. createInstance ( fromName: name) else {
104+ let riveFile = baseViewModel? . riveModel? . riveFile,
105+ let viewModel = riveFile. defaultViewModel ( for: artboard) ,
106+ let instance = viewModel. createInstance ( fromName: name)
107+ else {
108108 return
109109 }
110110 stateMachine? . bind ( viewModelInstance: instance)
111- artboard. bind ( viewModelInstance: instance)
111+ // this should be added if we support only playing artboards on their own - https://github.com/rive-app/rive-nitro-react-native/pull/23#discussion_r2534698281
112+ // artboard.bind(viewModelInstance: instance)
112113
113114 case . instance( let instance) :
114115 stateMachine? . bind ( viewModelInstance: instance)
115116 artboard? . bind ( viewModelInstance: instance)
116117 }
117- if ( refresh) {
118+ if refresh {
118119 baseViewModel? . play ( )
119120 }
120121 }
121122
122123 func play( ) {
123124 baseViewModel? . play ( )
124125 }
125-
126+
126127 func pause( ) {
127128 baseViewModel? . pause ( )
128129 }
@@ -136,74 +137,76 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
136137 func addEventListener( _ onEvent: @escaping ( UnifiedRiveEvent ) -> Void ) {
137138 eventListeners. append ( onEvent)
138139 }
139-
140+
140141 func removeEventListeners( ) {
141142 eventListeners. removeAll ( )
142143 }
143-
144+
144145 func setNumberInputValue( name: String , value: Float , path: String ? ) throws {
145146 try handleInput ( name: name, path: path, type: . number) { ( input: RiveRuntime . RiveSMINumber ) in
146147 input. setValue ( value)
147148 }
148149 }
149-
150+
150151 func getNumberInputValue( name: String , path: String ? ) throws -> Float {
151152 try handleInput ( name: name, path: path, type: . number) { ( input: RiveRuntime . RiveSMINumber ) in
152153 input. value ( )
153154 }
154155 }
155-
156+
156157 func setBooleanInputValue( name: String , value: Bool , path: String ? ) throws {
157158 try handleInput ( name: name, path: path, type: . boolean) { ( input: RiveRuntime . RiveSMIBool ) in
158159 input. setValue ( value)
159160 }
160161 }
161-
162+
162163 func getBooleanInputValue( name: String , path: String ? ) throws -> Bool {
163164 try handleInput ( name: name, path: path, type: . boolean) { ( input: RiveRuntime . RiveSMIBool ) in
164165 input. value ( )
165166 }
166167 }
167-
168+
168169 func triggerInput( name: String , path: String ? ) throws {
169170 try handleInput ( name: name, path: path, type: . trigger) { ( input: RiveRuntime . RiveSMITrigger ) in
170171 input. fire ( )
171172 }
172173 }
173-
174+
174175 func setTextRunValue( name: String , value: String , path: String ? ) throws {
175176 let textRun = try textRunOptionPath ( name: name, path: path)
176177 textRun. setText ( value)
177178 }
178-
179+
179180 func getTextRunValue( name: String , path: String ? ) throws -> String {
180181 let textRun = try textRunOptionPath ( name: name, path: path)
181182 return textRun. text ( )
182183 }
183-
184- private func textRunOptionPath( name: String , path: String ? ) throws -> RiveRuntime . RiveTextValueRun {
184+
185+ private func textRunOptionPath( name: String , path: String ? ) throws -> RiveRuntime . RiveTextValueRun
186+ {
185187 let textRun : RiveRuntime . RiveTextValueRun ?
186188 if let path = path {
187189 textRun = baseViewModel? . riveModel? . artboard? . textRun ( name, path: path)
188190 } else {
189191 textRun = baseViewModel? . riveModel? . artboard? . textRun ( name)
190192 }
191-
193+
192194 guard let textRun = textRun else {
193- throw RuntimeError . error ( withMessage: " Could not find text run ` \( name) ` \( path. map { " at path ` \( $0) ` " } ?? " " ) " )
195+ throw RuntimeError . error (
196+ withMessage: " Could not find text run ` \( name) ` \( path. map { " at path ` \( $0) ` " } ?? " " ) " )
194197 }
195-
198+
196199 return textRun
197200 }
198-
201+
199202 // MARK: - Internal
200203 deinit {
201204 cleanup ( )
202205 }
203-
206+
204207 private func createViewFromViewModel( ) {
205208 riveView = baseViewModel? . createRiveView ( )
206-
209+
207210 if let riveView = riveView {
208211 riveView. translatesAutoresizingMaskIntoConstraints = false
209212 addSubview ( riveView)
@@ -212,11 +215,11 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
212215 riveView. leadingAnchor. constraint ( equalTo: leadingAnchor) ,
213216 riveView. trailingAnchor. constraint ( equalTo: trailingAnchor) ,
214217 riveView. topAnchor. constraint ( equalTo: topAnchor) ,
215- riveView. bottomAnchor. constraint ( equalTo: bottomAnchor)
218+ riveView. bottomAnchor. constraint ( equalTo: bottomAnchor) ,
216219 ] )
217220 }
218221 }
219-
222+
220223 private func cleanup( ) {
221224 riveView? . removeFromSuperview ( )
222225 riveView? . stateMachineDelegate = nil
@@ -227,27 +230,30 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
227230 self . viewSource = nil
228231 }
229232 }
230-
233+
231234 @objc func onRiveEventReceived( onRiveEvent riveEvent: RiveRuntime . RiveEvent ) {
232235 let eventType = UnifiedRiveEvent (
233236 name: riveEvent. name ( ) ,
234- type: riveEvent is RiveRuntime . RiveOpenUrlEvent ? RiveEventType . openurl : RiveEventType . general,
237+ type: riveEvent is RiveRuntime . RiveOpenUrlEvent
238+ ? RiveEventType . openurl : RiveEventType . general,
235239 delay: Double ( riveEvent. delay ( ) ) ,
236240 properties: convertEventProperties ( riveEvent. properties ( ) ) ,
237241 url: ( riveEvent as? RiveRuntime . RiveOpenUrlEvent) ? . url ( ) ,
238242 target: ( riveEvent as? RiveRuntime . RiveOpenUrlEvent) ? . target ( )
239243 )
240-
244+
241245 for listener in eventListeners {
242246 listener ( eventType)
243247 }
244248 }
245-
246- private func convertEventProperties( _ properties: Dictionary < String , Any > ? ) -> Dictionary < String , EventPropertiesOutput > ? {
249+
250+ private func convertEventProperties( _ properties: [ String : Any ] ? ) -> [ String :
251+ EventPropertiesOutput ] ?
252+ {
247253 guard let properties = properties else { return nil }
248-
249- var newMap : Dictionary < String , EventPropertiesOutput > = [ : ]
250-
254+
255+ var newMap : [ String : EventPropertiesOutput ] = [ : ]
256+
251257 for (key, value) in properties {
252258 if let string = value as? String {
253259 newMap [ key] = . string( string)
@@ -257,17 +263,19 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
257263 newMap [ key] = . boolean( boolean)
258264 }
259265 }
260-
266+
261267 return newMap
262268 }
263-
269+
264270 private enum InputType {
265271 case number
266272 case boolean
267273 case trigger
268274 }
269-
270- private func handleInput< P: RiveRuntime . RiveSMIInput , T> ( name: String , path: String ? , type: InputType , onSuccess: ( P ) throws -> T ) throws -> T {
275+
276+ private func handleInput< P: RiveRuntime . RiveSMIInput , T> (
277+ name: String , path: String ? , type: InputType , onSuccess: ( P ) throws -> T
278+ ) throws -> T {
271279 let input : P ?
272280 if let path = path {
273281 switch type {
@@ -288,11 +296,12 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
288296 input = baseViewModel? . riveModel? . stateMachine? . getTrigger ( name) as? P
289297 }
290298 }
291-
299+
292300 guard let input = input else {
293- throw RuntimeError . error ( withMessage: " Could not find input ` \( name) ` \( path. map { " at path ` \( $0) ` " } ?? " " ) " )
301+ throw RuntimeError . error (
302+ withMessage: " Could not find input ` \( name) ` \( path. map { " at path ` \( $0) ` " } ?? " " ) " )
294303 }
295-
304+
296305 return try onSuccess ( input)
297306 }
298307}
0 commit comments