zaaReloaded2/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs

365 lines
11 KiB
C#
Raw Normal View History

/* SettingsRepositoryViewModel.cs
* part of zaaReloaded2
*
* Copyright 2015 Daniel Kraus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bovender.Mvvm;
using Bovender.Mvvm.ViewModels;
using Bovender.Mvvm.Messaging;
using zaaReloaded2.Controller;
using System.Collections.ObjectModel;
namespace zaaReloaded2.ViewModels
{
public class SettingsRepositoryViewModel : ViewModelBase
{
#region Properties
public ObservableCollection<SettingsViewModel> SettingsList { get; private set; }
/// <summary>
/// Gets the SettingsViewModel that was most recently selected. Whether
/// this view model is still selected can be found out be getting the
/// view model's IsSelected property.
/// </summary>
/// <remarks>
/// Due to the way the WPF ListBox (for example) is implemented, selecting
/// a list item will trigger an PropertyChanged event twice: Once for the
/// item being selected, and once for the item being deselected. Thus we
/// can only capture the last item that actually was selected. We cannot
/// howeve capture if an item was deselected without a new selection,
/// because we cannot logicaly connect two occurrences of the same event
/// from different objects.
/// </remarks>
public SettingsViewModel LastSelected { get; private set; }
#endregion
#region Commands
public DelegatingCommand AddSettingsCommand
{
get
{
if (_addSettingsCommand == null)
{
_addSettingsCommand = new DelegatingCommand(
param => DoAddSettings());
}
return _addSettingsCommand;
}
}
public DelegatingCommand EditSettingsCommand
{
get
{
if (_editSettingsCommand == null)
{
_editSettingsCommand = new DelegatingCommand(
param => DoEditSettings(),
param => CanEditSettings());
}
return _editSettingsCommand;
}
}
public DelegatingCommand UseSettingsCommand
{
get
{
if (_useSettingsCommand == null)
{
_useSettingsCommand = new DelegatingCommand(
param => DoUseSettings(),
param => CanUseSettings());
}
return _useSettingsCommand;
}
}
public DelegatingCommand DeleteSettingsCommand
{
get
{
if (_deleteSettingsCommand == null)
{
_deleteSettingsCommand = new DelegatingCommand(
param => DoDeleteSettings(),
param => CanDeleteSettings());
}
return _deleteSettingsCommand;
}
}
public DelegatingCommand CopySettingsCommand
{
get
{
if (_copySettingsCommand == null)
{
_copySettingsCommand = new DelegatingCommand(
param => DoCopySettings());
}
return _copySettingsCommand;
}
}
public DelegatingCommand ResetSettingsCommand
{
get
{
if (_resetSettingsCommand == null)
{
_resetSettingsCommand = new DelegatingCommand(
param => DoResetSettings());
}
return _resetSettingsCommand;
}
}
#endregion
#region Messages
public Message<ViewModelMessageContent> EditSettingsMessage
{
get
{
if (_editSettingsMessage == null)
{
_editSettingsMessage = new Message<ViewModelMessageContent>();
}
return _editSettingsMessage;
}
}
public Message<ViewModelMessageContent> UseSettingsMessage
{
get
{
if (_useSettingsMessage == null)
{
_useSettingsMessage = new Message<ViewModelMessageContent>();
}
return _useSettingsMessage;
}
}
public Message<ViewModelMessageContent> ConfirmDeleteSettingsMessage
{
get
{
if (_confirmDeleteSettingsMessage == null)
{
_confirmDeleteSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmDeleteSettingsMessage;
}
}
public Message<ViewModelMessageContent> ConfirmResetSettingsMessage
{
get
{
if (_confirmResetSettingsMessage == null)
{
_confirmResetSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmResetSettingsMessage;
}
}
#endregion
#region Constructors
public SettingsRepositoryViewModel(SettingsRepository repository)
{
_repository = repository;
SettingsList = new ObservableCollection<SettingsViewModel>();
foreach (Settings s in repository.SettingsList)
{
SettingsViewModel vm = new SettingsViewModel(s);
2015-08-02 05:11:16 +00:00
AddSettingsViewModel(vm);
}
2015-08-07 19:37:14 +00:00
RequestCloseView += (sender, args) =>
{
_repository.Store();
};
}
#endregion
#region Private methods
void DoEditSettings()
{
if (CanEditSettings())
{
EditSettingsMessage.Send(new ViewModelMessageContent(LastSelected));
}
}
bool CanEditSettings()
{
return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings();
}
void DoUseSettings()
{
UseSettingsMessage.Send(new ViewModelMessageContent(LastSelected));
CloseViewCommand.Execute(null);
}
bool CanUseSettings()
{
return LastSelected != null && LastSelected.IsSelected;
}
void DoAddSettings()
{
Settings s = new Settings("Neu");
SettingsViewModel vm = new SettingsViewModel(s);
_repository.SettingsList.Add(s);
2015-08-09 11:24:44 +00:00
AddSettingsViewModel(vm);
vm.IsSelected = true;
2015-08-09 11:24:44 +00:00
EditSettingsMessage.Send(new ViewModelMessageContent(vm));
}
bool CanDeleteSettings()
{
return LastSelected != null && LastSelected.IsSelected && !IsDefaultSettings();
}
void DoDeleteSettings()
{
if (CanDeleteSettings())
{
ConfirmDeleteSettingsMessage.Send(
new ViewModelMessageContent(LastSelected),
param => ConfirmDeleteSettings(param));
}
}
void ConfirmDeleteSettings(ViewModelMessageContent content)
{
SettingsViewModel vm = content.ViewModel as SettingsViewModel;
if (CanDeleteSettings() && content.Confirmed)
{
_repository.SettingsList.Remove(vm.RevealModelObject() as Settings);
SettingsList.Remove(vm);
}
}
void DoResetSettings()
{
ConfirmResetSettingsMessage.Send(
new ViewModelMessageContent(this),
param => ConfirmResetSettings(param));
}
void ConfirmResetSettings(ViewModelMessageContent content)
{
if (content.Confirmed)
{
}
}
void DoCopySettings()
{
if (LastSelected != null)
{
SettingsViewModel copy = LastSelected.Clone() as SettingsViewModel;
_repository.SettingsList.Add(copy.RevealModelObject() as Settings);
2015-08-02 05:11:16 +00:00
AddSettingsViewModel(copy);
LastSelected.IsSelected = false;
2015-08-02 05:11:16 +00:00
copy.IsSelected = true;
2015-08-09 11:24:44 +00:00
EditSettingsMessage.Send(new ViewModelMessageContent(copy));
}
}
void SettingsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
SettingsViewModel vm = sender as SettingsViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
if (vm.IsSelected) LastSelected = vm;
}
}
/// <summary>
/// Determines whether the selected SettingsViewModel belongs
/// to one of the default settings.
/// </summary>
bool IsDefaultSettings()
{
if (LastSelected != null)
{
return
(LastSelected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameClinic
|| LastSelected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameWard);
}
else
{
return false;
}
}
2015-08-02 05:11:16 +00:00
/// <summary>
/// Adds a settings view model to the collection and wires the
/// PropertyChanged event.
/// </summary>
/// <param name="settingsViewModel">SettingsViewModel to add.</param>
void AddSettingsViewModel(SettingsViewModel settingsViewModel)
{
settingsViewModel.PropertyChanged += SettingsViewModel_PropertyChanged;
SettingsList.Add(settingsViewModel);
}
#endregion
#region Implementation of ViewModelBase
public override object RevealModelObject()
{
return _repository;
}
#endregion
#region Fields
SettingsRepository _repository;
DelegatingCommand _useSettingsCommand;
DelegatingCommand _addSettingsCommand;
DelegatingCommand _editSettingsCommand;
DelegatingCommand _deleteSettingsCommand;
DelegatingCommand _resetSettingsCommand;
DelegatingCommand _copySettingsCommand;
Message<ViewModelMessageContent> _confirmDeleteSettingsMessage;
Message<ViewModelMessageContent> _confirmResetSettingsMessage;
Message<ViewModelMessageContent> _editSettingsMessage;
Message<ViewModelMessageContent> _useSettingsMessage;
#endregion
}
}