Skip to content

Commit

Permalink
Handle batch change at single method.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Nov 8, 2020
1 parent 56a2d47 commit 7340e42
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions osu.Game.Rulesets.Karaoke/Edit/Singers/SingerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,6 +23,9 @@ public class SingerManager : Component

public readonly BindableList<Singer> Singers = new BindableList<Singer>();

[Resolved(CanBeNull = true)]
private IEditorChangeHandler changeHandler { get; set; }

[Resolved]
private EditorBeatmap beatmap { get; set; }

Expand Down Expand Up @@ -54,22 +58,66 @@ public IEnumerable<MenuItem> CreateSingerContextMenu(List<Lyric> 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> { singer }, new List<Lyric> { lyric });
public void AddSingersToLyrics(List<Singer> singers, List<Lyric> 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<int>();
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<int>();
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> { singer }, new List<Lyric> { lyric });
public void RemoveSingersToLyrics(List<Singer> singers, List<Lyric> 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)
Expand Down

0 comments on commit 7340e42

Please sign in to comment.