Using SwiftCurrent with MVVM Part 1
Hard? Hardly.
I was thinking about how hard it would be to use SwiftCurrent in an existing project built with the MVVM architecture when I happened to see this collection of apps and went perusing through the apps. I ended up finding two MVVM projects that looked interesting to me that both happened to be written by Sameer Nawaz. The two apps are Expenso and Wiggles. I thought Wiggles provided a simple example to try out SwiftCurrent in an MVVM app. Let’s dive in and see how hard it is.
Wiggles Basic
Getting Started
First, I fork Wiggles and, since it doesn’t have any dependencies, choose to use Swift Package Manager to add in SwiftCurrent. Since it’s a SwiftUI based app with no tests, I’ll just grab SwiftCurrent and SwiftCurrent_SwiftUI.
Updating the Screens
Now I need to track down the screens that need to adopt the FlowRepresentable protocol. I start with ContentView, see that HomeView is our first screen, and update that screen.
Those 3 line changes were all it took to get HomeView started. Next, I’ll look for navigation links, and I find 1 in a ForEach leading to the DetailsView. So I update that one.
I’m not going to screencap all the changes with changing an initializer, but all that changed was the parameter name “model” becoming “with model”. Now let’s build it and run to make sure we haven’t broken anything.
Great! It all still works.
Utilizing SwiftCurrent
We aren’t utilizing SwiftCurrent yet, so let’s do that. This change is going to take a bit more work because we need to define the workflow like so:
Then we have to update HomeView to output the right type for DetailsView:
And finally, we change the NavigationLink into a Button that calls proceedInWorkflow
Let’s see if it works with the SwiftCurrent navigation…
Woot woot! Now we are up and running with SwiftCurrent!
Wiggles, SwiftCurrent, MVVM, Oh my!
So we got the navigation handled by SwiftCurrent, and we really didn’t have to touch anything in the models. This seemed all very straightforward. And because the app is so simple, there weren’t a lot of other changes that we took on. Maybe we’ll see more changes in the more complicated Expenso! Stay tuned for Part 2.
UPDATED: Part 2 can be found here: https://medium.com/swiftcurrent/using-swiftcurrent-with-mvvm-part-2-286dbdafa00b