Skip to content

Commit f500ad7

Browse files
committed
fix: keep favorite collection cursor valid
1 parent a946e1e commit f500ad7

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

Assets/Scripts/Misc/Collections/MyFavoriteSongCollection.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public MyFavoriteSongCollection(List<ISongDetail> dataSet, HashSet<string> hashS
2727

2828
public void Add(ISongDetail item)
2929
{
30+
NormalizeIndex();
31+
var currentHash = IsEmpty ? null : Current.Hash;
3032
if (!_hashSet.Add(item.Hash))
3133
{
3234
return;
@@ -43,11 +45,19 @@ public void Add(ISongDetail item)
4345
sorted.Add(item);
4446
Sorted = sorted.ToArray();
4547
}
48+
NormalizeIndex();
49+
if (currentHash is not null)
50+
{
51+
SetCursor(currentHash);
52+
}
4653
}
4754
public void Clear()
4855
{
4956
_dataSet.Clear();
5057
_hashSet.Clear();
58+
Origin = Array.Empty<ISongDetail>();
59+
Sorted = Origin;
60+
NormalizeIndex();
5161
}
5262
public bool Contains(ISongDetail item)
5363
{
@@ -63,26 +73,12 @@ public void CopyTo(ISongDetail[] array, int arrayIndex)
6373
}
6474
public bool Remove(ISongDetail item)
6575
{
66-
if(!_hashSet.Remove(item.Hash))
67-
{
68-
return false;
69-
}
70-
_dataSet.Remove(item);
71-
Origin = _dataSet.ToArray();
72-
if (!IsSorted)
73-
{
74-
Sorted = Origin;
75-
}
76-
else if (Sorted.Any(x => x.Hash == item.Hash))
77-
{
78-
var sorted = new List<ISongDetail>(Sorted);
79-
sorted.Remove(item);
80-
Sorted = sorted.ToArray();
81-
}
82-
return true;
76+
return Remove(item.Hash);
8377
}
8478
public bool Remove(string hashBase64Str)
8579
{
80+
NormalizeIndex();
81+
var currentHash = IsEmpty ? null : Current.Hash;
8682
if (!_hashSet.Remove(hashBase64Str))
8783
{
8884
return false;
@@ -98,6 +94,15 @@ public bool Remove(string hashBase64Str)
9894
{
9995
Sorted = Origin;
10096
}
97+
else
98+
{
99+
Sorted = Sorted.Where(x => x.Hash != hashBase64Str).ToArray();
100+
}
101+
NormalizeIndex();
102+
if (currentHash is not null && currentHash != hashBase64Str)
103+
{
104+
SetCursor(currentHash);
105+
}
101106
return true;
102107
}
103108
public HashSet<string> ExportHashSet()

Assets/Scripts/Misc/Collections/SongCollection.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public int Index
3232
{
3333
throw new ArgumentOutOfRangeException("this collection is empty");
3434
}
35-
_index = value.Clamp(0, Origin.Length - 1);
35+
_index = value.Clamp(0, Sorted.Length - 1);
3636
}
3737
}
3838
public ISongDetail this[int index]
@@ -167,6 +167,7 @@ public void Move(int diff)
167167
}
168168
public void SortAndFilter(SongOrder orderBy)
169169
{
170+
NormalizeIndex();
170171
if(Type == ChartStorageType.Dan || IsEmpty)
171172
{
172173
return;
@@ -184,6 +185,7 @@ public async Task SortAndFilterAsync(SongOrder orderBy)
184185
}
185186
public void Reset()
186187
{
188+
NormalizeIndex();
187189
if (!IsSorted)
188190
{
189191
return;
@@ -201,6 +203,15 @@ public void Reset()
201203
}
202204
Sorted = Origin;
203205
}
206+
protected void NormalizeIndex()
207+
{
208+
if (IsEmpty)
209+
{
210+
_index = 0;
211+
return;
212+
}
213+
_index = _index.Clamp(0, Sorted.Length - 1);
214+
}
204215
public void SetCursor(ISongDetail target)
205216
{
206217
if (IsEmpty)

0 commit comments

Comments
 (0)