Better image caching
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ struct RecipeDetailView: View {
|
|||||||
Divider()
|
Divider()
|
||||||
RecipeYieldSection(recipeDetail: recipeDetail)
|
RecipeYieldSection(recipeDetail: recipeDetail)
|
||||||
RecipeDurationSection(recipeDetail: recipeDetail)
|
RecipeDurationSection(recipeDetail: recipeDetail)
|
||||||
|
LazyVGrid(columns: [GridItem(.adaptive(minimum: 400), alignment: .top)]) {
|
||||||
if(!recipeDetail.recipeIngredient.isEmpty) {
|
if(!recipeDetail.recipeIngredient.isEmpty) {
|
||||||
RecipeIngredientSection(recipeDetail: recipeDetail)
|
RecipeIngredientSection(recipeDetail: recipeDetail)
|
||||||
}
|
}
|
||||||
@@ -54,6 +55,7 @@ struct RecipeDetailView: View {
|
|||||||
if(!recipeDetail.recipeInstructions.isEmpty) {
|
if(!recipeDetail.recipeInstructions.isEmpty) {
|
||||||
RecipeInstructionSection(recipeDetail: recipeDetail)
|
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) {
|
||||||
|
Text("\u{2022}")
|
||||||
|
Text("\(ingredient)")
|
||||||
.multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
|
}
|
||||||
.padding(4)
|
.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) {
|
||||||
|
Text("\u{2022}")
|
||||||
|
Text("\(tool)")
|
||||||
.multilineTextAlignment(.leading)
|
.multilineTextAlignment(.leading)
|
||||||
|
}
|
||||||
.padding(4)
|
.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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user