1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Add import/deletion progress notifications

This commit is contained in:
smoogipoo 2020-09-08 17:58:56 +09:00
parent 4737add00b
commit 06328e0000
3 changed files with 55 additions and 4 deletions

View File

@ -57,6 +57,10 @@ namespace osu.Game.Collections
c.Changed += backgroundSave; c.Changed += backgroundSave;
Collections.CollectionChanged += (_, __) => backgroundSave(); Collections.CollectionChanged += (_, __) => backgroundSave();
} }
/// <summary>
/// Set an endpoint for notifications to be posted to.
/// </summary>
public Action<Notification> PostNotification { protected get; set; }
/// <summary> /// <summary>
/// Set a storage with access to an osu-stable install for import purposes. /// Set a storage with access to an osu-stable install for import purposes.
@ -93,9 +97,25 @@ namespace osu.Game.Collections
}); });
} }
public async Task Import(Stream stream) => await Task.Run(async () => public async Task Import(Stream stream)
{ {
var collection = readCollections(stream); var notification = new ProgressNotification
{
State = ProgressNotificationState.Active,
Text = "Collections import is initialising..."
};
PostNotification?.Invoke(notification);
await import(stream, notification);
}
private async Task import(Stream stream, ProgressNotification notification = null) => await Task.Run(async () =>
{
if (notification != null)
notification.Progress = 0;
var collection = readCollections(stream, notification);
bool importCompleted = false; bool importCompleted = false;
Schedule(() => Schedule(() =>
@ -106,6 +126,12 @@ namespace osu.Game.Collections
while (!IsDisposed && !importCompleted) while (!IsDisposed && !importCompleted)
await Task.Delay(10); await Task.Delay(10);
if (notification != null)
{
notification.CompletionText = $"Imported {collection.Count} collections";
notification.State = ProgressNotificationState.Completed;
}
}); });
private void importCollections(List<BeatmapCollection> newCollections) private void importCollections(List<BeatmapCollection> newCollections)
@ -124,8 +150,14 @@ namespace osu.Game.Collections
} }
} }
private List<BeatmapCollection> readCollections(Stream stream) private List<BeatmapCollection> readCollections(Stream stream, ProgressNotification notification = null)
{ {
if (notification != null)
{
notification.Text = "Reading collections...";
notification.Progress = 0;
}
var result = new List<BeatmapCollection>(); var result = new List<BeatmapCollection>();
try try
@ -139,11 +171,17 @@ namespace osu.Game.Collections
for (int i = 0; i < collectionCount; i++) for (int i = 0; i < collectionCount; i++)
{ {
if (notification?.CancellationToken.IsCancellationRequested == true)
return result;
var collection = new BeatmapCollection { Name = { Value = sr.ReadString() } }; var collection = new BeatmapCollection { Name = { Value = sr.ReadString() } };
int mapCount = sr.ReadInt32(); int mapCount = sr.ReadInt32();
for (int j = 0; j < mapCount; j++) for (int j = 0; j < mapCount; j++)
{ {
if (notification?.CancellationToken.IsCancellationRequested == true)
return result;
string checksum = sr.ReadString(); string checksum = sr.ReadString();
var beatmap = beatmaps.QueryBeatmap(b => b.MD5Hash == checksum); var beatmap = beatmaps.QueryBeatmap(b => b.MD5Hash == checksum);
@ -151,6 +189,12 @@ namespace osu.Game.Collections
collection.Beatmaps.Add(beatmap); collection.Beatmaps.Add(beatmap);
} }
if (notification != null)
{
notification.Text = $"Imported {i + 1} of {collectionCount} collections";
notification.Progress = (float)(i + 1) / collectionCount;
}
result.Add(collection); result.Add(collection);
} }
} }
@ -163,6 +207,12 @@ namespace osu.Game.Collections
return result; return result;
} }
public void DeleteAll()
{
Collections.Clear();
PostNotification?.Invoke(new SimpleNotification { Text = "Deleted all collections!" });
}
private readonly object saveLock = new object(); private readonly object saveLock = new object();
private int lastSave; private int lastSave;
private int saveFailures; private int saveFailures;

View File

@ -537,6 +537,7 @@ namespace osu.Game
ScoreManager.GetStableStorage = GetStorageForStableInstall; ScoreManager.GetStableStorage = GetStorageForStableInstall;
ScoreManager.PresentImport = items => PresentScore(items.First()); ScoreManager.PresentImport = items => PresentScore(items.First());
CollectionManager.PostNotification = n => notifications.Post(n);
CollectionManager.GetStableStorage = GetStorageForStableInstall; CollectionManager.GetStableStorage = GetStorageForStableInstall;
Container logoContainer; Container logoContainer;

View File

@ -126,7 +126,7 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
Text = "Delete ALL collections", Text = "Delete ALL collections",
Action = () => Action = () =>
{ {
dialogOverlay?.Push(new DeleteAllBeatmapsDialog(() => collectionManager.Collections.Clear())); dialogOverlay?.Push(new DeleteAllBeatmapsDialog(collectionManager.DeleteAll));
} }
}); });