What I did yesterday
Back on track with SwiftUI! Worked up to day 87 of 100 Days of SwiftUI.
- Completed Project 16 of the course’s journey and a long standing question I’ve had about notifications was answered gracefully. That question was: If the user hasn’t allowed notifications and you try to send one, at what point does that break?. This was more of a flow control question because I would imagine you would have to check if the user has notifications allowed and the code for that looked like this:
func addNotification(for prospect: Prospect) {
let center = UNUserNotificationCenter.current()
let addRequest = {
let content = UNMutableNotificationContent()
content.title = "Contact \(prospect.name)"
content.subtitle = prospect.emailAddress
content.sound = UNNotificationSound.default
// var dateComponents = DateComponents()
// dateComponents.hour = 9
// let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) \\ using this for testing sake
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
}
center.getNotificationSettings { settings in
if settings.authorizationStatus == .authorized {
addRequest()
} else {
center.requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
addRequest()
} else if let error {
print(error.localizedDescription)
}
}
}
}
}
- I wrote code for using SwiftUI gestures! Adding gestures to SwiftUI code is as simple as adding a modifier. Nothing complicated at all. I did come across some interesting concepts: Chaining gestures. Sometimes some gestures get in the way of others and that is an interesting concept. With html/react when there’s 2 components, the parent and the child, and both have
onClick
handlers, the parent tends to take precedence but in SwiftUI, it’s the total opposite! That made my head spin a little because that makes you think about the order of rendering and in the beginning of the course, it was mentioned that with SwiftUI, the order of the components and the modifiers is key to how things are rendered and this was an example of how nesting is treated in SwiftUI. - Still on the topic of gestures, when there happens to be a conflict with gestures, one of two options can be used. Using this code for example:
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
.onTapGesture {
print("Text tapped")
}
}
.onTapGesture {
print("VStack tapped")
}
}
}
In this situation SwiftUI will always give the child’s gesture priority, which means when you tap the text view above you’ll see “Text tapped”. We can get around this by doing one of two things:
- Assign priority to the second tap gesture:
.highPriorityGesture(
TapGesture()
.onEnded {
print("VStack tapped")
}
)
- Tell SwiftUI to trigger both simultaneously
.simultaneousGesture(
TapGesture()
.onEnded {
print("VStack tapped")
}
)
- Worked with SwiftUI’s native QR code generator and it’s really as simple as providing a string to the API to get a QR code image. There’s so much being demystified.
- Worked with Swipe Actions as well🏄🏽♂️. Really just as simple as adding
.swipeActions
! SwiftUI is so cool
.swipeActions {
Button("Delete", systemImage: "trash", role: .destructive) {
modelContext.delete(prospect)
}
... // omitted for brevity
}
Simple as that
Today
As I near the end of my RC batch I really am starting to get RC’s term “Never Graduate” because this way of learning has really become a part of me and the more I learn at this pace the more I feel I have to share it. Love it. Shoutout to the faculty and everyone who’s made RC possible!
- So I turned my morning walks into morning runs🏃🏽♂️. My neighbor’s dogs have become my morning companions so we keep ourselves company. I unfortunately do not get the chance to listen to podcasts while I run so no morning wisdom but maybe I’ll move that to another time slot in my day.
Upcoming
- I would be commuting today to the outskirts to checkout a little plot of land and also use the chance to visit distant friends. Starting a small okra farm with a friend as my introduction to farming and I am approaching this with the same curiosity as a do with learning code. Another reason is I’ll need a source of income to support myself to be at least be able to afford a Raspberry Pi😅
- Gearing up to hear about everyone’s projects today during RC presentations!