Skip to content

Commit

Permalink
fix: Show an alert on Calendar API access errors (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Oct 12, 2023
1 parent 850184a commit e8276ff
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
6 changes: 5 additions & 1 deletion macos/Overview.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -83,6 +84,7 @@
D8DCDE0725F6F9410083DF48 /* MonthView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonthView.swift; sourceTree = "<group>"; };
D8DCDE1125F6FD060083DF48 /* YearView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = YearView.swift; sourceTree = "<group>"; };
D8FA1F412AD7691100E18E26 /* OverviewRelease.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OverviewRelease.entitlements; sourceTree = "<group>"; };
D8FA1F422AD7872000E18E26 /* OverviewError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverviewError.swift; sourceTree = "<group>"; };
D8FE3AD229116FE900C6F7FE /* interact */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = interact; path = ../interact; sourceTree = "<group>"; };
D8FE3AD52911738E00C6F7FE /* interact-license */ = {isa = PBXFileReference; lastKnownFileType = text; path = "interact-license"; sourceTree = "<group>"; };
D8FE3AD82911789500C6F7FE /* Summary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Summary.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -249,6 +251,7 @@
D8B435A129119A96008F4F6F /* CalendarError.swift */,
D8FE3ADA291178BF00C6F7FE /* CalendarItem.swift */,
D8FE3ADC29117B8000C6F7FE /* Event.swift */,
D8FA1F422AD7872000E18E26 /* OverviewError.swift */,
D8FE3AD82911789500C6F7FE /* Summary.swift */,
D8B4359F29119A5D008F4F6F /* WindowModel.swift */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
1 change: 1 addition & 0 deletions macos/Overview/Interface/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct ContentView: View {
}
}
}
.presents($applicationModel.error)
.onAppear {
windowModel.start()
}
Expand Down
13 changes: 10 additions & 3 deletions macos/Overview/Model/ApplicationModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Notification, Never>
Expand All @@ -41,17 +42,23 @@ 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
}
}
}
}

func start() {
dispatchPrecondition(condition: .onQueue(.main))

fetchAvailableYears()

updates
.receive(on: updateQueue)
.map { notification in
Expand Down
38 changes: 38 additions & 0 deletions macos/Overview/Model/OverviewError.swift
Original file line number Diff line number Diff line change
@@ -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."
}
}

}

0 comments on commit e8276ff

Please sign in to comment.