mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Chapter 14 samples
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using MyMediaCollection.Enums;
|
||||
using MyMediaCollection.Interfaces;
|
||||
using MyMediaCollection.Model;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyMediaCollection.ViewModels
|
||||
{
|
||||
public partial class ItemDetailsViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<string> locationTypes = new();
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<string> mediums = new();
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<string> itemTypes = new();
|
||||
private int _itemId;
|
||||
[ObservableProperty]
|
||||
private string itemName;
|
||||
[ObservableProperty]
|
||||
private string selectedMedium;
|
||||
[ObservableProperty]
|
||||
private string selectedItemType;
|
||||
[ObservableProperty]
|
||||
private string selectedLocation;
|
||||
[ObservableProperty]
|
||||
private bool isDirty;
|
||||
private int _selectedItemId = -1;
|
||||
protected INavigationService _navigationService;
|
||||
protected IDataService _dataService;
|
||||
|
||||
public ItemDetailsViewModel(INavigationService navigationService, IDataService dataService)
|
||||
{
|
||||
_navigationService = navigationService;
|
||||
_dataService = dataService;
|
||||
|
||||
PopulateLists();
|
||||
}
|
||||
|
||||
public void InitializeItemDetailData(int itemId)
|
||||
{
|
||||
_selectedItemId = itemId;
|
||||
IsDirty = false;
|
||||
}
|
||||
|
||||
private void PopulateLists()
|
||||
{
|
||||
ItemTypes.Clear();
|
||||
foreach (string iType in Enum.GetNames(typeof(ItemType)))
|
||||
ItemTypes.Add(iType);
|
||||
|
||||
LocationTypes.Clear();
|
||||
foreach (string lType in Enum.GetNames(typeof(LocationType)))
|
||||
LocationTypes.Add(lType);
|
||||
|
||||
Mediums = new ObservableCollection<string>();
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
MediaItem item;
|
||||
|
||||
if (_itemId > 0)
|
||||
{
|
||||
item = await _dataService.GetItemAsync(_itemId);
|
||||
|
||||
item.Name = ItemName;
|
||||
item.Location = (LocationType)Enum.Parse(typeof(LocationType), SelectedLocation);
|
||||
item.MediaType = (ItemType)Enum.Parse(typeof(ItemType), SelectedItemType);
|
||||
item.MediumInfo = _dataService.GetMedium(SelectedMedium);
|
||||
|
||||
await _dataService.UpdateItemAsync(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
item = new MediaItem
|
||||
{
|
||||
Name = ItemName,
|
||||
Location = (LocationType)Enum.Parse(typeof(LocationType), SelectedLocation),
|
||||
MediaType = (ItemType)Enum.Parse(typeof(ItemType), SelectedItemType),
|
||||
MediumInfo = _dataService.GetMedium(SelectedMedium)
|
||||
};
|
||||
|
||||
await _dataService.AddItemAsync(item);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveItemAndContinueAsync()
|
||||
{
|
||||
await SaveAsync();
|
||||
_itemId = 0;
|
||||
ItemName = string.Empty;
|
||||
SelectedMedium = null;
|
||||
SelectedLocation = null;
|
||||
SelectedItemType = null;
|
||||
IsDirty = false;
|
||||
}
|
||||
|
||||
public async Task SaveItemAndReturnAsync()
|
||||
{
|
||||
await SaveAsync();
|
||||
_navigationService.GoBack();
|
||||
}
|
||||
|
||||
partial void OnItemNameChanged(string value)
|
||||
{
|
||||
IsDirty = true;
|
||||
}
|
||||
|
||||
partial void OnSelectedMediumChanged(string value)
|
||||
{
|
||||
IsDirty = true;
|
||||
}
|
||||
|
||||
partial void OnSelectedItemTypeChanged(string value)
|
||||
{
|
||||
IsDirty = true;
|
||||
Mediums.Clear();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
foreach (string med in _dataService.GetMediums((ItemType)Enum.Parse(typeof(ItemType), SelectedItemType)).Select(m => m.Name))
|
||||
Mediums.Add(med);
|
||||
}
|
||||
}
|
||||
|
||||
partial void OnSelectedLocationChanged(string value)
|
||||
{
|
||||
IsDirty = true;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
_navigationService.GoBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using MyMediaCollection.Helpers;
|
||||
using MyMediaCollection.Interfaces;
|
||||
using MyMediaCollection.Model;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MyMediaCollection.ViewModels
|
||||
{
|
||||
public partial class MainViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private string selectedMedium;
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<MediaItem> items = new ObservableCollection<MediaItem>();
|
||||
private ObservableCollection<MediaItem> allItems;
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<string> mediums;
|
||||
[ObservableProperty]
|
||||
[NotifyCanExecuteChangedFor(nameof(DeleteCommand))]
|
||||
private MediaItem selectedMediaItem;
|
||||
private INavigationService _navigationService;
|
||||
private IDataService _dataService;
|
||||
private const string AllMediums = "All";
|
||||
|
||||
public MainViewModel(INavigationService navigationService, IDataService dataService)
|
||||
{
|
||||
_navigationService = navigationService;
|
||||
_dataService = dataService;
|
||||
|
||||
PopulateDataAsync();
|
||||
}
|
||||
|
||||
public async Task PopulateDataAsync()
|
||||
{
|
||||
Items.Clear();
|
||||
|
||||
foreach (var item in await _dataService.GetItemsAsync())
|
||||
{
|
||||
Items.Add(item);
|
||||
}
|
||||
|
||||
allItems = new ObservableCollection<MediaItem>(Items);
|
||||
|
||||
Mediums = new ObservableCollection<string>
|
||||
{
|
||||
AllMediums
|
||||
};
|
||||
|
||||
foreach (var itemType in _dataService.GetItemTypes())
|
||||
{
|
||||
Mediums.Add(itemType.ToString());
|
||||
}
|
||||
|
||||
SelectedMedium = Mediums[0];
|
||||
}
|
||||
|
||||
partial void OnSelectedMediumChanged(string value)
|
||||
{
|
||||
Items.Clear();
|
||||
|
||||
foreach (var item in allItems)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)
|
||||
|| value == "All"
|
||||
|| value == item.MediaType.ToString())
|
||||
{
|
||||
Items.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void AddEdit()
|
||||
{
|
||||
var selectedItemId = -1;
|
||||
|
||||
if (SelectedMediaItem != null)
|
||||
{
|
||||
selectedItemId = SelectedMediaItem.Id;
|
||||
}
|
||||
|
||||
_navigationService.NavigateTo("ItemDetailsPage", selectedItemId);
|
||||
}
|
||||
|
||||
public void ListViewDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
|
||||
{
|
||||
AddEdit();
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanDeleteItem))]
|
||||
private async Task DeleteAsync()
|
||||
{
|
||||
await _dataService.DeleteItemAsync(SelectedMediaItem);
|
||||
allItems.Remove(SelectedMediaItem);
|
||||
Items.Remove(SelectedMediaItem);
|
||||
}
|
||||
|
||||
private bool CanDeleteItem() => SelectedMediaItem != null;
|
||||
|
||||
[RelayCommand]
|
||||
private void SendToast()
|
||||
{
|
||||
if (ToastWithAvatar.SendToast())
|
||||
NotificationShared.ToastSentSuccessfully();
|
||||
else
|
||||
NotificationShared.CouldNotSendToast();
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SendToastWithText()
|
||||
{
|
||||
if (ToastWithText.SendToast())
|
||||
NotificationShared.ToastSentSuccessfully();
|
||||
else
|
||||
NotificationShared.CouldNotSendToast();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user