refactor: add removeAll method to observable collection
This commit is contained in:
parent
905b189bf2
commit
35e3f5e487
@ -0,0 +1,24 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Client.Application.Extensions
|
||||||
|
{
|
||||||
|
public static class ObservableCollectionExtensions
|
||||||
|
{
|
||||||
|
public static void RemoveAll<T>(this ObservableCollection<T> collection,
|
||||||
|
Func<T, bool> condition)
|
||||||
|
{
|
||||||
|
for (int i = collection.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (condition(collection[i]))
|
||||||
|
{
|
||||||
|
collection.RemoveAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using Client.Domain.Common;
|
using Client.Application.Extensions;
|
||||||
|
using Client.Domain.Common;
|
||||||
using Client.Domain.Entities;
|
using Client.Domain.Entities;
|
||||||
using Client.Domain.ValueObjects;
|
using Client.Domain.ValueObjects;
|
||||||
using Client.Domain.ViewModels;
|
using Client.Domain.ViewModels;
|
||||||
@ -11,6 +12,8 @@ using System.Numerics;
|
|||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Media;
|
||||||
|
|
||||||
namespace Client.Application.ViewModels
|
namespace Client.Application.ViewModels
|
||||||
{
|
{
|
||||||
@ -45,13 +48,7 @@ namespace Client.Application.ViewModels
|
|||||||
|
|
||||||
public void RemoveNpc(NPC npc)
|
public void RemoveNpc(NPC npc)
|
||||||
{
|
{
|
||||||
foreach (var item in Creatures)
|
Creatures.RemoveAll(x => x.Id == npc.Id);
|
||||||
{
|
|
||||||
if (item.Id == npc.Id)
|
|
||||||
{
|
|
||||||
Creatures.Remove(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddPlayer(Player player)
|
public void AddPlayer(Player player)
|
||||||
@ -64,13 +61,7 @@ namespace Client.Application.ViewModels
|
|||||||
|
|
||||||
public void RemovePlayer(Player player)
|
public void RemovePlayer(Player player)
|
||||||
{
|
{
|
||||||
foreach (var item in Creatures)
|
Creatures.RemoveAll(x => x.Id == player.Id);
|
||||||
{
|
|
||||||
if (item.Id == player.Id)
|
|
||||||
{
|
|
||||||
Creatures.Remove(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddDrop(Drop drop)
|
public void AddDrop(Drop drop)
|
||||||
@ -83,13 +74,7 @@ namespace Client.Application.ViewModels
|
|||||||
|
|
||||||
public void RemoveDrop(Drop drop)
|
public void RemoveDrop(Drop drop)
|
||||||
{
|
{
|
||||||
foreach (var item in Drops)
|
Drops.RemoveAll(x => x.Id == drop.Id);
|
||||||
{
|
|
||||||
if (item.Id == drop.Id)
|
|
||||||
{
|
|
||||||
Drops.Remove(item);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObservableCollection<ChatMessageViewModel> ChatMessages { get; } = new ObservableCollection<ChatMessageViewModel>();
|
public ObservableCollection<ChatMessageViewModel> ChatMessages { get; } = new ObservableCollection<ChatMessageViewModel>();
|
||||||
|
Loading…
Reference in New Issue
Block a user