Skip to content

Commit 61e78ba

Browse files
committed
Changed PrefetchViewModel init with params to init()
Use load(...) functions instead now.
1 parent 72ad00b commit 61e78ba

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

Source/PrefetchImageModel.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,60 @@ public final class PrefetchViewModel: ObservableObject, ScrollViewPrefetcherDele
1414
private let scrollViewPrefetcer: ScrollViewPrefetcher
1515
public private(set) var urls: [URL]
1616

17-
public init(photoURLs: [URL]) {
18-
self.imagePrefetcher = ImagePrefetcher()
19-
self.scrollViewPrefetcer = ScrollViewPrefetcher()
20-
self.urls = photoURLs
21-
22-
self.scrollViewPrefetcer.delegate = self
17+
public init() {
18+
imagePrefetcher = ImagePrefetcher()
19+
scrollViewPrefetcer = ScrollViewPrefetcher()
20+
urls = []
21+
scrollViewPrefetcer.delegate = self
22+
}
23+
24+
public func load(photoURLs: [URL]) {
25+
urls = photoURLs
26+
scrollViewPrefetcer.scheduleRefreshIfNeeded()
2327
}
2428

25-
public init(photoReferences: [StorageReference], uniqueURL: @escaping (StorageReference) -> URL?) {
26-
self.imagePrefetcher = ImagePrefetcher()
27-
self.scrollViewPrefetcer = ScrollViewPrefetcher()
28-
self.urls = []
29-
self.scrollViewPrefetcer.delegate = self
29+
public func load(photoReferences: [StorageReference], uniqueURL: @escaping (StorageReference) -> URL?) {
30+
let loadTask = DispatchGroup()
31+
let totalRefs = photoReferences.count
32+
var currentRef: Int = 1
3033

3134
for ref in photoReferences {
35+
if currentRef == 1 {
36+
loadTask.enter()
37+
}
38+
3239
if let cachedURL = uniqueURL(ref) {
33-
self.urls.append(cachedURL)
40+
urls.append(cachedURL)
41+
42+
currentRef = currentRef + 1
43+
if currentRef >= totalRefs {
44+
loadTask.leave()
45+
}
3446
continue
3547
} else {
3648
DispatchQueue.global(qos: .userInitiated).async {
3749
ref.downloadURL { (foundURL, thrownError) in
3850
if let safeFoundURL = foundURL {
3951
self.urls.append(safeFoundURL)
52+
currentRef = currentRef + 1
4053
} else {
4154
if let safeError = thrownError {
4255
print("[PrefetchViewModel] Unable to fetch download URL for photo. \(safeError)")
56+
currentRef = currentRef + 1
4357
}
4458
}
59+
60+
if currentRef >= totalRefs {
61+
loadTask.leave()
62+
}
4563
}
4664
}
4765
}
4866
}
67+
68+
loadTask.notify(queue: DispatchQueue.main) {
69+
self.scrollViewPrefetcer.scheduleRefreshIfNeeded()
70+
}
4971
}
5072

5173
func onAppear(_ index: Int) {

Source/ScrollViewPrefetcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public final class ScrollViewPrefetcher {
4343

4444
/// SwiftUI sometimes calls onAppear in unexpected order, buffer takes care of it.
4545
///
46-
private func scheduleRefreshIfNeeded() {
46+
internal func scheduleRefreshIfNeeded() {
4747
guard !isRefreshScheduled else { return }
4848
isRefreshScheduled = true
4949
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { [weak self] in

0 commit comments

Comments
 (0)