-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListModel.cs
45 lines (39 loc) · 1.14 KB
/
ListModel.cs
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
45
using System.Collections.Generic;
namespace TodoApplication
{
public class ListModel<TItem>
{
public List<TItem> Items { get; }
private readonly LimitedSizeStack<(bool isAdded, int number, TItem item)> _tupleStack;
public ListModel(int limit)
{
Items = new List<TItem>();
_tupleStack = new LimitedSizeStack<(bool, int, TItem)>(limit);
}
public void AddItem(TItem item)
{
Items.Add(item);
var tuple = (true, Items.Count - 1, item);
_tupleStack.Push(tuple);
}
public void RemoveItem(int index)
{
var tuple = (false, index, Items[index]);
_tupleStack.Push(tuple);
Items.RemoveAt(index);
}
public bool CanUndo()
{
return _tupleStack.Count > 0;
}
public void Undo()
{
if (!CanUndo()) return;
var tuple = _tupleStack.Pop();
if (tuple.isAdded)
Items.RemoveAt(tuple.number);
else
Items.Insert(tuple.number, tuple.item);
}
}
}