Add unread count to `INotificationOverlay` and fix toolbar button DI logic

This commit is contained in:
Dean Herbert 2022-04-19 11:10:11 +09:00
parent 94335c2938
commit 83e89b3e80
4 changed files with 27 additions and 11 deletions

View File

@ -2,6 +2,7 @@
// 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.Linq; using System.Linq;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
@ -22,6 +23,17 @@ public class TestSceneToolbar : OsuManualInputManagerTestScene
[Resolved] [Resolved]
private IRulesetStore rulesets { get; set; } private IRulesetStore rulesets { get; set; }
private readonly Mock<INotificationOverlay> notifications = new Mock<INotificationOverlay>();
private readonly BindableInt unreadNotificationCount = new BindableInt();
[BackgroundDependencyLoader]
private void load()
{
Dependencies.CacheAs(notifications.Object);
notifications.SetupGet(n => n.UnreadCount).Returns(unreadNotificationCount);
}
[SetUp] [SetUp]
public void SetUp() => Schedule(() => public void SetUp() => Schedule(() =>
{ {
@ -31,10 +43,6 @@ public void SetUp() => Schedule(() =>
[Test] [Test]
public void TestNotificationCounter() public void TestNotificationCounter()
{ {
ToolbarNotificationButton notificationButton = null;
AddStep("retrieve notification button", () => notificationButton = toolbar.ChildrenOfType<ToolbarNotificationButton>().Single());
setNotifications(1); setNotifications(1);
setNotifications(2); setNotifications(2);
setNotifications(3); setNotifications(3);
@ -43,7 +51,7 @@ public void TestNotificationCounter()
void setNotifications(int count) void setNotifications(int count)
=> AddStep($"set notification count to {count}", => AddStep($"set notification count to {count}",
() => notificationButton.NotificationCount.Value = count); () => unreadNotificationCount.Value = count);
} }
[TestCase(false)] [TestCase(false)]

View File

@ -2,6 +2,7 @@
// 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 osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
namespace osu.Game.Overlays namespace osu.Game.Overlays
@ -22,5 +23,10 @@ public interface INotificationOverlay
/// Hide the overlay, if it is currently visible. /// Hide the overlay, if it is currently visible.
/// </summary> /// </summary>
void Hide(); void Hide();
/// <summary>
/// Current number of unread notifications.
/// </summary>
IBindable<int> UnreadCount { get; }
} }
} }

View File

@ -99,7 +99,9 @@ protected override void LoadComplete()
OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true); OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true);
} }
public readonly BindableInt UnreadCount = new BindableInt(); public IBindable<int> UnreadCount => unreadCount;
private readonly BindableInt unreadCount = new BindableInt();
private int runningDepth; private int runningDepth;
@ -180,7 +182,7 @@ private void playDebouncedSample(string sampleName)
private void updateCounts() private void updateCounts()
{ {
UnreadCount.Value = sections.Select(c => c.UnreadCount).Sum(); unreadCount.Value = sections.Select(c => c.UnreadCount).Sum();
} }
private void markAllRead() private void markAllRead()

View File

@ -18,7 +18,7 @@ public class ToolbarNotificationButton : ToolbarOverlayToggleButton
{ {
protected override Anchor TooltipAnchor => Anchor.TopRight; protected override Anchor TooltipAnchor => Anchor.TopRight;
public BindableInt NotificationCount = new BindableInt(); public IBindable<int> NotificationCount = new BindableInt();
private readonly CountCircle countDisplay; private readonly CountCircle countDisplay;
@ -36,10 +36,10 @@ public ToolbarNotificationButton()
}); });
} }
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader]
private void load(NotificationOverlay notificationOverlay) private void load(INotificationOverlay notificationOverlay)
{ {
StateContainer = notificationOverlay; StateContainer = notificationOverlay as NotificationOverlay;
if (notificationOverlay != null) if (notificationOverlay != null)
NotificationCount.BindTo(notificationOverlay.UnreadCount); NotificationCount.BindTo(notificationOverlay.UnreadCount);