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

Avoid writing changes to realm when collection name wasn't actually changed

This commit is contained in:
Dean Herbert 2024-11-14 14:12:31 +09:00
parent 695f156f5f
commit ebe9a9f083
No known key found for this signature in database
2 changed files with 36 additions and 16 deletions

View File

@ -140,12 +140,35 @@ namespace osu.Game.Collections
var previous = PlaceholderItem; var previous = PlaceholderItem;
placeholderContainer.Clear(false); placeholderContainer.Clear(false);
placeholderContainer.Add(PlaceholderItem = new DrawableCollectionListItem(new BeatmapCollection().ToLiveUnmanaged(), false)); placeholderContainer.Add(PlaceholderItem = new NewCollectionEntryItem());
return previous; return previous;
} }
} }
private class NewCollectionEntryItem : DrawableCollectionListItem
{
[Resolved]
private RealmAccess realm { get; set; } = null!;
public NewCollectionEntryItem()
: base(new BeatmapCollection().ToLiveUnmanaged(), false)
{
}
protected override void LoadComplete()
{
base.LoadComplete();
TextBox.OnCommit += (sender, newText) =>
{
if (newText && !string.IsNullOrEmpty(TextBox.Current.Value))
realm.Write(r => r.Add(new BeatmapCollection(TextBox.Current.Value)));
TextBox.Text = string.Empty;
};
}
}
/// <summary> /// <summary>
/// The flow of <see cref="DrawableCollectionListItem"/>. Disables layout easing unless a drag is in progress. /// The flow of <see cref="DrawableCollectionListItem"/>. Disables layout easing unless a drag is in progress.
/// </summary> /// </summary>

View File

@ -28,6 +28,10 @@ namespace osu.Game.Collections
private const float item_height = 35; private const float item_height = 35;
private const float button_width = item_height * 0.75f; private const float button_width = item_height * 0.75f;
protected TextBox TextBox => content.TextBox;
private ItemContent content = null!;
/// <summary> /// <summary>
/// Creates a new <see cref="DrawableCollectionListItem"/>. /// Creates a new <see cref="DrawableCollectionListItem"/>.
/// </summary> /// </summary>
@ -48,7 +52,7 @@ namespace osu.Game.Collections
CornerRadius = item_height / 2; CornerRadius = item_height / 2;
} }
protected override Drawable CreateContent() => new ItemContent(Model); protected override Drawable CreateContent() => content = new ItemContent(Model);
/// <summary> /// <summary>
/// The main content of the <see cref="DrawableCollectionListItem"/>. /// The main content of the <see cref="DrawableCollectionListItem"/>.
@ -57,10 +61,7 @@ namespace osu.Game.Collections
{ {
private readonly Live<BeatmapCollection> collection; private readonly Live<BeatmapCollection> collection;
private ItemTextBox textBox = null!; public ItemTextBox TextBox { get; private set; } = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
public ItemContent(Live<BeatmapCollection> collection) public ItemContent(Live<BeatmapCollection> collection)
{ {
@ -80,7 +81,7 @@ namespace osu.Game.Collections
{ {
Anchor = Anchor.CentreRight, Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight, Origin = Anchor.CentreRight,
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v) IsTextBoxHovered = v => TextBox.ReceivePositionalInputAt(v)
} }
: Empty(), : Empty(),
new Container new Container
@ -89,7 +90,7 @@ namespace osu.Game.Collections
Padding = new MarginPadding { Right = collection.IsManaged ? button_width : 0 }, Padding = new MarginPadding { Right = collection.IsManaged ? button_width : 0 },
Children = new Drawable[] Children = new Drawable[]
{ {
textBox = new ItemTextBox TextBox = new ItemTextBox
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Size = Vector2.One, Size = Vector2.One,
@ -107,18 +108,14 @@ namespace osu.Game.Collections
base.LoadComplete(); base.LoadComplete();
// Bind late, as the collection name may change externally while still loading. // Bind late, as the collection name may change externally while still loading.
textBox.Current.Value = collection.PerformRead(c => c.IsValid ? c.Name : string.Empty); TextBox.Current.Value = collection.PerformRead(c => c.IsValid ? c.Name : string.Empty);
textBox.OnCommit += onCommit; TextBox.OnCommit += onCommit;
} }
private void onCommit(TextBox sender, bool newText) private void onCommit(TextBox sender, bool newText)
{ {
if (collection.IsManaged) if (collection.IsManaged && newText)
collection.PerformWrite(c => c.Name = textBox.Current.Value); collection.PerformWrite(c => c.Name = TextBox.Current.Value);
else if (!string.IsNullOrEmpty(textBox.Current.Value))
realm.Write(r => r.Add(new BeatmapCollection(textBox.Current.Value)));
textBox.Text = string.Empty;
} }
} }