Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fatal error in mass downloaders when Twitch API lies about having a next page #1235

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading