File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
Sources/LoadableView/Protocols Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ public protocol IDedLoadableViewModel: BaseLoadableViewModel {
1818
1919 // MARK: - Optional implementation
2020 func cancel( id: ID ) async
21+
22+ var cache : ( any ViewDataCache < ID , Element > ) ? { get }
2123}
2224
2325public extension IDedLoadableViewModel {
@@ -26,6 +28,10 @@ public extension IDedLoadableViewModel {
2628 // Implement to cancel loading
2729 }
2830
31+ var cache : ( any ViewDataCache < ID , Element > ) ? {
32+ return nil
33+ }
34+
2935 func cancel( ) async {
3036 guard let id = self . id else {
3137 assertionFailure ( " No ID " )
@@ -60,9 +66,14 @@ public extension IDedLoadableViewModel {
6066 guard case . notLoaded = viewState else {
6167 return
6268 }
63- setIsLoading ( true )
69+ if let cache, let cached = cache. value ( forKey: id) {
70+ viewState = . loaded( cached)
71+ } else {
72+ setIsLoading ( true )
73+ }
6474 self . id = id
6575 let item = try await load ( id: id)
76+ cache? . insert ( item, forKey: id)
6677 if let reloadsWhenForegrounding = self as? ( any ReloadsWhenForegrounding ) {
6778 reloadsWhenForegrounding. setLastLoaded ( )
6879 }
Original file line number Diff line number Diff line change 1+ // Created by Axel Ancona Esselmann on 11/20/24.
2+ //
3+
4+ import Foundation
5+
6+ public protocol ViewDataCache < Key, Value> {
7+ associatedtype Key
8+ where Key: Hashable
9+ associatedtype Value
10+
11+ func insert( _ value: Value , forKey key: Key )
12+ func value( forKey key: Key ) -> Value ?
13+ }
You can’t perform that action at this time.
0 commit comments