Image caching fixes

This commit is contained in:
Vicnet
2023-09-17 20:45:35 +02:00
parent 9088301b15
commit 5fdfed9413
5 changed files with 65 additions and 13 deletions

View File

@@ -63,6 +63,7 @@ struct RecipeDetail: Codable {
} }
struct RecipeImage { struct RecipeImage {
var imageExists: Bool = true
var thumb: UIImage? var thumb: UIImage?
var full: UIImage? var full: UIImage?
} }

View File

@@ -16,10 +16,18 @@ class DataStore {
appropriateFor: nil, appropriateFor: nil,
create: false create: false
) )
.appendingPathComponent(appending) .appendingPathComponent(appending)
} }
private static func fileURL() throws -> URL {
try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
)
}
func load<D: Decodable>(fromPath path: String) async throws -> D? { func load<D: Decodable>(fromPath path: String) async throws -> D? {
let task = Task<D?, Error> { let task = Task<D?, Error> {
let fileURL = try Self.fileURL(appending: path) let fileURL = try Self.fileURL(appending: path)
@@ -45,12 +53,24 @@ class DataStore {
} }
} }
func clearAll() { func clearAll() -> Bool {
print("Attempting to delete all data ...")
let fm = FileManager.default
guard let folderPath = fm.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
print("Folder path: ", folderPath)
do { do {
try FileManager.default.removeItem(at: Self.fileURL(appending: "")) let filePaths = try fm.contentsOfDirectory(atPath: folderPath)
} catch { for filePath in filePaths {
print("Could not delete file, probably read-only filesystem") print("File path: ", filePath)
try fm.removeItem(atPath: folderPath + filePath)
} }
} catch {
print("Could not delete documents folder contents: \(error)")
return false
}
print("Done.")
return true
} }
} }

View File

@@ -77,22 +77,36 @@ import UIKit
/// - needsUpdate: Determines wether the image should be loaded directly from the server, or if it should be loaded from cache/store first. /// - needsUpdate: Determines wether the image should be loaded directly from the server, or if it should be loaded from cache/store first.
/// - Returns: The image if found locally or on the server, otherwise nil. /// - Returns: The image if found locally or on the server, otherwise nil.
func loadImage(recipeId: Int, full: Bool, needsUpdate: Bool = false) async -> UIImage? { func loadImage(recipeId: Int, full: Bool, needsUpdate: Bool = false) async -> UIImage? {
print("loadImage(recipeId: \(recipeId), full: \(full), needsUpdate: \(needsUpdate)") print("loadImage(recipeId: \(recipeId), full: \(full), needsUpdate: \(needsUpdate))")
// If the image needs an update, request it from the server and overwrite the stored image // If the image needs an update, request it from the server and overwrite the stored image
if needsUpdate { if needsUpdate {
if let data = await imageDataFromServer(recipeId: recipeId, full: full) { if let data = await imageDataFromServer(recipeId: recipeId, full: full) {
guard let image = UIImage(data: data) else { return nil } guard let image = UIImage(data: data) else {
imageCache[recipeId] = RecipeImage(imageExists: false)
return nil
}
await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full)) await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full))
imageToCache(image: image, recipeId: recipeId, full: full) imageToCache(image: image, recipeId: recipeId, full: full)
return image return image
} else {
imageCache[recipeId] = RecipeImage(imageExists: false)
return nil
} }
} }
// Check imageExists flag to detect if we attempted to load a non-existing image before.
// This allows us to avoid sending requests to the server if we already know the recipe has no image.
if imageCache[recipeId] != nil {
guard imageCache[recipeId]!.imageExists else { return nil }
}
// Try to load image from cache // Try to load image from cache
print("Attempting to load image from cache ...") print("Attempting to load image from cache ...")
if imageCache[recipeId] != nil { if let image = imageFromCache(recipeId: recipeId, full: full) {
print("Image found in cache.") print("Image found in cache.")
return imageFromCache(recipeId: recipeId, full: full) return image
} }
// Try to load from store // Try to load from store
print("Attempting to load image from local storage ...") print("Attempting to load image from local storage ...")
if let image = await imageFromStore(recipeId: recipeId, full: full) { if let image = await imageFromStore(recipeId: recipeId, full: full) {
@@ -100,18 +114,32 @@ import UIKit
imageToCache(image: image, recipeId: recipeId, full: full) imageToCache(image: image, recipeId: recipeId, full: full)
return image return image
} }
// Try to load from the server. Store if successfull. // Try to load from the server. Store if successfull.
print("Attempting to load image from server ...") print("Attempting to load image from server ...")
if let data = await imageDataFromServer(recipeId: recipeId, full: full) { if let data = await imageDataFromServer(recipeId: recipeId, full: full) {
print("Image data received.") print("Image data received.")
imageCache[recipeId] = RecipeImage() // Create empty RecipeImage for each recipe even if no image found, so that further server requests are only sent if explicitly requested. // Create empty RecipeImage for each recipe even if no image found, so that further server requests are only sent if explicitly requested.
guard let image = UIImage(data: data) else { return nil } guard let image = UIImage(data: data) else {
imageCache[recipeId] = RecipeImage(imageExists: false)
return nil
}
await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full)) await dataStore.save(data: data.base64EncodedString(), toPath: localImagePath(recipeId, full))
imageToCache(image: image, recipeId: recipeId, full: full) imageToCache(image: image, recipeId: recipeId, full: full)
return image return image
} }
imageCache[recipeId] = RecipeImage(imageExists: false)
return nil return nil
} }
func deleteAllData() {
if dataStore.clearAll() {
self.categories = []
self.recipes = [:]
self.imageCache = [:]
self.recipeDetails = [:]
}
}
} }
@@ -140,11 +168,13 @@ extension MainViewModel {
private func imageToCache(image: UIImage, recipeId: Int, full: Bool) { private func imageToCache(image: UIImage, recipeId: Int, full: Bool) {
if imageCache[recipeId] == nil { if imageCache[recipeId] == nil {
imageCache[recipeId] = RecipeImage() imageCache[recipeId] = RecipeImage(imageExists: true)
} }
if full { if full {
imageCache[recipeId]!.imageExists = true
imageCache[recipeId]!.full = image imageCache[recipeId]!.full = image
} else { } else {
imageCache[recipeId]!.imageExists = true
imageCache[recipeId]!.thumb = image imageCache[recipeId]!.thumb = image
} }
} }

View File

@@ -30,7 +30,7 @@ struct MainView: View {
} }
.navigationTitle("CookBook") .navigationTitle("CookBook")
.toolbar { .toolbar {
NavigationLink( destination: SettingsView(userSettings: userSettings)) { NavigationLink( destination: SettingsView(userSettings: userSettings, viewModel: viewModel)) {
Image(systemName: "gear") Image(systemName: "gear")
} }
} }

View File

@@ -10,6 +10,7 @@ import SwiftUI
struct SettingsView: View { struct SettingsView: View {
@ObservedObject var userSettings: UserSettings @ObservedObject var userSettings: UserSettings
@ObservedObject var viewModel: MainViewModel
var body: some View { var body: some View {
List { List {
@@ -33,7 +34,7 @@ struct SettingsView: View {
{ {
Button("Clear Cache") { Button("Clear Cache") {
print("Clear cache.") print("Clear cache.")
viewModel.deleteAllData()
} }
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
.accentColor(.red) .accentColor(.red)