From ae6bd6870782e1f267322b144e343108b6ec0f2c Mon Sep 17 00:00:00 2001
From: watsonb8 <watsonb8133@gmail.com>
Date: Sat, 18 May 2019 18:18:28 -0400
Subject: [PATCH] Group headings working for navigation menu

---
 .../NavigationMenu/NavigationGroupItem.cs     | 21 +++++++++++++
 .../NavigationMenu/NavigationItem.cs          |  2 +-
 .../NavigationMenu/NavigationMenu.xaml        |  9 ++++++
 .../NavigationMenu/NavigationMenu.xaml.cs     | 30 +++++++++++++++++--
 .../Frontend/Views/MainView/MainView.xaml.cs  |  2 +-
 .../Frontend/Views/MainView/MainViewModel.cs  |  2 +-
 6 files changed, 61 insertions(+), 5 deletions(-)
 create mode 100644 Aurora/Frontend/Components/NavigationMenu/NavigationGroupItem.cs

diff --git a/Aurora/Frontend/Components/NavigationMenu/NavigationGroupItem.cs b/Aurora/Frontend/Components/NavigationMenu/NavigationGroupItem.cs
new file mode 100644
index 0000000..2688b8d
--- /dev/null
+++ b/Aurora/Frontend/Components/NavigationMenu/NavigationGroupItem.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+
+namespace Aurora.Frontend.Components.NavigationMenu
+{
+    public class NavigationGroupItem : List<NavigationItem>
+    {
+        public NavigationGroupItem()
+        {
+        }
+
+        public NavigationGroupItem(string heading)
+        {
+            GroupHeading = heading;
+        }
+
+        public List<NavigationItem> Items => this;
+        public string GroupHeading { get; set; }
+    }
+}
diff --git a/Aurora/Frontend/Components/NavigationMenu/NavigationItem.cs b/Aurora/Frontend/Components/NavigationMenu/NavigationItem.cs
index 27933f7..c7107ed 100644
--- a/Aurora/Frontend/Components/NavigationMenu/NavigationItem.cs
+++ b/Aurora/Frontend/Components/NavigationMenu/NavigationItem.cs
@@ -5,7 +5,7 @@ using System.Text;
 using System.Threading.Tasks;
 using Aurora.Frontend.Views.Main;
 
-namespace Aurora.Frontend.Views.Components.NavigationMenu
+namespace Aurora.Frontend.Components.NavigationMenu
 {
     public class NavigationItem
     {
diff --git a/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml b/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml
index fc4032a..f247cfa 100644
--- a/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml
+++ b/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml
@@ -9,6 +9,7 @@
                       SeparatorVisibility="None" 
                       HasUnevenRows="true" 
                       BackgroundColor="{StaticResource MenuBackgroundColor}"
+                      IsGroupingEnabled="true"
                       CachingStrategy="RecycleElement">
                 <ListView.Header>
                     <Grid BackgroundColor="#03A9F4">
@@ -27,6 +28,14 @@
                     </Grid>
                 </ListView.Header>
                 
+                <ListView.GroupHeaderTemplate>
+                    <DataTemplate>
+                        <ViewCell>
+                            <Label VerticalOptions="FillAndExpand" VerticalTextAlignment="Start" Text="{Binding GroupHeading}" FontSize="18" TextColor="White"/>
+                        </ViewCell>   
+                    </DataTemplate>
+                </ListView.GroupHeaderTemplate>
+                
                 <ListView.ItemTemplate>
                     <DataTemplate>
                         <ViewCell>
diff --git a/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml.cs b/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml.cs
index c5c0426..98f4124 100644
--- a/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml.cs
+++ b/Aurora/Frontend/Components/NavigationMenu/NavigationMenu.xaml.cs
@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
-using Aurora.Frontend.Views.Components.NavigationMenu;
 using Xamarin.Forms;
 
 namespace Aurora.Frontend.Components.NavigationMenu
@@ -38,7 +37,34 @@ namespace Aurora.Frontend.Components.NavigationMenu
         private static void OnItemsChanged(BindableObject bindable, object oldValue, object newValue)
         {
             var control = (NavigationMenu)bindable;
-            control.MenuItemsListView.ItemsSource = (ObservableCollection<NavigationItem>)newValue;
+            ObservableCollection<NavigationItem> items = (ObservableCollection<NavigationItem>)newValue;
+            Dictionary<string, NavigationGroupItem> groupDictioanry = new Dictionary<string, NavigationGroupItem>();
+
+            //Populate dictionary where group heading is the key
+            foreach(NavigationItem item in items)
+            { 
+                if(groupDictioanry.ContainsKey(item.Group))
+                {
+                    groupDictioanry.TryGetValue(item.Group, out var groupItem);
+                    groupItem.Items.Add(item);
+                }
+                else
+                {
+                    NavigationGroupItem groupItem = new NavigationGroupItem(item.Group);
+                    groupItem.Add(item);
+
+                    groupDictioanry.Add(item.Group, groupItem);
+                }
+            }
+
+            ObservableCollection<NavigationGroupItem> groups = new ObservableCollection<NavigationGroupItem>();
+            foreach(string groupHeading in groupDictioanry.Keys)
+            {
+                groupDictioanry.TryGetValue(groupHeading, out var groupItem);
+                groups.Add(groupItem);
+            }
+
+            control.MenuItemsListView.ItemsSource = groups;
         }
 
     }
diff --git a/Aurora/Frontend/Views/MainView/MainView.xaml.cs b/Aurora/Frontend/Views/MainView/MainView.xaml.cs
index 686cff4..309a97a 100644
--- a/Aurora/Frontend/Views/MainView/MainView.xaml.cs
+++ b/Aurora/Frontend/Views/MainView/MainView.xaml.cs
@@ -1,6 +1,6 @@
 using System;
 using System.Collections.Generic;
-using Aurora.Frontend.Views.Components.NavigationMenu;
+using Aurora.Frontend.Components.NavigationMenu;
 using Aurora.Frontend.Views.MainView;
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;
diff --git a/Aurora/Frontend/Views/MainView/MainViewModel.cs b/Aurora/Frontend/Views/MainView/MainViewModel.cs
index af90e05..ff0e0a1 100644
--- a/Aurora/Frontend/Views/MainView/MainViewModel.cs
+++ b/Aurora/Frontend/Views/MainView/MainViewModel.cs
@@ -2,9 +2,9 @@
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Runtime.CompilerServices;
+using Aurora.Frontend.Components.NavigationMenu;
 using Aurora.Frontend.Views.Albums;
 using Aurora.Frontend.Views.Artists;
-using Aurora.Frontend.Views.Components.NavigationMenu;
 using Aurora.Frontend.Views.Songs;
 using Aurora.Frontend.Views.Stations;