Merge branch 'download-rework' into api-update
This commit is contained in:
@@ -109,3 +109,30 @@ enum RecipeImportError: UserAlert {
|
||||
return [.OK]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
enum RequestAlert: UserAlert {
|
||||
case REQUEST_DELAYED,
|
||||
REQUEST_DROPPED,
|
||||
REQUEST_SUCCESS
|
||||
|
||||
var localizedDescription: LocalizedStringKey {
|
||||
switch self {
|
||||
case .REQUEST_DELAYED: return "Could not establish a connection to the server. The action will be retried upon reconnection."
|
||||
case .REQUEST_DROPPED: return "Unable to complete action."
|
||||
case .REQUEST_SUCCESS: return "Action completed."
|
||||
}
|
||||
}
|
||||
|
||||
var localizedTitle: LocalizedStringKey {
|
||||
switch self {
|
||||
case .REQUEST_DELAYED: return "Action delayed"
|
||||
case .REQUEST_DROPPED: return "Error"
|
||||
case .REQUEST_SUCCESS: return "Success"
|
||||
}
|
||||
}
|
||||
|
||||
var alertButtons: [AlertButton] {
|
||||
return [.OK]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,10 +61,10 @@ struct CategoryDetailView: View {
|
||||
}
|
||||
.searchable(text: $searchText, prompt: "Search recipes")
|
||||
.task {
|
||||
await viewModel.loadRecipeList(categoryName: categoryName)
|
||||
await viewModel.getCategory(named: categoryName, fetchMode: .preferLocal)
|
||||
}
|
||||
.refreshable {
|
||||
await viewModel.loadRecipeList(categoryName: categoryName, needsUpdate: true)
|
||||
await viewModel.getCategory(named: categoryName, fetchMode: .preferServer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,13 +79,20 @@ struct CategoryDetailView: View {
|
||||
|
||||
func downloadRecipes() {
|
||||
if let recipes = viewModel.recipes[categoryName] {
|
||||
let dispatchQueue = DispatchQueue(label: "RecipeDownload", qos: .background)
|
||||
dispatchQueue.async {
|
||||
Task {
|
||||
for recipe in recipes {
|
||||
Task {
|
||||
let _ = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
|
||||
let _ = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
|
||||
}
|
||||
let recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id, fetchMode: .onlyServer)
|
||||
await viewModel.saveLocal(recipeDetail, path: "recipe\(recipe.recipe_id).data")
|
||||
|
||||
let thumbnail = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, fetchMode: .onlyServer)
|
||||
guard let thumbnail = thumbnail else { continue }
|
||||
guard let thumbnailData = thumbnail.pngData() else { continue }
|
||||
await viewModel.saveLocal(thumbnailData.base64EncodedString(), path: "image\(recipe.recipe_id)_thumb")
|
||||
|
||||
let image = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, fetchMode: .onlyServer)
|
||||
guard let image = image else { continue }
|
||||
guard let imageData = image.pngData() else { continue }
|
||||
await viewModel.saveLocal(imageData.base64EncodedString(), path: "image\(recipe.recipe_id)_full")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ struct MainView: View {
|
||||
}
|
||||
.task {
|
||||
self.serverConnection = await viewModel.checkServerConnection()
|
||||
await viewModel.loadCategoryList()
|
||||
await viewModel.getCategories()//viewModel.loadCategoryList()
|
||||
// Open detail view for default category
|
||||
if userSettings.defaultCategory != "" {
|
||||
if let cat = viewModel.categories.first(where: { c in
|
||||
@@ -109,7 +109,7 @@ struct MainView: View {
|
||||
}
|
||||
.refreshable {
|
||||
self.serverConnection = await viewModel.checkServerConnection()
|
||||
await viewModel.loadCategoryList(needsUpdate: true)
|
||||
await viewModel.getCategories()//loadCategoryList(needsUpdate: true)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -212,7 +212,7 @@ struct RecipeSearchView: View {
|
||||
.navigationTitle("Search recipe")
|
||||
}
|
||||
.task {
|
||||
allRecipes = await viewModel.getAllRecipes()
|
||||
allRecipes = await viewModel.getRecipes()//.getAllRecipes()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,11 +51,11 @@ struct RecipeCardView: View {
|
||||
.clipShape(RoundedRectangle(cornerRadius: 17))
|
||||
.padding(.horizontal)
|
||||
.task {
|
||||
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true)
|
||||
recipeThumb = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, fetchMode: .preferLocal)
|
||||
self.isDownloaded = viewModel.recipeDetailExists(recipeId: recipe.recipe_id)
|
||||
}
|
||||
.refreshable {
|
||||
recipeThumb = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: true, needsUpdate: true)
|
||||
recipeThumb = await viewModel.getImage(id: recipe.recipe_id, size: .THUMB, fetchMode: .preferServer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,13 +106,13 @@ struct RecipeDetailView: View {
|
||||
}
|
||||
}
|
||||
.task {
|
||||
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id)
|
||||
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false)
|
||||
recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id, fetchMode: .preferLocal)//loadRecipeDetail(recipeId: recipe.recipe_id)
|
||||
recipeImage = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, fetchMode: .preferLocal)//.loadImage(recipeId: recipe.recipe_id, thumb: false)
|
||||
self.isDownloaded = viewModel.recipeDetailExists(recipeId: recipe.recipe_id)
|
||||
}
|
||||
.refreshable {
|
||||
recipeDetail = await viewModel.loadRecipeDetail(recipeId: recipe.recipe_id, needsUpdate: true)
|
||||
recipeImage = await viewModel.loadImage(recipeId: recipe.recipe_id, thumb: false, needsUpdate: true)
|
||||
recipeDetail = await viewModel.getRecipe(id: recipe.recipe_id, fetchMode: .preferServer)
|
||||
recipeImage = await viewModel.getImage(id: recipe.recipe_id, size: .FULL, fetchMode: .preferServer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,10 +45,12 @@ struct RecipeEditView: View {
|
||||
}
|
||||
Spacer()
|
||||
Button() {
|
||||
if viewModel.uploadNew {
|
||||
viewModel.uploadNewRecipe()
|
||||
} else {
|
||||
viewModel.uploadEditedRecipe()
|
||||
Task {
|
||||
if viewModel.uploadNew {
|
||||
await viewModel.uploadNewRecipe()
|
||||
} else {
|
||||
await viewModel.uploadEditedRecipe()
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text("Upload")
|
||||
@@ -141,7 +143,7 @@ struct RecipeEditView: View {
|
||||
}
|
||||
}
|
||||
.task {
|
||||
viewModel.keywordSuggestions = await viewModel.mainViewModel.getKeywords()
|
||||
viewModel.keywordSuggestions = await viewModel.mainViewModel.getKeywords(fetchMode: .preferServer)
|
||||
}
|
||||
.onAppear {
|
||||
viewModel.prepareView()
|
||||
@@ -150,13 +152,17 @@ struct RecipeEditView: View {
|
||||
ForEach(viewModel.alertType.alertButtons) { buttonType in
|
||||
if buttonType == .OK {
|
||||
Button(AlertButton.OK.rawValue, role: .cancel) {
|
||||
viewModel.alertAction()
|
||||
Task {
|
||||
await viewModel.alertAction()
|
||||
}
|
||||
}
|
||||
} else if buttonType == .CANCEL {
|
||||
Button(AlertButton.CANCEL.rawValue, role: .cancel) { }
|
||||
} else if buttonType == .DELETE {
|
||||
Button(AlertButton.DELETE.rawValue, role: .destructive) {
|
||||
viewModel.alertAction()
|
||||
Task {
|
||||
await viewModel.alertAction()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user