summaryrefslogtreecommitdiff
path: root/Juick/ViewModels/Validation/DataViewModelBase.cs
blob: dcbf34bac12068d3ba924955fe8db21f2920da5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Juick.ViewModels.Validation
{
    public class DataViewModelBase : ViewModelBase, IDataErrorInfo
    {

        private readonly Dictionary<String, List<String>> _errors =
           new Dictionary<string, List<string>>();

        // Adds the specified error to the errors collection if it is not already 
        // present, inserting it in the first position if isWarning is false. 
        public void AddError(string propertyName, string error, bool isWarning)
        {
            if (!_errors.ContainsKey(propertyName))
                _errors[propertyName] = new List<string>();

            if (_errors[propertyName].Contains(error)) return;
            if (isWarning) _errors[propertyName].Add(error);
            else _errors[propertyName].Insert(0, error);
        }

        // Removes the specified error from the errors collection if it is present. 
        public void RemoveError(string propertyName, string error)
        {
            if (!_errors.ContainsKey(propertyName) || !_errors[propertyName].Contains(error)) return;
            _errors[propertyName].Remove(error);
            if (_errors[propertyName].Count == 0) _errors.Remove(propertyName);
        }

        public string Error { get { throw new NotImplementedException(); } }

        public string this[string columnName]
        {
            get
            {
                return (!_errors.ContainsKey(columnName) ? null :
                    String.Join(Environment.NewLine, _errors[columnName]));
            }
        }
    }
}