1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 22:32:55 +08:00
osu-lazer/osu.Game/Overlays/Profile/Header/Components/FollowersButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
6.3 KiB
C#
Raw Normal View History

2019-04-26 12:29:58 +08:00
// 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.
2024-11-01 12:54:13 +08:00
using System.Linq;
2019-04-26 12:29:58 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2024-11-01 12:54:13 +08:00
using osu.Framework.Extensions.Color4Extensions;
2019-04-26 12:29:58 +08:00
using osu.Framework.Graphics.Sprites;
2024-11-01 12:54:13 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
2024-11-01 12:54:13 +08:00
using osu.Game.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Notifications;
2021-07-17 21:18:45 +08:00
using osu.Game.Resources.Localisation.Web;
2019-04-26 12:29:58 +08:00
2019-04-26 12:49:44 +08:00
namespace osu.Game.Overlays.Profile.Header.Components
2019-04-26 12:29:58 +08:00
{
public partial class FollowersButton : ProfileHeaderStatisticsButton
2019-04-26 12:29:58 +08:00
{
2023-01-11 02:24:54 +08:00
public readonly Bindable<UserProfileData?> User = new Bindable<UserProfileData?>();
2019-04-26 12:29:58 +08:00
2024-11-01 14:06:23 +08:00
// Because it is impossible to update the number of friends after the operation,
// the number of friends obtained is stored and modified locally.
private int followerCount;
2024-11-01 14:43:00 +08:00
public override LocalisableString TooltipText
{
get
{
switch (status.Value)
{
case FriendStatus.Self:
return FriendsStrings.ButtonsDisabled;
case FriendStatus.None:
return FriendsStrings.ButtonsAdd;
case FriendStatus.NotMutual:
case FriendStatus.Mutual:
return FriendsStrings.ButtonsRemove;
}
return FriendsStrings.TitleCompact;
}
}
2019-04-26 12:29:58 +08:00
2021-01-22 04:39:19 +08:00
protected override IconUsage Icon => FontAwesome.Solid.User;
2019-04-26 12:29:58 +08:00
2024-11-01 12:54:13 +08:00
private readonly IBindableList<APIRelation> apiFriends = new BindableList<APIRelation>();
private readonly IBindable<APIUser> localUser = new Bindable<APIUser>();
private readonly Bindable<FriendStatus> status = new Bindable<FriendStatus>();
[Resolved]
private OsuColour colour { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
2019-04-26 12:29:58 +08:00
[BackgroundDependencyLoader]
2024-11-01 13:58:08 +08:00
private void load(IAPIProvider api, INotificationOverlay? notifications)
2024-11-01 12:54:13 +08:00
{
localUser.BindTo(api.LocalUser);
status.BindValueChanged(_ =>
{
updateIcon();
updateColor();
});
2024-11-01 14:06:23 +08:00
User.BindValueChanged(u =>
{
followerCount = u.NewValue?.User.FollowerCount ?? 0;
updateStatus();
}, true);
2024-11-01 12:54:13 +08:00
apiFriends.BindTo(api.Friends);
apiFriends.BindCollectionChanged((_, _) => Schedule(updateStatus));
Action += () =>
{
if (User.Value == null)
return;
if (status.Value == FriendStatus.Self)
return;
2024-11-01 14:42:50 +08:00
ShowLoadingLayer();
2024-11-01 12:54:13 +08:00
APIRequest req = status.Value == FriendStatus.None ? new AddFriendRequest(User.Value.User.OnlineID) : new DeleteFriendRequest(User.Value.User.OnlineID);
2024-11-01 12:54:13 +08:00
req.Success += () =>
{
2024-11-01 14:06:23 +08:00
followerCount += status.Value == FriendStatus.None ? 1 : -1;
2024-11-01 12:54:13 +08:00
api.UpdateLocalFriends();
2024-11-01 14:42:50 +08:00
updateStatus();
HideLoadingLayer();
2024-11-01 12:54:13 +08:00
};
req.Failure += e =>
{
notifications?.Post(new SimpleNotification
{
Text = e.Message,
Icon = FontAwesome.Solid.Times,
});
2024-11-01 14:42:50 +08:00
HideLoadingLayer();
2024-11-01 12:54:13 +08:00
};
api.Queue(req);
};
}
protected override bool OnHover(HoverEvent e)
{
if (status.Value > FriendStatus.None)
{
SetIcon(FontAwesome.Solid.UserTimes);
}
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateIcon();
}
private void updateStatus()
{
2024-11-01 14:06:23 +08:00
SetValue(followerCount);
2024-11-01 12:54:13 +08:00
if (localUser.Value.OnlineID == User.Value?.User.OnlineID)
{
status.Value = FriendStatus.Self;
return;
}
var friend = apiFriends.FirstOrDefault(u => User.Value?.User.OnlineID == u.TargetID);
if (friend != null)
{
status.Value = friend.Mutual ? FriendStatus.Mutual : FriendStatus.NotMutual;
}
else
{
status.Value = FriendStatus.None;
}
}
private void updateIcon()
{
switch (status.Value)
{
case FriendStatus.Self:
SetIcon(FontAwesome.Solid.User);
break;
case FriendStatus.None:
SetIcon(FontAwesome.Solid.UserPlus);
break;
case FriendStatus.NotMutual:
SetIcon(FontAwesome.Solid.User);
break;
case FriendStatus.Mutual:
SetIcon(FontAwesome.Solid.UserFriends);
break;
}
}
private void updateColor()
{
switch (status.Value)
{
case FriendStatus.Self:
case FriendStatus.None:
IdleColour = colourProvider.Background6;
HoverColour = colourProvider.Background5;
2024-11-01 14:42:50 +08:00
SetBackgroundColour(colourProvider.Background6, 200);
2024-11-01 12:54:13 +08:00
break;
case FriendStatus.NotMutual:
IdleColour = colour.Green;
HoverColour = colour.Green.Lighten(0.1f);
2024-11-01 14:42:50 +08:00
SetBackgroundColour(colour.Green, 200);
2024-11-01 12:54:13 +08:00
break;
case FriendStatus.Mutual:
IdleColour = colour.Pink;
HoverColour = colour.Pink1.Lighten(0.1f);
2024-11-01 14:42:50 +08:00
SetBackgroundColour(colour.Pink, 200);
2024-11-01 12:54:13 +08:00
break;
}
}
private enum FriendStatus
2019-04-26 12:29:58 +08:00
{
2024-11-01 12:54:13 +08:00
Self,
None,
NotMutual,
Mutual,
2019-04-26 12:29:58 +08:00
}
}
}