2015-08-01 19:23:56 +00:00
|
|
|
|
/* 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;
|
2015-08-04 00:43:17 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
2015-08-01 19:23:56 +00:00
|
|
|
|
|
|
|
|
|
namespace zaaReloaded2.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class SettingsRepositoryViewModel : ViewModelBase
|
|
|
|
|
{
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2015-08-04 00:43:17 +00:00
|
|
|
|
public ObservableCollection<SettingsViewModel> SettingsList { get; private set; }
|
2015-08-01 19:23:56 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2015-08-04 00:43:17 +00:00
|
|
|
|
public Message<ViewModelMessageContent> EditSettingsMessage
|
2015-08-01 19:23:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_editSettingsMessage == null)
|
|
|
|
|
{
|
|
|
|
|
_editSettingsMessage = new Message<ViewModelMessageContent>();
|
|
|
|
|
}
|
|
|
|
|
return _editSettingsMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-04 00:43:17 +00:00
|
|
|
|
|
|
|
|
|
public Message<ViewModelMessageContent> UseSettingsMessage
|
2015-08-01 19:23:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_useSettingsMessage == null)
|
|
|
|
|
{
|
|
|
|
|
_useSettingsMessage = new Message<ViewModelMessageContent>();
|
|
|
|
|
}
|
|
|
|
|
return _useSettingsMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-04 00:43:17 +00:00
|
|
|
|
|
|
|
|
|
public Message<ViewModelMessageContent> ConfirmDeleteSettingsMessage
|
2015-08-01 19:23:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_confirmDeleteSettingsMessage == null)
|
|
|
|
|
{
|
|
|
|
|
_confirmDeleteSettingsMessage = new Message<ViewModelMessageContent>();
|
|
|
|
|
}
|
|
|
|
|
return _confirmDeleteSettingsMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-04 00:43:17 +00:00
|
|
|
|
|
|
|
|
|
public Message<ViewModelMessageContent> ConfirmResetSettingsMessage
|
2015-08-01 19:23:56 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_confirmResetSettingsMessage == null)
|
|
|
|
|
{
|
|
|
|
|
_confirmResetSettingsMessage = new Message<ViewModelMessageContent>();
|
|
|
|
|
}
|
|
|
|
|
return _confirmResetSettingsMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
public SettingsRepositoryViewModel(SettingsRepository repository)
|
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
2015-08-04 00:43:17 +00:00
|
|
|
|
SettingsList = new ObservableCollection<SettingsViewModel>();
|
2015-08-01 19:23:56 +00:00
|
|
|
|
foreach (Settings s in repository.SettingsList)
|
|
|
|
|
{
|
|
|
|
|
SettingsViewModel vm = new SettingsViewModel(s);
|
2015-08-02 05:11:16 +00:00
|
|
|
|
AddSettingsViewModel(vm);
|
2015-08-01 19:23:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
_repository.SettingsList.Add(copy.RevealModelObject() as Settings);
|
2015-08-02 05:11:16 +00:00
|
|
|
|
AddSettingsViewModel(copy);
|
|
|
|
|
Selected.IsSelected = false;
|
|
|
|
|
copy.IsSelected = true;
|
2015-08-01 19:23:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-01 19:23:56 +00:00
|
|
|
|
#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
|
|
|
|
|
}
|
|
|
|
|
}
|