forked from Programming-With-Chris/ShoppingList
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some Guard logic from CommunityToolkit.Diagnostics to the services
- Loading branch information
1 parent
2fb08bd
commit d1dd492
Showing
8 changed files
with
141 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,122 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
namespace ShoppingList.Services; | ||
|
||
namespace ShoppingList.Services | ||
public class ListSorter | ||
{ | ||
public class ListSorter | ||
{ | ||
|
||
public static bool FrozenFoodLast { get; set; } | ||
public static bool FrozenFoodLast { get; set; } | ||
|
||
public static bool StartAtBackOfStore { get; set; } | ||
public static bool StartAtBackOfStore { get; set; } | ||
|
||
|
||
public static List<Item> SortUserListItems(UserList userList) | ||
{ | ||
public static List<Item> SortUserListItems(UserList userList) | ||
{ | ||
|
||
SetPreferences(); | ||
Guard.IsNotNull(userList, nameof(userList)); | ||
Guard.IsNotNull(userList.Items, nameof(userList.Items)); | ||
|
||
SetPreferences(); | ||
|
||
|
||
List<Item> list = userList.Items; | ||
List<Item> meatList = new(); | ||
List<Item> produceList = new(); | ||
List<Item> aisleList = new(); | ||
List<Item> dairyList = new(); | ||
List<Item> frozenList = new(); | ||
|
||
|
||
List<Item> list = userList.Items; | ||
List<Item> meatList = new(); | ||
List<Item> produceList = new(); | ||
List<Item> aisleList = new(); | ||
List<Item> dairyList = new(); | ||
List<Item> frozenList = new(); | ||
|
||
var sortedItems = new List<Item>(); | ||
|
||
var sortedItems = new List<Item>(); | ||
bool wasSorted = false; | ||
|
||
bool wasSorted = false; | ||
foreach(var item in list) | ||
{ | ||
switch (item.Aisle) | ||
{ | ||
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; | ||
} | ||
|
||
foreach(var item in list) | ||
if (!wasSorted) | ||
{ | ||
switch (item.Aisle) | ||
if (item.Category.Contains("Frozen") && FrozenFoodLast) | ||
{ | ||
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) | ||
frozenList.Add(item); | ||
} else | ||
{ | ||
if (item.Category.Contains("Frozen") && FrozenFoodLast) | ||
{ | ||
frozenList.Add(item); | ||
} else | ||
{ | ||
aisleList.Add(item); | ||
} | ||
aisleList.Add(item); | ||
} | ||
|
||
wasSorted = false; | ||
} | ||
|
||
meatList = meatList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList(); | ||
dairyList = dairyList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList(); | ||
|
||
aisleList = aisleList.OrderBy(x => Int32.Parse(x.LocationData.Number)).ToList(); | ||
wasSorted = false; | ||
} | ||
|
||
meatList = meatList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList(); | ||
dairyList = dairyList.OrderBy(x => Int32.Parse(x.LocationData.BayNumber)).ToList(); | ||
|
||
//research bay number order on aisles (do we want to do alternating asc/desc to form a 'route'? | ||
aisleList = aisleList.OrderBy(x => Int32.Parse(x.LocationData.Number)).ToList(); | ||
|
||
|
||
foreach(var item in produceList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
//research bay number order on aisles (do we want to do alternating asc/desc to form a 'route'? | ||
|
||
|
||
foreach(var item in aisleList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
foreach(var item in dairyList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
foreach(var item in meatList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
foreach(var item in frozenList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
foreach(var item in produceList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
if(StartAtBackOfStore) | ||
{ | ||
sortedItems.Reverse(); | ||
} | ||
|
||
foreach(var item in aisleList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
// Finish with IsCompleted Sort, so completed items are at the bottom of the list | ||
var anotherSort = sortedItems.OrderBy(x => x.IsCompleted).ToList(); | ||
foreach(var item in dairyList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
userList.Items = anotherSort; | ||
foreach(var item in meatList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
return anotherSort; | ||
foreach(var item in frozenList) | ||
{ | ||
sortedItems.Add(item); | ||
} | ||
|
||
public static void SetPreferences() | ||
if(StartAtBackOfStore) | ||
{ | ||
FrozenFoodLast = Preferences.Get("FrozenFoodLast", true); | ||
StartAtBackOfStore = Preferences.Get("StartAtBackOfStore", false); | ||
sortedItems.Reverse(); | ||
} | ||
|
||
|
||
// Finish with IsCompleted Sort, so completed items are at the bottom of the list | ||
var anotherSort = sortedItems.OrderBy(x => x.IsCompleted).ToList(); | ||
|
||
userList.Items = anotherSort; | ||
|
||
return anotherSort; | ||
} | ||
|
||
public static void SetPreferences() | ||
{ | ||
FrozenFoodLast = Preferences.Get("FrozenFoodLast", true); | ||
StartAtBackOfStore = Preferences.Get("StartAtBackOfStore", false); | ||
} | ||
} | ||
|
Oops, something went wrong.