From 070704cba719a124bd48c6b5a9133ddaae157e92 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 8 Sep 2020 17:59:38 +0900 Subject: [PATCH] Asyncify initial load --- .../Collections/BeatmapCollectionManager.cs | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/osu.Game/Collections/BeatmapCollectionManager.cs b/osu.Game/Collections/BeatmapCollectionManager.cs index 6a5ed6bbbc..e4fc4c377b 100644 --- a/osu.Game/Collections/BeatmapCollectionManager.cs +++ b/osu.Game/Collections/BeatmapCollectionManager.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Collections.Specialized; using System.IO; using System.Linq; using System.Threading; @@ -15,6 +16,7 @@ using osu.Framework.Logging; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.IO.Legacy; +using osu.Game.Overlays.Notifications; namespace osu.Game.Collections { @@ -46,17 +48,46 @@ namespace osu.Game.Collections [BackgroundDependencyLoader] private void load() + { + Collections.CollectionChanged += collectionsChanged; + loadDatabase(); + } + + private void collectionsChanged(object sender, NotifyCollectionChangedEventArgs e) + { + switch (e.Action) + { + case NotifyCollectionChangedAction.Add: + foreach (var c in e.NewItems.Cast()) + c.Changed += backgroundSave; + break; + + case NotifyCollectionChangedAction.Remove: + foreach (var c in e.OldItems.Cast()) + c.Changed -= backgroundSave; + break; + + case NotifyCollectionChangedAction.Replace: + foreach (var c in e.OldItems.Cast()) + c.Changed -= backgroundSave; + + foreach (var c in e.NewItems.Cast()) + c.Changed += backgroundSave; + break; + } + + backgroundSave(); + } + + private void loadDatabase() => Task.Run(async () => { if (storage.Exists(database_name)) { using (var stream = storage.GetStream(database_name)) - importCollections(readCollections(stream)); + await import(stream); } + }); - foreach (var c in Collections) - c.Changed += backgroundSave; - Collections.CollectionChanged += (_, __) => backgroundSave(); - } /// /// Set an endpoint for notifications to be posted to. ///