Skip to content

Commit 3a6f5bc

Browse files
author
Laurenz Lazarus
committed
Only refresh images if at least one new image was downloaded
1 parent 8203131 commit 3a6f5bc

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

BingWallpaper.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@
587587
GCC_WARN_UNUSED_FUNCTION = YES;
588588
GCC_WARN_UNUSED_VARIABLE = YES;
589589
MACOSX_DEPLOYMENT_TARGET = 11.0;
590-
MARKETING_VERSION = 0.4.4;
590+
MARKETING_VERSION = 0.4.5b;
591591
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
592592
MTL_FAST_MATH = YES;
593593
ONLY_ACTIVE_ARCH = YES;
@@ -643,7 +643,7 @@
643643
GCC_WARN_UNUSED_FUNCTION = YES;
644644
GCC_WARN_UNUSED_VARIABLE = YES;
645645
MACOSX_DEPLOYMENT_TARGET = 11.0;
646-
MARKETING_VERSION = 0.4.4;
646+
MARKETING_VERSION = 0.4.5b;
647647
MTL_ENABLE_DEBUG_INFO = NO;
648648
MTL_FAST_MATH = YES;
649649
SDKROOT = macosx;

BingWallpaper/Controller/MenuController.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MenuController: NSObject {
2323
self.menu = createMenu()
2424
self.statusItem!.menu = menu
2525

26-
imagesUpdated()
26+
showNewestImage()
2727
}
2828

2929
private func createStatusBarItem() -> NSStatusItem {
@@ -186,16 +186,21 @@ class MenuController: NSObject {
186186
if description == nil { return "" }
187187
return description?.split(separator: "(").last?.replacingOccurrences(of: ")", with: "") ?? ""
188188
}
189+
190+
@MainActor
191+
private func showNewestImage() {
192+
self.descriptors = Database.instance.allImageDescriptors()
193+
.filter { $0.image.isOnDisk() }
194+
selectedDescriptorIndex = self.descriptors.firstIndex(where: { $0 == self.descriptors.last }) ?? self.descriptors.endIndex
195+
updateSelectedImage(newSelectedDescriptorIndex: selectedDescriptorIndex)
196+
}
189197
}
190198

191199
// MARK: - Delegates
192200

193201
extension MenuController: UpdateManagerDelegate {
194-
func imagesUpdated() {
195-
self.descriptors = Database.instance.allImageDescriptors()
196-
.filter { $0.image.isOnDisk() }
197-
selectedDescriptorIndex = self.descriptors.firstIndex(where: { $0 == self.descriptors.last }) ?? self.descriptors.endIndex
198-
updateSelectedImage(newSelectedDescriptorIndex: selectedDescriptorIndex)
202+
func downloadedNewImage() {
203+
showNewestImage()
199204
}
200205
}
201206

BingWallpaper/Database/Models/Image.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ class Image {
2424
return try Data(contentsOf: downloadPath)
2525
}
2626

27-
func download() async throws -> Data {
27+
func downloadAndSaveToDisk() async throws {
2828
guard let descriptor else {
2929
throw Error.missingDescriptor
3030
}
31-
return try await DownloadManager.downloadBinary(from: descriptor.imageUrl)
31+
let imageData = try await DownloadManager.downloadBinary(from: descriptor.imageUrl)
32+
try FileHandler.saveImageDataToDisk(imageData: imageData, toUrl: downloadPath)
3233
}
3334

3435
static func isSavedToDisk(descriptor: ImageDescriptor) -> Bool {
@@ -39,9 +40,4 @@ class Image {
3940
func isOnDisk() -> Bool {
4041
return FileManager.default.fileExists(atPath: downloadPath.relativePath)
4142
}
42-
43-
func saveToDisk(imageData: Data) throws {
44-
try FileHandler.saveImageDataToDisk(imageData: imageData, toUrl: downloadPath)
45-
}
46-
4743
}

BingWallpaper/Manager/UpdateManager.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import AppKit
33

44
protocol UpdateManagerDelegate: AnyObject {
55
@MainActor
6-
func imagesUpdated()
6+
func downloadedNewImage()
77
}
88

99
class UpdateManager {
@@ -84,17 +84,18 @@ class UpdateManager {
8484

8585
for descriptor in newDescriptors {
8686
do {
87-
let imageData = try await descriptor.image.download()
88-
try descriptor.image.saveToDisk(imageData: imageData)
87+
try await descriptor.image.downloadAndSaveToDisk()
8988
} catch {
90-
print("Failed to download and store image with error: \(error.localizedDescription)")
89+
print("Failed to download and store image \(descriptor.imageUrl) with error: \(error.localizedDescription)")
9190
}
9291
}
9392

9493
await MainActor.run { [weak self] in
9594
guard let self = self else { return }
9695
self.cleanup()
97-
self.delegate?.imagesUpdated()
96+
if newDescriptors.isEmpty == false {
97+
self.delegate?.downloadedNewImage()
98+
}
9899
let fetchInterval = UpdateScheduleManager.nextFetchTimeInterval()
99100
print("Update complete, next update at \(Date().addingTimeInterval(fetchInterval))")
100101

0 commit comments

Comments
 (0)