1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 08:27:49 +08:00

Simplify UserStatus to be an enumeration type

There were absolutely no gains from having it be a reference type /
class, only complications, especially when coming from the serialisation
angle.
This commit is contained in:
Bartłomiej Dach 2023-12-06 18:21:44 +01:00
parent cb823f367f
commit d66fa09320
No known key found for this signature in database
8 changed files with 53 additions and 46 deletions

View File

@ -33,7 +33,7 @@ namespace osu.Desktop
[Resolved] [Resolved]
private IAPIProvider api { get; set; } = null!; private IAPIProvider api { get; set; } = null!;
private readonly IBindable<UserStatus> status = new Bindable<UserStatus>(); private readonly IBindable<UserStatus?> status = new Bindable<UserStatus?>();
private readonly IBindable<UserActivity> activity = new Bindable<UserActivity>(); private readonly IBindable<UserActivity> activity = new Bindable<UserActivity>();
private readonly Bindable<DiscordRichPresenceMode> privacyMode = new Bindable<DiscordRichPresenceMode>(); private readonly Bindable<DiscordRichPresenceMode> privacyMode = new Bindable<DiscordRichPresenceMode>();
@ -86,13 +86,13 @@ namespace osu.Desktop
if (!client.IsInitialized) if (!client.IsInitialized)
return; return;
if (status.Value is UserStatusOffline || privacyMode.Value == DiscordRichPresenceMode.Off) if (status.Value == UserStatus.Offline || privacyMode.Value == DiscordRichPresenceMode.Off)
{ {
client.ClearPresence(); client.ClearPresence();
return; return;
} }
if (status.Value is UserStatusOnline && activity.Value != null) if (status.Value == UserStatus.Online && activity.Value != null)
{ {
bool hideIdentifiableInformation = privacyMode.Value == DiscordRichPresenceMode.Limited; bool hideIdentifiableInformation = privacyMode.Value == DiscordRichPresenceMode.Limited;
presence.State = truncate(activity.Value.GetStatus(hideIdentifiableInformation)); presence.State = truncate(activity.Value.GetStatus(hideIdentifiableInformation));

View File

@ -64,7 +64,7 @@ namespace osu.Game.Tests.Visual.Online
Colour = color ?? "000000", Colour = color ?? "000000",
Status = Status =
{ {
Value = new UserStatusOnline() Value = UserStatus.Online
}, },
}; };

View File

