Using SwiftCurrent with MVVM Part 1

Hard? Hardly.

Richard Gist
SwiftCurrent
3 min readOct 29, 2021

--

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.

Swift Package Manager adding SwiftCurrent with SwiftCurrent and SwiftCurrent underscore SwiftUI packages checked.

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.

Xcode showing an import SwiftCurrent line, struct home view being conformed to Flow Representable, and an added line for a weak variable called _workflowPointer.

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.

Xcode showing an import SwiftCurrent line, struct details view being conformed to Flow Representable, and an added line for a weak variable called underscore workflowPointer. The initializer has been updated to use with as the parameter name.

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.

Demonstration of the Wiggles App functioning as expected.
Wiggles working as it always did.

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:

The body of Content View has been updated to define a Workflow Launcher and calls to a method called then proceed that takes in the types for Home View and Details View.

Then we have to update HomeView to output the right type for DetailsView:

Home View now has a type alias for Dog Model called Workflow Output.

And finally, we change the NavigationLink into a Button that calls proceedInWorkflow

What use to be a Navigation Link is now a Button with an action to proceed in workflow with a model.

Let’s see if it works with the SwiftCurrent navigation…

Wiggles working with SwiftCurrent navigation. All appears the same as before.
Look at that Wiggles wiggle! Just like before.

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

--

--

Richard Gist
SwiftCurrent

I’m the technical lead on SwiftCurrent. I like Swift and solving problems. Check me out on LinkedIn(/in/richardgist) and GitHub(/Richard-Gist).