Skip to content

Commit e838f5c

Browse files
committed
Adding two new onStoreDidLoad methods which make it easier to be notified when a Store is loaded
1 parent 0f703f4 commit e838f5c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

Demo/Demo/Components/FavoritesCarouselView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ struct FavoritesCarouselView: View {
8080
})
8181
.frame(height: 200.0)
8282
.background(Color.palette.background)
83-
.task({
84-
do {
85-
try await self.imagesController.$images.itemsHaveLoaded()
86-
self.itemsHaveLoaded = true
87-
} catch {
83+
.onStoreDidLoad(
84+
self.imagesController.$images,
85+
update: $itemsHaveLoaded,
86+
onError: { error in
8887
print("Failed to load images", error)
8988
}
90-
})
89+
)
9190
}
9291
}
9392

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import SwiftUI
2+
3+
public extension View {
4+
func onStoreDidLoad<StorableItem: Codable & Sendable>(_ store: Store<StorableItem>, onLoad: @escaping () -> Void, onError: ((Error) -> Void)? = nil) -> some View {
5+
self.task({
6+
do {
7+
try await store.itemsHaveLoaded()
8+
onLoad()
9+
} catch {
10+
onError?(error)
11+
}
12+
})
13+
}
14+
15+
func onStoreDidLoad<StorableItem: Codable & Sendable>(_ store: Store<StorableItem>, update hasLoadedState: Binding<Bool>, onError: ((Error) -> Void)? = nil) -> some View {
16+
self.task({
17+
do {
18+
try await store.itemsHaveLoaded()
19+
hasLoadedState.wrappedValue = true
20+
} catch {
21+
onError?(error)
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)