Reappearing List Items

While using Viand my wife told me that items she had previously bought were reappearing on the Buy list mysteriously. I finally had some time to look into it.

Turns out it’s not so mysterious. When I create an item it’s boolean Buy property is set to zero or one to determine which list it goes on (zero is the Add list; one is the Buy list). I also add the newly created item to the database at the same time it’s added to one of the lists. When an item is moved between lists it Buy property is set to the correct value.

What I forgot to do is update the record in the database when it switches location from one list to another. To fix this I first created an UpdateDatabase method in the ItemDatabase class.

public void UpdateItem(Item item)
{
	database.Update(item);
}

Then I called this UpdateItem method in the BuyPage’s ItemBought method, passing in the item.

internal void ItemBought(BuyCell item)
{
	if (allItems != null) {
		var obj = allItems.First(x => x.Name == item.Text);
		if (obj != null) {
			obj.Buy = false;
			obj.Quantity = 1;
		}
		App.Database.UpdateItem((Item)obj);
	}

	UpdateBuyItemsList();
	MessagingCenter.Send<BuyPage>(this, "UpdateAddItemsListFromBuyList");
}

And did the same in AddPage’s BuyItem method.

internal void BuyItem(AddCell item)
		{
			if (Application.Current.Properties.ContainsKey("Items")) {
				allItems = (List<Item>)Application.Current.Properties["Items"];
				var obj = allItems.First(x => x.Name == item.Text);
				if (obj != null) obj.Buy = true;

				App.Database.UpdateItem((Item)obj);
			}

			addView.ItemsSource = UpdateAddItemsList();
			MessagingCenter.Send<AddPage>(this, "UpdateBuyItemsList");
		}

Silly mistake. Shows you what can happen programming after hours.

This entry was posted in Side Projects, Software Development and tagged , , . Bookmark the permalink.

One Response to Reappearing List Items

  1. Pingback: Viand Alpha – Phase 1 | Objective.Me

Comments are closed.