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
......@@ -8,15 +8,15 @@ using System.Linq;
namespace inutralia.ViewModels
{
public class CustomMenuViewModel : MenuBaseViewModel
{
public CustomMenuViewModel () : this ( new Models.Menu())
public class CustomMenuViewModel : MenuBaseViewModel
{
public CustomMenuViewModel() : this(new Models.Menu())
{
}
public CustomMenuViewModel (Models.Menu menu) : base (menu)
{
}
public CustomMenuViewModel(Models.Menu menu) : base(menu)
{
}
/// <summary>
/// El menú guardado en el almacenamiento local
......@@ -47,11 +47,11 @@ namespace inutralia.ViewModels
{
if ((Index >= 0) && (Index < _LocalMenu?.DaySelections?.Length))
{
var retVal = _LocalMenu.DaySelections [Index];
if(retVal == null)
var retVal = _LocalMenu.DaySelections[Index];
if (retVal == null)
{
retVal = new LocalMenu.MealSelections ();
_LocalMenu.DaySelections [Index] = retVal;
retVal = new LocalMenu.MealSelections();
_LocalMenu.DaySelections[Index] = retVal;
} //endif
return retVal;
......@@ -74,9 +74,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.LunchFirstIndex = value ? 0 : -1;
SelectionChanged ();
OnPropertyChanged ("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2");
SelectionChanged();
OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged("IsLunchFirstOption2");
} //endif
} //endif
}
......@@ -93,9 +93,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.LunchFirstIndex = value ? 1 : -1;
SelectionChanged ();
OnPropertyChanged ("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2");
SelectionChanged();
OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged("IsLunchFirstOption2");
} //endif
} //endif
}
......@@ -112,9 +112,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.LunchSecondIndex = value ? 0 : -1;
SelectionChanged ();
OnPropertyChanged ("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2");
SelectionChanged();
OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged("IsLunchSecondOption2");
} //endif
} //endif
}
......@@ -131,9 +131,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.LunchSecondIndex = value ? 1 : -1;
SelectionChanged ();
OnPropertyChanged ("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2");
SelectionChanged();
OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged("IsLunchSecondOption2");
} //endif
} //endif
}
......@@ -150,9 +150,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.DinnerFirstIndex = value ? 0 : -1;
SelectionChanged ();
OnPropertyChanged ("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2");
SelectionChanged();
OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged("IsDinnerFirstOption2");
} //endif
} //endif
}
......@@ -169,9 +169,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.DinnerFirstIndex = value ? 1 : -1;
SelectionChanged ();
OnPropertyChanged ("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2");
SelectionChanged();
OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged("IsDinnerFirstOption2");
} //endif
} //endif
}
......@@ -188,9 +188,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.DinnerSecondIndex = value ? 0 : -1;
SelectionChanged ();
OnPropertyChanged ("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2");
SelectionChanged();
OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged("IsDinnerSecondOption2");
} //endif
} //endif
}
......@@ -207,9 +207,9 @@ namespace inutralia.ViewModels
if (value != current)
{
CurrentSelections.DinnerSecondIndex = value ? 1 : -1;
SelectionChanged ();
OnPropertyChanged ("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2");
SelectionChanged();
OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged("IsDinnerSecondOption2");
} //endif
} //endif
}
......@@ -219,33 +219,33 @@ namespace inutralia.ViewModels
protected bool _SelectionHasBeenChanged = false;
public async Task LoadData ()
public async Task LoadData()
{
bool ShouldLoadRecipes = false;
IsBusy = true;
OnPropertyChanged ("NoMenuPanel");
OnPropertyChanged("NoMenuPanel");
// Obtener menú local
var lista = await App.LocalData.RefreshListAsync<LocalMenu> ();
var lista = await App.LocalData.RefreshListAsync<LocalMenu>();
if (lista.Count > 0)
{
_LocalMenu = lista [0];
OnDataRefreshed ();
OnPropertyChanged ("HasMenu");
_LocalMenu = lista[0];
OnDataRefreshed();
OnPropertyChanged("HasMenu");
} //endif
// Obtener menú del servidor
if (await App.API.RefreshItemAsync (Model))
if (await App.API.RefreshItemAsync(Model))
{
Models.Menu ServerMenu = Model as Models.Menu;
if (_LocalMenu == null)
{
// No hay menú local, copiar directamente el del servidor
_LocalMenu = new LocalMenu ();
_LocalMenu.AssignFromMenu (ServerMenu);
await App.LocalData.UpdateItemAsync (_LocalMenu, true);
_LocalMenu = new LocalMenu();
_LocalMenu.AssignFromMenu(ServerMenu);
await App.LocalData.UpdateItemAsync(_LocalMenu, true);
// Indicar que hay que cargar recetas
ShouldLoadRecipes = true;
......@@ -253,7 +253,7 @@ namespace inutralia.ViewModels
else
{
// 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 ...
if (await Application.Current?.MainPage.DisplayAlert("Nuevo menú semanal", "Hay un nuevo menú semanal disponible", "Utilizarlo", "Descartarlo"))
......@@ -286,26 +286,26 @@ namespace inutralia.ViewModels
// Si es necesario, cargar recetas y actualizar
if (ShouldLoadRecipes)
{
await LoadAllRecipesAsync ();
await App.LocalData.UpdateItemAsync (_LocalMenu, false);
await LoadAllRecipesAsync();
await App.LocalData.UpdateItemAsync(_LocalMenu, false);
} //endif
// Actualizar día seleccionado
OnDataRefreshed ();
OnPropertyChanged ("HasMenu");
OnDataRefreshed();
OnPropertyChanged("HasMenu");
_SelectionHasBeenChanged = false;
IsBusy = false;
OnPropertyChanged ("NoMenuPanel");
OnPropertyChanged("NoMenuPanel");
}
protected void SelectionChanged ()
protected void SelectionChanged()
{
_SelectionHasBeenChanged = true;
}
public async Task UpdateShoppingListAsync ()
public async Task UpdateShoppingListAsync()
{
if (_SelectionHasBeenChanged)
{
......@@ -313,41 +313,48 @@ namespace inutralia.ViewModels
// Esperar a que las recetas estén listas
while (IsLoadingRecipes)
await Task.Delay (200);
await Task.Delay(200);
// 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++)
{
if (i < _LocalMenu.Days.Count)
{
var Day = _LocalMenu.Days [i];
var meals = _LocalMenu.DaySelections [i];
var Day = _LocalMenu.Days[i];
var meals = _LocalMenu.DaySelections[i];
if (meals != null)
{
AddRecipe (ingredients, Day.LunchFirst, meals.LunchFirstIndex);
AddRecipe (ingredients, Day.LunchSecond, meals.LunchSecondIndex);
AddRecipe (ingredients, Day.DinnerFirst, meals.DinnerFirstIndex);
AddRecipe (ingredients, Day.DinnerSecond, meals.DinnerSecondIndex);
AddRecipe(ingredients, Day.LunchFirst, meals.LunchFirstIndex);
AddRecipe(ingredients, Day.LunchSecond, meals.LunchSecondIndex);
AddRecipe(ingredients, Day.DinnerFirst, meals.DinnerFirstIndex);
AddRecipe(ingredients, Day.DinnerSecond, meals.DinnerSecondIndex);
} //endif
} //endif
} //endfor
// Eliminar de la lista de la compra los ingredientes que no estén en los encontrados
var listaCompra = await App.LocalData.RefreshListAsync<ShoppingList> ();
foreach (var ing in listaCompra.Where (sl => sl.FromMenus && !ingredients.Contains (sl.Text)))
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.Any(o => o.Name.Contains(sl.Text))))
{
await App.LocalData.DeleteItemAsync (ing);
await App.LocalData.DeleteItemAsync(ing);
} //endforeach
// 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
await App.LocalData.UpdateItemAsync (_LocalMenu);
await App.LocalData.UpdateItemAsync(_LocalMenu);
IsBusy = false;
......@@ -355,48 +362,50 @@ namespace inutralia.ViewModels
} //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
} //endif
} //endif
}
protected override void OnDataRefreshed ()
protected override void OnDataRefreshed()
{
base.OnDataRefreshed ();
OnPropertyChanged ("CurrentSelections");
OnPropertyChanged ("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2");
OnPropertyChanged ("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2");
OnPropertyChanged ("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2");
OnPropertyChanged ("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2");
base.OnDataRefreshed();
OnPropertyChanged("CurrentSelections");
OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged("IsLunchFirstOption2");
OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged("IsLunchSecondOption2");
OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged("IsDinnerFirstOption2");
OnPropertyChanged("IsDinnerSecondOption1");
OnPropertyChanged("IsDinnerSecondOption2");
}
protected override void OnIndexChanged ()
protected override void OnIndexChanged()
{
base.OnIndexChanged ();
OnPropertyChanged ("CurrentSelections");
OnPropertyChanged ("IsLunchFirstOption1");
OnPropertyChanged ("IsLunchFirstOption2");
OnPropertyChanged ("IsLunchSecondOption1");
OnPropertyChanged ("IsLunchSecondOption2");
OnPropertyChanged ("IsDinnerFirstOption1");
OnPropertyChanged ("IsDinnerFirstOption2");
OnPropertyChanged ("IsDinnerSecondOption1");
OnPropertyChanged ("IsDinnerSecondOption2");
base.OnIndexChanged();
OnPropertyChanged("CurrentSelections");
OnPropertyChanged("IsLunchFirstOption1");
OnPropertyChanged("IsLunchFirstOption2");
OnPropertyChanged("IsLunchSecondOption1");
OnPropertyChanged("IsLunchSecondOption2");
OnPropertyChanged("IsDinnerFirstOption1");
OnPropertyChanged("IsDinnerFirstOption2");
OnPropertyChanged("IsDinnerSecondOption1");
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