Skip to content

Commit

Permalink
Fix an error occuring when all currently followed streams were blocke…
Browse files Browse the repository at this point in the history
…d by the user && remove unnecessary page change when page count changed
  • Loading branch information
Mrcubix committed Nov 21, 2023
1 parent 6c1d74c commit 9cf3133
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions WhoIsLive.UX/ViewModels/Screens/StreamsBrowserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ public StreamsBrowserViewModel(string clientID, string accessToken, string userI

CurrentPageLiveStreams = new ObservableCollection<LiveStreamViewModel>();

Page = 1;

// Elements per page

_elementsPerPage = SettingsScreenViewModel.ElementsPerPageChoices[settings.ElementsPerPageIndex];
Expand Down Expand Up @@ -195,7 +193,12 @@ public async Task Refresh()

private async Task<List<User>?> GetUsers(List<LiveStream> streams)
{
UsersRequestHelper helper = new(GET_USERS_URL, streams.Select(stream => stream.UserId).ToArray());
var nonBlockedStreams = streams.Select(stream => stream.UserId).ToArray();

if (nonBlockedStreams.Length == 0)
return new();

UsersRequestHelper helper = new(GET_USERS_URL, nonBlockedStreams);

return await FetchItems<User, Users, string[]>(helper, false);
}
Expand Down Expand Up @@ -293,8 +296,11 @@ private void ChangePage(int page, bool doRecalculation = true)

if (doRecalculation)
RecalculatePageCount();

FillPage((Page - 1) * _elementsPerPage);

if (PageCount > 0)
FillPage((Page - 1) * _elementsPerPage);

HasStreams = CurrentPageLiveStreams.Count > 0;
}

public void UnsubscribeFromEvents()
Expand All @@ -309,10 +315,12 @@ public void UnsubscribeFromEvents()
public void RecalculatePageCount()
{
PageCount = (int)Math.Ceiling((double)_liveStreams.Count / _elementsPerPage);
Page = Math.Clamp(Page, 1, PageCount);

if (PageCount > 0)
Page = Math.Clamp(Page, 1, PageCount);

CanGoBack = Page > 1;
CanGoNext = Page < PageCount;
CanGoNext = Page < PageCount && PageCount > 0;
}

private void FillPage(int fromIndex)
Expand Down Expand Up @@ -343,8 +351,6 @@ private void FillPage(int fromIndex)
stream.BlockRequested += OnBlockRequested;
stream.OpenRequested += OnOpenRequested;
}

HasStreams = CurrentPageLiveStreams.Count > 0;
}

public void Run() => _ = Task.Run(Refresh);
Expand Down Expand Up @@ -378,16 +384,22 @@ private void OnBlockRequested(object? sender, string userId)
CurrentPageLiveStreams.Remove(stream);

SettingsScreenViewModel.BlockedStreams.Add(new BlockedStreamViewModel(stream.Username));

var oldPageCount = PageCount;

// We start filling from the last index of the page post removal.
RecalculatePageCount();
//RecalculatePageCount();

// Cases:
// - An element got removed, and the last page is now empty, but we are not on the last page.
// Result: Just fill the current page.
// - An element got removed, and the last page is now empty, and we are on the last page.
// Result: Go back one page (The current value of Page since it got clamped to the last page)

if (oldPageCount == PageCount)
if (Page != PageCount || CurrentPageLiveStreams.Count > 0) // We are not on the last page, or the last page is not empty.
FillPage(Page * _elementsPerPage - 1);
else
ChangePage(Page, false);
else // We are on the last page, and it's empty.
GoBack();

RecalculatePageCount();
}

private void OnOpenRequested(object? sender, string url)
Expand Down

0 comments on commit 9cf3133

Please sign in to comment.