// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Collections.Generic; using System.Linq; using osu.Framework.Bindables; using osuTK; using osuTK.Graphics; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics; using osu.Game.Graphics.UserInterface; using osu.Game.Online.API; using osu.Game.Online.API.Requests; using osu.Game.Overlays.SearchableList; using osu.Game.Overlays.Social; using osu.Game.Users; using osu.Framework.Threading; using System; namespace osu.Game.Overlays { public class SocialOverlay : SearchableListOverlay { private readonly LoadingAnimation loading; private FillFlowContainer panels; protected override Color4 BackgroundColour => OsuColour.FromHex(@"60284b"); protected override Color4 TrianglesColourLight => OsuColour.FromHex(@"672b51"); protected override Color4 TrianglesColourDark => OsuColour.FromHex(@"5c2648"); protected override SearchableListHeader CreateHeader() => new Header(); protected override SearchableListFilterControl CreateFilterControl() => new FilterControl(); private IEnumerable users; public IEnumerable Users { get => users; set { if (users?.Equals(value) ?? false) return; users = value?.ToList(); } } public SocialOverlay() { Waves.FirstWaveColour = OsuColour.FromHex(@"cb5fa0"); Waves.SecondWaveColour = OsuColour.FromHex(@"b04384"); Waves.ThirdWaveColour = OsuColour.FromHex(@"9b2b6e"); Waves.FourthWaveColour = OsuColour.FromHex(@"6d214d"); Add(loading = new LoadingAnimation()); Filter.Search.Current.ValueChanged += text => { if (!string.IsNullOrEmpty(text.NewValue)) { // force searching in players until searching for friends is supported Header.Tabs.Current.Value = SocialTab.AllPlayers; if (Filter.Tabs.Current.Value != SocialSortCriteria.Rank) Filter.Tabs.Current.Value = SocialSortCriteria.Rank; } }; Header.Tabs.Current.ValueChanged += _ => queueUpdate(); Filter.Tabs.Current.ValueChanged += _ => queueUpdate(); Filter.DisplayStyleControl.DisplayStyle.ValueChanged += style => recreatePanels(style.NewValue); Filter.DisplayStyleControl.Dropdown.Current.ValueChanged += _ => onDropdownChanged(); currentQuery.BindTo(Filter.Search.Current); currentQuery.ValueChanged += query => { queryChangedDebounce?.Cancel(); if (string.IsNullOrEmpty(query.NewValue)) queueUpdate(); else queryChangedDebounce = Scheduler.AddDelayed(updateSearch, 500); }; } private APIRequest getUsersRequest; private readonly Bindable currentQuery = new Bindable(); private ScheduledDelegate queryChangedDebounce; private void queueUpdate() => Scheduler.AddOnce(updateSearch); private void updateSearch() { queryChangedDebounce?.Cancel(); if (!IsLoaded) return; Users = null; clearPanels(); getUsersRequest?.Cancel(); if (API?.IsLoggedIn != true) return; switch (Header.Tabs.Current.Value) { case SocialTab.Friends: var friendRequest = new GetFriendsRequest(); // TODO filter arguments? friendRequest.Success += updateUsers; API.Queue(getUsersRequest = friendRequest); break; default: var userRequest = new GetUsersRequest(); // TODO filter arguments! userRequest.Success += response => updateUsers(response.Select(r => r.User)); API.Queue(getUsersRequest = userRequest); break; } loading.Show(); } private void recreatePanels(PanelDisplayStyle displayStyle) { clearPanels(); loading.Show(); if (Users == null) { loading.Hide(); return; } var newPanels = new FillFlowContainer { RelativeSizeAxes = Axes.X, AutoSizeAxes = Axes.Y, Spacing = new Vector2(10f), Margin = new MarginPadding { Top = 10 }, ChildrenEnumerable = Users.Select(u => { SocialPanel panel; switch (displayStyle) { case PanelDisplayStyle.Grid: panel = new SocialGridPanel(u) { Anchor = Anchor.TopCentre, Origin = Anchor.TopCentre }; break; default: panel = new SocialListPanel(u); break; } panel.Status.BindTo(u.Status); panel.Activity.BindTo(u.Activity); return panel; }) }; LoadComponentAsync(newPanels, f => { loading.Hide(); ScrollFlow.Add(panels = newPanels); }); } private void updateUsers(IEnumerable newUsers) { loading.Show(); var sortDirection = Filter.DisplayStyleControl.Dropdown.Current.Value; IEnumerable sortedUsers = newUsers; if (sortedUsers.Any()) { switch (Filter.Tabs.Current.Value) { case SocialSortCriteria.Location: sortedUsers = sortBy(sortedUsers, u => u.Country.FullName, sortDirection); break; case SocialSortCriteria.Name: sortedUsers = sortBy(sortedUsers, u => u.Username, sortDirection); break; } } Users = sortedUsers; recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value); } private IEnumerable sortBy(IEnumerable users, Func condition, SortDirection sortDirection) => sortDirection == SortDirection.Ascending ? users.OrderBy(condition) : users.OrderByDescending(condition); private void onDropdownChanged() { if (Users == null) { queueUpdate(); return; } updateUsers(Users); } private void clearPanels() { if (panels != null) { panels.Expire(); panels = null; } } public override void APIStateChanged(IAPIProvider api, APIState state) { switch (state) { case APIState.Online: queueUpdate(); break; default: Users = null; clearPanels(); break; } } } public enum SortDirection { Ascending, Descending } }