From 370d93912c852a04dbd64760d5dda0994879c705 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:23:29 -0400 Subject: [PATCH 1/3] Prevent cursorIndex from leaving the of bounds of cursorList.Count --- TwitchDownloaderWPF/WindowMassDownload.xaml.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs index 18cf974a..18e96947 100644 --- a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs +++ b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs @@ -220,7 +220,11 @@ private async void btnNext_Click(object sender, RoutedEventArgs e) { btnNext.IsEnabled = false; btnPrev.IsEnabled = false; - cursorIndex++; + if (cursorIndex < cursorList.Count - 1) + { + cursorIndex++; + } + await UpdateList(); } @@ -228,7 +232,11 @@ private async void btnPrev_Click(object sender, RoutedEventArgs e) { btnNext.IsEnabled = false; btnPrev.IsEnabled = false; - cursorIndex--; + if (cursorIndex > 0) + { + cursorIndex--; + } + await UpdateList(); } From 98efccf16a1d293c839e095804d86d5a19e10606 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:27:03 -0400 Subject: [PATCH 2/3] Fix crash caused by Twitch API returning pageInfo.hasNextPage but no video edges --- .../WindowMassDownload.xaml.cs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs index 18e96947..df12ac9f 100644 --- a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs +++ b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs @@ -125,13 +125,18 @@ private async Task UpdateList() }); } - btnNext.IsEnabled = res.data.user.videos.pageInfo.hasNextPage; btnPrev.IsEnabled = res.data.user.videos.pageInfo.hasPreviousPage; if (res.data.user.videos.pageInfo.hasNextPage) { - string newCursor = res.data.user.videos.edges[0].cursor; - if (!cursorList.Contains(newCursor)) - cursorList.Add(newCursor); + string newCursor = res.data.user.videos.edges.FirstOrDefault()?.cursor; + if (newCursor is not null) + { + btnNext.IsEnabled = true; + if (!cursorList.Contains(newCursor)) + { + cursorList.Add(newCursor); + } + } } } } @@ -184,13 +189,18 @@ private async Task UpdateList() }); } - btnNext.IsEnabled = res.data.user.clips.pageInfo.hasNextPage; btnPrev.IsEnabled = cursorIndex >= 0; if (res.data.user.clips.pageInfo.hasNextPage) { - string newCursor = res.data.user.clips.edges.First(x => x.cursor != null).cursor; - if (!cursorList.Contains(newCursor)) - cursorList.Add(newCursor); + string newCursor = res.data.user.clips.edges.FirstOrDefault(x => x.cursor != null)?.cursor; + if (newCursor is not null) + { + btnNext.IsEnabled = true; + if (!cursorList.Contains(newCursor)) + { + cursorList.Add(newCursor); + } + } } } } From 929639419f74856c34abc012b11f702d0732fc0d Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:27:43 -0400 Subject: [PATCH 3/3] Extract common reset code --- .../WindowMassDownload.xaml.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs index df12ac9f..3e65bce0 100644 --- a/TwitchDownloaderWPF/WindowMassDownload.xaml.cs +++ b/TwitchDownloaderWPF/WindowMassDownload.xaml.cs @@ -25,7 +25,7 @@ public partial class WindowMassDownload : Window public ObservableCollection videoList { get; set; } = new ObservableCollection(); public readonly List selectedItems = new List(); public readonly List cursorList = new List(); - public int cursorIndex = -1; + public int cursorIndex = 0; public string currentChannel = ""; public string period = ""; public int videoCount = 50; @@ -49,12 +49,18 @@ private async void btnChannel_Click(object sender, RoutedEventArgs e) await ChangeCurrentChannel(); } - private Task ChangeCurrentChannel() + private void ResetLists() { - currentChannel = textChannel.Text; videoList.Clear(); cursorList.Clear(); - cursorIndex = -1; + cursorList.Add(""); + cursorIndex = 0; + } + + private Task ChangeCurrentChannel() + { + currentChannel = textChannel.Text; + ResetLists(); return UpdateList(); } @@ -69,9 +75,7 @@ private async Task UpdateList() { // Pretend we are doing something so the status icon has time to show await Task.Delay(50); - videoList.Clear(); - cursorList.Clear(); - cursorIndex = -1; + ResetLists(); StatusImage.Visibility = Visibility.Hidden; return; } @@ -125,7 +129,7 @@ private async Task UpdateList() }); } - btnPrev.IsEnabled = res.data.user.videos.pageInfo.hasPreviousPage; + btnPrev.IsEnabled = cursorIndex > 0; if (res.data.user.videos.pageInfo.hasNextPage) { string newCursor = res.data.user.videos.edges.FirstOrDefault()?.cursor; @@ -189,7 +193,7 @@ private async Task UpdateList() }); } - btnPrev.IsEnabled = cursorIndex >= 0; + btnPrev.IsEnabled = cursorIndex > 0; if (res.data.user.clips.pageInfo.hasNextPage) { string newCursor = res.data.user.clips.edges.FirstOrDefault(x => x.cursor != null)?.cursor; @@ -278,9 +282,7 @@ private void btnQueue_Click(object sender, RoutedEventArgs e) private async void ComboSortByDate_SelectionChanged(object sender, SelectionChangedEventArgs e) { period = ((ComboBoxItem)ComboSortByDate.SelectedItem).Tag.ToString(); - videoList.Clear(); - cursorList.Clear(); - cursorIndex = -1; + ResetLists(); await UpdateList(); } @@ -330,9 +332,7 @@ private async void TextChannel_OnKeyDown(object sender, KeyEventArgs e) private async void ComboVideoCount_SelectionChanged(object sender, SelectionChangedEventArgs e) { videoCount = int.Parse((string)((ComboBoxItem)ComboVideoCount.SelectedValue).Content); - videoList.Clear(); - cursorList.Clear(); - cursorIndex = -1; + ResetLists(); await UpdateList(); }