1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 06:33:20 +08:00

Update BindableList usages

This commit is contained in:
Dean Herbert 2020-09-01 11:56:23 +09:00
parent c8aa197e5b
commit d45a1521a1
5 changed files with 53 additions and 37 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using osuTK;
using osuTK.Graphics;
@ -218,14 +219,13 @@ namespace osu.Game.Overlays
Schedule(() =>
{
// TODO: consider scheduling bindable callbacks to not perform when overlay is not present.
channelManager.JoinedChannels.ItemsAdded += onChannelAddedToJoinedChannels;
channelManager.JoinedChannels.ItemsRemoved += onChannelRemovedFromJoinedChannels;
channelManager.JoinedChannels.CollectionChanged += joinedChannelsChanged;
foreach (Channel channel in channelManager.JoinedChannels)
ChannelTabControl.AddChannel(channel);
channelManager.AvailableChannels.ItemsAdded += availableChannelsChanged;
channelManager.AvailableChannels.ItemsRemoved += availableChannelsChanged;
ChannelSelectionOverlay.UpdateAvailableChannels(channelManager.AvailableChannels);
channelManager.AvailableChannels.CollectionChanged += availableChannelsChanged;
availableChannelsChanged(null, null);
currentChannel = channelManager.CurrentChannel.GetBoundCopy();
currentChannel.BindValueChanged(currentChannelChanged, true);
@ -384,34 +384,41 @@ namespace osu.Game.Overlays
base.PopOut();
}
private void onChannelAddedToJoinedChannels(IEnumerable<Channel> channels)
private void joinedChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
foreach (Channel channel in channels)
ChannelTabControl.AddChannel(channel);
}
private void onChannelRemovedFromJoinedChannels(IEnumerable<Channel> channels)
{
foreach (Channel channel in channels)
switch (args.Action)
{
ChannelTabControl.RemoveChannel(channel);
case NotifyCollectionChangedAction.Add:
foreach (Channel channel in args.NewItems.Cast<Channel>())
ChannelTabControl.AddChannel(channel);
break;
var loaded = loadedChannels.Find(c => c.Channel == channel);
case NotifyCollectionChangedAction.Remove:
foreach (Channel channel in args.OldItems.Cast<Channel>())
{
ChannelTabControl.RemoveChannel(channel);
if (loaded != null)
{
loadedChannels.Remove(loaded);
var loaded = loadedChannels.Find(c => c.Channel == channel);
// Because the container is only cleared in the async load callback of a new channel, it is forcefully cleared
// to ensure that the previous channel doesn't get updated after it's disposed
currentChannelContainer.Remove(loaded);
loaded.Dispose();
}
if (loaded != null)
{
loadedChannels.Remove(loaded);
// Because the container is only cleared in the async load callback of a new channel, it is forcefully cleared
// to ensure that the previous channel doesn't get updated after it's disposed
currentChannelContainer.Remove(loaded);
loaded.Dispose();
}
}
break;
}
}
private void availableChannelsChanged(IEnumerable<Channel> channels)
=> ChannelSelectionOverlay.UpdateAvailableChannels(channelManager.AvailableChannels);
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
ChannelSelectionOverlay.UpdateAvailableChannels(channelManager.AvailableChannels);
}
protected override void Dispose(bool isDisposing)
{
@ -420,10 +427,8 @@ namespace osu.Game.Overlays
if (channelManager != null)
{
channelManager.CurrentChannel.ValueChanged -= currentChannelChanged;
channelManager.JoinedChannels.ItemsAdded -= onChannelAddedToJoinedChannels;
channelManager.JoinedChannels.ItemsRemoved -= onChannelRemovedFromJoinedChannels;
channelManager.AvailableChannels.ItemsAdded -= availableChannelsChanged;
channelManager.AvailableChannels.ItemsRemoved -= availableChannelsChanged;
channelManager.JoinedChannels.CollectionChanged -= joinedChannelsChanged;
channelManager.AvailableChannels.CollectionChanged -= availableChannelsChanged;
}
}

View File

@ -163,8 +163,7 @@ namespace osu.Game.Overlays.Settings.Sections.Graphics
scalingSettings.ForEach(s => s.TransferValueOnCommit = mode.NewValue == ScalingMode.Everything);
}, true);
windowModes.ItemsAdded += _ => windowModesChanged();
windowModes.ItemsRemoved += _ => windowModesChanged();
windowModes.CollectionChanged += (sender, args) => windowModesChanged();
windowModesChanged();
}

View File

@ -112,8 +112,7 @@ namespace osu.Game.Screens.Edit.Timing
};
controlPoints = group.ControlPoints.GetBoundCopy();
controlPoints.ItemsAdded += _ => createChildren();
controlPoints.ItemsRemoved += _ => createChildren();
controlPoints.CollectionChanged += (_, __) => createChildren();
createChildren();
}

View File

@ -124,8 +124,7 @@ namespace osu.Game.Screens.Edit.Timing
selectedGroup.BindValueChanged(selected => { deleteButton.Enabled.Value = selected.NewValue != null; }, true);
controlGroups = Beatmap.Value.Beatmap.ControlPointInfo.Groups.GetBoundCopy();
controlGroups.ItemsAdded += _ => createContent();
controlGroups.ItemsRemoved += _ => createContent();
controlGroups.CollectionChanged += (sender, args) => createContent();
createContent();
}

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
@ -53,8 +54,7 @@ namespace osu.Game.Screens.Multi.Lounge.Components
protected override void LoadComplete()
{
rooms.ItemsAdded += addRooms;
rooms.ItemsRemoved += removeRooms;
rooms.CollectionChanged += roomsChanged;
roomManager.RoomsUpdated += updateSorting;
rooms.BindTo(roomManager.Rooms);
@ -82,6 +82,20 @@ namespace osu.Game.Screens.Multi.Lounge.Components
});
}
private void roomsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
addRooms(args.NewItems.Cast<Room>());
break;
case NotifyCollectionChangedAction.Remove:
removeRooms(args.OldItems.Cast<Room>());
break;
}
}
private void addRooms(IEnumerable<Room> rooms)
{
foreach (var room in rooms)