148 lines
4.9 KiB
C#
148 lines
4.9 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
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<UserLoggedInEvent>().Subscribe(OnUserLoggedInReceived);
|
|
_eventAggregator.GetEvent<UserLoggedOutEvent>().Subscribe(OnUserLoggedOutReceived);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shows a window, if none is already open.
|
|
/// </summary>
|
|
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();
|
|
}
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hides the main window. This command is only enabled if a window is open.
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Shuts down the application.
|
|
/// </summary>
|
|
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<SigninControl>();
|
|
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<LoginControl>();
|
|
contextMenuRegion.RemoveAll();
|
|
contextMenuRegion.Add(loginView);
|
|
contextMenuRegion.Activate(loginView);
|
|
}
|
|
}
|
|
}
|