mirror of
https://github.com/ckaczor/FeedCenter.git
synced 2026-02-16 10:58:31 -05:00
More cleanup and fixes
This commit is contained in:
@@ -1,33 +1,90 @@
|
||||
using Realms;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
|
||||
namespace FeedCenter
|
||||
{
|
||||
public class Category : RealmObject
|
||||
public class Category : RealmObject, INotifyDataErrorInfo
|
||||
{
|
||||
private const string DefaultCategoryName = "< default >";
|
||||
public const string DefaultName = "< default >";
|
||||
|
||||
private readonly Dictionary<string, List<string>> _errorsByPropertyName = new();
|
||||
|
||||
[PrimaryKey]
|
||||
[MapTo("ID")]
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
[MapTo("Name")]
|
||||
private string RawName { get; set; } = string.Empty;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get => RawName;
|
||||
set
|
||||
{
|
||||
RawName = value;
|
||||
|
||||
ValidateName();
|
||||
RaisePropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
[Ignored]
|
||||
public ICollection<Feed> Feeds { get; set; }
|
||||
|
||||
public static Category Create()
|
||||
{
|
||||
return new Category { Id = Guid.NewGuid() };
|
||||
}
|
||||
public static Category CreateDefault()
|
||||
{
|
||||
return new Category { Id = Guid.NewGuid(), Name = DefaultCategoryName };
|
||||
return new Category { Name = DefaultName, IsDefault = true };
|
||||
}
|
||||
|
||||
public bool IsDefault => Name == DefaultCategoryName;
|
||||
public bool IsDefault { get; internal set; }
|
||||
|
||||
// ReSharper disable once UnusedMember.Global
|
||||
public int SortKey => IsDefault ? 0 : 1;
|
||||
|
||||
public bool HasErrors => _errorsByPropertyName.Any();
|
||||
|
||||
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
|
||||
|
||||
public IEnumerable GetErrors(string propertyName)
|
||||
{
|
||||
return _errorsByPropertyName.TryGetValue(propertyName, out var value) ? value : null;
|
||||
}
|
||||
|
||||
private void OnErrorsChanged(string propertyName)
|
||||
{
|
||||
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
private void ValidateName()
|
||||
{
|
||||
ClearErrors(nameof(Name));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
AddError(nameof(Name), "Name cannot be empty");
|
||||
}
|
||||
|
||||
private void AddError(string propertyName, string error)
|
||||
{
|
||||
if (!_errorsByPropertyName.ContainsKey(propertyName))
|
||||
_errorsByPropertyName[propertyName] = new List<string>();
|
||||
|
||||
if (_errorsByPropertyName[propertyName].Contains(error))
|
||||
return;
|
||||
|
||||
_errorsByPropertyName[propertyName].Add(error);
|
||||
OnErrorsChanged(propertyName);
|
||||
}
|
||||
|
||||
private void ClearErrors(string propertyName)
|
||||
{
|
||||
if (!_errorsByPropertyName.ContainsKey(propertyName))
|
||||
return;
|
||||
|
||||
_errorsByPropertyName.Remove(propertyName);
|
||||
OnErrorsChanged(propertyName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user