[recipes] Persist the removed ingredients on the shopping list
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] Persist the removed ingredients on the shopping list
- Date: Wed, 8 Mar 2017 11:01:47 +0000 (UTC)
commit 6769e76da6b3d62b5e01fc9ba8e4e0b0b813b099
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Mar 7 21:57:00 2017 -0500
Persist the removed ingredients on the shopping list
This was just an oversight; we need to be able to restore
the same shopping list the next time the application is
opened.
data/org.gnome.recipes.gschema.xml | 7 +++
src/gr-recipe-store.c | 92 +++++++++++++++++++++++++++++++-----
src/gr-recipe-store.h | 6 ++
src/gr-shopping-page.c | 19 +++++++-
4 files changed, 110 insertions(+), 14 deletions(-)
---
diff --git a/data/org.gnome.recipes.gschema.xml b/data/org.gnome.recipes.gschema.xml
index 8d64e16..a39b446 100644
--- a/data/org.gnome.recipes.gschema.xml
+++ b/data/org.gnome.recipes.gschema.xml
@@ -48,6 +48,13 @@
to a serving count.
</description>
</key>
+ <key type="as" name="shopping-list-removed-ingredients">
+ <default>[]</default>
+ <summary>Ingredients that were removed from the shopping list</summary>
+ <description>
+ The names of ingredients that were removed from the shopping list.
+ </description>
+ </key>
</schema>
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index a7b26e0..02db34f 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -81,6 +81,7 @@ struct _GrRecipeStore
char **picks;
char **favorites;
GVariantDict *shopping_list;
+ char **shopping_removed;
char **featured_chefs;
char *user;
@@ -614,6 +615,7 @@ load_shopping (GrRecipeStore *self)
self->shopping_list = g_variant_dict_new (value);
timestamp = g_settings_get_int64 (settings, "shopping-list-last-change");
self->shopping_change = g_date_time_new_from_unix_utc (timestamp);
+ self->shopping_removed = g_settings_get_strv (settings, "shopping-list-removed-ingredients");
}
static void
@@ -623,6 +625,7 @@ save_shopping (GrRecipeStore *self)
g_autoptr(GVariant) value = NULL;
gint64 timestamp;
+ g_settings_set_strv (settings, "shopping-list-removed-ingredients", (const char * const
*)self->shopping_removed);
value = g_variant_ref_sink (g_variant_dict_end (self->shopping_list));
g_settings_set_value (settings, "shopping-list", value);
g_variant_dict_unref (self->shopping_list);
@@ -1257,13 +1260,53 @@ gr_recipe_store_get_all_ingredients (GrRecipeStore *self,
return result;
}
-void
-gr_recipe_store_add_favorite (GrRecipeStore *self,
- GrRecipe *recipe)
+static void
+strv_prepend (char ***strv_in,
+ const char *s)
{
char **strv;
int length;
int i;
+
+ length = g_strv_length (*strv_in);
+ strv = g_new (char *, length + 2);
+ strv[0] = g_strdup (s);
+ for (i = 0; i < length; i++)
+ strv[i + 1] = *strv_in[i];
+ strv[length + 1] = NULL;
+
+ g_free (*strv_in);
+ *strv_in = strv;
+}
+
+static void
+strv_remove (char ***strv_in,
+ const char *s)
+{
+ int i, j;
+ char **strv;
+ int length;
+
+ length = g_strv_length (*strv_in);
+ strv = g_new (char *, length + 1);
+
+ for (i = 0, j = 0; i < length; i++) {
+ if (strcmp (strv[i], s) == 0) {
+ g_free (strv[i]);
+ continue;
+ }
+ strv[j++] = strv[i];
+ }
+ strv[j] = NULL;
+
+ g_free (*strv_in);
+ *strv_in = strv;
+}
+
+void
+gr_recipe_store_add_favorite (GrRecipeStore *self,
+ GrRecipe *recipe)
+{
const char *id;
id = gr_recipe_get_id (recipe);
@@ -1271,15 +1314,7 @@ gr_recipe_store_add_favorite (GrRecipeStore *self,
if (g_strv_contains ((const char * const*)self->favorites, id))
return;
- length = g_strv_length (self->favorites);
- strv = g_new (char *, length + 2);
- strv[0] = g_strdup (id);
- for (i = 0; i < length; i++)
- strv[i + 1] = self->favorites[i];
- strv[length + 1] = NULL;
-
- g_free (self->favorites);
- self->favorites = strv;
+ strv_prepend (&self->favorites, id);
if (self->favorite_change)
g_date_time_unref (self->favorite_change);
@@ -1400,6 +1435,39 @@ gr_recipe_store_get_shopping_serves (GrRecipeStore *self,
return 0;
}
+gboolean
+gr_recipe_store_not_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient)
+{
+ return g_strv_contains ((const char * const *)self->shopping_removed, ingredient);
+}
+
+void
+gr_recipe_store_remove_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient)
+{
+ strv_prepend (&self->shopping_removed, ingredient);
+
+ if (self->shopping_change)
+ g_date_time_unref (self->shopping_change);
+ self->shopping_change = g_date_time_new_now_utc ();
+
+ save_shopping (self);
+}
+
+void
+gr_recipe_store_readd_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient)
+{
+ strv_remove (&self->shopping_removed, ingredient);
+
+ if (self->shopping_change)
+ g_date_time_unref (self->shopping_change);
+ self->shopping_change = g_date_time_new_now_utc ();
+
+ save_shopping (self);
+}
+
GDateTime *
gr_recipe_store_last_shopping_change (GrRecipeStore *self)
{
diff --git a/src/gr-recipe-store.h b/src/gr-recipe-store.h
index 997de3a..e7a213f 100644
--- a/src/gr-recipe-store.h
+++ b/src/gr-recipe-store.h
@@ -84,6 +84,12 @@ gboolean gr_recipe_store_is_in_shopping (GrRecipeStore *self,
GrRecipe *recipe);
int gr_recipe_store_get_shopping_serves (GrRecipeStore *store,
GrRecipe *recipe);
+gboolean gr_recipe_store_not_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient);
+void gr_recipe_store_remove_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient);
+void gr_recipe_store_readd_shopping_ingredient (GrRecipeStore *self,
+ const char *ingredient);
GDateTime *gr_recipe_store_last_shopping_change (GrRecipeStore *self);
gboolean gr_recipe_store_has_diet (GrRecipeStore *self,
GrDiets diet);
diff --git a/src/gr-shopping-page.c b/src/gr-shopping-page.c
index d3f7c16..6c411f5 100644
--- a/src/gr-shopping-page.c
+++ b/src/gr-shopping-page.c
@@ -111,11 +111,11 @@ recount_recipes (GrShoppingPage *page)
int count;
g_autofree char *tmp = NULL;
GtkWidget *window;
-
+
children = gtk_container_get_children (GTK_CONTAINER (page->recipe_list));
count = g_list_length (children);
g_list_free (children);
-
+
g_free (page->title);
page->title = g_strdup_printf (ngettext ("Buy Ingredients (%d recipe)",
"Buy Ingredients (%d recipes)", count),
@@ -155,12 +155,17 @@ static Ingredient *
ingredient_new (const char *ingredient)
{
Ingredient *ing;
+ GrRecipeStore *store;
+
+ store = gr_app_get_recipe_store (GR_APP (g_application_get_default ()));
ing = g_new0 (Ingredient, 1);
ing->ingredient = g_strdup (ingredient);
ing->units = g_array_new (FALSE, TRUE, sizeof (Unit));
g_array_set_clear_func (ing->units, clear_unit);
+ ing->removed = gr_recipe_store_not_shopping_ingredient (store, ingredient);
+
return ing;
}
@@ -267,6 +272,9 @@ remove_ingredient (GtkButton *button, GrShoppingPage *page)
const char *name;
const char *unit;
Ingredient *ing;
+ GrRecipeStore *store;
+
+ store = gr_app_get_recipe_store (GR_APP (g_application_get_default ()));
row = gtk_widget_get_ancestor (GTK_WIDGET (button), GTK_TYPE_LIST_BOX_ROW);
@@ -278,6 +286,8 @@ remove_ingredient (GtkButton *button, GrShoppingPage *page)
ing = (Ingredient *)g_hash_table_lookup (page->ingredients, name);
ing->removed = TRUE;
+ gr_recipe_store_remove_shopping_ingredient (store, name);
+
add_removed_row (page, unit, name);
gtk_widget_destroy (row);
@@ -387,6 +397,9 @@ removed_row_activated (GtkListBox *list,
const char *name;
const char *unit;
Ingredient *ing;
+ GrRecipeStore *store;
+
+ store = gr_app_get_recipe_store (GR_APP (g_application_get_default ()));
popover = gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_POPOVER);
gtk_popover_popdown (GTK_POPOVER (popover));
@@ -399,6 +412,8 @@ removed_row_activated (GtkListBox *list,
ing = (Ingredient *)g_hash_table_lookup (page->ingredients, name);
ing->removed = FALSE;
+ gr_recipe_store_readd_shopping_ingredient (store, name);
+
add_ingredient_row (page, unit, name);
gtk_widget_destroy (GTK_WIDGET (row));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]