rive-app/rive-ios

unable to set filename via variable and have setinput also work

Opened this issue · 0 comments

Description

I want to set the filename of my rive file via a variable, let's say filename: "\(person.name)"
I have found a way of doing this, via a computed var, however setInputs does not work with my solution.

Provide a Repro

import SwiftUI
import RiveRuntime

struct Person {
    let id = UUID()
    var name: String
    var shoes: String
}

struct ContentView: View {
    
    let people = [
        Person(name: "Mick", shoes: "black"),
        Person(name: "Keith", shoes: "blue"),
        Person(name: "Ronny", shoes: "brown")
    ]
    
    var pages = [1, 2, 3, 4, 5]
    
    var body: some View {
        
        TabView {
            NavigationStack {
                TabView {
                    ForEach(people, id: \.id) { person in
                        NavigationLink(destination: DetailView(person: person)){
                            Text("\(person.name)")
                        }
                    }
                }
                .navigationTitle("Stones")
                .tabViewStyle(.page)
            }
            .tabItem {
                Label("Menu", systemImage: "list.dash")
            }
            NavigationStack {
                Text("Tab Two")
                    .navigationTitle("Stones")
            }.tabItem {
                Label("Order", systemImage: "square.and.pencil")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

struct DetailView: View {
    let mick_animation = RiveViewModel(fileName: "Mick")
    var person: Person
    @State var howHigh: Double = 30.0

    // Computed property, taking filename from ViewModel above
    var mick2_animation: RiveViewModel {
        // Initialise the animation, using name
        RiveViewModel(fileName: "\(person.name)")
    }

    var body: some View {
        VStack{
            HStack {
                Text("Setting the filename in the body")
                    .padding()
                // Initialise the animation, using name
                RiveViewModel(fileName: "\(person.name)")
                    .view()
                    // Cant do this here
                    //.setInput("height", value: Double(timerPercentage))
            }
            HStack {
                //Only this works, but doesn' allow the filename to be variable
                Text("Hardcoding the filename in view init")
                    .padding()
                mick_animation
                    .view()
            }
            HStack {
                Text("Setting the filename in view initialize using compound var")
                    .padding()
                mick2_animation
                    .view()
            }
            Text("\(howHigh)")
            Button {
                howHigh += 1
            } label: {
                Image(systemName: "plus")
            }
        }
        .onChange(of: howHigh) { newValue in
            changeValue()
        }
        .onAppear {
            changeValue()
        }
    }
    
    func changeValue() {
        mick_animation.setInput("height", value: howHigh)
        mick2_animation.setInput("height", value: howHigh)
    }
}

Source .riv/.rev file

The rive file is simply an animation with a height input between 0-100, and high/low states.

Expected behavior

In the above example only the hardcoded example will be affected by the changeValue func and increase in height.
But this means I cannot set the filename by a variable and have to use switch cases and hardcoding, which is causing my app to hang.

Screenshots

n/a

Screenshot

Device & Versions (please complete the following information)

  • Device: ios 11 max pro, iPhone 14
  • iOS version 16.5

Additional context

Posted this info in the discord chat was asked by Zach to post here. I'd love to be able to use Rive in my app, as it has a lot of potential, but I can't get this to work and it's a deal breaker for me atm.

Thanks
Conor