Skip to content

Commit

Permalink
Refactor chat service.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Dec 19, 2020
1 parent 2eb5702 commit d639773
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 129 deletions.
19 changes: 19 additions & 0 deletions Desktop.Core/Interfaces/IChatUiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Remotely.Desktop.Core.Interfaces
{
public interface IChatUiService
{
event EventHandler ChatWindowClosed;

void ShowChatWindow(string organizationName, StreamWriter writer);
void ReceiveChat(ChatMessage chatMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Win.ViewModels;
using Remotely.Desktop.Win.Views;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace Remotely.Desktop.Win.Services
namespace Remotely.Desktop.Core.Services
{
public class ChatHostServiceWin : IChatHostService
public class ChatHostService : IChatHostService
{
private ChatWindowViewModel ChatViewModel { get; set; }
private readonly IChatUiService _chatUiService;

public ChatHostService(IChatUiService chatUiService)
{
_chatUiService = chatUiService;
}

private NamedPipeServerStream NamedPipeStream { get; set; }
private StreamReader Reader { get; set; }
private StreamWriter Writer { get; set; }
Expand All @@ -37,20 +43,14 @@ public async Task StartChat(string requesterID, string organizationName)
Environment.Exit(0);
}

App.Current.Dispatcher.Invoke(() =>
{
var chatWindow = new ChatWindow();
chatWindow.Closing += ChatWindow_Closing;
ChatViewModel = chatWindow.DataContext as ChatWindowViewModel;
ChatViewModel.PipeStreamWriter = Writer;
ChatViewModel.OrganizationName = organizationName;
chatWindow.Show();
});
_chatUiService.ChatWindowClosed += OnChatWindowClosed;

_chatUiService.ShowChatWindow(organizationName, Writer);

_ = Task.Run(ReadFromStream);
}

private void ChatWindow_Closing(object sender, EventArgs e)
private void OnChatWindowClosed(object sender, EventArgs e)
{
try
{
Expand All @@ -70,17 +70,8 @@ private async Task ReadFromStream()
if (!string.IsNullOrWhiteSpace(messageJson))
{
var chatMessage = JsonSerializer.Deserialize<ChatMessage>(messageJson);
App.Current.Dispatcher.Invoke(() =>
{
if (chatMessage.Disconnected)
{
MessageBox.Show("Your partner has disconnected.", "Partner Disconnected", MessageBoxButton.OK, MessageBoxImage.Information);
App.Current.Shutdown();
return;
}
ChatViewModel.SenderName = chatMessage.SenderName;
ChatViewModel.ChatMessages.Add(chatMessage);
});
_chatUiService.ReceiveChat(chatMessage);

}
}
catch (Exception ex)
Expand Down
3 changes: 2 additions & 1 deletion Desktop.Linux/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ private static void BuildServices()
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddSingleton<IChatHostService, ChatHostServiceLinux>();
serviceCollection.AddSingleton<IChatHostService, ChatHostService>();
serviceCollection.AddSingleton<IChatUiService, ChatUiServiceLinux>();
serviceCollection.AddTransient<IScreenCapturer, ScreenCapturerLinux>();
serviceCollection.AddTransient<Viewer>();
serviceCollection.AddScoped<IFileTransferService, FileTransferServiceLinux>();
Expand Down
100 changes: 0 additions & 100 deletions Desktop.Linux/Services/ChatHostServiceLinux.cs

This file was deleted.

62 changes: 62 additions & 0 deletions Desktop.Linux/Services/ChatUiServiceLinux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Avalonia.Controls;
using Avalonia.Threading;
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Linux.Controls;
using Remotely.Desktop.Linux.ViewModels;
using Remotely.Desktop.Linux.Views;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Remotely.Desktop.Linux.Services
{
public class ChatUiServiceLinux : IChatUiService
{
private ChatWindowViewModel ChatViewModel { get; set; }

public event EventHandler ChatWindowClosed;

public void ReceiveChat(ChatMessage chatMessage)
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
if (chatMessage.Disconnected)
{
await MessageBox.Show("The partner has disconnected.", "Partner Disconnected", MessageBoxType.OK);
Environment.Exit(0);
return;
}

if (ChatViewModel != null)
{
ChatViewModel.SenderName = chatMessage.SenderName;
ChatViewModel.ChatMessages.Add(chatMessage);
}
});
}

public void ShowChatWindow(string organizationName, StreamWriter writer)
{
Dispatcher.UIThread.Post(() =>
{
var chatWindow = new ChatWindow();
chatWindow.Closing += ChatWindow_Closing; ;
ChatViewModel = chatWindow.DataContext as ChatWindowViewModel;
ChatViewModel.PipeStreamWriter = writer;
ChatViewModel.OrganizationName = organizationName;
App.Current.Run(chatWindow);
});
}

private void ChatWindow_Closing(object sender, CancelEventArgs e)
{
ChatWindowClosed?.Invoke(this, null);
}
}
}
3 changes: 2 additions & 1 deletion Desktop.Win/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ private static void BuildServices()
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddSingleton<IChatHostService, ChatHostServiceWin>();
serviceCollection.AddSingleton<IChatHostService, ChatHostService>();
serviceCollection.AddSingleton<IChatUiService, ChatUiServiceWin>();
serviceCollection.AddTransient<IScreenCapturer, ScreenCapturerWin>();
serviceCollection.AddTransient<Viewer>();
serviceCollection.AddScoped<IWebRtcSessionFactory, WebRtcSessionFactory>();
Expand Down
59 changes: 59 additions & 0 deletions Desktop.Win/Services/ChatUiServiceWin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Win.ViewModels;
using Remotely.Desktop.Win.Views;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Remotely.Desktop.Win.Services
{
public class ChatUiServiceWin : IChatUiService
{
private ChatWindowViewModel ChatViewModel { get; set; }

public event EventHandler ChatWindowClosed;

public void ReceiveChat(ChatMessage chatMessage)
{
App.Current.Dispatcher.Invoke(() =>
{
if (chatMessage.Disconnected)
{
MessageBox.Show("Your partner has disconnected.", "Partner Disconnected", MessageBoxButton.OK, MessageBoxImage.Information);
App.Current.Shutdown();
return;
}

if (ChatViewModel != null)
{
ChatViewModel.SenderName = chatMessage.SenderName;
ChatViewModel.ChatMessages.Add(chatMessage);
}
});
}

public void ShowChatWindow(string organizationName, StreamWriter writer)
{
App.Current.Dispatcher.Invoke(() =>
{
var chatWindow = new ChatWindow();
chatWindow.Closing += ChatWindow_Closing;
ChatViewModel = chatWindow.DataContext as ChatWindowViewModel;
ChatViewModel.PipeStreamWriter = writer;
ChatViewModel.OrganizationName = organizationName;
chatWindow.Show();
});
}

private void ChatWindow_Closing(object sender, CancelEventArgs e)
{
ChatWindowClosed?.Invoke(this, null);
}
}
}

0 comments on commit d639773

Please sign in to comment.