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

Create placeholder classes instead of changing Children.

- Add MessagePlaceholder
- Use MessagePlacholder for when API is offline/user isn't a supporter
- Remove unnecessary placeholderFlow field
- Hook into API state changes
This commit is contained in:
naoey 2017-12-20 20:00:52 +05:30
parent 6d471da459
commit 23f4799840

View File

@ -34,7 +34,6 @@ namespace osu.Game.Screens.Select.Leaderboards
private readonly ScrollContainer scrollContainer;
private readonly Container placeholderContainer;
private readonly FillFlowContainer placeholderFlow;
private FillFlowContainer<LeaderboardScore> scrollFlow;
@ -63,22 +62,7 @@ namespace osu.Game.Screens.Select.Leaderboards
if (!scores.Any())
{
placeholderFlow.Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.fa_exclamation_circle,
Size = new Vector2(26),
Margin = new MarginPadding { Right = 10 },
},
new OsuSpriteText
{
Text = @"No records yet!",
TextSize = 22,
},
};
placeholderContainer.FadeIn(fade_duration);
replacePlaceholder(new MessagePlaceholder(@"No records yet!"));
return;
}
@ -135,17 +119,9 @@ namespace osu.Game.Screens.Select.Leaderboards
placeholderContainer = new Container
{
Alpha = 0,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
placeholderFlow = new FillFlowContainer
{
Direction = FillDirection.Horizontal,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Both,
},
},
AutoSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
}
@ -181,6 +157,9 @@ namespace osu.Game.Screens.Select.Leaderboards
if (osuGame != null)
osuGame.Ruleset.ValueChanged += handleRulesetChange;
if (api != null)
api.OnStateChange += handleApiStateChange;
}
protected override void Dispose(bool isDisposing)
@ -195,6 +174,23 @@ namespace osu.Game.Screens.Select.Leaderboards
private void handleRulesetChange(RulesetInfo ruleset) => UpdateScores();
private void handleApiStateChange(APIState oldState, APIState newState)
{
if (Scope == LeaderboardScope.Local)
// No need to respond to API state change while current scope is local
return;
if (newState == APIState.Offline)
{
Scores = null;
replacePlaceholder(new MessagePlaceholder(@"Please login to view online leaderboards!"));
return;
}
if (newState == APIState.Online)
UpdateScores();
}
protected virtual void UpdateScores()
{
if (!IsLoaded) return;
@ -215,6 +211,20 @@ namespace osu.Game.Screens.Select.Leaderboards
return;
}
if (!api.IsLoggedIn)
{
loading.Hide();
replacePlaceholder(new MessagePlaceholder(@"Please login to view online leaderboards!"));
return;
}
if (Scope != LeaderboardScope.Global && !api.LocalUser.Value.IsSupporter)
{
loading.Hide();
replacePlaceholder(new MessagePlaceholder(@"Please invest in a supporter tag to view this leaderboard!"));
return;
}
getScoresRequest = new GetScoresRequest(Beatmap, osuGame?.Ruleset.Value ?? Beatmap.Ruleset, Scope);
getScoresRequest.Success += r =>
{
@ -230,24 +240,21 @@ namespace osu.Game.Screens.Select.Leaderboards
if (e is OperationCanceledException) return;
Scores = null;
placeholderFlow.Children = new Drawable[]
replacePlaceholder(new RetrievalFailurePlaceholder
{
new RetryButton
{
Action = UpdateScores,
Margin = new MarginPadding { Right = 10 },
},
new OsuSpriteText
{
Anchor = Anchor.TopLeft,
Text = @"Couldn't retrieve scores!",
TextSize = 22,
},
};
placeholderContainer.FadeIn(fade_duration);
OnRetry = UpdateScores,
});
Logger.Error(e, @"Couldn't fetch beatmap scores!");
}
private void replacePlaceholder(Drawable placeholder)
{
placeholderContainer.FadeOutFromOne(fade_duration, Easing.OutQuint);
placeholderContainer.Clear(true);
placeholderContainer.Child = placeholder;
placeholderContainer.FadeInFromZero(fade_duration, Easing.OutQuint);
}
protected override void Update()
{
base.Update();
@ -277,6 +284,54 @@ namespace osu.Game.Screens.Select.Leaderboards
}
}
private class MessagePlaceholder : FillFlowContainer
{
public MessagePlaceholder(string message)
{
Direction = FillDirection.Horizontal;
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
new SpriteIcon
{
Icon = FontAwesome.fa_exclamation_circle,
Size = new Vector2(26),
Margin = new MarginPadding { Right = 10 },
},
new OsuSpriteText
{
Text = message,
TextSize = 22,
},
};
}
}
private class RetrievalFailurePlaceholder : FillFlowContainer
{
public Action OnRetry;
public RetrievalFailurePlaceholder()
{
Direction = FillDirection.Horizontal;
AutoSizeAxes = Axes.Both;
Children = new Drawable[]
{
new RetryButton
{
Action = () => OnRetry?.Invoke(),
Margin = new MarginPadding { Right = 10 },
},
new OsuSpriteText
{
Anchor = Anchor.TopLeft,
Text = @"Couldn't retrieve scores!",
TextSize = 22,
},
};
}
}
private class RetryButton : BeatSyncedContainer
{
private readonly SpriteIcon icon;