zaaReloaded2/zaaReloaded2/ViewModels/SettingsRepositoryViewModel.cs

331 lines
9.3 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;
namespace zaaReloaded2.ViewModels
{
public class SettingsRepositoryViewModel : ViewModelBase
{
#region Properties
public IList<SettingsViewModel> SettingsList { get; private set; }
public SettingsViewModel Selected { 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
Message<ViewModelMessageContent> EditSettingsMessage
{
get
{
if (_editSettingsMessage == null)
{
_editSettingsMessage = new Message<ViewModelMessageContent>();
}
return _editSettingsMessage;
}
}
Message<ViewModelMessageContent> UseSettingsMessage
{
get
{
if (_useSettingsMessage == null)
{
_useSettingsMessage = new Message<ViewModelMessageContent>();
}
return _useSettingsMessage;
}
}
Message<ViewModelMessageContent> ConfirmDeleteSettingsMessage
{
get
{
if (_confirmDeleteSettingsMessage == null)
{
_confirmDeleteSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmDeleteSettingsMessage;
}
}
Message<ViewModelMessageContent> ConfirmResetSettingsMessage
{
get
{
if (_confirmResetSettingsMessage == null)
{
_confirmResetSettingsMessage = new Message<ViewModelMessageContent>();
}
return _confirmResetSettingsMessage;
}
}
#endregion
#region Constructors
public SettingsRepositoryViewModel(SettingsRepository repository)
{
_repository = repository;
SettingsList = new List<SettingsViewModel>();
foreach (Settings s in repository.SettingsList)
{
SettingsViewModel vm = new SettingsViewModel(s);
vm.PropertyChanged += SettingsViewModel_PropertyChanged;
SettingsList.Add(vm);
}
}
#endregion
#region Private methods
void DoEditSettings()
{
if (CanEditSettings())
{
EditSettingsMessage.Send(new ViewModelMessageContent(Selected));
}
}
bool CanEditSettings()
{
return Selected != null && !IsDefaultSettings();
}
void DoUseSettings()
{
UseSettingsMessage.Send(new ViewModelMessageContent(Selected));
}
bool CanUseSettings()
{
return Selected != null;
}
void DoAddSettings()
{
Settings s = new Settings("Neu");
SettingsViewModel vm = new SettingsViewModel(s);
_repository.SettingsList.Add(s);
SettingsList.Add(vm);
vm.IsSelected = true;
}
bool CanDeleteSettings()
{
return Selected != null && !IsDefaultSettings();
}
void DoDeleteSettings()
{
if (CanDeleteSettings())
{
ConfirmDeleteSettingsMessage.Send(
new ViewModelMessageContent(Selected),
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 (Selected != null)
{
SettingsViewModel copy = Selected.Clone() as SettingsViewModel;
SettingsList.Add(copy);
_repository.SettingsList.Add(copy.RevealModelObject() as Settings);
}
}
void SettingsViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
SettingsViewModel vm = sender as SettingsViewModel;
if (vm != null && e.PropertyName == "IsSelected")
{
Selected = vm.IsSelected ? vm : null;
}
}
/// <summary>
/// Determines whether the selected SettingsViewModel belongs
/// to one of the default settings.
/// </summary>
bool IsDefaultSettings()
{
if (Selected != null)
{
return
(Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameClinic
|| Selected.Name == zaaReloaded2.Properties.Settings.Default.SettingsNameWard);
}
else
{
return false;
}
}
#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
}
}