mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add chapter 4 completed solution
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
using MyMediaCollection.Enums;
|
||||
using MyMediaCollection.Interfaces;
|
||||
using MyMediaCollection.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MyMediaCollection.Services
|
||||
{
|
||||
public class DataService : IDataService
|
||||
{
|
||||
private IList<MediaItem> _items;
|
||||
private IList<ItemType> _itemTypes;
|
||||
private IList<Medium> _mediums;
|
||||
private IList<LocationType> _locationTypes;
|
||||
|
||||
public DataService()
|
||||
{
|
||||
PopulateItemTypes();
|
||||
PopulateMediums();
|
||||
PopulateLocationTypes();
|
||||
PopulateItems();
|
||||
}
|
||||
|
||||
private void PopulateItems()
|
||||
{
|
||||
var cd = new MediaItem
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Classical Favorites",
|
||||
MediaType = ItemType.Music,
|
||||
MediumInfo = _mediums.FirstOrDefault(m => m.Name == "CD"),
|
||||
Location = LocationType.InCollection
|
||||
};
|
||||
|
||||
var book = new MediaItem
|
||||
{
|
||||
Id = 2,
|
||||
Name = "Classic Fairy Tales",
|
||||
MediaType = ItemType.Book,
|
||||
MediumInfo = _mediums.FirstOrDefault(m => m.Name == "Hardcover"),
|
||||
Location = LocationType.InCollection
|
||||
};
|
||||
|
||||
var bluRay = new MediaItem
|
||||
{
|
||||
Id = 3,
|
||||
Name = "The Mummy",
|
||||
MediaType = ItemType.Video,
|
||||
MediumInfo = _mediums.FirstOrDefault(m => m.Name == "Blu Ray"),
|
||||
Location = LocationType.InCollection
|
||||
};
|
||||
|
||||
_items = new List<MediaItem>
|
||||
{
|
||||
cd,
|
||||
book,
|
||||
bluRay
|
||||
};
|
||||
}
|
||||
|
||||
private void PopulateMediums()
|
||||
{
|
||||
var cd = new Medium { Id = 1, MediaType = ItemType.Music, Name = "CD" };
|
||||
var vinyl = new Medium { Id = 2, MediaType = ItemType.Music, Name = "Vinyl" };
|
||||
var hardcover = new Medium { Id = 3, MediaType = ItemType.Book, Name = "Hardcover" };
|
||||
var paperback = new Medium { Id = 4, MediaType = ItemType.Book, Name = "Paperback" };
|
||||
var dvd = new Medium { Id = 5, MediaType = ItemType.Video, Name = "DVD" };
|
||||
var bluRay = new Medium { Id = 6, MediaType = ItemType.Video, Name = "Blu Ray" };
|
||||
|
||||
_mediums = new List<Medium>
|
||||
{
|
||||
cd,
|
||||
vinyl,
|
||||
hardcover,
|
||||
paperback,
|
||||
dvd,
|
||||
bluRay
|
||||
};
|
||||
}
|
||||
|
||||
private void PopulateItemTypes()
|
||||
{
|
||||
_itemTypes = new List<ItemType>
|
||||
{
|
||||
ItemType.Book,
|
||||
ItemType.Music,
|
||||
ItemType.Video
|
||||
};
|
||||
}
|
||||
|
||||
private void PopulateLocationTypes()
|
||||
{
|
||||
_locationTypes = new List<LocationType>
|
||||
{
|
||||
LocationType.InCollection,
|
||||
LocationType.Loaned
|
||||
};
|
||||
}
|
||||
|
||||
public int AddItem(MediaItem item)
|
||||
{
|
||||
item.Id = _items.Max(i => i.Id) + 1;
|
||||
_items.Add(item);
|
||||
|
||||
return item.Id;
|
||||
}
|
||||
|
||||
public MediaItem GetItem(int id)
|
||||
{
|
||||
return _items.FirstOrDefault(i => i.Id == id);
|
||||
}
|
||||
|
||||
public IList<MediaItem> GetItems()
|
||||
{
|
||||
return _items;
|
||||
}
|
||||
|
||||
public IList<ItemType> GetItemTypes()
|
||||
{
|
||||
return _itemTypes;
|
||||
}
|
||||
|
||||
public IList<Medium> GetMediums()
|
||||
{
|
||||
return _mediums;
|
||||
}
|
||||
|
||||
public IList<Medium> GetMediums(ItemType itemType)
|
||||
{
|
||||
return _mediums
|
||||
.Where(m => m.MediaType == itemType)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public IList<LocationType> GetLocationTypes()
|
||||
{
|
||||
return _locationTypes;
|
||||
}
|
||||
|
||||
public void UpdateItem(MediaItem item)
|
||||
{
|
||||
var idx = -1;
|
||||
var matchedItem =
|
||||
(from x in _items
|
||||
let ind = idx++
|
||||
where x.Id == item.Id
|
||||
select ind).FirstOrDefault();
|
||||
|
||||
if (idx == -1)
|
||||
{
|
||||
throw new Exception("Unable to update item. Item not found in collection.");
|
||||
}
|
||||
|
||||
_items[idx] = item;
|
||||
}
|
||||
|
||||
public Medium GetMedium(string name)
|
||||
{
|
||||
return _mediums.FirstOrDefault(m => m.Name == name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using MyMediaCollection.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace MyMediaCollection.Services
|
||||
{
|
||||
public class NavigationService : INavigationService
|
||||
{
|
||||
public NavigationService(Frame rootFrame)
|
||||
{
|
||||
AppFrame = rootFrame;
|
||||
}
|
||||
|
||||
private readonly IDictionary<string, Type> _pages = new ConcurrentDictionary<string, Type>();
|
||||
|
||||
public const string RootPage = "(Root)";
|
||||
|
||||
public const string UnknownPage = "(Unknown)";
|
||||
|
||||
private static Frame AppFrame;
|
||||
|
||||
public void Configure(string page, Type type)
|
||||
{
|
||||
if (_pages.Values.Any(v => v == type))
|
||||
{
|
||||
throw new ArgumentException($"The {type.Name} view has already been registered under another name.");
|
||||
}
|
||||
|
||||
_pages[page] = type;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the currently displayed page.
|
||||
/// </summary>
|
||||
public string CurrentPage
|
||||
{
|
||||
get
|
||||
{
|
||||
var frame = AppFrame;
|
||||
|
||||
if (frame.BackStackDepth == 0)
|
||||
return RootPage;
|
||||
|
||||
if (frame.Content == null)
|
||||
return UnknownPage;
|
||||
|
||||
var type = frame.Content.GetType();
|
||||
|
||||
if (_pages.Values.All(v => v != type))
|
||||
return UnknownPage;
|
||||
|
||||
var item = _pages.Single(i => i.Value == type);
|
||||
|
||||
return item.Key;
|
||||
}
|
||||
}
|
||||
|
||||
public void NavigateTo(string page)
|
||||
{
|
||||
NavigateTo(page, null);
|
||||
}
|
||||
|
||||
public void NavigateTo(string page, object parameter)
|
||||
{
|
||||
if (!_pages.ContainsKey(page))
|
||||
{
|
||||
throw new ArgumentException($"Unable to find a page registered with the name {page}.");
|
||||
}
|
||||
|
||||
AppFrame.Navigate(_pages[page], parameter);
|
||||
}
|
||||
|
||||
public void GoBack()
|
||||
{
|
||||
if (AppFrame?.CanGoBack == true)
|
||||
{
|
||||
AppFrame.GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user