From e8276ff741323a8f4165a387b6e8351001d3a923 Mon Sep 17 00:00:00 2001 From: Jason Morley Date: Wed, 11 Oct 2023 17:06:09 -1000 Subject: [PATCH] fix: Show an alert on Calendar API access errors (#72) --- macos/Overview.xcodeproj/project.pbxproj | 6 +++- macos/Overview/Interface/ContentView.swift | 1 + macos/Overview/Model/ApplicationModel.swift | 13 +++++-- macos/Overview/Model/OverviewError.swift | 38 +++++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 macos/Overview/Model/OverviewError.swift diff --git a/macos/Overview.xcodeproj/project.pbxproj b/macos/Overview.xcodeproj/project.pbxproj index 40d5535..8abff0e 100644 --- a/macos/Overview.xcodeproj/project.pbxproj +++ b/macos/Overview.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ D8DCDDF725F664460083DF48 /* OverviewUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8DCDDF625F664460083DF48 /* OverviewUITests.swift */; }; D8DCDE0825F6F9410083DF48 /* MonthView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8DCDE0725F6F9410083DF48 /* MonthView.swift */; }; D8DCDE1225F6FD060083DF48 /* YearView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8DCDE1125F6FD060083DF48 /* YearView.swift */; }; + D8FA1F432AD7872000E18E26 /* OverviewError.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FA1F422AD7872000E18E26 /* OverviewError.swift */; }; D8FE3AD42911700A00C6F7FE /* Interact in Frameworks */ = {isa = PBXBuildFile; productRef = D8FE3AD32911700A00C6F7FE /* Interact */; }; D8FE3AD62911738E00C6F7FE /* interact-license in Resources */ = {isa = PBXBuildFile; fileRef = D8FE3AD52911738E00C6F7FE /* interact-license */; }; D8FE3AD92911789500C6F7FE /* Summary.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FE3AD82911789500C6F7FE /* Summary.swift */; }; @@ -83,6 +84,7 @@ D8DCDE0725F6F9410083DF48 /* MonthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonthView.swift; sourceTree = ""; }; D8DCDE1125F6FD060083DF48 /* YearView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YearView.swift; sourceTree = ""; }; D8FA1F412AD7691100E18E26 /* OverviewRelease.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OverviewRelease.entitlements; sourceTree = ""; }; + D8FA1F422AD7872000E18E26 /* OverviewError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewError.swift; sourceTree = ""; }; D8FE3AD229116FE900C6F7FE /* interact */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = interact; path = ../interact; sourceTree = ""; }; D8FE3AD52911738E00C6F7FE /* interact-license */ = {isa = PBXFileReference; lastKnownFileType = text; path = "interact-license"; sourceTree = ""; }; D8FE3AD82911789500C6F7FE /* Summary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Summary.swift; sourceTree = ""; }; @@ -189,8 +191,8 @@ D8DCDDD725F664440083DF48 /* Overview */ = { isa = PBXGroup; children = ( - D8FA1F412AD7691100E18E26 /* OverviewRelease.entitlements */, D8DCDDE225F664450083DF48 /* Overview.entitlements */, + D8FA1F412AD7691100E18E26 /* OverviewRelease.entitlements */, D8DCDDE125F664450083DF48 /* Info.plist */, D8DCDDD825F664440083DF48 /* OverviewApp.swift */, D8DCDDDC25F664450083DF48 /* Assets.xcassets */, @@ -249,6 +251,7 @@ D8B435A129119A96008F4F6F /* CalendarError.swift */, D8FE3ADA291178BF00C6F7FE /* CalendarItem.swift */, D8FE3ADC29117B8000C6F7FE /* Event.swift */, + D8FA1F422AD7872000E18E26 /* OverviewError.swift */, D8FE3AD82911789500C6F7FE /* Summary.swift */, D8B4359F29119A5D008F4F6F /* WindowModel.swift */, ); @@ -406,6 +409,7 @@ D8B435A029119A5D008F4F6F /* WindowModel.swift in Sources */, D8805D9626039D4D00C23C11 /* Int.swift in Sources */, D8A3D3D42911F105002AFC32 /* Set.swift in Sources */, + D8FA1F432AD7872000E18E26 /* OverviewError.swift in Sources */, D8FE3ADB291178BF00C6F7FE /* CalendarItem.swift in Sources */, D832648828E8EBF300D1C1B7 /* DateRangeIterator.swift in Sources */, D8805D9B26039DED00C23C11 /* CheckboxStyle.swift in Sources */, diff --git a/macos/Overview/Interface/ContentView.swift b/macos/Overview/Interface/ContentView.swift index c1caf0a..108f11b 100644 --- a/macos/Overview/Interface/ContentView.swift +++ b/macos/Overview/Interface/ContentView.swift @@ -61,6 +61,7 @@ struct ContentView: View { } } } + .presents($applicationModel.error) .onAppear { windowModel.start() } diff --git a/macos/Overview/Model/ApplicationModel.swift b/macos/Overview/Model/ApplicationModel.swift index c0dc836..e423686 100644 --- a/macos/Overview/Model/ApplicationModel.swift +++ b/macos/Overview/Model/ApplicationModel.swift @@ -25,6 +25,7 @@ import SwiftUI class ApplicationModel: ObservableObject { + @Published var error: Error? = nil @Published var calendars: [EKCalendar] = [] @Published var years: [Int] = [Date.now.year] let updates: AnyPublisher @@ -41,10 +42,14 @@ class ApplicationModel: ObservableObject { .eraseToAnyPublisher() store.requestFullAccessToEvents { granted, error in - // TODO: Handle the error. DispatchQueue.main.async { - print("granted = \(granted), error = \(String(describing: error))") - self.fetchAvailableYears() + if let error = error { + self.error = error + return + } + if !granted { + self.error = OverviewError.accessDenied + } } } } @@ -52,6 +57,8 @@ class ApplicationModel: ObservableObject { func start() { dispatchPrecondition(condition: .onQueue(.main)) + fetchAvailableYears() + updates .receive(on: updateQueue) .map { notification in diff --git a/macos/Overview/Model/OverviewError.swift b/macos/Overview/Model/OverviewError.swift new file mode 100644 index 0000000..b34589f --- /dev/null +++ b/macos/Overview/Model/OverviewError.swift @@ -0,0 +1,38 @@ +// Copyright (c) 2021-2023 InSeven Limited +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +import Foundation + +enum OverviewError: Error { + + case accessDenied + +} + +extension OverviewError: LocalizedError { + + public var errorDescription: String? { + switch self { + case .accessDenied: + return "Calendar access denied." + } + } + +}