From a131875a7b40346b62f67175a40016fe7c998c0a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 21 May 2019 13:34:35 +0900 Subject: [PATCH] Use bindables the whole way --- .../Requests/Responses/APIChangelogBuild.cs | 4 +- .../API/Requests/Responses/APIUpdateStream.cs | 8 +- .../Overlays/Changelog/ChangelogHeader.cs | 42 +++++----- osu.Game/Overlays/ChangelogOverlay.cs | 81 ++++++++++--------- 4 files changed, 65 insertions(+), 70 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIChangelogBuild.cs b/osu.Game/Online/API/Requests/Responses/APIChangelogBuild.cs index 504c65928d..36c9cc610f 100644 --- a/osu.Game/Online/API/Requests/Responses/APIChangelogBuild.cs +++ b/osu.Game/Online/API/Requests/Responses/APIChangelogBuild.cs @@ -7,7 +7,7 @@ namespace osu.Game.Online.API.Requests.Responses { - public class APIChangelogBuild + public class APIChangelogBuild : IEquatable { [JsonProperty("id")] public long Id { get; set; } @@ -42,6 +42,8 @@ public class VersionNatigation public APIChangelogBuild Previous { get; set; } } + public bool Equals(APIChangelogBuild other) => this.Id == other?.Id; + public override string ToString() => $"{UpdateStream.DisplayName} {DisplayVersion}"; } } diff --git a/osu.Game/Online/API/Requests/Responses/APIUpdateStream.cs b/osu.Game/Online/API/Requests/Responses/APIUpdateStream.cs index 4c65b562dd..0c3fee88c7 100644 --- a/osu.Game/Online/API/Requests/Responses/APIUpdateStream.cs +++ b/osu.Game/Online/API/Requests/Responses/APIUpdateStream.cs @@ -25,13 +25,7 @@ public class APIUpdateStream : IEquatable [JsonProperty("latest_build")] public APIChangelogBuild LatestBuild { get; set; } - public bool Equals(APIUpdateStream other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - - return Id == other.Id; - } + public bool Equals(APIUpdateStream other) => this.Id == other?.Id; public ColourInfo Colour { diff --git a/osu.Game/Overlays/Changelog/ChangelogHeader.cs b/osu.Game/Overlays/Changelog/ChangelogHeader.cs index 4f406daabe..ccc976c4dc 100644 --- a/osu.Game/Overlays/Changelog/ChangelogHeader.cs +++ b/osu.Game/Overlays/Changelog/ChangelogHeader.cs @@ -3,6 +3,7 @@ using System; using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; @@ -17,6 +18,8 @@ namespace osu.Game.Overlays.Changelog { public class ChangelogHeader : OverlayHeader { + public readonly Bindable Current = new Bindable(); + public Action ListingSelected; private const string listing_string = "Listing"; @@ -29,6 +32,8 @@ public ChangelogHeader() if (e.NewValue == listing_string) ListingSelected?.Invoke(); }; + + Current.ValueChanged += showBuild; } [BackgroundDependencyLoader] @@ -37,35 +42,24 @@ private void load(OsuColour colours) TabControl.AccentColour = colours.Violet; } - private APIChangelogBuild displayedBuild; - private ChangelogHeaderTitle title; - public void ShowBuild(APIChangelogBuild build) + private void showBuild(ValueChangedEvent e) { - hideBuildTab(); + if (e.OldValue != null) + TabControl.RemoveItem(e.OldValue.ToString()); - displayedBuild = build; - - TabControl.AddItem(build.ToString()); - TabControl.Current.Value = build.ToString(); - - title.Version = build.UpdateStream.DisplayName; - } - - public void ShowListing() - { - hideBuildTab(); - - title.Version = null; - } - - private void hideBuildTab() - { - if (displayedBuild != null) + if (e.NewValue != null) { - TabControl.RemoveItem(displayedBuild.ToString()); - displayedBuild = null; + TabControl.AddItem(e.NewValue.ToString()); + TabControl.Current.Value = e.NewValue.ToString(); + + title.Version = e.NewValue.UpdateStream.DisplayName; + } + else + { + TabControl.Current.Value = listing_string; + title.Version = null; } } diff --git a/osu.Game/Overlays/ChangelogOverlay.cs b/osu.Game/Overlays/ChangelogOverlay.cs index 430fa569f1..34347d8e0a 100644 --- a/osu.Game/Overlays/ChangelogOverlay.cs +++ b/osu.Game/Overlays/ChangelogOverlay.cs @@ -1,12 +1,14 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Collections.Generic; -using System.Linq; using System.Threading; +using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Audio; using osu.Framework.Audio.Sample; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; @@ -32,6 +34,23 @@ public class ChangelogOverlay : FullscreenOverlay private List builds; + public readonly Bindable Current = new Bindable(); + + public void ShowListing() => Current.Value = null; + + /// + /// Fetches and shows a specific build from a specific update stream. + /// + /// Must contain at least and + /// . If and + /// are specified, the header will instantly display them. + public void ShowBuild([NotNull] APIChangelogBuild build) + { + if (build == null) throw new ArgumentNullException(nameof(build)); + + Current.Value = build; + } + [BackgroundDependencyLoader] private void load(AudioManager audio, OsuColour colour) { @@ -73,22 +92,30 @@ private void load(AudioManager audio, OsuColour colour) }, }; - // todo: better badges.Current.ValueChanged += e => { - if (e.NewValue?.LatestBuild != null) + if (e.NewValue != null) ShowBuild(e.NewValue.LatestBuild); }; sampleBack = audio.Sample.Get(@"UI/generic-select-soft"); - } - protected override void PopIn() - { - base.PopIn(); + header.Current.BindTo(Current); - if (!initialFetchPerformed) - fetchListing(); + Current.BindValueChanged(e => + { + if (e.NewValue != null) + { + badges.Current.Value = e.NewValue.UpdateStream; + + loadContent(new ChangelogSingleBuild(e.NewValue)); + } + else + { + badges.Current.Value = null; + loadContent(new ChangelogListing(builds)); + } + }); } public override bool OnPressed(GlobalAction action) @@ -96,13 +123,13 @@ public override bool OnPressed(GlobalAction action) switch (action) { case GlobalAction.Back: - if (content.Child is ChangelogListing) + if (Current.Value == null) { State = Visibility.Hidden; } else { - ShowListing(); + Current.Value = null; sampleBack?.Play(); } @@ -112,34 +139,12 @@ public override bool OnPressed(GlobalAction action) return false; } - public void ShowListing() + protected override void PopIn() { - if (content.Children.FirstOrDefault() is ChangelogListing) - return; + base.PopIn(); - header.ShowListing(); - badges.Current.Value = null; - loadContent(new ChangelogListing(builds)); - } - - /// - /// Fetches and shows a specific build from a specific update stream. - /// - /// Must contain at least and - /// . If and - /// are specified, the header will instantly display them. - public void ShowBuild(APIChangelogBuild build) - { - if (build == null) - { - ShowListing(); - return; - } - - header.ShowBuild(build); - badges.Current.Value = build.UpdateStream; - - loadContent(new ChangelogSingleBuild(build)); + if (!initialFetchPerformed) + fetchListing(); } private bool initialFetchPerformed; @@ -158,7 +163,7 @@ private void fetchListing() builds = res.Builds; badges.Populate(res.Streams); - ShowListing(); + Current.TriggerChange(); }; req.Failure += _ => initialFetchPerformed = false;