Meet Billie, the smart invoice assistant for macOS! After more than a decade of building iOS applications, it was time to get my feet wet and build something for macOS. The past 3 months I’ve been working hard to release my first macOS application to make renaming & archiving invoices easy and quick, of course with a touch of machine learning (as you do nowadays).
Last week I’ve launched Billie on ProductHunt and at the end of the day, Billie ended as the #9 project of the day!
The past few weeks I have been working on a iOS application for kids to learn to read the clock. So today, I’m really excited to announce that Clockwise – Learn to read the clock is available in the App Store!
I already had the idea for a while to create an educational app for my 3 kids, but because of the Covid19 pandemic, I finally decided to have a go at it.
In most cases a View Controller shows one screen to a user, but the user can interact with the screen in multiple ways. Every one of these interactions is handled differently in code. This can be through delegation, target-action, notifications, callbacks, …
For example, take the Travelplanner screen of the Reisplanner Extra app I’m working on.
A modal Location Picker is presented when the row is pressed
From & To locations are switched when pressed
A modal Location Picker is presented when the row is pressed
A modal Date Picker is presented when the row is pressed
The Now toggle button can be in an on/off/automatic state and changes the behavior of the time row
A modal Travel Options screen is presented when the button is pressed
8. 9. 10. … more actions 🙂
As you can see, there are a lot of interactions (and there are actually even more interactions for different states of this screen), and all of these interactions need to be handled in code.
In most cases you’ll end up with all kinds of different methods handling these cases, but over time it can start to become unclear where to look when something needs to be changed or added inside your View Controller.
When a (new) team member has to work in the codebase that he has never touched before, it can take some time to find his/her way and know where to look. So how do I try to streamline this?
Swift Enums, your best friend.
Every interaction on the screen, triggers something, and I model all these interactions in a Swift enum called PerformAction .
The enum for the TravelPlanner screen looks like this:
enum PerformAction {
case fromLocationRowPressed( location:Location? )
case toLocationRowPressed( location:Location? )
case switchLocationButtonPressed
case departureArrivalRowPressed( queryType:TravelTimeQueryType, date:Date )
case nowButtonPressed
case travelOptionsButtonPressed
case widgetDirectTo_optionsButtonPressed
case widgetDirectTo_configureAddressButtonPressed
case widgetDirectTo_selectLocation( location:Location )
case widgetCiCo_optionsButtonPressed
case nothing
}
Implementation
This enum is added as a nested type inside the TravelPlannerViewController, so you can reuse the same PerformAction enum name across your files without getting name collisions.
class TravelPlanneViewController: UIViewController {
enum PerformAction { ... }
}
If a new member starts reading through your codebase, he/she can check this enum and see at a glance what all the different actions are for this screen.
To react on these interactions, we create a property performedActionand add a didSet property observer.
var actionPerformed:PerformAction = .nothing {
didSet {
switch actionPerformed {
case .fromLocationRowPressed( let location ):
presentLocationPicker( location: location )
case .toLocationRowPressed( let location ):
presentLocationPicker( location: location )
case .switchLocationButtonPressed:
switchLocations()
case .departureArrivalRowPressed( let queryType, let date ):
presentDatePicker( queryType, date: date )
...
}
}
}
Proxying
Now you have 1 point of entry to handle all your interactions and call your methods.
When using RxSwift I use a PublishSubject to keep track of changes.
// Create the subject to keep track of the performed actions
private let performedActionSubject = PublishSubject<PerformedAction>()
// Add a new action to the stream
performActionSubject.onNext( .travelOptionsButtonPressed )
// Get updates when an action has been performed
viewModel.outputs.actionPerformedObservable.subscribe(onNext: { [weak self] (performedAction) in
switch performedAction {
case ...
}
}).disposed( by: disposeBag )
Final thoughts
This is not a technique I use for a screen with only one or two actions. I tend to model it like this, if there are a lot of interactions.
The downside is you’ll get a bit of extra code, because you proxy your interaction handling to the PerformAction enum.
The benefits are
See all the interactions in the enum at a glance
Clarity of having one point where all the interactions are handled
For newcomers (or your future-you) it will be easier to get around the code.
If you enjoyed this post, follow me on Twitter @thenerd_be
It often occurs while writing code that you need to assign a value of a certain type, based on the value of Boolean. To do this there are multiple ways, but the 2 most known are
1. The let’s-write-a-lot-of-code approach
let isAWildBoolean = true
// Set the value of `offSet` to a value based on the Boolean
let offSet:Int
if isAWildBoolean {
offSet = 10
} else {
offSet = 20
}
I don’t like the first approach, because it creates a bloated codebase. A better way is to use the ternary operator (also known as the inline-if).
2. The one-line-star-developer (ternary operator) approach
You specify the Boolean you want to evaluate, add a question mark, followed by the value if the Boolean is true, followed by a colon, followed by the value if the Boolean is false.
I was using the Prisma app (like half of the world probably) and noticed the info bubble when you swipe with your fingers to adjust the strength of the effect.
This is my implementation (written in Swift) of this component, which comes with the Prisma look out of the box.
A few years ago Apple introduced TouchID on the iPhone5S. Instead of asking your user for a password, you can just ask for their fingerprint (if their device has TouchID) which improves the UX by a gazillion times.
With the introduction of iOS7, it was impossible for a developer to use the fingerprint sensor for authentication. Luckily in iOS8, Apple provided us with an API to do so.
In this tutorial I’ll show you how you can integrate TouchID authentication in your application.
At the 2014 WWDC conference Apple announced Swift as a new language to write iOS app. In February 2015 they released Swift 1.2, which fixed a lot of issues (especially with the compiler) and added new language features.
In June 2015 Apple announced at WWDC Swift 2, which will be made open source later this year. In this post I will cover the new features in Swift 2.
Last year I published TNCheckboxGroup for Objective-C, but I had a few comments it didn’t work when using with Swift. So I just published a Swift version on Github. This versions leverages UICollectionView to handle big sets of checkboxes. TNSwiftyCheckboxGroup, create checkbox groups in Swift.
For a project I needed to add a UIPickerView with custom cells using AutoLayout. UIKit allows this via the UIPickerViewDelegate method pickerView(_:viewForRow:forComponent:reusingView).
The cell just needed an UIImageView and UILabel, so I thought it would be pretty straightforward to do, but there are some caveats you need to know. Big thanks to Tom Adriaenssen for pointing them out and not ruining my Sunday afternoon 🙂