Skip to content

Commit

Permalink
Avoid writing changes to realm when collection name wasn't actually c…
Browse files Browse the repository at this point in the history
…hanged
  • Loading branch information
peppy committed Nov 14, 2024
1 parent bc06109 commit 17b76f7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
25 changes: 24 additions & 1 deletion osu.Game/Collections/DrawableCollectionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,35 @@ public DrawableCollectionListItem ReplacePlaceholder()
var previous = PlaceholderItem;

placeholderContainer.Clear(false);
placeholderContainer.Add(PlaceholderItem = new DrawableCollectionListItem(new BeatmapCollection().ToLiveUnmanaged(), false));
placeholderContainer.Add(PlaceholderItem = new NewCollectionEntryItem());

return previous;
}
}

private class NewCollectionEntryItem : DrawableCollectionListItem

Check warning on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Build only (Android)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check warning on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Build only (Android)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Code Quality

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Code Quality

This type, or a nested type, is a candidate for dependency injection and should be partial

Check warning on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Build only (iOS)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check warning on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Build only (iOS)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Windows, windows-latest, SingleThread)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Windows, windows-latest, SingleThread)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Windows, windows-latest, MultiThreaded)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Windows, windows-latest, MultiThreaded)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Linux, ubuntu-latest, SingleThread)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Linux, ubuntu-latest, SingleThread)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Linux, ubuntu-latest, MultiThreaded)

This type, or a nested type, is a candidate for dependency injection and should be partial

Check failure on line 149 in osu.Game/Collections/DrawableCollectionList.cs

View workflow job for this annotation

GitHub Actions / Test (Linux, ubuntu-latest, MultiThreaded)

This type, or a nested type, is a candidate for dependency injection and should be partial
{
[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>
/// The flow of <see cref="DrawableCollectionListItem"/>. Disables layout easing unless a drag is in progress.
/// </summary>
Expand Down
27 changes: 12 additions & 15 deletions osu.Game/Collections/DrawableCollectionListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public partial class DrawableCollectionListItem : OsuRearrangeableListItem<Live<
private const float item_height = 35;
private const float button_width = item_height * 0.75f;

protected TextBox TextBox => content.TextBox;

private ItemContent content = null!;

/// <summary>
/// Creates a new <see cref="DrawableCollectionListItem"/>.
/// </summary>
Expand All @@ -48,7 +52,7 @@ public DrawableCollectionListItem(Live<BeatmapCollection> item, bool isCreated)
CornerRadius = item_height / 2;
}

protected override Drawable CreateContent() => new ItemContent(Model);
protected override Drawable CreateContent() => content = new ItemContent(Model);

/// <summary>
/// The main content of the <see cref="DrawableCollectionListItem"/>.
Expand All @@ -57,10 +61,7 @@ private partial class ItemContent : CompositeDrawable
{
private readonly Live<BeatmapCollection> collection;

private ItemTextBox textBox = null!;

[Resolved]
private RealmAccess realm { get; set; } = null!;
public ItemTextBox TextBox { get; private set; } = null!;

public ItemContent(Live<BeatmapCollection> collection)
{
Expand All @@ -80,7 +81,7 @@ private void load()
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v)
IsTextBoxHovered = v => TextBox.ReceivePositionalInputAt(v)
}
: Empty(),
new Container
Expand All @@ -89,7 +90,7 @@ private void load()
Padding = new MarginPadding { Right = collection.IsManaged ? button_width : 0 },
Children = new Drawable[]
{
textBox = new ItemTextBox
TextBox = new ItemTextBox
{
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
Expand All @@ -107,18 +108,14 @@ protected override void LoadComplete()
base.LoadComplete();

// 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.OnCommit += onCommit;
TextBox.Current.Value = collection.PerformRead(c => c.IsValid ? c.Name : string.Empty);
TextBox.OnCommit += onCommit;
}

private void onCommit(TextBox sender, bool newText)
{
if (collection.IsManaged)
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;
if (collection.IsManaged && newText)
collection.PerformWrite(c => c.Name = TextBox.Current.Value);
}
}

Expand Down

0 comments on commit 17b76f7

Please sign in to comment.