Skip to content

Commit 43933bf

Browse files
- Updated recents store to support testability
1 parent dcb9971 commit 43933bf

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Sources/WelcomeWindow/Model/RecentsStore.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public enum RecentsStore {
1010

1111
/// Notification sent when the recent projects list is updated.
1212
public static let didUpdateNotification = Notification.Name("RecentsStore.didUpdate")
13+
14+
/// For tests (or previews) before any API call.
15+
public static var defaults: UserDefaults = .standard
1316

1417
/// Internal representation of a bookmark entry.
1518
private struct BookmarkEntry: Codable, Equatable {
@@ -160,8 +163,8 @@ public enum RecentsStore {
160163
///
161164
/// - Returns: An array of `BookmarkEntry` values decoded from UserDefaults.
162165
private static func loadBookmarks() -> [BookmarkEntry] {
163-
guard let data = UserDefaults.standard.data(forKey: bookmarksKey),
164-
let decoded = try? PropertyListDecoder().decode([BookmarkEntry].self, from: data)
166+
guard let data = defaults.data(forKey: bookmarksKey),
167+
let decoded = try? PropertyListDecoder().decode([BookmarkEntry].self, from: data)
165168
else { return [] }
166169
return decoded
167170
}
@@ -171,7 +174,7 @@ public enum RecentsStore {
171174
/// - Parameter entries: The bookmark entries to save.
172175
private static func saveBookmarks(_ entries: [BookmarkEntry]) {
173176
guard let data = try? PropertyListEncoder().encode(entries) else { return }
174-
UserDefaults.standard.set(data, forKey: bookmarksKey)
177+
defaults.set(data, forKey: bookmarksKey)
175178
NotificationCenter.default.post(name: didUpdateNotification, object: nil)
176179
}
177180

0 commit comments

Comments
 (0)