mirror of
https://github.com/ppy/osu.git
synced 2024-11-06 06:57:39 +08:00
Replace ObservableCollection with BindableCollection
This commit is contained in:
parent
cd2737156f
commit
a14b6ac9df
@ -31,6 +31,9 @@ namespace osu.Game.Online.Chat
|
||||
@"#lobby"
|
||||
};
|
||||
|
||||
private readonly BindableCollection<Channel> availableChannels = new BindableCollection<Channel>();
|
||||
private readonly BindableCollection<Channel> joinedChannels = new BindableCollection<Channel>();
|
||||
|
||||
/// <summary>
|
||||
/// The currently opened channel
|
||||
/// </summary>
|
||||
@ -39,12 +42,12 @@ namespace osu.Game.Online.Chat
|
||||
/// <summary>
|
||||
/// The Channels the player has joined
|
||||
/// </summary>
|
||||
public ObservableCollection<Channel> JoinedChannels { get; } = new ObservableCollection<Channel>(); //todo: should be publicly readonly
|
||||
public IBindableCollection<Channel> JoinedChannels => joinedChannels;
|
||||
|
||||
/// <summary>
|
||||
/// The channels available for the player to join
|
||||
/// </summary>
|
||||
public ObservableCollection<Channel> AvailableChannels { get; } = new ObservableCollection<Channel>(); //todo: should be publicly readonly
|
||||
public IBindableCollection<Channel> AvailableChannels => availableChannels;
|
||||
|
||||
private IAPIProvider api;
|
||||
private ScheduledDelegate fetchMessagesScheduleder;
|
||||
@ -344,9 +347,10 @@ namespace osu.Game.Online.Chat
|
||||
{
|
||||
if (channel == null) return;
|
||||
|
||||
if (channel == CurrentChannel.Value) CurrentChannel.Value = null;
|
||||
if (channel == CurrentChannel.Value)
|
||||
CurrentChannel.Value = null;
|
||||
|
||||
JoinedChannels.Remove(channel);
|
||||
joinedChannels.Remove(channel);
|
||||
|
||||
if (channel.Joined.Value)
|
||||
{
|
||||
|
@ -20,6 +20,7 @@ using osu.Game.Online.Chat;
|
||||
using osu.Game.Overlays.Chat;
|
||||
using osu.Game.Overlays.Chat.Selection;
|
||||
using osu.Game.Overlays.Chat.Tabs;
|
||||
using System.Linq;
|
||||
|
||||
namespace osu.Game.Overlays
|
||||
{
|
||||
@ -181,28 +182,6 @@ namespace osu.Game.Overlays
|
||||
channelSelection.OnRequestLeave = channel => channelManager.LeaveChannel(channel);
|
||||
}
|
||||
|
||||
private void joinedChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
switch (args.Action)
|
||||
{
|
||||
case NotifyCollectionChangedAction.Add:
|
||||
foreach (Channel newChannel in args.NewItems)
|
||||
{
|
||||
channelTabControl.AddChannel(newChannel);
|
||||
}
|
||||
|
||||
break;
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
foreach (Channel removedChannel in args.OldItems)
|
||||
{
|
||||
channelTabControl.RemoveChannel(removedChannel);
|
||||
loadedChannels.Remove(loadedChannels.Find(c => c.Channel == removedChannel));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void currentChannelChanged(Channel channel)
|
||||
{
|
||||
if (channel == null)
|
||||
@ -322,19 +301,35 @@ namespace osu.Game.Overlays
|
||||
|
||||
this.channelManager = channelManager;
|
||||
channelManager.CurrentChannel.ValueChanged += currentChannelChanged;
|
||||
channelManager.JoinedChannels.CollectionChanged += joinedChannelsChanged;
|
||||
channelManager.AvailableChannels.CollectionChanged += availableChannelsChanged;
|
||||
channelManager.JoinedChannels.ItemsAdded += onChannelAddedToJoinedChannels;
|
||||
channelManager.JoinedChannels.ItemsRemoved += onChannelRemovedFromJoinedChannels;
|
||||
channelManager.AvailableChannels.ItemsAdded += availableChannelsChanged;
|
||||
channelManager.AvailableChannels.ItemsRemoved += availableChannelsChanged;
|
||||
|
||||
//for the case that channelmanager was faster at fetching the channels than our attachment to CollectionChanged.
|
||||
channelSelection.UpdateAvailableChannels(channelManager.AvailableChannels);
|
||||
joinedChannelsChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, channelManager.JoinedChannels));
|
||||
foreach (Channel channel in channelManager.JoinedChannels)
|
||||
channelTabControl.AddChannel(channel);
|
||||
}
|
||||
|
||||
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
private void onChannelAddedToJoinedChannels(IEnumerable<Channel> channels)
|
||||
{
|
||||
channelSelection.UpdateAvailableChannels(channelManager.AvailableChannels);
|
||||
foreach (Channel channel in channels)
|
||||
channelTabControl.AddChannel(channel);
|
||||
}
|
||||
|
||||
private void onChannelRemovedFromJoinedChannels(IEnumerable<Channel> channels)
|
||||
{
|
||||
foreach (Channel channel in channels)
|
||||
{
|
||||
channelTabControl.RemoveChannel(channel);
|
||||
loadedChannels.Remove(loadedChannels.Find(c => c.Channel == channel));
|
||||
}
|
||||
}
|
||||
|
||||
private void availableChannelsChanged(IEnumerable<Channel> channels)
|
||||
=> channelSelection.UpdateAvailableChannels(channelManager.AvailableChannels);
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
@ -342,8 +337,10 @@ namespace osu.Game.Overlays
|
||||
if (channelManager != null)
|
||||
{
|
||||
channelManager.CurrentChannel.ValueChanged -= currentChannelChanged;
|
||||
channelManager.JoinedChannels.CollectionChanged -= joinedChannelsChanged;
|
||||
channelManager.AvailableChannels.CollectionChanged -= availableChannelsChanged;
|
||||
channelManager.JoinedChannels.ItemsAdded -= onChannelAddedToJoinedChannels;
|
||||
channelManager.JoinedChannels.ItemsRemoved -= onChannelRemovedFromJoinedChannels;
|
||||
channelManager.AvailableChannels.ItemsAdded -= availableChannelsChanged;
|
||||
channelManager.AvailableChannels.ItemsRemoved -= availableChannelsChanged;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user