Skip to content

Commit

Permalink
Bug fixes and channel moving
Browse files Browse the repository at this point in the history
  • Loading branch information
WamWooWam committed Sep 22, 2019
1 parent 76784dc commit 6ccd940
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Libraries/DSharpPlus/DSharpPlus/DiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,8 @@ internal async Task OnVoiceStateUpdateEventAsync(JObject raw)
mbr.IsDeafened = vstateNew.IsServerDeafened;
}

vstateOld?.Channel?.InvokePropertyChanged(nameof(vstateNew.Channel.ConnectedUsers));
vstateNew?.Channel?.InvokePropertyChanged(nameof(vstateNew.Channel.ConnectedUsers));

var ea = new VoiceStateUpdateEventArgs(this)
{
Expand Down
11 changes: 11 additions & 0 deletions Unicord.Universal.Voice/ServiceBackgroundTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation
}
}
break;
case VoiceServiceRequest::GuildMoveRequest:
{
if (voiceClient != nullptr && activeCall != nullptr) {
activeCall.ContactName(unbox_value<hstring>(data.Lookup(L"contact_name")));
voiceClientOptions.ChannelId(unbox_value<uint64_t>(data.Lookup(L"channel_id")));

// already connected, so raise the event again
RaiseEvent(VoiceServiceEvent::Connected, event_values);
}
}
break;
case VoiceServiceRequest::StateRequest:
if (voiceClient != nullptr) {
values.Insert(L"state", box_value((uint32_t)VoiceServiceState::Connected));
Expand Down
1 change: 1 addition & 0 deletions Unicord.Universal.Voice/VoiceServiceRequest.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Unicord.Universal.Voice.Background

StateRequest,
GuildConnectRequest,
GuildMoveRequest,
DisconnectRequest,
MuteRequest,
DeafenRequest,
Expand Down
24 changes: 21 additions & 3 deletions Unicord.Universal/Pages/DiscordPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,29 @@ internal async void Navigate(DiscordChannel channel, NavigationTransitionInfo in

if (channel.Type == ChannelType.Voice)
{
var model = DataContext as DiscordPageModel;
var voiceModel = model.VoiceModel;

try
{
var voice = new VoiceConnectionModel(channel);
(DataContext as DiscordPageModel).VoiceModel = voice;
await voice.ConnectAsync();
if (voiceModel == null)
{
voiceModel = new VoiceConnectionModel(channel);
model.VoiceModel = voiceModel;
await voiceModel.ConnectAsync();
}
else if (voiceModel.Channel.Guild == channel.Guild)
{
await voiceModel.MoveAsync(channel);
}
else
{
await voiceModel.DisconnectAsync();

voiceModel = new VoiceConnectionModel(channel);
model.VoiceModel = voiceModel;
await voiceModel.ConnectAsync();
}
}
catch (Exception ex)
{
Expand Down
31 changes: 30 additions & 1 deletion Unicord.Universal/Voice/VoiceConnectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class VoiceConnectionModel : PropertyChangedBase, IDisposable
private AppServiceConnection _appServiceConnection;
private VoipCallCoordinator _voipCallCoordinator;
private ResourceLoader _strings;

private TaskCompletionSource<VoiceStateUpdateEventArgs> _voiceStateUpdateCompletion;
private TaskCompletionSource<VoiceServerUpdateEventArgs> _voiceServerUpdateCompletion;

Expand All @@ -41,7 +42,7 @@ public class VoiceConnectionModel : PropertyChangedBase, IDisposable
private string _connectionStatus;

public string ConnectionStatus { get => _connectionStatus; set => OnPropertySet(ref _connectionStatus, value); }
public DiscordChannel Channel { get; }
public DiscordChannel Channel { get; private set; }

public bool Muted
{
Expand Down Expand Up @@ -148,6 +149,32 @@ public async Task UpdatePreferredAudioDevicesAsync(string audioRender, string au
await SendRequestAsync(set);
}

public async Task MoveAsync(DiscordChannel newChannel)
{
if (Channel.Guild != newChannel.Guild)
{
throw new InvalidOperationException("Can only move inside a guild");
}

Channel = newChannel;
ConnectionStatus = _strings.GetString("ConnectionState3");

App.Discord.VoiceStateUpdated += OnVoiceStateUpdated;
SendVoiceStateUpdate(Channel.Id);

var vstu = await _voiceStateUpdateCompletion.Task.ConfigureAwait(false);
ConnectionStatus = string.Format(_strings.GetString("ConnectionState4Format"), Channel.Name);

var connectionRequest = new ValueSet()
{
["req"] = (uint)VoiceServiceRequest.GuildMoveRequest,
["channel_id"] = Channel.Id,
["contact_name"] = Channel.Guild != null ? $"{Channel.Name} - {Channel.Guild.Name}" : DMNameConverter.Instance.Convert(Channel, null, null, null),
};

await SendRequestAsync(connectionRequest);
}

public async Task ConnectAsync()
{
if (ApiInformation.IsTypePresent("Windows.ApplicationModel.Calls.VoipPhoneCallResourceReservationStatus"))
Expand Down Expand Up @@ -311,6 +338,7 @@ private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequ
ConnectionStatus = _strings.GetString("DisconnectedState");
Disconnected?.Invoke(this, null);
SendVoiceStateUpdate(null);
Dispose();
break;
case VoiceServiceEvent.Muted:
await PlayCueAsync(_muted ? "mute" : "unmute");
Expand Down Expand Up @@ -395,6 +423,7 @@ public void Dispose()
{
_appServiceConnection?.Dispose();
_mediaPlayer.MediaPlayer?.Dispose();
App.Discord.VoiceStateUpdated -= OnVoiceStateUpdated;
}
}

Expand Down

0 comments on commit 6ccd940

Please sign in to comment.