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

Improve channel bindable logic in ChatOverlay to avoid potential nullrefs

This commit is contained in:
Salman Ahmed 2022-03-17 08:31:38 +03:00
parent 46e66e66e4
commit 7aae9bbd1b

View File

@ -73,6 +73,10 @@ namespace osu.Game.Overlays
private Container channelSelectionContainer; private Container channelSelectionContainer;
protected ChannelSelectionOverlay ChannelSelectionOverlay; protected ChannelSelectionOverlay ChannelSelectionOverlay;
private readonly IBindableList<Channel> availableChannels = new BindableList<Channel>();
private readonly IBindableList<Channel> joinedChannels = new BindableList<Channel>();
private readonly Bindable<Channel> currentChannel = new Bindable<Channel>();
public override bool Contains(Vector2 screenSpacePos) => chatContainer.ReceivePositionalInputAt(screenSpacePos) public override bool Contains(Vector2 screenSpacePos) => chatContainer.ReceivePositionalInputAt(screenSpacePos)
|| (ChannelSelectionOverlay.State.Value == Visibility.Visible && ChannelSelectionOverlay.ReceivePositionalInputAt(screenSpacePos)); || (ChannelSelectionOverlay.State.Value == Visibility.Visible && ChannelSelectionOverlay.ReceivePositionalInputAt(screenSpacePos));
@ -198,9 +202,13 @@ namespace osu.Game.Overlays
}, },
}; };
availableChannels.BindTo(channelManager.AvailableChannels);
joinedChannels.BindTo(channelManager.JoinedChannels);
currentChannel.BindTo(channelManager.CurrentChannel);
textBox.OnCommit += postMessage; textBox.OnCommit += postMessage;
ChannelTabControl.Current.ValueChanged += current => channelManager.CurrentChannel.Value = current.NewValue; ChannelTabControl.Current.ValueChanged += current => currentChannel.Value = current.NewValue;
ChannelTabControl.ChannelSelectorActive.ValueChanged += active => ChannelSelectionOverlay.State.Value = active.NewValue ? Visibility.Visible : Visibility.Hidden; ChannelTabControl.ChannelSelectorActive.ValueChanged += active => ChannelSelectionOverlay.State.Value = active.NewValue ? Visibility.Visible : Visibility.Hidden;
ChannelSelectionOverlay.State.ValueChanged += state => ChannelSelectionOverlay.State.ValueChanged += state =>
{ {
@ -238,18 +246,12 @@ namespace osu.Game.Overlays
Schedule(() => Schedule(() =>
{ {
// TODO: consider scheduling bindable callbacks to not perform when overlay is not present. // TODO: consider scheduling bindable callbacks to not perform when overlay is not present.
channelManager.JoinedChannels.BindCollectionChanged(joinedChannelsChanged, true); joinedChannels.BindCollectionChanged(joinedChannelsChanged, true);
availableChannels.BindCollectionChanged(availableChannelsChanged, true);
channelManager.AvailableChannels.CollectionChanged += availableChannelsChanged;
availableChannelsChanged(null, null);
currentChannel = channelManager.CurrentChannel.GetBoundCopy();
currentChannel.BindValueChanged(currentChannelChanged, true); currentChannel.BindValueChanged(currentChannelChanged, true);
}); });
} }
private Bindable<Channel> currentChannel;
private void currentChannelChanged(ValueChangedEvent<Channel> e) private void currentChannelChanged(ValueChangedEvent<Channel> e)
{ {
if (e.NewValue == null) if (e.NewValue == null)
@ -318,7 +320,7 @@ namespace osu.Game.Overlays
if (!channel.Joined.Value) if (!channel.Joined.Value)
channel = channelManager.JoinChannel(channel); channel = channelManager.JoinChannel(channel);
channelManager.CurrentChannel.Value = channel; currentChannel.Value = channel;
} }
channel.HighlightedMessage.Value = message; channel.HighlightedMessage.Value = message;
@ -407,7 +409,7 @@ namespace osu.Game.Overlays
return true; return true;
case PlatformAction.DocumentClose: case PlatformAction.DocumentClose:
channelManager.LeaveChannel(channelManager.CurrentChannel.Value); channelManager.LeaveChannel(currentChannel.Value);
return true; return true;
} }
@ -487,19 +489,7 @@ namespace osu.Game.Overlays
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args) private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
{ {
ChannelSelectionOverlay.UpdateAvailableChannels(channelManager.AvailableChannels); ChannelSelectionOverlay.UpdateAvailableChannels(availableChannels);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (channelManager != null)
{
channelManager.CurrentChannel.ValueChanged -= currentChannelChanged;
channelManager.JoinedChannels.CollectionChanged -= joinedChannelsChanged;
channelManager.AvailableChannels.CollectionChanged -= availableChannelsChanged;
}
} }
private void postMessage(TextBox textBox, bool newText) private void postMessage(TextBox textBox, bool newText)