Commit a8b87fb2 by Javier Piris

Adaptados los métodos del CustonMenúViewModel para que envíe también los ids a…

Adaptados los métodos del CustonMenúViewModel para que envíe también los ids a la lista de la compra
parent c91fa16f
...@@ -10,11 +10,11 @@ namespace inutralia.ViewModels ...@@ -10,11 +10,11 @@ namespace inutralia.ViewModels
{ {
public class CustomMenuViewModel : MenuBaseViewModel public class CustomMenuViewModel : MenuBaseViewModel
{ {
public CustomMenuViewModel () : this ( new Models.Menu()) public CustomMenuViewModel() : this(new Models.Menu())
{ {
} }
public CustomMenuViewModel (Models.Menu menu) : base (menu) public CustomMenuViewModel(Models.Menu menu) : base(menu)
{ {
} }
...@@ -47,11 +47,11 @@ namespace inutralia.ViewModels ...@@ -47,11 +47,11 @@ namespace inutralia.ViewModels
{ {
if ((Index >= 0) && (Index < _LocalMenu?.DaySelections?.Length)) if ((Index >= 0) && (Index < _LocalMenu?.DaySelections?.Length))
{ {
var retVal = _LocalMenu.DaySelections [Index]; var retVal = _LocalMenu.DaySelections[Index];
if(retVal == null) if (retVal == null)
{ {
retVal = new LocalMenu.MealSelections (); retVal = new LocalMenu.MealSelections();
_LocalMenu.DaySelections [Index] = retVal; _LocalMenu.DaySelections[Index] = retVal;
} //endif } //endif
return retVal; return retVal;
...@@ -74,9 +74,9 @@ namespace inutralia.ViewModels ...@@ -74,9 +74,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.LunchFirstIndex = value ? 0 : -1; CurrentSelections.LunchFirstIndex = value ? 0 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsLunchFirstOption1"); OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2"); OnPropertyChanged("IsLunchFirstOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -93,9 +93,9 @@ namespace inutralia.ViewModels ...@@ -93,9 +93,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.LunchFirstIndex = value ? 1 : -1; CurrentSelections.LunchFirstIndex = value ? 1 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsLunchFirstOption1"); OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2"); OnPropertyChanged("IsLunchFirstOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -112,9 +112,9 @@ namespace inutralia.ViewModels ...@@ -112,9 +112,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.LunchSecondIndex = value ? 0 : -1; CurrentSelections.LunchSecondIndex = value ? 0 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsLunchSecondOption1"); OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2"); OnPropertyChanged("IsLunchSecondOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -131,9 +131,9 @@ namespace inutralia.ViewModels ...@@ -131,9 +131,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.LunchSecondIndex = value ? 1 : -1; CurrentSelections.LunchSecondIndex = value ? 1 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsLunchSecondOption1"); OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2"); OnPropertyChanged("IsLunchSecondOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -150,9 +150,9 @@ namespace inutralia.ViewModels ...@@ -150,9 +150,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.DinnerFirstIndex = value ? 0 : -1; CurrentSelections.DinnerFirstIndex = value ? 0 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsDinnerFirstOption1"); OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2"); OnPropertyChanged("IsDinnerFirstOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -169,9 +169,9 @@ namespace inutralia.ViewModels ...@@ -169,9 +169,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.DinnerFirstIndex = value ? 1 : -1; CurrentSelections.DinnerFirstIndex = value ? 1 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsDinnerFirstOption1"); OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2"); OnPropertyChanged("IsDinnerFirstOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -188,9 +188,9 @@ namespace inutralia.ViewModels ...@@ -188,9 +188,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.DinnerSecondIndex = value ? 0 : -1; CurrentSelections.DinnerSecondIndex = value ? 0 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsDinnerSecondOption1"); OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2"); OnPropertyChanged("IsDinnerSecondOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -207,9 +207,9 @@ namespace inutralia.ViewModels ...@@ -207,9 +207,9 @@ namespace inutralia.ViewModels
if (value != current) if (value != current)
{ {
CurrentSelections.DinnerSecondIndex = value ? 1 : -1; CurrentSelections.DinnerSecondIndex = value ? 1 : -1;
SelectionChanged (); SelectionChanged();
OnPropertyChanged ("IsDinnerSecondOption1"); OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2"); OnPropertyChanged("IsDinnerSecondOption2");
} //endif } //endif
} //endif } //endif
} }
...@@ -219,33 +219,33 @@ namespace inutralia.ViewModels ...@@ -219,33 +219,33 @@ namespace inutralia.ViewModels
protected bool _SelectionHasBeenChanged = false; protected bool _SelectionHasBeenChanged = false;
public async Task LoadData () public async Task LoadData()
{ {
bool ShouldLoadRecipes = false; bool ShouldLoadRecipes = false;
IsBusy = true; IsBusy = true;
OnPropertyChanged ("NoMenuPanel"); OnPropertyChanged("NoMenuPanel");
// Obtener menú local // Obtener menú local
var lista = await App.LocalData.RefreshListAsync<LocalMenu> (); var lista = await App.LocalData.RefreshListAsync<LocalMenu>();
if (lista.Count > 0) if (lista.Count > 0)
{ {
_LocalMenu = lista [0]; _LocalMenu = lista[0];
OnDataRefreshed (); OnDataRefreshed();
OnPropertyChanged ("HasMenu"); OnPropertyChanged("HasMenu");
} //endif } //endif
// Obtener menú del servidor // Obtener menú del servidor
if (await App.API.RefreshItemAsync (Model)) if (await App.API.RefreshItemAsync(Model))
{ {
Models.Menu ServerMenu = Model as Models.Menu; Models.Menu ServerMenu = Model as Models.Menu;
if (_LocalMenu == null) if (_LocalMenu == null)
{ {
// No hay menú local, copiar directamente el del servidor // No hay menú local, copiar directamente el del servidor
_LocalMenu = new LocalMenu (); _LocalMenu = new LocalMenu();
_LocalMenu.AssignFromMenu (ServerMenu); _LocalMenu.AssignFromMenu(ServerMenu);
await App.LocalData.UpdateItemAsync (_LocalMenu, true); await App.LocalData.UpdateItemAsync(_LocalMenu, true);
// Indicar que hay que cargar recetas // Indicar que hay que cargar recetas
ShouldLoadRecipes = true; ShouldLoadRecipes = true;
...@@ -253,7 +253,7 @@ namespace inutralia.ViewModels ...@@ -253,7 +253,7 @@ namespace inutralia.ViewModels
else else
{ {
// Hay menú local. Comprobar si el del servidor es superior al último recibido // Hay menú local. Comprobar si el del servidor es superior al último recibido
if(_LocalMenu.LastReceivedMenuId != ServerMenu.Id) if (_LocalMenu.LastReceivedMenuId != ServerMenu.Id)
{ {
// En ese caso, preguntar si se quiere utilizar el nuevo ... // En ese caso, preguntar si se quiere utilizar el nuevo ...
if (await Application.Current?.MainPage.DisplayAlert("Nuevo menú semanal", "Hay un nuevo menú semanal disponible", "Utilizarlo", "Descartarlo")) if (await Application.Current?.MainPage.DisplayAlert("Nuevo menú semanal", "Hay un nuevo menú semanal disponible", "Utilizarlo", "Descartarlo"))
...@@ -286,26 +286,26 @@ namespace inutralia.ViewModels ...@@ -286,26 +286,26 @@ namespace inutralia.ViewModels
// Si es necesario, cargar recetas y actualizar // Si es necesario, cargar recetas y actualizar
if (ShouldLoadRecipes) if (ShouldLoadRecipes)
{ {
await LoadAllRecipesAsync (); await LoadAllRecipesAsync();
await App.LocalData.UpdateItemAsync (_LocalMenu, false); await App.LocalData.UpdateItemAsync(_LocalMenu, false);
} //endif } //endif
// Actualizar día seleccionado // Actualizar día seleccionado
OnDataRefreshed (); OnDataRefreshed();
OnPropertyChanged ("HasMenu"); OnPropertyChanged("HasMenu");
_SelectionHasBeenChanged = false; _SelectionHasBeenChanged = false;
IsBusy = false; IsBusy = false;
OnPropertyChanged ("NoMenuPanel"); OnPropertyChanged("NoMenuPanel");
} }
protected void SelectionChanged () protected void SelectionChanged()
{ {
_SelectionHasBeenChanged = true; _SelectionHasBeenChanged = true;
} }
public async Task UpdateShoppingListAsync () public async Task UpdateShoppingListAsync()
{ {
if (_SelectionHasBeenChanged) if (_SelectionHasBeenChanged)
{ {
...@@ -313,41 +313,48 @@ namespace inutralia.ViewModels ...@@ -313,41 +313,48 @@ namespace inutralia.ViewModels
// Esperar a que las recetas estén listas // Esperar a que las recetas estén listas
while (IsLoadingRecipes) while (IsLoadingRecipes)
await Task.Delay (200); await Task.Delay(200);
// Preparar la lista de ingredientes a partir de las recetas seleccionadas // Preparar la lista de ingredientes a partir de las recetas seleccionadas
var ingredients = new HashSet<string> ();
//var ingredients = new HashSet<string>();
var ingredients = new HashSet<Ingredient>();
for (int i = 0; i < _LocalMenu?.DaySelections?.Length; i++) for (int i = 0; i < _LocalMenu?.DaySelections?.Length; i++)
{ {
if (i < _LocalMenu.Days.Count) if (i < _LocalMenu.Days.Count)
{ {
var Day = _LocalMenu.Days [i]; var Day = _LocalMenu.Days[i];
var meals = _LocalMenu.DaySelections [i]; var meals = _LocalMenu.DaySelections[i];
if (meals != null) if (meals != null)
{ {
AddRecipe (ingredients, Day.LunchFirst, meals.LunchFirstIndex); AddRecipe(ingredients, Day.LunchFirst, meals.LunchFirstIndex);
AddRecipe (ingredients, Day.LunchSecond, meals.LunchSecondIndex); AddRecipe(ingredients, Day.LunchSecond, meals.LunchSecondIndex);
AddRecipe (ingredients, Day.DinnerFirst, meals.DinnerFirstIndex); AddRecipe(ingredients, Day.DinnerFirst, meals.DinnerFirstIndex);
AddRecipe (ingredients, Day.DinnerSecond, meals.DinnerSecondIndex); AddRecipe(ingredients, Day.DinnerSecond, meals.DinnerSecondIndex);
} //endif } //endif
} //endif } //endif
} //endfor } //endfor
// Eliminar de la lista de la compra los ingredientes que no estén en los encontrados // Eliminar de la lista de la compra los ingredientes que no estén en los encontrados
var listaCompra = await App.LocalData.RefreshListAsync<ShoppingList> (); var listaCompra = await App.LocalData.RefreshListAsync<ShoppingList>();
foreach (var ing in listaCompra.Where (sl => sl.FromMenus && !ingredients.Contains (sl.Text)))
//foreach (var ing in listaCompra.Where(sl => sl.FromMenus && !ingredients.Contains(sl.Text)))
foreach (var ing in listaCompra.Where(sl => sl.FromMenus && !ingredients.Any(o => o.Name.Contains(sl.Text))))
{ {
await App.LocalData.DeleteItemAsync (ing); await App.LocalData.DeleteItemAsync(ing);
} //endforeach } //endforeach
// Añadir a la lista de la compra los que falten // Añadir a la lista de la compra los que falten
foreach (string ing in ingredients.Where (i => listaCompra.Find (sl => sl.FromMenus && sl.Text.Equals (i)) == null))
//foreach (string ing in ingredients.Where (i => listaCompra.Find (sl => sl.FromMenus && sl.Text.Equals (i)) == null))
foreach (Ingredient ing in ingredients.Where(i => listaCompra.Find(sl => sl.FromMenus && sl.Text.Equals(i)) == null))
{ {
await App.LocalData.UpdateItemAsync (new ShoppingList () { FromMenus = true, Select = false, Text = ing }, true); //await App.LocalData.UpdateItemAsync(new ShoppingList() { FromMenus = true, Select = false, Text = ing }, true);
await App.LocalData.UpdateItemAsync(new ShoppingList() { FromMenus = true, Select = false, Text = ing.Name, Id = ing.Id }, true);
} //endforeach } //endforeach
await App.LocalData.UpdateItemAsync (_LocalMenu); await App.LocalData.UpdateItemAsync(_LocalMenu);
IsBusy = false; IsBusy = false;
...@@ -355,48 +362,50 @@ namespace inutralia.ViewModels ...@@ -355,48 +362,50 @@ namespace inutralia.ViewModels
} //endif } //endif
} }
protected void AddRecipe ( HashSet<string> ingredients, IList<Recipe> recipes, int index) //protected void AddRecipe ( HashSet<string> ingredients, IList<Recipe> recipes, int index)
protected void AddRecipe(HashSet<Ingredient> ingredients, IList<Recipe> recipes, int index)
{ {
if( (index >= 0) && (index < recipes.Count) ) if ((index >= 0) && (index < recipes.Count))
{ {
if (recipes [index].Ingredients != null) if (recipes[index].Ingredients != null)
{ {
foreach (var ing in recipes [index].Ingredients) foreach (var ing in recipes[index].Ingredients)
{ {
ingredients.Add (ing.Name); //ingredients.Add (ing.Name);
ingredients.Add(new Ingredient() { Name = ing.Name, Id = ing.Id });
} //endforeach } //endforeach
} //endif } //endif
} //endif } //endif
} }
protected override void OnDataRefreshed () protected override void OnDataRefreshed()
{ {
base.OnDataRefreshed (); base.OnDataRefreshed();
OnPropertyChanged ("CurrentSelections"); OnPropertyChanged("CurrentSelections");
OnPropertyChanged ("IsLunchFirstOption1"); OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2"); OnPropertyChanged("IsLunchFirstOption2");
OnPropertyChanged ("IsLunchSecondOption1"); OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2"); OnPropertyChanged("IsLunchSecondOption2");
OnPropertyChanged ("IsDinnerFirstOption1"); OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2"); OnPropertyChanged("IsDinnerFirstOption2");
OnPropertyChanged ("IsDinnerSecondOption1"); OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2"); OnPropertyChanged("IsDinnerSecondOption2");
} }
protected override void OnIndexChanged () protected override void OnIndexChanged()
{ {
base.OnIndexChanged (); base.OnIndexChanged();
OnPropertyChanged ("CurrentSelections"); OnPropertyChanged("CurrentSelections");
OnPropertyChanged ("IsLunchFirstOption1"); OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2"); OnPropertyChanged("IsLunchFirstOption2");
OnPropertyChanged ("IsLunchSecondOption1"); OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2"); OnPropertyChanged("IsLunchSecondOption2");
OnPropertyChanged ("IsDinnerFirstOption1"); OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2"); OnPropertyChanged("IsDinnerFirstOption2");
OnPropertyChanged ("IsDinnerSecondOption1"); OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2"); OnPropertyChanged("IsDinnerSecondOption2");
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment