-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatWindowManager.cs
66 lines (60 loc) · 2.1 KB
/
ChatWindowManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using DropZone.Protocol;
using DropZone.Protocol.Chat;
using DropZone.Utils;
using DropZone.ViewModels;
using DropZone.Views;
using System;
using System.Collections.Generic;
using System.Windows;
namespace DropZone
{
internal static class ChatWindowManager
{
private static readonly Dictionary<ChatViewModel, ChatWindow> _showingWindows = new Dictionary<ChatViewModel, ChatWindow>();
private static ChatWindow GetShowingWindow(Station.Neighbor withNeighbor)
{
foreach (var pair in _showingWindows)
{
if (pair.Key.Neighbor.Address == withNeighbor.Address)
{
return pair.Value;
}
}
return null;
}
public static void Show(Station.Neighbor withNeighbor, ChatClient chatClient, bool active = false)
{
ThreadUtils.RunOnUiAndWait(() =>
{
var chatWindow = GetShowingWindow(withNeighbor);
if (chatWindow == null)
{
chatWindow = new ChatWindow { Owner = Application.Current.MainWindow };
var vm = chatClient != null
? new ChatViewModel(chatClient, withNeighbor)
: new ChatViewModel(withNeighbor);
chatWindow.DataContext = vm;
chatWindow.Closed += ChatWindow_Closed;
chatWindow.Show();
_showingWindows.Add(vm, chatWindow);
}
else if (chatClient != null && chatWindow.DataContext is ChatViewModel vm)
{
vm.SetChatClient(chatClient);
}
if (active)
{
chatWindow.Activate();
}
});
}
private static void ChatWindow_Closed(object sender, EventArgs e)
{
if (sender is ChatWindow chatWindow)
{
var vm = chatWindow.DataContext as ChatViewModel;
_showingWindows.Remove(vm);
}
}
}
}