2019-05-18 21:25:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
2019-07-05 18:17:09 +00:00
|
|
|
|
namespace Aurora.Design.Components.NavigationMenu
|
2019-05-18 21:25:36 +00:00
|
|
|
|
{
|
2019-11-30 19:41:36 +00:00
|
|
|
|
public partial class NavigationMenu : ContentView
|
2019-05-18 21:25:36 +00:00
|
|
|
|
{
|
|
|
|
|
public NavigationMenu()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
ListView = MenuItemsListView;
|
2019-12-02 04:20:55 +00:00
|
|
|
|
ListView.ItemSelected += ListView_ItemSelected;
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 00:14:36 +00:00
|
|
|
|
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
|
|
|
|
|
{
|
2019-12-02 04:20:55 +00:00
|
|
|
|
this.SelectedItem = e.SelectedItem as NavigationItem;
|
2019-12-02 00:14:36 +00:00
|
|
|
|
}
|
2019-12-01 11:53:30 +00:00
|
|
|
|
|
2019-05-18 21:25:36 +00:00
|
|
|
|
public ListView ListView;
|
|
|
|
|
|
2019-12-02 04:20:55 +00:00
|
|
|
|
public static readonly BindableProperty SelectedItemProperty =
|
|
|
|
|
BindableProperty.Create(propertyName: nameof(SelectedItem),
|
|
|
|
|
returnType: typeof(NavigationItem),
|
|
|
|
|
declaringType: typeof(NavigationMenu),
|
|
|
|
|
defaultBindingMode: BindingMode.OneWayToSource);
|
|
|
|
|
|
|
|
|
|
public NavigationItem SelectedItem
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (NavigationItem)GetValue(SelectedItemProperty);
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetValue(SelectedItemProperty, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-01 11:53:30 +00:00
|
|
|
|
|
2019-05-18 21:25:36 +00:00
|
|
|
|
public static readonly BindableProperty ItemsProperty =
|
|
|
|
|
BindableProperty.Create(propertyName: nameof(Items),
|
|
|
|
|
returnType: typeof(ObservableCollection<NavigationItem>),
|
|
|
|
|
declaringType: typeof(NavigationMenu),
|
|
|
|
|
defaultBindingMode: BindingMode.TwoWay,
|
|
|
|
|
propertyChanged: OnItemsChanged);
|
|
|
|
|
|
|
|
|
|
public ObservableCollection<NavigationItem> Items
|
|
|
|
|
{
|
2019-05-27 16:23:14 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return (ObservableCollection<NavigationItem>)GetValue(ItemsProperty);
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
2019-05-27 16:23:14 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetValue(ItemsProperty, value);
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-27 16:23:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Items changed event handler. Organizes items in groups for display.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bindable">The changed Item.</param>
|
|
|
|
|
/// <param name="oldValue">The previous value.</param>
|
|
|
|
|
/// <param name="newValue">The new value.</param>
|
2019-05-18 21:25:36 +00:00
|
|
|
|
private static void OnItemsChanged(BindableObject bindable, object oldValue, object newValue)
|
|
|
|
|
{
|
|
|
|
|
var control = (NavigationMenu)bindable;
|
2019-05-18 22:18:28 +00:00
|
|
|
|
ObservableCollection<NavigationItem> items = (ObservableCollection<NavigationItem>)newValue;
|
|
|
|
|
Dictionary<string, NavigationGroupItem> groupDictioanry = new Dictionary<string, NavigationGroupItem>();
|
|
|
|
|
|
|
|
|
|
//Populate dictionary where group heading is the key
|
2019-05-27 16:23:14 +00:00
|
|
|
|
foreach (NavigationItem item in items)
|
|
|
|
|
{
|
|
|
|
|
if (groupDictioanry.ContainsKey(item.Group))
|
2019-05-18 22:18:28 +00:00
|
|
|
|
{
|
|
|
|
|
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>();
|
2019-05-27 16:23:14 +00:00
|
|
|
|
foreach (string groupHeading in groupDictioanry.Keys)
|
2019-05-18 22:18:28 +00:00
|
|
|
|
{
|
|
|
|
|
groupDictioanry.TryGetValue(groupHeading, out var groupItem);
|
|
|
|
|
groups.Add(groupItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
control.MenuItemsListView.ItemsSource = groups;
|
2019-05-18 21:25:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|