Skip to content

Commit 52d040a

Browse files
Ability to not show loading overlay when cached
1 parent 7e5992d commit 52d040a

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Sources/LoadableView/Protocols/LoadableView/BaseLoadableViewModel.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public protocol BaseLoadableViewModel: ObservableObject, AnyObject {
2222

2323
func shouldAnimate(_ oldItem: Element?, newItem: Element) async -> Bool
2424

25+
func showLoadingStateWhenCached() -> Bool
26+
2527
// MARK: - Don't implement
2628
func onAppear()
2729
func onDisappear()
@@ -102,4 +104,8 @@ public extension BaseLoadableViewModel {
102104
func onLoadingChange(isLoading: Bool) {
103105

104106
}
107+
108+
func showLoadingStateWhenCached() -> Bool {
109+
return true
110+
}
105111
}

Sources/LoadableView/Protocols/LoadableView/IDedLoadableViewModel.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,17 @@ public extension IDedLoadableViewModel {
100100
}
101101

102102
func refresh(showLoading: Bool) async throws {
103-
setIsLoading(showLoading)
104103
do {
105104
guard let id = self.id else {
106105
throw LoadableViewError.noId
107106
}
107+
let combinedShowLoading: Bool
108+
if let cache, cache.hasValue(forKey: id) {
109+
combinedShowLoading = showLoadingStateWhenCached()
110+
} else {
111+
combinedShowLoading = showLoading
112+
}
113+
setIsLoading(combinedShowLoading)
108114
let item = try await load(id: id)
109115
cache?.insert(item, forKey: id)
110116
guard await shouldRefresh(viewState.loaded, newItem: item) else {

Sources/LoadableView/Protocols/ViewDataCache.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public protocol ViewDataCache<Key, Value> {
1010

1111
func insert(_ value: Value, forKey key: Key)
1212
func value(forKey key: Key) -> Value?
13+
func hasValue(forKey key: Key) -> Bool
1314
}

0 commit comments

Comments
 (0)