From 7340e423f1e86a1247eaeb3a3b7d8faa474391a3 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 8 Nov 2020 15:32:40 +0900 Subject: [PATCH] Handle batch change at single method. --- .../Edit/Singers/SingerManager.cs | 68 ++++++++++++++++--- 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke/Edit/Singers/SingerManager.cs b/osu.Game.Rulesets.Karaoke/Edit/Singers/SingerManager.cs index b23ac424c..633c16010 100644 --- a/osu.Game.Rulesets.Karaoke/Edit/Singers/SingerManager.cs +++ b/osu.Game.Rulesets.Karaoke/Edit/Singers/SingerManager.cs @@ -11,6 +11,7 @@ using osu.Game.Rulesets.Karaoke.Beatmaps.Metadatas; using osu.Game.Rulesets.Karaoke.Objects; using osu.Game.Screens.Edit; +using System; using System.Collections.Generic; using System.Linq; @@ -22,6 +23,9 @@ public class SingerManager : Component public readonly BindableList Singers = new BindableList(); + [Resolved(CanBeNull = true)] + private IEditorChangeHandler changeHandler { get; set; } + [Resolved] private EditorBeatmap beatmap { get; set; } @@ -54,22 +58,66 @@ public IEnumerable CreateSingerContextMenu(List lyrics) bool allSingerInLyric(Singer singer) => lyrics.All(lyric => SingerInLyric(singer, lyric)); } - public void AddSingerToLyric(Singer singer, Lyric lyric) + public void AddSingerToLyric(Singer singer, Lyric lyric) => AddSingersToLyrics(new List { singer }, new List { lyric }); + public void AddSingersToLyrics(List singers, List lyrics) { - if (SingerInLyric(singer, lyric)) - return; + if (!(singers?.Any() ?? false)) + throw new ArgumentNullException($"{nameof(singers)} cannot be numm or empty."); + + if (!(lyrics?.Any() ?? false)) + throw new ArgumentNullException($"{nameof(lyrics)} cannot be numm or empty."); + + changeHandler?.BeginChange(); + + foreach (var lyric in lyrics) + { + foreach (var singer in singers) + { + addSingerToLyric(singer, lyric); + } + } + + changeHandler?.EndChange(); - var existSingerList = lyric.Singers?.ToList() ?? new List(); - existSingerList.Add(singer.ID); - lyric.Singers = existSingerList.ToArray(); + void addSingerToLyric(Singer singer, Lyric lyric) + { + if (SingerInLyric(singer, lyric)) + return; + + var existSingerList = lyric.Singers?.ToList() ?? new List(); + existSingerList.Add(singer.ID); + lyric.Singers = existSingerList.ToArray(); + } } - public void RemoveSingerToLyric(Singer singer, Lyric lyric) + public void RemoveSingerToLyric(Singer singer, Lyric lyric) => RemoveSingersToLyrics(new List { singer }, new List { lyric }); + public void RemoveSingersToLyrics(List singers, List lyrics) { - if (!SingerInLyric(singer, lyric)) - return; + if (!(singers?.Any() ?? false)) + throw new ArgumentNullException($"{nameof(singers)} cannot be numm or empty."); - lyric.Singers = lyric.Singers?.Where(x => x != singer.ID).ToArray(); + if (!(lyrics?.Any() ?? false)) + throw new ArgumentNullException($"{nameof(lyrics)} cannot be numm or empty."); + + changeHandler?.BeginChange(); + + foreach (var lyric in lyrics) + { + foreach (var singer in singers) + { + removeSingerToLyric(singer, lyric); + } + } + + changeHandler?.EndChange(); + + void removeSingerToLyric(Singer singer, Lyric lyric) + { + if (!SingerInLyric(singer, lyric)) + return; + + lyric.Singers = lyric.Singers?.Where(x => x != singer.ID).ToArray(); + } } public bool SingerInLyric(Singer singer, Lyric lyric)