-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Widget Kit Refreshing Bug #778
Merged
aaronbrethorst
merged 3 commits into
OneBusAway:main
from
manu-r12:widgetKit_refresh_bug
Dec 10, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,48 +10,66 @@ import CoreLocation | |
|
||
/// `WidgetDataProvider` is responsible for fetching and providing relevant data to the widget timeline provider. | ||
class WidgetDataProvider: NSObject, ObservableObject { | ||
|
||
public let formatters = Formatters( | ||
locale: Locale.autoupdatingCurrent, | ||
calendar: Calendar.autoupdatingCurrent, | ||
themeColors: ThemeColors.shared | ||
) | ||
|
||
static let shared = WidgetDataProvider() | ||
|
||
private let userDefaults = UserDefaults(suiteName: Bundle.main.appGroup!)! | ||
|
||
private lazy var locationManager = CLLocationManager() | ||
private lazy var locationService = LocationService( | ||
userDefaults: userDefaults, | ||
locationManager: locationManager | ||
) | ||
|
||
private lazy var app: CoreApplication = { | ||
let bundledRegions = Bundle.main.path(forResource: "regions", ofType: "json")! | ||
let config = CoreAppConfig(appBundle: Bundle.main, userDefaults: userDefaults, bundledRegionsFilePath: bundledRegions) | ||
let config = CoreAppConfig( | ||
appBundle: Bundle.main, | ||
userDefaults: userDefaults, | ||
bundledRegionsFilePath: Bundle.main.path(forResource: "regions", ofType: "json")! | ||
) | ||
return CoreApplication(config: config) | ||
}() | ||
|
||
private var bestAvailableBookmarks: [Bookmark] { | ||
var bookmarks = app.userDataStore.favoritedBookmarks | ||
if bookmarks.isEmpty { | ||
bookmarks = app.userDataStore.bookmarks | ||
} | ||
return bookmarks | ||
} | ||
|
||
|
||
/// Dictionary mapping trip bookmark keys to arrival/departure data. | ||
private var arrDepDic = [TripBookmarkKey: [ArrivalDeparture]]() | ||
|
||
/// Formatters for localization and styling. | ||
let formatters = Formatters( | ||
locale: Locale.autoupdatingCurrent, | ||
calendar: Calendar.autoupdatingCurrent, | ||
themeColors: ThemeColors.shared | ||
) | ||
|
||
/// Loads arrivals and departures for all favorited bookmarks for the widget. | ||
public func loadData() async { | ||
guard let apiService = app.apiService else { return } | ||
|
||
func loadData() async { | ||
arrDepDic = [:] | ||
|
||
guard let apiService = app.getNewRefreshedRESTAPIService() else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done ✅ |
||
Logger.error("Failed to get REST API Service.") | ||
return | ||
} | ||
|
||
let bookmarks = getBookmarks() | ||
.filter { $0.isTripBookmark && $0.regionIdentifier == app.regionsService.currentRegion?.id } | ||
|
||
for bookmark in bookmarks { | ||
await fetchArrivalData(for: bookmark, apiService: apiService) | ||
guard !bookmarks.isEmpty else { | ||
Logger.info("No bookmarks found to load data.") | ||
return | ||
} | ||
|
||
await withTaskGroup(of: Void.self) { group in | ||
bookmarks.forEach { bookmark in | ||
group.addTask { [weak self] in | ||
await self?.fetchArrivalData(for: bookmark, apiService: apiService) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Fetch arrival data for a specific bookmark and update the dictionary. | ||
private func fetchArrivalData(for bookmark: Bookmark, apiService: RESTAPIService) async { | ||
do { | ||
|
@@ -60,31 +78,27 @@ class WidgetDataProvider: NSObject, ObservableObject { | |
minutesBefore: 0, | ||
minutesAfter: 60 | ||
).entry | ||
|
||
await MainActor.run { | ||
let keysAndDeps = stopArrivals.arrivalsAndDepartures.tripKeyGroupedElements | ||
for (key, deps) in keysAndDeps { | ||
self.arrDepDic[key] = deps | ||
stopArrivals.arrivalsAndDepartures.tripKeyGroupedElements.forEach { key, deps in | ||
arrDepDic[key] = deps | ||
} | ||
} | ||
} catch { | ||
Logger | ||
.error( | ||
"Error fetching data for bookmark \(bookmark.name) with bookmark id: \(bookmark.id): \(error)" | ||
) | ||
Logger.error(""" | ||
Error fetching data for bookmark: '\(bookmark.name)' | ||
(ID: \(bookmark.id)). Error: \(error.localizedDescription) | ||
""") | ||
} | ||
} | ||
|
||
/// Looks up arrival and departure data for a given trip key. | ||
public func lookupArrivalDeparture(with key: TripBookmarkKey) -> [ArrivalDeparture] { | ||
return arrDepDic[key, default: []] | ||
func lookupArrivalDeparture(with key: TripBookmarkKey) -> [ArrivalDeparture] { | ||
arrDepDic[key, default: []] | ||
} | ||
/// Retrieves the best available bookmarks. | ||
|
||
/// Gets bookmarks of the selected region. | ||
public func getBookmarks() -> [Bookmark] { | ||
return bestAvailableBookmarks | ||
return bestAvailableBookmarks.filter { $0.isTripBookmark && $0.regionIdentifier == app.regionsService.currentRegion?.id } | ||
} | ||
|
||
/// Dictionary to store arrival and departure data grouped by trip keys. | ||
private var arrDepDic = [TripBookmarkKey: [ArrivalDeparture]]() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete this