Skip to content

Commit 2cbdde1

Browse files
committed
fix: chapter list not updating in UI
1 parent 5af20d4 commit 2cbdde1

2 files changed

Lines changed: 32 additions & 21 deletions

File tree

Screenbox/Controls/ChapterProgressBar.xaml.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
6+
using System.Collections.Specialized;
67
using Windows.Media.Core;
8+
using Windows.System;
79
using Windows.UI.Xaml;
810
using Windows.UI.Xaml.Controls;
11+
using Microsoft.Toolkit.Uwp.UI;
12+
using Screenbox.Core.Playback;
913
using Screenbox.ViewModels;
1014

1115
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
@@ -66,8 +70,11 @@ public int ChapterIndex
6670

6771
private const double Spacing = 1;
6872

73+
private readonly DispatcherQueueTimer _chaptersUpdateTimer;
74+
6975
public ChapterProgressBar()
7076
{
77+
_chaptersUpdateTimer = DispatcherQueue.GetForCurrentThread().CreateTimer();
7178
ProgressItems = new ObservableCollection<ChapterViewModel>();
7279
this.InitializeComponent();
7380
SizeChanged += OnSizeChanged;
@@ -76,7 +83,17 @@ public ChapterProgressBar()
7683
private static void OnChaptersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
7784
{
7885
ChapterProgressBar view = (ChapterProgressBar)d;
79-
view.PopulateProgressItems();
86+
if (e.NewValue is PlaybackChapterList { Count: 0 } chapterList)
87+
{
88+
INotifyCollectionChanged observableCollection = chapterList;
89+
observableCollection.CollectionChanged -= view.ChaptersOnCollectionChanged;
90+
observableCollection.CollectionChanged += view.ChaptersOnCollectionChanged;
91+
}
92+
else
93+
{
94+
view.PopulateProgressItems();
95+
}
96+
8097
}
8198

8299
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -106,6 +123,11 @@ private void OnSizeChanged(object sender, SizeChangedEventArgs e)
106123
}
107124
}
108125

126+
private void ChaptersOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
127+
{
128+
_chaptersUpdateTimer.Debounce(PopulateProgressItems, TimeSpan.FromMilliseconds(50));
129+
}
130+
109131
private void UpdateProgress()
110132
{
111133
if (ProgressItems.Count == 1)
Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
44
using System.Linq;
55
using Windows.Media.Core;
66
using LibVLCSharp.Shared.Structures;
77

88
namespace Screenbox.Core.Playback
99
{
10-
public sealed class PlaybackChapterList : IReadOnlyList<ChapterCue>
10+
public sealed class PlaybackChapterList : ReadOnlyObservableCollection<ChapterCue>
1111
{
12-
public int Count => _chapters.Count;
12+
private readonly ObservableCollection<ChapterCue> _chapters;
1313

14-
public ChapterCue this[int index] => _chapters[index];
15-
16-
private readonly List<ChapterCue> _chapters;
17-
18-
public PlaybackChapterList()
14+
public PlaybackChapterList() : base(new ObservableCollection<ChapterCue>())
1915
{
20-
_chapters = new List<ChapterCue>();
16+
_chapters = (ObservableCollection<ChapterCue>)Items;
2117
}
2218

2319
internal void Load(IEnumerable<ChapterDescription> vlcChapters)
@@ -30,17 +26,10 @@ internal void Load(IEnumerable<ChapterDescription> vlcChapters)
3026
});
3127

3228
_chapters.Clear();
33-
_chapters.AddRange(chapterCues);
34-
}
35-
36-
public IEnumerator<ChapterCue> GetEnumerator()
37-
{
38-
return _chapters.GetEnumerator();
39-
}
40-
41-
IEnumerator IEnumerable.GetEnumerator()
42-
{
43-
return _chapters.GetEnumerator();
29+
foreach (ChapterCue chapterCue in chapterCues)
30+
{
31+
_chapters.Add(chapterCue);
32+
}
4433
}
4534
}
4635
}

0 commit comments

Comments
 (0)