migration
All checks were successful
All checks were successful
This commit is contained in:
152
SimpleToolkit.MVVM/ObservableDictionary.cs
Normal file
152
SimpleToolkit.MVVM/ObservableDictionary.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SimpleToolkit.MVVM
|
||||
{
|
||||
public class ObservableDictionary<TKey, TValue> : ObservableCollection<ObservableKeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>, IEnumerable<ObservableKeyValuePair<TKey, TValue>>
|
||||
{
|
||||
#region PROPERTIES
|
||||
|
||||
public ICollection<TKey> Keys => Items.Select(p => p.Key).ToList();
|
||||
public ICollection<TValue> Values => Items.Select(p => p.Value).ToList();
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region INDEXERS
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!TryGetValue(key, out TValue result))
|
||||
{
|
||||
throw new ArgumentException("Key not found");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (ContainsKey(key))
|
||||
{
|
||||
GetKeyValuePairByTheKey(key).Value = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public ObservableDictionary() : base()
|
||||
{ }
|
||||
|
||||
public ObservableDictionary(IDictionary<TKey, TValue> dictionary) : base()
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TValue> pair in dictionary)
|
||||
{
|
||||
Add(pair);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
if (ContainsKey(key))
|
||||
{
|
||||
throw new ArgumentException("The dictionary already contains the key");
|
||||
}
|
||||
Add(new ObservableKeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
ObservableKeyValuePair<TKey, TValue> pair = GetKeyValuePairByTheKey(item.Key);
|
||||
if (Equals(pair, default(ObservableKeyValuePair<TKey, TValue>)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Equals(pair.Value, item.Value);
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
ObservableKeyValuePair<TKey, TValue> pair = ((ObservableCollection<ObservableKeyValuePair<TKey, TValue>>)this).FirstOrDefault((i) => Equals(key, i.Key));
|
||||
|
||||
return !Equals(default(ObservableKeyValuePair<TKey, TValue>), pair);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
List<ObservableKeyValuePair<TKey, TValue>> remove = ((ObservableCollection<ObservableKeyValuePair<TKey, TValue>>)this).Where(pair => Equals(key, pair.Key)).ToList();
|
||||
foreach (ObservableKeyValuePair<TKey, TValue> pair in remove)
|
||||
{
|
||||
Remove(pair);
|
||||
}
|
||||
return remove.Count > 0;
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
ObservableKeyValuePair<TKey, TValue> pair = GetKeyValuePairByTheKey(item.Key);
|
||||
if (Equals(pair, default(ObservableKeyValuePair<TKey, TValue>)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!Equals(pair.Value, item.Value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return Remove(pair);
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
|
||||
{
|
||||
value = default;
|
||||
var pair = GetKeyValuePairByTheKey(key);
|
||||
if (Equals(pair, default(ObservableKeyValuePair<TKey, TValue>)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
value = pair.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => ((ObservableCollection<ObservableKeyValuePair<TKey, TValue>>)this).Select(i => new KeyValuePair<TKey, TValue>(i.Key, i.Value)).GetEnumerator();
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private ObservableKeyValuePair<TKey, TValue> GetKeyValuePairByTheKey(TKey key) => ((ObservableCollection<ObservableKeyValuePair<TKey, TValue>>)this).FirstOrDefault(i => i.Key.Equals(key));
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
75
SimpleToolkit.MVVM/ObservableKeyValuePair.cs
Normal file
75
SimpleToolkit.MVVM/ObservableKeyValuePair.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SimpleToolkit.MVVM
|
||||
{
|
||||
public class ObservableKeyValuePair<TKey, TValue> : INotifyPropertyChanged
|
||||
{
|
||||
#region FIELDS
|
||||
|
||||
private TKey _key;
|
||||
private TValue _value;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PROPERTIES
|
||||
|
||||
public TKey Key
|
||||
{
|
||||
get => _key;
|
||||
set
|
||||
{
|
||||
_key = value;
|
||||
NotifyPropertyChanged(nameof(Key));
|
||||
}
|
||||
}
|
||||
public TValue Value
|
||||
{
|
||||
get => _value;
|
||||
set
|
||||
{
|
||||
_value = value;
|
||||
NotifyPropertyChanged(nameof(Value));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region CONSTRUCTORS
|
||||
|
||||
public ObservableKeyValuePair() : this(default, default)
|
||||
{ }
|
||||
|
||||
public ObservableKeyValuePair(TKey key, TValue value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private void NotifyPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region EVENTS
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
51
SimpleToolkit.MVVM/README.md
Normal file
51
SimpleToolkit.MVVM/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# SimpleToolkit.MVVM
|
||||
|
||||
### Set of helpers, class extensions, UI controls used in my other C# projects
|
||||
|
||||
SimpleToolkit is package of useful classes, helpers, extensions and UI controls, I use in my C# projects. MVVM subpackage contains class, method and property MVVM.
|
||||
|
||||
---
|
||||
|
||||
## NuGet registry status
|
||||
|
||||
| Subpackage | Status |
|
||||
|-----------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| **SimpleToolkit.MVVM** | [](https://www.nuget.org/packages/SimpleToolkit.MVVM/) |
|
||||
| SimpleToolkit.Extensions | [](https://www.nuget.org/packages/SimpleToolkit.Extensions/) |
|
||||
| SimpleToolkit.Attributes | [](https://www.nuget.org/packages/SimpleToolkit.Attributes/) |
|
||||
| SimpleToolkit.UI.Models | [](https://www.nuget.org/packages/SimpleToolkit.UI.Models/) |
|
||||
| SimpleToolkit.UI.WinUI.Behaviors | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Behaviors/) |
|
||||
| SimpleToolkit.UI.WinUI.Converters | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Converters/) |
|
||||
| SimpleToolkit.UI.WinUI.Controls | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Controls/) |
|
||||
| SimpleToolkit.UI.WinUI.Helpers | [](https://www.nuget.org/packages/SimpleToolkit.UI.WinUI.Helpers/) |
|
||||
|
||||
## Features
|
||||
|
||||
- **ObservableKeyValuePair** - observable version of KeyValuePair
|
||||
- **ObservableDictionary** - observable version of Dictionary
|
||||
-
|
||||
## Installation and usage
|
||||
|
||||
You can download package from official NuGet registry or .nupkg file itself from Releases tab.
|
||||
|
||||
**CLI:**
|
||||
|
||||
```
|
||||
dotnet add package SimpleToolkit.MVVM
|
||||
```
|
||||
|
||||
**Package reference in .csproj file:**
|
||||
|
||||
```
|
||||
<PackageReference Include="SimpleToolkit.MVVM" Version="<version>" />
|
||||
```
|
||||
|
||||
## Attribution and contribution
|
||||
|
||||
This project is open source on MIT License, so you can just copy and upload again to your repository. But according to the license, you must include information about the original author. You can find license [here](https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.MVVM/src/branch/main/LICENSE).
|
||||
|
||||
However, the preferred way to contribute would be to propose improvements in a pull request, through issues, or through other means of communication.
|
||||
|
||||
**Other sources:**
|
||||
|
||||
- Icon by [Icons8](icons8.com)
|
||||
27
SimpleToolkit.MVVM/SimpleToolkit.MVVM.csproj
Normal file
27
SimpleToolkit.MVVM/SimpleToolkit.MVVM.csproj
Normal file
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Title>SimpleToolkit.MVVM</Title>
|
||||
<Authors>Mateusz Skoczek</Authors>
|
||||
<Copyright>Mateusz Skoczek</Copyright>
|
||||
<PackageProjectUrl>https://repos.mateuszskoczek.com/SimpleToolkit/</PackageProjectUrl>
|
||||
<PackageLicenseUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.MVVM/src/branch/main/LICENSE</PackageLicenseUrl>
|
||||
<PackageIcon>icon.png</PackageIcon>
|
||||
<RepositoryUrl>https://repos.mateuszskoczek.com/SimpleToolkit/SimpleToolkit.MVVM</RepositoryUrl>
|
||||
<Description>Set of helpers, class extensions, UI controls used in my other C# projects - MVVM architecture models and helper classes</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="icon.png">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
<None Update="README.md">
|
||||
<Pack>True</Pack>
|
||||
<PackagePath></PackagePath>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
BIN
SimpleToolkit.MVVM/icon.png
Normal file
BIN
SimpleToolkit.MVVM/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.7 KiB |
Reference in New Issue
Block a user