1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 17:32:54 +08:00

Maintain download progress between switching result views.

- Check for existing download requests on creating DirectPanel
- Actually remove downloaded beatmap from results
This commit is contained in:
naoey 2017-09-08 23:55:20 +05:30
parent 31a507372a
commit 00306b6e38
No known key found for this signature in database
GPG Key ID: 2CCE71F6713C9069
3 changed files with 72 additions and 54 deletions

View File

@ -68,7 +68,7 @@ namespace osu.Game.Beatmaps
private readonly APIAccess api;
private readonly Dictionary<BeatmapSetInfo, DownloadBeatmapSetRequest> downloadsMap;
private readonly Dictionary<int?, DownloadBeatmapSetRequest> downloadsMap;
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
private BeatmapIPCChannel ipc;
@ -100,7 +100,7 @@ namespace osu.Game.Beatmaps
if (importHost != null)
ipc = new BeatmapIPCChannel(importHost, this);
downloadsMap = new Dictionary<BeatmapSetInfo, DownloadBeatmapSetRequest>();
downloadsMap = new Dictionary<int?, DownloadBeatmapSetRequest>();
}
/// <summary>
@ -191,9 +191,10 @@ namespace osu.Game.Beatmaps
/// Download a beatmap
/// </summary>
/// <param name="beatmapSetInfo">The beatmap to be downloaded</param>
/// <returns>The new <see cref="DownloadBeatmapSetRequest"/>, or null if a download already exists for the same beatmap.</returns>
public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo)
{
if (api == null || downloadsMap.ContainsKey(beatmapSetInfo)) return null;
if (api == null || downloadsMap.ContainsKey(beatmapSetInfo.OnlineBeatmapSetID)) return null;
ProgressNotification downloadNotification = new ProgressNotification
{
@ -216,24 +217,26 @@ namespace osu.Game.Beatmaps
using (var archive = new OszArchiveReader(stream))
Import(archive);
downloadsMap.Remove(beatmapSetInfo);
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID);
};
request.Failure += data =>
{
downloadNotification.State = ProgressNotificationState.Completed;
Logger.Error(data, "Failed to get beatmap download information");
downloadsMap.Remove(beatmapSetInfo);
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID);
};
downloadNotification.CancelRequested += () =>
{
Logger.Log("Cancel requested");
downloadsMap.Remove(beatmapSetInfo);
downloadsMap[beatmapSetInfo.OnlineBeatmapSetID].Cancel();
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID);
downloadNotification.State = ProgressNotificationState.Cancelled;
return true;
};
downloadsMap[beatmapSetInfo] = request;
downloadsMap[beatmapSetInfo.OnlineBeatmapSetID] = request;
PostNotification?.Invoke(downloadNotification);
// don't run in the main api queue as this is a long-running task.
@ -243,11 +246,11 @@ namespace osu.Game.Beatmaps
}
/// <summary>
/// Check if a beatmap is already downloading.
/// Get an existing download request if it exists.
/// </summary>
/// <param name="beatmap">The <see cref="BeatmapSetInfo"/>to check against.</param>
/// <returns>true if a download request already exists, false if it doesn't.</returns>
public bool IsDownloading(BeatmapSetInfo beatmap) => downloadsMap.ContainsKey(beatmap);
/// <param name="beatmap">The <see cref="BeatmapSetInfo"/> whose download request is wanted.</param>
/// <returns>The <see cref="DownloadBeatmapSetRequest"/> object if it exists, or null.</returns>
public DownloadBeatmapSetRequest GetExistingDownload(BeatmapSetInfo beatmap) => downloadsMap.GetOrDefault(beatmap.OnlineBeatmapSetID);
/// <summary>
/// Delete a beatmap from the manager.

View File

