Better image caching

This commit is contained in:
Vicnet
2023-09-17 16:37:08 +02:00
parent 3504ce2a25
commit 9088301b15
3 changed files with 37 additions and 28 deletions

View File

@@ -77,7 +77,7 @@ 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))") 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) {
@@ -88,17 +88,23 @@ import UIKit
} }
} }
// Try to load image from cache // Try to load image from cache
print("Attempting to load image from local storage ...") print("Attempting to load image from cache ...")
if let image = imageFromCache(recipeId: recipeId, full: full) { if imageCache[recipeId] != nil {
return image print("Image found in cache.")
return imageFromCache(recipeId: recipeId, full: full)
} }
// Try to load from store // Try to load from store
print("Attempting to load image from server ...") 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) {
print("Image found in local storage.")
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 ...")
if let data = await imageDataFromServer(recipeId: recipeId, full: full) { if let data = await imageDataFromServer(recipeId: recipeId, full: full) {
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.
guard let image = UIImage(data: data) else { return nil } guard let image = UIImage(data: data) else { 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)

View File

@@ -14,7 +14,7 @@ struct RecipeBookView: View {
@State var categoryName: String @State var categoryName: String
@ObservedObject var viewModel: MainViewModel @ObservedObject var viewModel: MainViewModel
var body: some View { var body: some View {
ScrollView { ScrollView(showsIndicators: false) {
LazyVStack { LazyVStack {
if let recipes = viewModel.recipes[categoryName] { if let recipes = viewModel.recipes[categoryName] {
ForEach(recipes, id: \.recipe_id) { recipe in ForEach(recipes, id: \.recipe_id) { recipe in

View File

@@ -45,14 +45,16 @@ struct RecipeDetailView: View {
Divider() Divider()
RecipeYieldSection(recipeDetail: recipeDetail) RecipeYieldSection(recipeDetail: recipeDetail)
RecipeDurationSection(recipeDetail: recipeDetail) RecipeDurationSection(recipeDetail: recipeDetail)
if(!recipeDetail.recipeIngredient.isEmpty) { LazyVGrid(columns: [GridItem(.adaptive(minimum: 400), alignment: .top)]) {
RecipeIngredientSection(recipeDetail: recipeDetail) if(!recipeDetail.recipeIngredient.isEmpty) {
} RecipeIngredientSection(recipeDetail: recipeDetail)
if(!recipeDetail.tool.isEmpty) { }
RecipeToolSection(recipeDetail: recipeDetail) if(!recipeDetail.tool.isEmpty) {
} RecipeToolSection(recipeDetail: recipeDetail)
if(!recipeDetail.recipeInstructions.isEmpty) { }
RecipeInstructionSection(recipeDetail: recipeDetail) if(!recipeDetail.recipeInstructions.isEmpty) {
RecipeInstructionSection(recipeDetail: recipeDetail)
}
} }
}.padding(.horizontal, 5) }.padding(.horizontal, 5)
@@ -130,19 +132,20 @@ struct RecipeIngredientSection: View {
@State var recipeDetail: RecipeDetail @State var recipeDetail: RecipeDetail
var body: some View { var body: some View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Divider()
HStack { HStack {
SecondaryLabel(text: "Ingredients") SecondaryLabel(text: "Ingredients")
Spacer() Spacer()
} }
ForEach(recipeDetail.recipeIngredient, id: \.self) { ingredient in ForEach(recipeDetail.recipeIngredient, id: \.self) { ingredient in
Text("\u{2022} \(ingredient)") HStack(alignment: .top) {
.multilineTextAlignment(.leading) Text("\u{2022}")
.padding(4) Text("\(ingredient)")
.multilineTextAlignment(.leading)
}
.padding(4)
} }
}.padding() }.padding()
.frame(maxWidth: .infinity)
.background(Color("accent"))
.clipShape(RoundedRectangle(cornerRadius: 10))
} }
} }
@@ -150,19 +153,20 @@ struct RecipeToolSection: View {
@State var recipeDetail: RecipeDetail @State var recipeDetail: RecipeDetail
var body: some View { var body: some View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Divider()
HStack { HStack {
SecondaryLabel(text: "Tools") SecondaryLabel(text: "Tools")
Spacer() Spacer()
} }
ForEach(recipeDetail.tool, id: \.self) { tool in ForEach(recipeDetail.tool, id: \.self) { tool in
Text("\u{2022} \(tool)") HStack(alignment: .top) {
.multilineTextAlignment(.leading) Text("\u{2022}")
.padding(4) Text("\(tool)")
.multilineTextAlignment(.leading)
}
.padding(4)
} }
}.padding() }.padding()
.frame(maxWidth: .infinity)
.background(Color("accent"))
.clipShape(RoundedRectangle(cornerRadius: 10))
} }
} }
@@ -170,6 +174,7 @@ struct RecipeInstructionSection: View {
@State var recipeDetail: RecipeDetail @State var recipeDetail: RecipeDetail
var body: some View { var body: some View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Divider()
HStack { HStack {
SecondaryLabel(text: "Instructions") SecondaryLabel(text: "Instructions")
Spacer() Spacer()
@@ -181,8 +186,6 @@ struct RecipeInstructionSection: View {
}.padding(4) }.padding(4)
} }
}.padding() }.padding()
.background(Color("accent"))
.clipShape(RoundedRectangle(cornerRadius: 10))
} }
} }