Skip to content

Commit

Permalink
Fix fatal error in mass downloaders when Twitch API lies about having…
Browse files Browse the repository at this point in the history
… a next page (#1235)

* Prevent cursorIndex from leaving the of bounds of cursorList.Count

* Fix crash caused by Twitch API returning pageInfo.hasNextPage but no video edges

* Extract common reset code
  • Loading branch information
ScrubN authored Oct 28, 2024
1 parent cb805e9 commit 594c990
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions TwitchDownloaderWPF/WindowMassDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public partial class WindowMassDownload : Window
public ObservableCollection<TaskData> videoList { get; set; } = new ObservableCollection<TaskData>();
public readonly List<TaskData> selectedItems = new List<TaskData>();
public readonly List<string> cursorList = new List<string>();
public int cursorIndex = -1;
public int cursorIndex = 0;
public string currentChannel = "";
public string period = "";
public int videoCount = 50;
Expand All @@ -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();
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -125,13 +129,18 @@ private async Task UpdateList()
});
}

btnNext.IsEnabled = res.data.user.videos.pageInfo.hasNextPage;
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[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);
}
}
}
}
}
Expand Down Expand Up @@ -184,13 +193,18 @@ private async Task UpdateList()
});
}

btnNext.IsEnabled = res.data.user.clips.pageInfo.hasNextPage;
btnPrev.IsEnabled = cursorIndex >= 0;
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);
}
}
}
}
}
Expand Down Expand Up @@ -220,15 +234,23 @@ private async void btnNext_Click(object sender, RoutedEventArgs e)
{
btnNext.IsEnabled = false;
btnPrev.IsEnabled = false;
cursorIndex++;
if (cursorIndex < cursorList.Count - 1)
{
cursorIndex++;
}

await UpdateList();
}

private async void btnPrev_Click(object sender, RoutedEventArgs e)
{
btnNext.IsEnabled = false;
btnPrev.IsEnabled = false;
cursorIndex--;
if (cursorIndex > 0)
{
cursorIndex--;
}

await UpdateList();
}

Expand Down Expand Up @@ -260,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();
}

Expand Down Expand Up @@ -312,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();
}

Expand Down

0 comments on commit 594c990

Please sign in to comment.