After hitting yet another wall with Beta 3, the moment Beta 4 was released I’ve updated all my devices, recompiled the application and looked at it on the watch.
This time, without any change on my part, things just fell into place:
Location permission worked correctly. I had my list of Bikes sorted by location
What I didn’t like as much though were all the deprecation warnings I was already accumulating. During this years Beta Period, none of the APIs presented at WWDC were finalized. SwiftUI and Combine are still very much in flux and the APIs change on a biweekly basis.
At the time of this writing, Beta 5 has already removed all older deprecations and has added even more deprecations. We’re now at a point where most of the videos from WWDC sessions that are explaining SwiftUI and combine are not applicable to the real world any more, but that’s for another post.
Some lipstick on the pig
With things mostly working right, there was one thing that was bugging me in the list of bikes you’re seeing in the screenshot above: The distances to my current location were manually formatted in meters, but I knew that Apples platforms come with very good locale dependent unit formatters, so I wanted to fix that.
MeasurementFormatter has a
.naturalScale unit option that’s supposed to pick scale and unit automatically dependent on the user’s locale. In case of distances, here in Switzerland, I would expect a distance to be shown in meters up until 1 km at which point I would expect a distance to be shown in km with one fractional digit of accuracy.
But that’s now what
MeasurementFormatter does: It insisted on using km and it insisted on three fractional digits of accuracy. That’s why I’ve decided to format on my own. But I knew there must be a proper solution.
It tuns out, there is – but it’s part of MapKit, not of Foundation: There’s
MKDistanceFormatter to use for this and while MeasurementFormatter has a
unitOptions property, the
MKDistanceFormatter has a
unitStyle property which you can set to
.abbreviated to get to the proper effect.
So I have added that and also used battery icons based on SFSymbols to display the bikes battery levels like so:
Reactive warts in SwiftUI
Remember when I said that the whole UI hierarchy was going to be built from one parent SwiftUI view that would decide on a state object? That’s the design I totally went with:
My main ContentView is just a big switch statement, completely exchanging its subview hierarchy dependent on what the global state handler object thinks should be currently active.
As you can see in above code, there’s only
.ListingBikes – there’s no state to list a single bike. That’s fine, because that’s up to that
BikeList view to decide to instead of listing a list of bikes it wants to show a single bike.
I did this using a
NavigationButton setting its destination to the detail view:
What’s nice is that you get a free sliding animation and a free back button out of this. However, what’s not as nice is that if you do this, the detail view gets pushed as another native view on top of the existing view.
Which means that even when the big switch statement from the screenshot above causes a different sub-view to be rendered (and it does get rendered), then the additional view pushed by the NavigationLink remains shown on top and does not get closed.
In the end, here on Beta 4, I went with a
NavigationDestinationLink so I could first close the detail view and then tell the state handler I wanted to create a booking.
At the time of this writing (Beta 5 has just been released),
NavigationDestinationLink is already deprecated again and the state whether the destination is showing or not can be passed as a
$binding, however, also at the time of this writing, this currently messes with the free back button
Another thing that falls in the same bucket of re-painting the whole hierarchy does not in fact repaint the whole hierarchy is a SwiftUI View’s
navigationBarTitle modifier: if you set that once on any subview, it will persist even through you removing that subview and replacing it by another which doesn’t use the
Meaning that setting a property on a subview as an effect on global state.
This feels wrong to me.
Anyways – enough complaining. With all of this in place, I did my very first commute with a smide bike using nothing but my watch. Here you see me at the end of the trip, ready to end the booking:
That felt great.
What doesn’t feel great is the mess Beta 5 made out of my nicely layouted UI. But that’s for another day.