Skip to content

Commit

Permalink
added warning when user doesn't have store location set
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Aug 24, 2022
1 parent c3806a3 commit 2d110ac
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 52 deletions.
28 changes: 28 additions & 0 deletions ShoppingList/Model/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,32 @@ public class Item : ObservableObject
[OneToOne]
[Column("itemlocationdata")]
public ItemLocationData LocationData { get; set; }


public Item()
{
}

public Item(Item item)
{
this.Name = item.Name;
this.Description = item.Description;
this.Category = item.Category;
this.EstimatedPrice = item.EstimatedPrice;
this.ParentId = item.ParentId;

}

public Item(Item item, ItemLocationData ild)
{
this.Name = item.Name;
this.Description = item.Description;
this.Category = item.Category;
this.EstimatedPrice = item.EstimatedPrice;
this.ParentId = item.ParentId;

this.LocationData = ild;
this.Aisle = ild.Description;

}
}
4 changes: 3 additions & 1 deletion ShoppingList/Services/KrogerAPIService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<Dictionary<string, string>> GetLocationNearZipAsync(string zip

Guard.IsNotNullOrEmpty(zip, nameof(zip));
Guard.IsNotNull(apiConfig, nameof(apiConfig));
// add code to check that we got a zip (and not, idk, say, some type of script attack lol)

Dictionary<string, string> closeKrogerNames = new Dictionary<string, string>();

Expand Down Expand Up @@ -107,7 +108,8 @@ public async Task<Dictionary<string, string>> GetLocationNearZipAsync(string zip
Guard.IsNotNull(apiConfig, nameof(apiConfig));

if (Int32.Parse(locationId) == 0)
throw new ArgumentException($"Location ID: {locationId} is zero and not valid (Is one not set and it went with the default?)");
throw new FeatureNotEnabledException($"Location ID: {locationId} is zero and not valid (Is one not set and it went with the default?)");



string productQuery = $"?filter.locationId={locationId}&filter.term={term}&filter.fulfillment=ais&filter.limit=50";
Expand Down
69 changes: 40 additions & 29 deletions ShoppingList/Services/ListSorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static List<Item> SortUserListItems(UserList userList)
List<Item> aisleList = new();
List<Item> dairyList = new();
List<Item> frozenList = new();
List<Item> unsortedList = new();


var sortedItems = new List<Item>();
Expand All @@ -43,40 +44,49 @@ public static List<Item> SortUserListItems(UserList userList)

foreach(var item in list)
{
switch (item.Aisle.ToUpper())
{
case "MEAT":
meatList.Add(item);
wasSorted = true;
break;
case "SEAFOOD":
meatList.Add(item);
wasSorted = true;
break;
case "DAIRY":
dairyList.Add(item);
wasSorted = true;
break;
case "PRODUCE":
produceList.Add(item);
wasSorted = true;
break;
}

if (!wasSorted)
{
if (item.Category.ToUpper().Contains("FROZEN") && FrozenFoodLast && !StartAtBackOfStore)
if (item.Aisle is not null &&
item.Category is not null)
{
switch (item.Aisle.ToUpper())
{
frozenList.Add(item);
} else
case "MEAT":
meatList.Add(item);
wasSorted = true;
break;
case "SEAFOOD":
meatList.Add(item);
wasSorted = true;
break;
case "DAIRY":
dairyList.Add(item);
wasSorted = true;
break;
case "PRODUCE":
produceList.Add(item);
wasSorted = true;
break;
}

if (!wasSorted)
{
aisleList.Add(item);
if (item.Category.ToUpper().Contains("FROZEN") && FrozenFoodLast && !StartAtBackOfStore)
{
frozenList.Add(item);
} else
{
aisleList.Add(item);
}
}
}

wasSorted = false;
wasSorted = false;

} else
{
unsortedList.Add(item);
}
}

// need a way to be defensive in the case where the item doesn't have loc data or a bay num, etc
meatList = meatList.OrderByDescending(x => Int32.Parse(x.LocationData.BayNumber)).ToList();
dairyList = dairyList.OrderByDescending(x => Int32.Parse(x.LocationData.BayNumber)).ToList();
produceList = produceList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList();
Expand All @@ -90,7 +100,8 @@ public static List<Item> SortUserListItems(UserList userList)
sortedItems.AddRange(aisleList);
sortedItems.AddRange(dairyList);
sortedItems.AddRange(meatList);
sortedItems.AddRange(frozenList);
sortedItems.AddRange(frozenList);
sortedItems.AddRange(unsortedList);


if(StartAtBackOfStore)
Expand Down
90 changes: 68 additions & 22 deletions ShoppingList/ViewModel/UserListDetailViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Timers;
using System.Diagnostics;

namespace ShoppingList.ViewModels;

Expand Down Expand Up @@ -124,40 +125,85 @@ public async void NewItemDialog()
public async void OnItemEntryCompleted(string itemName)
{

Item newItem = new()

try {

var apiConfig = await _krogerAPIService.GetStartupConfigAsync();

var done = await _krogerAPIService.SetAuthTokensAsync(apiConfig);

var result = await _krogerAPIService.GetProductLocationDataAsync(itemName, Preferences.Get("KrogerLocation", "0000000"), apiConfig);

ItemLocationData ild = result.Item1;
Item item = result.Item2;


item.ParentId = UserList.Id;

var newItem = new Item(item, ild);

newItem = _itemService.CreateItem(newItem);
newItem.LocationData.ParentId = newItem.Id;
UserList.Items.Add(newItem);

ListSorter.SortUserListItems(userList);

UserListNotifers();


} catch(FeatureNotEnabledException e) //this is probably a misuse of this exception type, but oh well, it kinda fits lol
{
Name = itemName
};
var promptedZipNotSetYet = Preferences.Get("FirstTimePromptForZip", true);

if (promptedZipNotSetYet)
{
var goToSettings = await Shell.Current.DisplayAlert("Error!",
$"Unable To Get Kroger Data, You Need To Set Your Kroger Location In The Settings. Go There Now?", "Ok", "Cancel");

var apiConfig = await _krogerAPIService.GetStartupConfigAsync();
if (goToSettings)
{
await Shell.Current.GoToAsync($"{nameof(SettingsView)}");
} else {

var done = await _krogerAPIService.SetAuthTokensAsync(apiConfig);
Item newItem = new Item()
{
Name = itemName,
ParentId = UserList.Id,
LocationData = new ItemLocationData()
};

//Will do this in background thread later
var result = await _krogerAPIService.GetProductLocationDataAsync(newItem.Name, Preferences.Get("KrogerLocation", "0000000"), apiConfig);
newItem = _itemService.CreateItem(newItem);
newItem.LocationData.ParentId = newItem.Id;
UserList.Items.Add(newItem);

ItemLocationData ild = result.Item1;
Item item = result.Item2;
ListSorter.SortUserListItems(userList);

newItem.LocationData = ild;
newItem.Description = item.Description;
newItem.Category = item.Category;
newItem.EstimatedPrice = item.EstimatedPrice;
UserListNotifers();

newItem.Aisle = ild.Description;
newItem.ParentId = UserList.Id;

newItem = _itemService.CreateItem(newItem);
}
} else
{
Item newItem = new Item()
{
Name = itemName,
ParentId = UserList.Id,
LocationData = new ItemLocationData()
};

newItem.LocationData.ParentId = newItem.Id;
UserList.Items.Add(newItem);
newItem = _itemService.CreateItem(newItem);
newItem.LocationData.ParentId = newItem.Id;
UserList.Items.Add(newItem);

ListSorter.SortUserListItems(userList);
ListSorter.SortUserListItems(userList);

UserListNotifers();

}

Preferences.Set("FirstTimePromptForZip", false);

//UserList = UserList;
UserListNotifers();

}
}

[RelayCommand]
Expand Down

0 comments on commit 2d110ac

Please sign in to comment.