From b58fe2d80a88c90cb7f25accffd5be5dd62e8809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Sat, 13 Nov 2021 15:38:04 +0100 Subject: [PATCH] Restructure card to use single tracker at the top level --- .../TestSceneBeatmapCardDownloadButton.cs | 5 +-- .../Beatmaps/Drawables/Cards/BeatmapCard.cs | 16 ++++++--- .../Cards/BeatmapCardDownloadProgressBar.cs | 34 +++++++------------ .../Drawables/Cards/Buttons/DownloadButton.cs | 19 +++++++---- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardDownloadButton.cs b/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardDownloadButton.cs index 4a8bd7cff0..67ad699615 100644 --- a/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardDownloadButton.cs +++ b/osu.Game.Tests/Visual/Beatmaps/TestSceneBeatmapCardDownloadButton.cs @@ -178,8 +178,7 @@ namespace osu.Game.Tests.Visual.Beatmaps private class TestDownloadButton : DownloadButton { - public readonly Bindable State = new Bindable(); - public readonly BindableNumber Progress = new BindableNumber(); + public new Bindable State => base.State; public new BeatmapCardIconButton Download => base.Download; public new BeatmapCardIconButton Play => base.Play; @@ -187,8 +186,6 @@ namespace osu.Game.Tests.Visual.Beatmaps public TestDownloadButton(APIBeatmapSet beatmapSet) : base(beatmapSet) { - Tracker.State.BindTo(State); - Tracker.Progress.BindTo(Progress); } } } diff --git a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs index 09ceb72a10..23009cda5e 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCard.cs @@ -16,6 +16,7 @@ using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; +using osu.Game.Online; using osu.Game.Online.API.Requests.Responses; using osu.Game.Overlays; using osu.Game.Overlays.BeatmapSet; @@ -38,6 +39,9 @@ namespace osu.Game.Beatmaps.Drawables.Cards private readonly APIBeatmapSet beatmapSet; private readonly Bindable favouriteState; + [Cached] + private readonly BeatmapDownloadTracker downloadTracker; + private UpdateableOnlineBeatmapSetCover leftCover; private FillFlowContainer leftIconArea; @@ -61,6 +65,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards { this.beatmapSet = beatmapSet; favouriteState = new Bindable(new BeatmapSetFavouriteState(beatmapSet.HasFavourited, beatmapSet.FavouriteCount)); + downloadTracker = new BeatmapDownloadTracker(beatmapSet); } [BackgroundDependencyLoader] @@ -73,6 +78,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards InternalChildren = new Drawable[] { + downloadTracker, new Box { RelativeSizeAxes = Axes.Both, @@ -270,7 +276,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards } } }, - downloadProgressBar = new BeatmapCardDownloadProgressBar(beatmapSet) + downloadProgressBar = new BeatmapCardDownloadProgressBar { RelativeSizeAxes = Axes.X, Height = 6, @@ -314,7 +320,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards { base.LoadComplete(); - downloadProgressBar.IsActive.BindValueChanged(_ => updateState(), true); + downloadTracker.State.BindValueChanged(_ => updateState(), true); FinishTransforms(true); } @@ -367,8 +373,10 @@ namespace osu.Game.Beatmaps.Drawables.Cards statisticsContainer.FadeTo(IsHovered ? 1 : 0, TRANSITION_DURATION, Easing.OutQuint); rightButtonArea.FadeTo(IsHovered ? 1 : 0, TRANSITION_DURATION, Easing.OutQuint); - idleBottomContent.FadeTo(downloadProgressBar.IsActive.Value ? 0 : 1, TRANSITION_DURATION, Easing.OutQuint); - downloadProgressBar.FadeTo(downloadProgressBar.IsActive.Value ? 1 : 0, TRANSITION_DURATION, Easing.OutQuint); + bool showProgress = downloadTracker.State.Value == DownloadState.Downloading || downloadTracker.State.Value == DownloadState.Importing; + + idleBottomContent.FadeTo(showProgress ? 0 : 1, TRANSITION_DURATION, Easing.OutQuint); + downloadProgressBar.FadeTo(showProgress ? 1 : 0, TRANSITION_DURATION, Easing.OutQuint); } } } diff --git a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardDownloadProgressBar.cs b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardDownloadProgressBar.cs index 37a7e7d41d..a94900b0c7 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardDownloadProgressBar.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/BeatmapCardDownloadProgressBar.cs @@ -8,37 +8,33 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; using osu.Game.Graphics; using osu.Game.Online; -using osu.Game.Online.API.Requests.Responses; using osu.Game.Overlays; namespace osu.Game.Beatmaps.Drawables.Cards { public class BeatmapCardDownloadProgressBar : CompositeDrawable { - private readonly BindableBool isActive = new BindableBool(); - public IBindable IsActive => isActive; - public override bool IsPresent => true; - private readonly BeatmapDownloadTracker tracker; - private readonly CircularContainer background; private readonly CircularContainer foreground; private readonly Box backgroundFill; private readonly Box foregroundFill; + private readonly Bindable state = new Bindable(); + private readonly BindableDouble progress = new BindableDouble(); + [Resolved] private OsuColour colours { get; set; } [Resolved] private OverlayColourProvider colourProvider { get; set; } - public BeatmapCardDownloadProgressBar(APIBeatmapSet beatmapSet) + public BeatmapCardDownloadProgressBar() { InternalChildren = new Drawable[] { - tracker = new BeatmapDownloadTracker(beatmapSet), - background = new CircularContainer + new CircularContainer { RelativeSizeAxes = Axes.Both, Masking = true, @@ -60,44 +56,40 @@ namespace osu.Game.Beatmaps.Drawables.Cards } [BackgroundDependencyLoader] - private void load() + private void load(BeatmapDownloadTracker downloadTracker) { backgroundFill.Colour = colourProvider.Background6; + + ((IBindable)state).BindTo(downloadTracker.State); + ((IBindable)progress).BindTo(downloadTracker.Progress); } protected override void LoadComplete() { base.LoadComplete(); - tracker.State.BindValueChanged(_ => stateChanged(), true); - tracker.Progress.BindValueChanged(_ => progressChanged(), true); + state.BindValueChanged(_ => stateChanged(), true); + progress.BindValueChanged(_ => progressChanged(), true); } private void stateChanged() { - switch (tracker.State.Value) + switch (state.Value) { case DownloadState.Downloading: FinishTransforms(true); foregroundFill.Colour = colourProvider.Highlight1; - isActive.Value = true; break; case DownloadState.Importing: foregroundFill.FadeColour(colours.Yellow, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); - isActive.Value = true; - break; - - default: - isActive.Value = false; break; } } private void progressChanged() { - double progress = tracker.Progress.Value; - foreground.ResizeWidthTo((float)progress, progress > 0 ? BeatmapCard.TRANSITION_DURATION : 0, Easing.OutQuint); + foreground.ResizeWidthTo((float)progress.Value, progress.Value > 0 ? BeatmapCard.TRANSITION_DURATION : 0, Easing.OutQuint); } } } diff --git a/osu.Game/Beatmaps/Drawables/Cards/Buttons/DownloadButton.cs b/osu.Game/Beatmaps/Drawables/Cards/Buttons/DownloadButton.cs index 85e2988614..0e6d63830c 100644 --- a/osu.Game/Beatmaps/Drawables/Cards/Buttons/DownloadButton.cs +++ b/osu.Game/Beatmaps/Drawables/Cards/Buttons/DownloadButton.cs @@ -21,7 +21,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons { protected readonly DownloadIcon Download; protected readonly PlayIcon Play; - protected readonly BeatmapDownloadTracker Tracker; + protected readonly Bindable State = new Bindable(); [Resolved] private OsuColour colours { get; set; } = null!; @@ -37,27 +37,32 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons InternalChildren = new Drawable[] { - Tracker = new BeatmapDownloadTracker(beatmapSet), Download = new DownloadIcon(beatmapSet), Play = new PlayIcon(beatmapSet) }; } + [BackgroundDependencyLoader(true)] + private void load(BeatmapDownloadTracker? tracker) + { + if (tracker != null) + ((IBindable)State).BindTo(tracker.State); + } + protected override void LoadComplete() { base.LoadComplete(); - Tracker.Progress.BindValueChanged(_ => updateState()); - Tracker.State.BindValueChanged(_ => updateState(), true); + State.BindValueChanged(_ => updateState(), true); FinishTransforms(true); } private void updateState() { - Download.FadeTo(Tracker.State.Value != DownloadState.LocallyAvailable ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); - Download.Enabled.Value = Tracker.State.Value == DownloadState.NotDownloaded; + Download.FadeTo(State.Value != DownloadState.LocallyAvailable ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); + Download.Enabled.Value = State.Value == DownloadState.NotDownloaded; - Play.FadeTo(Tracker.State.Value == DownloadState.LocallyAvailable ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); + Play.FadeTo(State.Value == DownloadState.LocallyAvailable ? 1 : 0, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint); } protected class DownloadIcon : BeatmapCardIconButton