1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +08:00

Implement local sorting by name and country in SocialOverlay (#5847)

Implement local sorting by name and country in SocialOverlay

Co-authored-by: Dan Balasescu <smoogipoo@smgi.me>
Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Dean Herbert 2019-12-19 12:54:02 +09:00 committed by GitHub
commit c94c803562
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
@ -15,6 +16,8 @@ using osu.Game.Online.API.Requests;
using osu.Game.Overlays.SearchableList; using osu.Game.Overlays.SearchableList;
using osu.Game.Overlays.Social; using osu.Game.Overlays.Social;
using osu.Game.Users; using osu.Game.Users;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Threading; using osu.Framework.Threading;
namespace osu.Game.Overlays namespace osu.Game.Overlays
@ -31,17 +34,20 @@ namespace osu.Game.Overlays
protected override SearchableListHeader<SocialTab> CreateHeader() => new Header(); protected override SearchableListHeader<SocialTab> CreateHeader() => new Header();
protected override SearchableListFilterControl<SocialSortCriteria, SortDirection> CreateFilterControl() => new FilterControl(); protected override SearchableListFilterControl<SocialSortCriteria, SortDirection> CreateFilterControl() => new FilterControl();
private IEnumerable<User> users; private User[] users = Array.Empty<User>();
public IEnumerable<User> Users public User[] Users
{ {
get => users; get => users;
set set
{ {
if (ReferenceEquals(users, value)) if (users == value)
return; return;
users = value?.ToList(); users = value ?? Array.Empty<User>();
if (LoadState >= LoadState.Ready)
recreatePanels();
} }
} }
@ -67,11 +73,10 @@ namespace osu.Game.Overlays
}; };
Header.Tabs.Current.ValueChanged += _ => queueUpdate(); Header.Tabs.Current.ValueChanged += _ => queueUpdate();
Filter.Tabs.Current.ValueChanged += _ => onFilterUpdate();
Filter.Tabs.Current.ValueChanged += _ => queueUpdate(); Filter.DisplayStyleControl.DisplayStyle.ValueChanged += _ => recreatePanels();
Filter.DisplayStyleControl.Dropdown.Current.ValueChanged += _ => recreatePanels();
Filter.DisplayStyleControl.DisplayStyle.ValueChanged += style => recreatePanels(style.NewValue);
Filter.DisplayStyleControl.Dropdown.Current.ValueChanged += _ => queueUpdate();
currentQuery.BindTo(Filter.Search.Current); currentQuery.BindTo(Filter.Search.Current);
currentQuery.ValueChanged += query => currentQuery.ValueChanged += query =>
@ -85,6 +90,12 @@ namespace osu.Game.Overlays
}; };
} }
[BackgroundDependencyLoader]
private void load()
{
recreatePanels();
}
private APIRequest getUsersRequest; private APIRequest getUsersRequest;
private readonly Bindable<string> currentQuery = new Bindable<string>(); private readonly Bindable<string> currentQuery = new Bindable<string>();
@ -93,6 +104,8 @@ namespace osu.Game.Overlays
private void queueUpdate() => Scheduler.AddOnce(updateSearch); private void queueUpdate() => Scheduler.AddOnce(updateSearch);
private CancellationTokenSource loadCancellation;
private void updateSearch() private void updateSearch()
{ {
queryChangedDebounce?.Cancel(); queryChangedDebounce?.Cancel();
@ -102,7 +115,6 @@ namespace osu.Game.Overlays
Users = null; Users = null;
clearPanels(); clearPanels();
loading.Hide();
getUsersRequest?.Cancel(); getUsersRequest?.Cancel();
if (API?.IsLoggedIn != true) if (API?.IsLoggedIn != true)
@ -112,26 +124,43 @@ namespace osu.Game.Overlays
{ {
case SocialTab.Friends: case SocialTab.Friends:
var friendRequest = new GetFriendsRequest(); // TODO filter arguments? var friendRequest = new GetFriendsRequest(); // TODO filter arguments?
friendRequest.Success += updateUsers; friendRequest.Success += users => Users = users.ToArray();
API.Queue(getUsersRequest = friendRequest); API.Queue(getUsersRequest = friendRequest);
break; break;
default: default:
var userRequest = new GetUsersRequest(); // TODO filter arguments! var userRequest = new GetUsersRequest(); // TODO filter arguments!
userRequest.Success += res => updateUsers(res.Users.Select(r => r.User)); userRequest.Success += res => Users = res.Users.Select(r => r.User).ToArray();
API.Queue(getUsersRequest = userRequest); API.Queue(getUsersRequest = userRequest);
break; break;
} }
loading.Show();
} }
private void recreatePanels(PanelDisplayStyle displayStyle) private void recreatePanels()
{ {
clearPanels(); clearPanels();
if (Users == null) if (Users == null)
{
loading.Hide();
return; return;
}
IEnumerable<User> sortedUsers = Users;
switch (Filter.Tabs.Current.Value)
{
case SocialSortCriteria.Location:
sortedUsers = sortedUsers.OrderBy(u => u.Country.FullName);
break;
case SocialSortCriteria.Name:
sortedUsers = sortedUsers.OrderBy(u => u.Username);
break;
}
if (Filter.DisplayStyleControl.Dropdown.Current.Value == SortDirection.Descending)
sortedUsers = sortedUsers.Reverse();
var newPanels = new FillFlowContainer<SocialPanel> var newPanels = new FillFlowContainer<SocialPanel>
{ {
@ -139,11 +168,11 @@ namespace osu.Game.Overlays
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10f), Spacing = new Vector2(10f),
Margin = new MarginPadding { Top = 10 }, Margin = new MarginPadding { Top = 10 },
ChildrenEnumerable = Users.Select(u => ChildrenEnumerable = sortedUsers.Select(u =>
{ {
SocialPanel panel; SocialPanel panel;
switch (displayStyle) switch (Filter.DisplayStyleControl.DisplayStyle.Value)
{ {
case PanelDisplayStyle.Grid: case PanelDisplayStyle.Grid:
panel = new SocialGridPanel(u) panel = new SocialGridPanel(u)
@ -169,19 +198,28 @@ namespace osu.Game.Overlays
if (panels != null) if (panels != null)
ScrollFlow.Remove(panels); ScrollFlow.Remove(panels);
loading.Hide();
ScrollFlow.Add(panels = newPanels); ScrollFlow.Add(panels = newPanels);
}); }, (loadCancellation = new CancellationTokenSource()).Token);
} }
private void updateUsers(IEnumerable<User> newUsers) private void onFilterUpdate()
{ {
Users = newUsers; if (Filter.Tabs.Current.Value == SocialSortCriteria.Rank)
loading.Hide(); {
recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value); queueUpdate();
return;
}
recreatePanels();
} }
private void clearPanels() private void clearPanels()
{ {
loading.Show();
loadCancellation?.Cancel();
if (panels != null) if (panels != null)
{ {
panels.Expire(); panels.Expire();