Skip to content

Commit

Permalink
Do not crash today widget when goals unavailable (#398)
Browse files Browse the repository at this point in the history
The today widget previously assumed that the app had made goals
available, and
would crash if it was empty. This could happen if the user had never
logged in,
or never fetched goals. Now instead just show an empty UI. We could
ideally show
an error state, but given today widgets are deprecated we'll accept not
crashing.

To test this more easily also clear the today widget on logout. This
should fix
#236.

Testing:
Logged out and verified the today widget did not crash in the xcode
debugger
  • Loading branch information
theospears authored Jul 31, 2023
1 parent 2d014cf commit 93403cc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 4 additions & 0 deletions BeeSwift/GoalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ actor GoalManager {
self.goalsBox.set(nil)
self.goalsFetchedAt = Date(timeIntervalSince1970: 0)
currentUserManager.removeObject(forKey: cachedLastFetchedGoalsKey)

if let sharedDefaults = UserDefaults(suiteName: Constants.appGroupIdentifier) {
sharedDefaults.removeObject(forKey: "todayGoalDictionaries")
}
}

// MARK: Today Widget
Expand Down
3 changes: 2 additions & 1 deletion BeeSwiftToday/TodayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class TodayViewController: UIViewController {

@objc func updateDataSource() {
let defaults = UserDefaults(suiteName: Constants.appGroupIdentifier)
self.goalDictionaries = defaults?.object(forKey: "todayGoalDictionaries") as! Array<NSDictionary>
// Goal dictionaries will not be available if the user is logged out or has never fetched goals
self.goalDictionaries = defaults?.object(forKey: "todayGoalDictionaries") as? Array<NSDictionary> ?? Array()
}
}

Expand Down

0 comments on commit 93403cc

Please sign in to comment.