diff --git a/qtcnet-client/Forms/MainForm.cs b/qtcnet-client/Forms/MainForm.cs index ebb5d31..664e69e 100644 --- a/qtcnet-client/Forms/MainForm.cs +++ b/qtcnet-client/Forms/MainForm.cs @@ -408,12 +408,55 @@ namespace qtcnet_client private void _gatewayService_OnUserForceLogout(object? sender, EventArgs e) { - throw new NotImplementedException(); + KryptonMessageBox.Show("The Server Has Logged You Out. Please Try Signing In Again.", "Uh Oh.", + KryptonMessageBoxButtons.OK, KryptonMessageBoxIcon.Error); + + _credentialService.DeleteAccessToken(); + + SuspendLayout(); + + // remove controls + Controls.Remove(MainTabControl); + Controls.Remove(CurrentProfileControl); + + // dispose of them + MainTabControl?.Dispose(); + MainTabControl = null; + CurrentProfileControl?.Dispose(); + CurrentProfileControl = null; + + // readd login control and branding + Size = LoggedOutSize; + + // add branding control + BrandingControl = new() + { + Location = new(-2, -17), + Anchor = AnchorStyles.Top | AnchorStyles.Left + }; + + // add login control + LoginControl = new() + { + Location = new(12, 233) + }; + LoginControl.OnSuccessfulLogin += LoginControl_OnSuccessfulLogin; + LoginControl.OnRegisterPressed += LoginControl_OnRegisterPressed; + + Controls.Add(BrandingControl); + Controls.Add(LoginControl); + + ResumeLayout(true); } private void _gatewayService_OnServerConfigReceived(object? sender, EventArgs e) { - throw new NotImplementedException(); + if(e is ServerConfigEventArgs _args) + { + if (InvokeRequired) + Invoke(() => Text = $"{Text} - Connected To {_args.ServerConfig.Name}"); + else Text = $"{Text} - Connected To {_args.ServerConfig.Name}"; + } } private async void _gatewayService_OnRefreshContactsListReceived(object? sender, EventArgs e) @@ -449,19 +492,39 @@ namespace qtcnet_client await SetupDirectoryUI(_currentUserDirectory.Data); } - private void _gatewayService_OnServerDisconnect(object? sender, EventArgs e) + private async void _gatewayService_OnServerDisconnect(object? sender, EventArgs e) { - throw new NotImplementedException(); + if(e is ServerConnectionClosedEventArgs _args) + { + Reconnect: // probably not the best idea to use labels but whatever + var _dialogRes = KryptonMessageBox.Show($"Connection To The Server Lost. Would You Like To Try Again?\n\nError - {_args.Error?.Message}", "Uh Oh.", + KryptonMessageBoxButtons.YesNo, KryptonMessageBoxIcon.Error); + if(_dialogRes == DialogResult.Yes) + { + // completely restart connection + await _gatewayService.StopAsync(); + await _gatewayService.StartAsync(); + + if (_gatewayService.HubConnection?.State == Microsoft.AspNetCore.SignalR.Client.HubConnectionState.Connected) + _gatewayService_OnServerReconnected(sender, e); + else + goto Reconnect; + } + } } private void _gatewayService_OnServerReconnected(object? sender, EventArgs e) { - throw new NotImplementedException(); + // reenable interactive controls + CurrentProfileControl?.Enabled = true; + MainTabControl?.Enabled = true; } private void _gatewayService_OnServerReconnecting(object? sender, EventArgs e) { - throw new NotImplementedException(); + // disable interactive controls + CurrentProfileControl?.Enabled = false; + MainTabControl?.Enabled = false; } private void _gatewayService_OnDirectMessageReceived(object? sender, EventArgs e) @@ -529,17 +592,17 @@ namespace qtcnet_client // subscribe to gateway events - //_gatewayService.OnServerReconnecting += _gatewayService_OnServerReconnecting; - //_gatewayService.OnServerReconnected += _gatewayService_OnServerReconnected; - //_gatewayService.OnServerDisconnect += _gatewayService_OnServerDisconnect; + _gatewayService.OnServerReconnecting += _gatewayService_OnServerReconnecting; + _gatewayService.OnServerReconnected += _gatewayService_OnServerReconnected; + _gatewayService.OnServerDisconnect += _gatewayService_OnServerDisconnect; //_gatewayService.OnDirectMessageReceived += _gatewayService_OnDirectMessageReceived; _gatewayService.OnRoomMessageReceived += _gatewayService_OnRoomMessageReceived; _gatewayService.OnRoomUserListReceived += _gatewayService_OnRoomUserListReceived; _gatewayService.OnRefreshUserListsReceived += _gatewayService_OnRefreshUserListReceived; - //_gatewayService.OnRefreshRoomListReceived += _gatewayService_OnRefreshRoomListReceived; + _gatewayService.OnRefreshRoomListReceived += _gatewayService_OnRefreshRoomListReceived; _gatewayService.OnRefreshContactsListReceived += _gatewayService_OnRefreshContactsListReceived; - //_gatewayService.OnServerConfigReceived += _gatewayService_OnServerConfigReceived; - //_gatewayService.OnUserForceLogout += _gatewayService_OnUserForceLogout; + _gatewayService.OnServerConfigReceived += _gatewayService_OnServerConfigReceived; + _gatewayService.OnUserForceLogout += _gatewayService_OnUserForceLogout; // start connection await _gatewayService.StartAsync();