Simplify bindable logic greatly

This commit is contained in:
Dean Herbert 2022-07-15 19:02:12 +09:00
parent 8a48cb701d
commit 0e788ac714

View File

@ -5,6 +5,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
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.Scoring; using osu.Game.Scoring;
@ -20,16 +21,15 @@ namespace osu.Game.Screens.Play
private readonly Func<Task<ScoreInfo>> importFailedScore; private readonly Func<Task<ScoreInfo>> importFailedScore;
private Task<ScoreInfo>? saveFailedScoreTask;
private ScoreInfo? score; private ScoreInfo? score;
private DownloadButton button = null!; private DownloadButton button = null!;
public SaveFailedScoreButton(Func<Task<ScoreInfo>> requestImportFailedScore) public SaveFailedScoreButton(Func<Task<ScoreInfo>> importFailedScore)
{ {
Size = new Vector2(50, 30); Size = new Vector2(50, 30);
importFailedScore = requestImportFailedScore;
this.importFailedScore = importFailedScore;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -38,80 +38,48 @@ namespace osu.Game.Screens.Play
InternalChild = button = new DownloadButton InternalChild = button = new DownloadButton
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
}; State = { BindTarget = state },
Action = () =>
button.Action = () =>
{
switch (state.Value)
{ {
case DownloadState.LocallyAvailable: switch (state.Value)
game?.PresentScore(score, ScorePresentType.Gameplay); {
break; case DownloadState.LocallyAvailable:
game?.PresentScore(score, ScorePresentType.Gameplay);
break;
case DownloadState.Importing: case DownloadState.NotDownloaded:
break; state.Value = DownloadState.Importing;
default: Task.Run(importFailedScore).ContinueWith(result => Schedule(() =>
saveScore(); {
break; score = result.GetResultSafely();
state.Value = score != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded;
}));
break;
}
} }
}; };
state.BindValueChanged(state => state.BindValueChanged(state =>
{ {
switch (state.NewValue) switch (state.NewValue)
{ {
case DownloadState.LocallyAvailable: case DownloadState.LocallyAvailable:
button.State.Value = DownloadState.LocallyAvailable; button.TooltipText = @"watch replay";
button.Enabled.Value = true;
break; break;
case DownloadState.Importing: case DownloadState.Importing:
button.State.Value = DownloadState.Importing; button.TooltipText = @"importing score";
button.Enabled.Value = false;
break; break;
case DownloadState.NotDownloaded: default:
button.State.Value = DownloadState.NotDownloaded; button.TooltipText = @"save score";
button.Enabled.Value = true;
break; break;
} }
}, true); }, true);
state.BindValueChanged(updateState, true);
}
private void saveScore()
{
if (saveFailedScoreTask != null)
{
return;
}
state.Value = DownloadState.Importing;
saveFailedScoreTask = Task.Run(importFailedScore);
saveFailedScoreTask.ContinueWith(s => Schedule(() =>
{
score = s.GetAwaiter().GetResult();
state.Value = score != null ? DownloadState.LocallyAvailable : DownloadState.NotDownloaded;
}));
}
private void updateState(ValueChangedEvent<DownloadState> state)
{
switch (state.NewValue)
{
case DownloadState.LocallyAvailable:
button.TooltipText = @"Watch replay";
button.Enabled.Value = true;
break;
case DownloadState.Importing:
button.TooltipText = @"Importing score";
button.Enabled.Value = false;
break;
default:
button.TooltipText = @"Save score";
button.Enabled.Value = true;
break;
}
} }
} }
} }