using System.Windows;
using System.Windows.Input;
using Flexitime.Objects;
using FlexitimeTaskbarUtility.Components;
using FlexitimeTaskbarUtility.Events;
using NotifyIconWpf.Sample.Windowless;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using Application = System.Windows.Application;
namespace FlexitimeTaskbarUtility.ViewModels
{
///
/// Provides bindable properties and commands for the NotifyIcon. In this sample, the
/// view model is assigned to the NotifyIcon in XAML. Alternatively, the startup routing
/// in App.xaml.cs could have created this view model, and assigned it to the NotifyIcon.
///
public class NotifyIconViewModel : BindableBase
{
private readonly IEventAggregator _eventAggregator;
private readonly IRegionManager _regionManager;
private readonly IContainerProvider _containerProvider;
public NotifyIconViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IContainerProvider containerProvider)
{
_eventAggregator = eventAggregator;
_regionManager = regionManager;
_containerProvider = containerProvider;
UserStatus = UserState.Unknown;
_eventAggregator.GetEvent().Subscribe(OnUserLoggedInReceived);
_eventAggregator.GetEvent().Subscribe(OnUserLoggedOutReceived);
}
///
/// Shows a window, if none is already open.
///
public ICommand ShowWindowCommand
{
get
{
return new DelegateCommand
{
CanExecuteFunc = () =>
Application.Current.MainWindow == null || Application.Current.MainWindow.Visibility != Visibility.Visible,
CommandAction = () =>
{
Application.Current.MainWindow = new MainWindow();
Application.Current.MainWindow.Show();
}
};
}
}
///
/// Hides the main window. This command is only enabled if a window is open.
///
public ICommand HideWindowCommand
{
get
{
return new DelegateCommand
{
CommandAction = () =>
{
Application.Current.MainWindow.Close();
},
CanExecuteFunc = () => Application.Current.MainWindow != null
&& Application.Current.MainWindow.Visibility == Visibility.Visible
};
}
}
private UserState _userStatus;
public UserState UserStatus
{
get => _userStatus;
set => SetProperty(ref _userStatus, value);
}
///
/// Shuts down the application.
///
public ICommand ExitApplicationCommand
{
get
{
return new DelegateCommand { CommandAction = () => Application.Current.Shutdown() };
}
}
public ICommand LoginCommand
{
get
{
return new DelegateCommand { CommandAction = () => UserStatus = UserState.Out };
}
}
public ICommand SignInCommand
{
get
{
return new DelegateCommand { CommandAction = () => UserStatus = UserState.Unknown };
}
}
public ICommand TrayContextMenuOpened
{
get
{
return new DelegateCommand{CommandAction = LoadLoginControl};
}
}
private void OnUserLoggedInReceived(LoginResponse loginResponse)
{
UserStatus = UserState.Out;
IRegion contextMenuRegion = _regionManager.Regions["ContextMenuRegion"];
var signInView = _containerProvider.Resolve();
contextMenuRegion.RemoveAll();
contextMenuRegion.Add(signInView);
contextMenuRegion.Activate(signInView);
}
private void OnUserLoggedOutReceived()
{
UserStatus = UserState.Unknown;
LoadLoginControl();
}
private void LoadLoginControl()
{
IRegion contextMenuRegion = _regionManager.Regions["ContextMenuRegion"];
var loginView = _containerProvider.Resolve();
contextMenuRegion.RemoveAll();
contextMenuRegion.Add(loginView);
contextMenuRegion.Activate(loginView);
}
}
}