@ -24,7 +24,7 @@ namespace osu.Game.Tests.Visual.Online
public partial class TestSceneUserPanel : OsuTestScene public partial class TestSceneUserPanel : OsuTestScene
{ {
private readonly Bindable<UserActivity> activity = new Bindable<UserActivity>(); private readonly Bindable<UserActivity> activity = new Bindable<UserActivity>();
private readonly Bindable<UserStatus> status = new Bindable<UserStatus>(); private readonly Bindable<UserStatus?> status = new Bindable<UserStatus?>();
private UserGridPanel boundPanel1; private UserGridPanel boundPanel1;
private TestUserListPanel boundPanel2; private TestUserListPanel boundPanel2;
@ -66,7 +66,7 @@ namespace osu.Game.Tests.Visual.Online
Id = 3103765, Id = 3103765,
CountryCode = CountryCode.JP, CountryCode = CountryCode.JP,
CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c6.jpg", CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c6.jpg",
Status = { Value = new UserStatusOnline() } Status = { Value = UserStatus.Online }
}) { Width = 300 }, }) { Width = 300 },
boundPanel1 = new UserGridPanel(new APIUser boundPanel1 = new UserGridPanel(new APIUser
{ {
@ -99,16 +99,16 @@ namespace osu.Game.Tests.Visual.Online
[Test] [Test]
public void TestUserStatus() public void TestUserStatus()
{ {
AddStep("online", () => status.Value = new UserStatusOnline()); AddStep("online", () => status.Value = UserStatus.Online);
AddStep("do not disturb", () => status.Value = new UserStatusDoNotDisturb()); AddStep("do not disturb", () => status.Value = UserStatus.DoNotDisturb);
AddStep("offline", () => status.Value = new UserStatusOffline()); AddStep("offline", () => status.Value = UserStatus.Offline);
AddStep("null status", () => status.Value = null); AddStep("null status", () => status.Value = null);
} }
[Test] [Test]
public void TestUserActivity() public void TestUserActivity()
{ {
AddStep("set online status", () => status.Value = new UserStatusOnline()); AddStep("set online status", () => status.Value = UserStatus.Online);
AddStep("idle", () => activity.Value = null); AddStep("idle", () => activity.Value = null);
AddStep("watching replay", () => activity.Value = new UserActivity.WatchingReplay(createScore(@"nats"))); AddStep("watching replay", () => activity.Value = new UserActivity.WatchingReplay(createScore(@"nats")));
@ -127,12 +127,12 @@ namespace osu.Game.Tests.Visual.Online
public void TestUserActivityChange() public void TestUserActivityChange()
{ {
AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent);
AddStep("set online status", () => status.Value = new UserStatusOnline()); AddStep("set online status", () => status.Value = UserStatus.Online);
AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent);
AddStep("set choosing activity", () => activity.Value = new UserActivity.ChoosingBeatmap()); AddStep("set choosing activity", () => activity.Value = new UserActivity.ChoosingBeatmap());
AddStep("set offline status", () => status.Value = new UserStatusOffline()); AddStep("set offline status", () => status.Value = UserStatus.Offline);
AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is visible", () => boundPanel2.LastVisitMessage.IsPresent);
AddStep("set online status", () => status.Value = new UserStatusOnline()); AddStep("set online status", () => status.Value = UserStatus.Online);
AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent); AddAssert("visit message is not visible", () => !boundPanel2.LastVisitMessage.IsPresent);
} }

View File

@ -247,7 +247,7 @@ namespace osu.Game.Online.API
userReq.Success += user => userReq.Success += user =>
{ {
// todo: save/pull from settings // todo: save/pull from settings
user.Status.Value = new UserStatusOnline(); user.Status.Value = UserStatus.Online;
setLocalUser(user); setLocalUser(user);

View File

@ -43,7 +43,7 @@ namespace osu.Game.Online.API.Requests.Responses
set => countryCodeString = value.ToString(); set => countryCodeString = value.ToString();
} }
public readonly Bindable<UserStatus> Status = new Bindable<UserStatus>(); public readonly Bindable<UserStatus?> Status = new Bindable<UserStatus?>();
public readonly Bindable<UserActivity> Activity = new Bindable<UserActivity>(); public readonly Bindable<UserActivity> Activity = new Bindable<UserActivity>();

View File

@ -148,17 +148,17 @@ namespace osu.Game.Overlays.Login
switch (action.NewValue) switch (action.NewValue)
{ {
case UserAction.Online: case UserAction.Online:
api.LocalUser.Value.Status.Value = new UserStatusOnline(); api.LocalUser.Value.Status.Value = UserStatus.Online;
dropdown.StatusColour = colours.Green; dropdown.StatusColour = colours.Green;
break; break;
case UserAction.DoNotDisturb: case UserAction.DoNotDisturb:
api.LocalUser.Value.Status.Value = new UserStatusDoNotDisturb(); api.LocalUser.Value.Status.Value = UserStatus.DoNotDisturb;
dropdown.StatusColour = colours.Red; dropdown.StatusColour = colours.Red;
break; break;
case UserAction.AppearOffline: case UserAction.AppearOffline:
api.LocalUser.Value.Status.Value = new UserStatusOffline(); api.LocalUser.Value.Status.Value = UserStatus.Offline;
dropdown.StatusColour = colours.Gray7; dropdown.StatusColour = colours.Gray7;
break; break;

View File

@ -6,6 +6,7 @@
using osuTK; using osuTK;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -18,7 +19,7 @@ namespace osu.Game.Users
{ {
public abstract partial class ExtendedUserPanel : UserPanel public abstract partial class ExtendedUserPanel : UserPanel
{ {
public readonly Bindable<UserStatus> Status = new Bindable<UserStatus>(); public readonly Bindable<UserStatus?> Status = new Bindable<UserStatus?>();
public readonly IBindable<UserActivity> Activity = new Bindable<UserActivity>(); public readonly IBindable<UserActivity> Activity = new Bindable<UserActivity>();
@ -97,14 +98,14 @@ namespace osu.Game.Users
return statusContainer; return statusContainer;
} }
private void displayStatus(UserStatus status, UserActivity activity = null) private void displayStatus(UserStatus? status, UserActivity activity = null)
{ {
if (status != null) if (status != null)
{ {
LastVisitMessage.FadeTo(status is UserStatusOffline && User.LastVisit.HasValue ? 1 : 0); LastVisitMessage.FadeTo(status == UserStatus.Offline && User.LastVisit.HasValue ? 1 : 0);
// Set status message based on activity (if we have one) and status is not offline // Set status message based on activity (if we have one) and status is not offline
if (activity != null && !(status is UserStatusOffline)) if (activity != null && status != UserStatus.Offline)
{ {
statusMessage.Text = activity.GetStatus(); statusMessage.Text = activity.GetStatus();
statusIcon.FadeColour(activity.GetAppropriateColour(Colours), 500, Easing.OutQuint); statusIcon.FadeColour(activity.GetAppropriateColour(Colours), 500, Easing.OutQuint);
@ -112,8 +113,8 @@ namespace osu.Game.Users
} }
// Otherwise use only status // Otherwise use only status
statusMessage.Text = status.Message; statusMessage.Text = status.GetLocalisableDescription();
statusIcon.FadeColour(status.GetAppropriateColour(Colours), 500, Easing.OutQuint); statusIcon.FadeColour(status.Value.GetAppropriateColour(Colours), 500, Easing.OutQuint);
return; return;
} }
@ -121,11 +122,11 @@ namespace osu.Game.Users
// Fallback to web status if local one is null // Fallback to web status if local one is null
if (User.IsOnline) if (User.IsOnline)
{ {
Status.Value = new UserStatusOnline(); Status.Value = UserStatus.Online;
return; return;
} }
Status.Value = new UserStatusOffline(); Status.Value = UserStatus.Offline;
} }
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)

View File

@ -1,6 +1,8 @@
// 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.ComponentModel;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osuTK.Graphics; using osuTK.Graphics;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -8,32 +10,36 @@ using osu.Game.Resources.Localisation.Web;
namespace osu.Game.Users namespace osu.Game.Users
{ {
public abstract class UserStatus public enum UserStatus
{ {
public abstract LocalisableString Message { get; } [LocalisableDescription(typeof(UsersStrings), nameof(UsersStrings.StatusOffline))]
public abstract Color4 GetAppropriateColour(OsuColour colours); Offline,
[Description("Do not disturb")]
DoNotDisturb,
[LocalisableDescription(typeof(UsersStrings), nameof(UsersStrings.StatusOnline))]
Online,
} }
public class UserStatusOnline : UserStatus public static class UserStatusExtensions
{ {
public override LocalisableString Message => UsersStrings.StatusOnline; public static Color4 GetAppropriateColour(this UserStatus userStatus, OsuColour colours)
public override Color4 GetAppropriateColour(OsuColour colours) => colours.GreenLight; {
} switch (userStatus)
{
case UserStatus.Offline:
return Color4.Black;
public abstract class UserStatusBusy : UserStatusOnline case UserStatus.DoNotDisturb:
{ return colours.RedDark;
public override Color4 GetAppropriateColour(OsuColour colours) => colours.YellowDark;
}
public class UserStatusOffline : UserStatus case UserStatus.Online:
{ return colours.GreenDark;
public override LocalisableString Message => UsersStrings.StatusOffline;
public override Color4 GetAppropriateColour(OsuColour colours) => Color4.Black;
}
public class UserStatusDoNotDisturb : UserStatus default:
{ throw new ArgumentOutOfRangeException(nameof(userStatus), userStatus, "Unsupported user status");
public override LocalisableString Message => "Do not disturb"; }
public override Color4 GetAppropriateColour(OsuColour colours) => colours.RedDark; }
} }
} }