Retail_manager/RMWPFUserInterface/ViewModels/UserDisplayViewModel.cs

77 lines
2.2 KiB
C#

using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace RMWPFUserInterface.ViewModels
{
public class UserDisplayViewModel : Screen
{
private readonly StatusInfoViewModel _status;
private readonly IWindowManager _window;
private readonly IUserEndPoint _userEndpoint;
private BindingList<UserModel> _users;
public BindingList<UserModel> Users
{
get
{
return _users;
}
set
{
_users = value;
NotifyOfPropertyChange(() => Users);
}
}
public UserDisplayViewModel(StatusInfoViewModel status, IWindowManager window, IUserEndPoint user)
{
_status = status;
_window = window;
_userEndpoint = user;
}
protected override async void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
try
{
await LoadUsers();
}
catch (Exception ex)
{
dynamic settings = new ExpandoObject();
settings.WindowStartUpLocation = WindowStartupLocation.CenterOwner;
settings.ResizeMode = ResizeMode.NoResize;
settings.Title = "System Error";
if (ex.Message == "Unauthorized")
{
_status.UpdateMessage("Unauthorized Access", "You do not have permission to interact with Sales Form.");
_window.ShowDialogAsync(_status, null, settings);
}
else
{
_status.UpdateMessage("Fatal exception", ex.Message);
_window.ShowDialogAsync(_status, null, settings);
}
TryCloseAsync();
}
}
private async Task LoadUsers()
{
var userList = await _userEndpoint.GetAll();
Users = new BindingList<UserModel>(userList);
}
}
}