Skip to content

Commit

Permalink
chore: cleanup ios examples
Browse files Browse the repository at this point in the history
This PR:
- Removes developer comments + Todos
- Changes the samples to use @StateObject for the RiveViewModel instances. This is the SwiftUI way to instantiate an ObservableObject. This also ensures the Rive data is only loaded when the view is displayed and not when the app is started. The `view` method may also be called multiple times for various reasons, so moving this logic to be in the view state is more efficient. @mjtalbot I think you made comments about this in the past.

By doing the above it also fixes the samples in `SwiftWidgets` that was giving issues
- For example, the sliders didn't work. It does now.
- The toggle `RSwitch` still does not work, as it's an Animation and not a StateMachine (I believe there is now logic that disables touch events if there are no active listeners, or it only does touch processing when it's a state machine).
- We should remove this file entirely or just keep the sliders. This seems to have been made before state machines were a thing, and implementing buttons etc. should rather be done through listeners and events.

### TODO
- Some samples still create the RiveViewModel in the view but rely on other state data. Need to check to see what is the correct way to lazily create those.

Diffs=
fbb092d8c chore: cleanup ios examples (#6351)

Co-authored-by: Gordon <pggordonhayes@gmail.com>
  • Loading branch information
HayesGordon and HayesGordon committed Dec 15, 2023
1 parent 81ab388 commit b3d16a9
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 156 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bd71143bc0964a789174f30f01d14fa551e33bb9
fbb092d8c71e2854102a30cdec6e0a57ad10a3ba
2 changes: 1 addition & 1 deletion .rive_renderer
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7e2d06a1e206eac1c65dcc88e153e6fe1e8067c9
9859a2759e36e12f10e95324c11602ef5e7fa6a7
Binary file removed Example-iOS/Assets/switch.riv
Binary file not shown.
16 changes: 1 addition & 15 deletions Example-iOS/RiveExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 52;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -94,7 +94,6 @@
04E51C662A151C230075E473 /* pull.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88CA2644447400E7DBB2 /* pull.riv */; };
04E51C672A151C230075E473 /* rope.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88CC2644447400E7DBB2 /* rope.riv */; };
04E51C682A151C230075E473 /* skills.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88D82644447500E7DBB2 /* skills.riv */; };
04E51C692A151C230075E473 /* switch.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9E040A8264DFCFD009ABC7C /* switch.riv */; };
04E51C6A2A151C230075E473 /* trailblaze.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88D52644447500E7DBB2 /* trailblaze.riv */; };
04E51C6B2A151C230075E473 /* ui_swipe_left_to_delete.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88CB2644447400E7DBB2 /* ui_swipe_left_to_delete.riv */; };
04E51C6C2A151C230075E473 /* vader.riv in Resources */ = {isa = PBXBuildFile; fileRef = 042C88DA2644447500E7DBB2 /* vader.riv */; };
Expand All @@ -108,7 +107,6 @@
83C89ACF2988709400044C17 /* marty.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83C89ACE2988709400044C17 /* marty.riv */; };
83C89AD1298870A700044C17 /* paper.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83C89AD0298870A700044C17 /* paper.riv */; };
83DE4CB52AB397A800B88B72 /* Bear.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83DE4CB42AB3974300B88B72 /* Bear.riv */; };
C324DB5628071EB80060589F /* RiveSwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5528071EB80060589F /* RiveSwitch.swift */; };
C324DB5B2807216B0060589F /* RiveSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5A2807216B0060589F /* RiveSlider.swift */; };
C324DB5D280728690060589F /* RiveButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5C280728690060589F /* RiveButton.swift */; };
C324DB5F280740FB0060589F /* rbutton.riv in Resources */ = {isa = PBXBuildFile; fileRef = C324DB5E280740FB0060589F /* rbutton.riv */; };
Expand All @@ -117,7 +115,6 @@
C3745FCE282AE2320087F4AF /* hero_editor.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3745FCB282AE2320087F4AF /* hero_editor.riv */; };
C3C074AB283FC75900E8EB33 /* testanimation.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3C074A7283FC75800E8EB33 /* testanimation.riv */; };
C3C074AC283FC75900E8EB33 /* teststatemachine.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3C074AA283FC75900E8EB33 /* teststatemachine.riv */; };
C3C074EE28414F4600E8EB33 /* SwiftTestParityAnimSM.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C074ED28414F4600E8EB33 /* SwiftTestParityAnimSM.swift */; };
C3C074F2284161E400E8EB33 /* halloween.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3C074F1284161E400E8EB33 /* halloween.riv */; };
C3D187F3280751A8008B739A /* RiveProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D187F2280751A8008B739A /* RiveProgressBar.swift */; };
C3D187F728075B6C008B739A /* riveslider.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3D187F628075B6C008B739A /* riveslider.riv */; };
Expand All @@ -140,7 +137,6 @@
C9CE8266263B90E000F98DDB /* juice_v7.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9CE8265263B90E000F98DDB /* juice_v7.riv */; };
C9D3DE5B264F3B51001BA265 /* liquid.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9D3DE5A264F3B51001BA265 /* liquid.riv */; };
C9D3DE68264F49F4001BA265 /* life_bar.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9D3DE67264F49F4001BA265 /* life_bar.riv */; };
C9E040A9264DFCFD009ABC7C /* switch.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9E040A8264DFCFD009ABC7C /* switch.riv */; };
E57798A02A72C77900FF25C3 /* testtext.riv in Resources */ = {isa = PBXBuildFile; fileRef = E577989D2A72C77900FF25C3 /* testtext.riv */; };
E57798A92A730A9B00FF25C3 /* SwiftTestText.swift in Sources */ = {isa = PBXBuildFile; fileRef = E57798A82A730A9B00FF25C3 /* SwiftTestText.swift */; };
E57798AC2A731C5800FF25C3 /* testtext.riv in Resources */ = {isa = PBXBuildFile; fileRef = E577989D2A72C77900FF25C3 /* testtext.riv */; };
Expand Down Expand Up @@ -259,7 +255,6 @@
83C89ACE2988709400044C17 /* marty.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = marty.riv; sourceTree = "<group>"; };
83C89AD0298870A700044C17 /* paper.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = paper.riv; sourceTree = "<group>"; };
83DE4CB42AB3974300B88B72 /* Bear.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = Bear.riv; sourceTree = "<group>"; };
C324DB5528071EB80060589F /* RiveSwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveSwitch.swift; sourceTree = "<group>"; };
C324DB5A2807216B0060589F /* RiveSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveSlider.swift; sourceTree = "<group>"; };
C324DB5C280728690060589F /* RiveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveButton.swift; sourceTree = "<group>"; };
C324DB5E280740FB0060589F /* rbutton.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = rbutton.riv; sourceTree = "<group>"; };
Expand All @@ -268,7 +263,6 @@
C3745FCB282AE2320087F4AF /* hero_editor.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = hero_editor.riv; sourceTree = "<group>"; };
C3C074A7283FC75800E8EB33 /* testanimation.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = testanimation.riv; sourceTree = "<group>"; };
C3C074AA283FC75900E8EB33 /* teststatemachine.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = teststatemachine.riv; sourceTree = "<group>"; };
C3C074ED28414F4600E8EB33 /* SwiftTestParityAnimSM.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftTestParityAnimSM.swift; sourceTree = "<group>"; };
C3C074F1284161E400E8EB33 /* halloween.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = halloween.riv; sourceTree = "<group>"; };
C3D187F2280751A8008B739A /* RiveProgressBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveProgressBar.swift; sourceTree = "<group>"; };
C3D187F628075B6C008B739A /* riveslider.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = riveslider.riv; sourceTree = "<group>"; };
Expand All @@ -293,7 +287,6 @@
C9CE8265263B90E000F98DDB /* juice_v7.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = juice_v7.riv; sourceTree = "<group>"; };
C9D3DE5A264F3B51001BA265 /* liquid.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = liquid.riv; sourceTree = "<group>"; };
C9D3DE67264F49F4001BA265 /* life_bar.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = life_bar.riv; sourceTree = "<group>"; };
C9E040A8264DFCFD009ABC7C /* switch.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = switch.riv; sourceTree = "<group>"; };
E577989D2A72C77900FF25C3 /* testtext.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = testtext.riv; sourceTree = "<group>"; };
E57798A82A730A9B00FF25C3 /* SwiftTestText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftTestText.swift; sourceTree = "<group>"; };
E57798AD2A73264100FF25C3 /* text_test_2.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = text_test_2.riv; sourceTree = "<group>"; };
Expand Down Expand Up @@ -395,7 +388,6 @@
isa = PBXGroup;
children = (
C324DB5C280728690060589F /* RiveButton.swift */,
C324DB5528071EB80060589F /* RiveSwitch.swift */,
C324DB5A2807216B0060589F /* RiveSlider.swift */,
C3D187F2280751A8008B739A /* RiveProgressBar.swift */,
C3ECAC2E281840A300A81123 /* ClockViewModel.swift */,
Expand Down Expand Up @@ -452,7 +444,6 @@
042C88CA2644447400E7DBB2 /* pull.riv */,
042C88CC2644447400E7DBB2 /* rope.riv */,
042C88D82644447500E7DBB2 /* skills.riv */,
C9E040A8264DFCFD009ABC7C /* switch.riv */,
042C88D52644447500E7DBB2 /* trailblaze.riv */,
042C88CB2644447400E7DBB2 /* ui_swipe_left_to_delete.riv */,
042C88DA2644447500E7DBB2 /* vader.riv */,
Expand All @@ -476,7 +467,6 @@
E5CD7D7027DC331900BFE5E2 /* SwiftMeshAnimation.swift */,
C3ECAC262817BE4600A81123 /* SwiftTouchEvents.swift */,
C3E2B58B2833ECFE00A8651B /* SwiftCannonGame.swift */,
C3C074ED28414F4600E8EB33 /* SwiftTestParityAnimSM.swift */,
E57798A82A730A9B00FF25C3 /* SwiftTestText.swift */,
E5964AAA2A9CD49200140479 /* SwiftEvents.swift */,
041265212B0BC5A5009400EC /* SwiftSimpleAssets.swift */,
Expand Down Expand Up @@ -686,7 +676,6 @@
E57798AF2A73264100FF25C3 /* text_test_2.riv in Resources */,
04E51C672A151C230075E473 /* rope.riv in Resources */,
04E51C682A151C230075E473 /* skills.riv in Resources */,
04E51C692A151C230075E473 /* switch.riv in Resources */,
04E51C6A2A151C230075E473 /* trailblaze.riv in Resources */,
0453FCB52B012D17001185C8 /* picture-47982.jpeg in Resources */,
04E51C6B2A151C230075E473 /* ui_swipe_left_to_delete.riv in Resources */,
Expand Down Expand Up @@ -725,7 +714,6 @@
042C88E92644447500E7DBB2 /* off_road_car_blog.riv in Resources */,
042C88832643D6B900E7DBB2 /* Main.storyboard in Resources */,
042C88EA2644447500E7DBB2 /* skills.riv in Resources */,
C9E040A9264DFCFD009ABC7C /* switch.riv in Resources */,
042C88E62644447500E7DBB2 /* artboard_animations.riv in Resources */,
042C88E72644447500E7DBB2 /* trailblaze.riv in Resources */,
C3D187F728075B6C008B739A /* riveslider.riv in Resources */,
Expand Down Expand Up @@ -797,11 +785,9 @@
042C88902644250D00E7DBB2 /* MultipleAnimations.swift in Sources */,
C3468E6227FDCBC6008652FD /* SimpleSlider.swift in Sources */,
041265222B0BC5A5009400EC /* SwiftSimpleAssets.swift in Sources */,
C3C074EE28414F4600E8EB33 /* SwiftTestParityAnimSM.swift in Sources */,
83C89ACB29886ECB00044C17 /* StressTest.swift in Sources */,
E5E87A012AE5A83800E7295F /* SwiftVariableFPS.swift in Sources */,
C3357CA1280F42EC00F03B6F /* ExamplesMaster.swift in Sources */,
C324DB5628071EB80060589F /* RiveSwitch.swift in Sources */,
C3ECAC272817BE4600A81123 /* SwiftTouchEvents.swift in Sources */,
C3ECAC2F281840A300A81123 /* ClockViewModel.swift in Sources */,
04026DC827CE3EE6002B3DBF /* SwiftLayout.swift in Sources */,
Expand Down
2 changes: 0 additions & 2 deletions Example-iOS/Source/Examples/Storyboard/CachedAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AssetLoader{
var cachedImage: RiveImageAsset?;
var factory: RiveFactory?;

// pretty naive way to clean up any outstanding requests.
var tasks: [URLSessionDataTask] = [];

func fillFontCache(){
Expand Down Expand Up @@ -193,7 +192,6 @@ class AssetLoader{


class OutOfBandAssetsController: UIViewController, UIGestureRecognizerDelegate {
// TODO: talk to people to see if we can make this nice...
var loader: AssetLoader = AssetLoader();
var viewModel: RiveViewModel?;

Expand Down
3 changes: 0 additions & 3 deletions Example-iOS/Source/Examples/Storyboard/SimpleOutOfBand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ func loadAndSetRenderFont(named resourceName: String, asset: RiveFontAsset, fact
}

class SimpleOutOfBandController: UIViewController, UIGestureRecognizerDelegate {
// TODO: talk to people to see if we can make this nice...
var loader: AssetLoader = AssetLoader();
var viewModel: RiveViewModel?;



required init?(coder: NSCoder) {

self.viewModel = RiveViewModel(fileName: "simple_assets", loadCdn: false, customLoader: { (asset: RiveFileAsset, data: Data, factory: RiveFactory) -> Bool in
Expand Down
13 changes: 7 additions & 6 deletions Example-iOS/Source/Examples/SwiftUI/SwiftCannonGame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import RiveRuntime
struct SwiftCannonGame: DismissableView {
var dismiss: () -> Void = {}

// MARK: RiveViewModel
// This view model controls a file configured with:
// - StateMachine
// - Listeners
@StateObject private var riveViewModel = RiveViewModel(fileName: "bullet_man_game")

var body: some View {
// MARK: RiveViewModel
// This view model controls a file configured with:
// - StateMachine
// - Listeners

RiveViewModel(fileName: "bullet_man_game").view()
riveViewModel.view()
}
}
11 changes: 10 additions & 1 deletion Example-iOS/Source/Examples/SwiftUI/SwiftLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@
import SwiftUI
import RiveRuntime



struct SwiftLayout: DismissableView {
var dismiss: () -> Void = {}

@State private var fit: RiveFit = .contain
@State private var alignment: RiveAlignment = .center
@StateObject private var riveViewModel = RiveViewModel(fileName: "truck_v7", fit: .contain, alignment: .center)

var body: some View {
VStack {
RiveViewModel(fileName: "truck_v7", fit: fit, alignment: alignment).view()
riveViewModel.view()
.onChange(of: fit) { value in
riveViewModel.fit = value
}
.onChange(of: alignment) { value in
riveViewModel.alignment = value
}
}
HStack {
Text("Fit")
Expand Down
5 changes: 1 addition & 4 deletions Example-iOS/Source/Examples/SwiftUI/SwiftMeshAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ struct SwiftMeshAnimation: DismissableView {

// MARK: RiveViewModel
// This view model specifies the exact StateMachine that it wants from the file

// TODO:
// Review viewmodel set-up here to avoid recreation & also loading files on launch.
@State var isTapped: Bool = false
@StateObject private var tapePlayer = RiveViewModel(fileName: "prop_example", stateMachineName: "State Machine 1")

var body: some View {
let tapePlayer = RiveViewModel(fileName: "prop_example", stateMachineName: "State Machine 1")
tapePlayer.view()
.aspectRatio(1, contentMode: .fit)
.onTapGesture {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import RiveRuntime
/// different animations within those artboards
struct SwiftMultipleAnimations: DismissableView {
var dismiss: () -> Void = {}
let file = try! RiveFile(name: "artboard_animations")

var body: some View {
let file = try! RiveFile(name: "artboard_animations")
ScrollView{
VStack {
Text("Square - go around")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import RiveRuntime

struct SwiftSimpleAnimation: DismissableView {
var dismiss: () -> Void = {}
@StateObject private var riveViewModel = RiveViewModel(fileName: "halloween", autoPlay: false)

var body: some View {
SwiftVMPlayer(
viewModels:
RiveViewModel(fileName: "halloween", autoPlay: false)
riveViewModel
)
}
}
60 changes: 30 additions & 30 deletions Example-iOS/Source/Examples/SwiftUI/SwiftSimpleAssets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,38 @@ import RiveRuntime

struct SwiftSimpleAssets: DismissableView {
var dismiss: () -> Void = {}

var body: some View {
RiveViewModel(fileName: "simple_assets", autoPlay: false, loadCdn: false, customLoader: { (asset: RiveFileAsset, data: Data, factory: RiveFactory) -> Bool in
@StateObject private var riveViewModel = RiveViewModel(fileName: "simple_assets", autoPlay: false, loadCdn: false, customLoader: { (asset: RiveFileAsset, data: Data, factory: RiveFactory) -> Bool in
if (asset is RiveImageAsset){

// TODO: this looks kinda compolex, can this be simpler?
if (asset is RiveImageAsset){

guard let url = (.main as Bundle).url(forResource: "picture-47982", withExtension: "jpeg") else {
fatalError("Failed to locate 'picture-47982' in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
}
(asset as! RiveImageAsset).renderImage(
factory.decodeImage(data)
)
return true;
}else if (asset is RiveFontAsset) {
guard let url = (.main as Bundle).url(forResource: "Inter-45562", withExtension: "ttf") else {
fatalError("Failed to locate 'Inter-45562' in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
}

(asset as! RiveFontAsset).font(
factory.decodeFont(data)
)
return true;
guard let url = (.main as Bundle).url(forResource: "picture-47982", withExtension: "jpeg") else {
fatalError("Failed to locate 'picture-47982' in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
}
(asset as! RiveImageAsset).renderImage(
factory.decodeImage(data)
)
return true;
}else if (asset is RiveFontAsset) {
guard let url = (.main as Bundle).url(forResource: "Inter-45562", withExtension: "ttf") else {
fatalError("Failed to locate 'Inter-45562' in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
}

return false;
}).view()
(asset as! RiveFontAsset).font(
factory.decodeFont(data)
)
return true;
}

return false;
});

var body: some View {
riveViewModel.view()
}
}
3 changes: 2 additions & 1 deletion Example-iOS/Source/Examples/SwiftUI/SwiftStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ struct SwiftStateMachine: DismissableView {

// MARK: RiveViewModel
// This view model specifies the exact StateMachine that it wants from the file
@StateObject private var stateChanger = RiveViewModel(fileName: "skills", stateMachineName: "Designer's Test")

var body: some View {
let stateChanger = RiveViewModel(fileName: "skills", stateMachineName: "Designer's Test")
ScrollView{
VStack {
stateChanger.view()
Expand Down
23 changes: 0 additions & 23 deletions Example-iOS/Source/Examples/SwiftUI/SwiftTestParityAnimSM.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Example-iOS/Source/Examples/SwiftUI/SwiftTestText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct TextInputView: DismissableView {
var dismiss: () -> Void = {}

@State private var userInput: String = ""
@State private var rvm = RiveViewModel(fileName: "text_test_2")
@StateObject private var rvm = RiveViewModel(fileName: "text_test_2")

var body: some View {
VStack(spacing: 20) {
Expand Down
Loading

0 comments on commit b3d16a9

Please sign in to comment.