@ -22,6 +22,7 @@ using osu.Game.Online.API;
using osu.Framework.Logging;
using osu.Game.Beatmaps.IO;
using osu.Game.Overlays.Notifications;
using osu.Game.Online.API.Requests;
namespace osu.Game.Overlays.Direct
{
@ -40,6 +41,44 @@ namespace osu.Game.Overlays.Direct
private BeatmapManager beatmaps;
private NotificationOverlay notifications;
private DownloadBeatmapSetRequest downloadRequest;
protected DownloadBeatmapSetRequest DownloadRequest
{
get { return downloadRequest; }
set
{
if (value == null) return;
downloadRequest = value;
progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);
progressBar.Current.Value = 0;
downloadRequest.Failure += e =>
{
progressBar.Current.Value = 0;
progressBar.FadeOut(500);
Logger.Error(e, "Failed to get beatmap download information");
};
downloadRequest.Progress += (current, total) =>
{
float progress = (float)current / total;
progressBar.Current.Value = progress;
};
downloadRequest.Success += data =>
{
progressBar.Current.Value = 1;
progressBar.FadeOut(500);
};
}
}
protected override Container<Drawable> Content => content;
protected DirectPanel(BeatmapSetInfo setInfo)
@ -97,6 +136,8 @@ namespace osu.Game.Overlays.Direct
},
}
});
DownloadRequest = beatmaps.GetExistingDownload(SetInfo);
}
protected override bool OnHover(InputState state)
@ -128,7 +169,7 @@ namespace osu.Game.Overlays.Direct
}
// we already have an active download running.
if (beatmaps.IsDownloading(SetInfo))
if ((DownloadRequest = beatmaps.Download(SetInfo)) == null)
{
content.MoveToX(-5, 50, Easing.OutSine).Then()
.MoveToX(5, 100, Easing.InOutSine).Then()
@ -136,34 +177,6 @@ namespace osu.Game.Overlays.Direct
.MoveToX(0, 50, Easing.InSine).Then();
return;
}
var downloadRequest = beatmaps.Download(SetInfo);
progressBar.FadeIn(400, Easing.OutQuint);
progressBar.ResizeHeightTo(4, 400, Easing.OutQuint);
progressBar.Current.Value = 0;
downloadRequest.Failure += e =>
{
progressBar.Current.Value = 0;
progressBar.FadeOut(500);
Logger.Error(e, "Failed to get beatmap download information");
};
downloadRequest.Progress += (current, total) =>
{
float progress = (float)current / total;
progressBar.Current.Value = progress;
};
downloadRequest.Success += data =>
{
progressBar.Current.Value = 1;
progressBar.FadeOut(500);
};
}
protected override void LoadComplete()

View File

@ -47,9 +47,22 @@ namespace osu.Game.Overlays
set
{
if (beatmapSets?.Equals(value) ?? false) return;
beatmapSets = value;
recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value);
if (beatmapSets == null) return;
var artists = new List<string>();
var songs = new List<string>();
var tags = new List<string>();
foreach (var s in beatmapSets)
{
artists.Add(s.Metadata.Artist);
songs.Add(s.Metadata.Title);
tags.AddRange(s.Metadata.Tags.Split(' '));
}
ResultAmounts = new ResultCounts(distinctCount(artists), distinctCount(songs), distinctCount(tags));
}
}
@ -159,6 +172,7 @@ namespace osu.Game.Overlays
{
// if a new map was imported, we should remove it from search results (download completed etc.)
panels?.FirstOrDefault(p => p.SetInfo.OnlineBeatmapSetID == set.OnlineBeatmapSetID)?.FadeOut(400).Expire();
BeatmapSets = BeatmapSets.Where(b => b.OnlineBeatmapSetID != set.OnlineBeatmapSetID);
}
private void updateResultCounts()
@ -244,19 +258,7 @@ namespace osu.Game.Overlays
Select(response => response.ToBeatmapSet(rulesets)).
Where(b => (beatmaps.QueryBeatmapSet(q => q.OnlineBeatmapSetID == b.OnlineBeatmapSetID) == null));
if (BeatmapSets == null) return;
var artists = new List<string>();
var songs = new List<string>();
var tags = new List<string>();
foreach (var s in BeatmapSets)
{
artists.Add(s.Metadata.Artist);
songs.Add(s.Metadata.Title);
tags.AddRange(s.Metadata.Tags.Split(' '));
}
ResultAmounts = new ResultCounts(distinctCount(artists), distinctCount(songs), distinctCount(tags));
recreatePanels(Filter.DisplayStyleControl.DisplayStyle.Value);
};
api.Queue(getSetsRequest);