Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix collection management dialog refreshing full display when any change occurs #30616

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,21 @@ public void TestRemoveCollectionViaButton()

AddStep("click first delete button", () =>
{
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().First(), new Vector2(5, 0));
InputManager.MoveMouseTo(dialog
.ChildrenOfType<DrawableCollectionListItem>().Single(i => i.Model.Value.Name == "1")
.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().Single(), new Vector2(5, 0));
InputManager.Click(MouseButton.Left);
});

AddAssert("dialog not displayed", () => dialogOverlay.CurrentDialog == null);
assertCollectionCount(1);
assertCollectionName(0, "2");

AddStep("click first delete button", () =>
AddStep("click second delete button", () =>
{
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().First(), new Vector2(5, 0));
InputManager.MoveMouseTo(dialog
.ChildrenOfType<DrawableCollectionListItem>().Single(i => i.Model.Value.Name == "2")
.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().Single(), new Vector2(5, 0));
InputManager.Click(MouseButton.Left);
});

Expand Down Expand Up @@ -261,6 +265,7 @@ public void TestCollectionNotRemovedWhenDialogCancelled()
}

[Test]
[Solo]
public void TestCollectionRenamedExternal()
{
BeatmapCollection first = null!;
Expand Down Expand Up @@ -310,7 +315,7 @@ public void TestCollectionRenamedOnTextChange(bool commitWithEnter)

AddStep("focus first collection", () =>
{
InputManager.MoveMouseTo(firstItem = dialog.ChildrenOfType<DrawableCollectionListItem>().First());
InputManager.MoveMouseTo(firstItem = dialog.ChildrenOfType<DrawableCollectionListItem>().Single(i => i.Model.Value.Name == "1"));
InputManager.Click(MouseButton.Left);
});

Expand All @@ -337,6 +342,6 @@ private void assertCollectionCount(int count)
=> AddUntilStep($"{count} collections shown", () => dialog.ChildrenOfType<DrawableCollectionListItem>().Count() == count + 1); // +1 for placeholder

private void assertCollectionName(int index, string name)
=> AddUntilStep($"item {index + 1} has correct name", () => dialog.ChildrenOfType<DrawableCollectionListItem>().ElementAt(index).ChildrenOfType<TextBox>().First().Text == name);
=> AddUntilStep($"item {index + 1} has correct name", () => dialog.ChildrenOfType<DrawableCollectionList>().Single().OrderedItems.ElementAt(index).ChildrenOfType<TextBox>().First().Text == name);
}
}
55 changes: 51 additions & 4 deletions osu.Game/Collections/DrawableCollectionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
Expand Down Expand Up @@ -29,7 +30,11 @@ public partial class DrawableCollectionList : OsuRearrangeableListContainer<Live

private IDisposable? realmSubscription;

protected override FillFlowContainer<RearrangeableListItem<Live<BeatmapCollection>>> CreateListFillFlowContainer() => new Flow
private Flow flow = null!;

public IEnumerable<Drawable> OrderedItems => flow.FlowingChildren;

protected override FillFlowContainer<RearrangeableListItem<Live<BeatmapCollection>>> CreateListFillFlowContainer() => flow = new Flow
{
DragActive = { BindTarget = DragActive }
};
Expand All @@ -43,8 +48,25 @@ protected override void LoadComplete()

private void collectionsChanged(IRealmCollection<BeatmapCollection> collections, ChangeSet? changes)
{
Items.Clear();
Items.AddRange(collections.AsEnumerable().Select(c => c.ToLive(realm)));
if (changes == null)
{
Items.AddRange(collections.AsEnumerable().Select(c => c.ToLive(realm)));
return;
}

foreach (int i in changes.DeletedIndices.OrderDescending())
Items.RemoveAt(i);

foreach (int i in changes.InsertedIndices)
Items.Insert(i, collections[i].ToLive(realm));

foreach (int i in changes.NewModifiedIndices)
{
var updatedItem = collections[i];

Items.RemoveAt(i);
Items.Insert(i, updatedItem.ToLive(realm));
}
}

protected override OsuRearrangeableListItem<Live<BeatmapCollection>> CreateOsuDrawable(Live<BeatmapCollection> item)
Expand Down Expand Up @@ -123,12 +145,37 @@ 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 partial 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 (string.IsNullOrEmpty(TextBox.Text))
return;

realm.Write(r => r.Add(new BeatmapCollection(TextBox.Text)));
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 && collection.Value.Name != TextBox.Current.Value)
collection.PerformWrite(c => c.Name = TextBox.Current.Value);
}
}

Expand Down
Loading