Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source/CPDLCPlugin/Messages/ConnectedNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class ConnectedNotificationHandler(
AdscReportStore adscReportStore,
AdscTrackDisplay adscTrackDisplay,
AtsuSettingsStore atsuSettingsStore,
IJurisdictionChecker jurisdictionChecker,
IMediator mediator,
ILogger logger)
: INotificationHandler<ConnectedNotification>
{
Expand Down Expand Up @@ -73,5 +75,12 @@ public async Task Handle(ConnectedNotification notification, CancellationToken c

// Relay notification to UI
WeakReferenceMessenger.Default.Send(notification);

// If there are dialogues relevant to the controller, open the current messages window
var hasDialoguesToDisplay = dialogues.Any(d => !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d));
if (hasDialoguesToDisplay)
{
await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true), cancellationToken);
}
}
}
3 changes: 3 additions & 0 deletions source/CPDLCPlugin/Messages/FdrOwnerChanged.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace CPDLCPlugin.Messages;

public record FdrOwnerChanged(string Callsign);
10 changes: 8 additions & 2 deletions source/CPDLCPlugin/Messages/OpenCurrentMessagesWindowRequest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CommunityToolkit.Mvvm.Messaging;
using CPDLCPlugin.Configuration;
using CPDLCPlugin.ViewModels;
using CPDLCPlugin.Windows;
Expand All @@ -6,7 +7,8 @@

namespace CPDLCPlugin.Messages;

public record OpenCurrentMessagesWindowRequest : IRequest;
// When OpenedAutomatically is true, the window closes automatically when it has no messages.
public record OpenCurrentMessagesWindowRequest(bool OpenedAutomatically) : IRequest;

public class OpenCurrentMessagesWindowRequestHandler(
WindowManager windowManager,
Expand All @@ -20,6 +22,9 @@ public class OpenCurrentMessagesWindowRequestHandler(
{
public Task Handle(OpenCurrentMessagesWindowRequest request, CancellationToken cancellationToken)
{
// If the window is already open, tell it how it was opened
WeakReferenceMessenger.Default.Send(request);

windowManager.FocusOrCreateWindow(
WindowKeys.CurrentMessages,
"Current Messages",
Expand All @@ -32,7 +37,8 @@ public Task Handle(OpenCurrentMessagesWindowRequest request, CancellationToken c
guiInvoker,
errorReporter,
windowHandle,
jurisdictionChecker);
jurisdictionChecker,
closeWhenEmpty: request.OpenedAutomatically);

return new CurrentMessagesView(viewModel, windowHandle);
},
Expand Down
21 changes: 14 additions & 7 deletions source/CPDLCPlugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void AddToolbarItems()
_workQueue.Enqueue(async () =>
{
var mediator = ServiceProvider.GetRequiredService<IMediator>();
await mediator.Send(new OpenCurrentMessagesWindowRequest());
await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: false));
});
};

Expand Down Expand Up @@ -578,9 +578,15 @@ public void OnFDRUpdate(FDP2.FDR updated)

var jurisdictionChecker = ServiceProvider.GetRequiredService<IJurisdictionChecker>();

var previousRecord = jurisdictionChecker.GetOwnershipRecord(updated.Callsign);
jurisdictionChecker.RecordFdrOwner(updated.Callsign, updated.ControllerTracking?.Callsign);

var record = jurisdictionChecker.GetOwnershipRecord(updated.Callsign);

// Tell the Current Messages Window that the dialogues it must show can be different
if (previousRecord?.CurrentOwner != record?.CurrentOwner)
WeakReferenceMessenger.Default.Send(new FdrOwnerChanged(updated.Callsign));

if (record?.PreviousOwner != Network.Callsign)
return;

Expand All @@ -600,21 +606,22 @@ public void OnFDRUpdate(FDP2.FDR updated)
// Re-build the CPDLC status label items
_workQueue.Enqueue(() => RebuildCpdlcStatusLabelItems());

// If this flight has any open dialogues that we have jurisdiction over, open the current messages window
// If this flight has any dialogues that we have jurisdiction over, open the current messages window
// Ensures flights handed off to us open the window so we can see their requests
// A closed dialogue stays in the window until it is archived (e.g. a downlink that needs no response)
_workQueue.Enqueue(async () =>
{
var store = ServiceProvider.GetRequiredService<DialogueStore>();
var jurisdictionChecker = ServiceProvider.GetRequiredService<IJurisdictionChecker>();

var hasOpenDialogues = (await store.All(CancellationToken.None))
var hasDialoguesToDisplay = (await store.All(CancellationToken.None))
.Where(d => d.AircraftCallsign == updated.Callsign)
.Any(d => !d.IsClosed && !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d));
.Any(d => !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d));

if (hasOpenDialogues)
if (hasDialoguesToDisplay)
{
var mediator = ServiceProvider.GetRequiredService<IMediator>();
await mediator.Send(new OpenCurrentMessagesWindowRequest());
await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true));
}
});
}
Expand Down Expand Up @@ -716,7 +723,7 @@ public void Receive(DialogueChangedNotification dialogueChangedNotification)
}

var mediator = ServiceProvider.GetRequiredService<IMediator>();
await mediator.Send(new OpenCurrentMessagesWindowRequest());
await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true));
});
}
catch (Exception ex)
Expand Down
65 changes: 60 additions & 5 deletions source/CPDLCPlugin/ViewModels/CurrentMessagesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
namespace CPDLCPlugin.ViewModels;


public partial class CurrentMessagesViewModel : ObservableObject, IRecipient<DialogueChangedNotification>, IDisposable
public partial class CurrentMessagesViewModel :
ObservableObject,
IRecipient<DialogueChangedNotification>,
IRecipient<FdrOwnerChanged>,
IRecipient<ConnectedNotification>,
IRecipient<OpenCurrentMessagesWindowRequest>,
IDisposable
{
readonly PluginConfiguration _configuration;
readonly DialogueStore _dialogueStore;
Expand All @@ -19,6 +25,7 @@ public partial class CurrentMessagesViewModel : ObservableObject, IRecipient<Dia
readonly IErrorReporter _errorReporter;
readonly IWindowHandle _windowHandle;
readonly IJurisdictionChecker _jurisdictionChecker;
bool _closeWhenEmpty;
bool _disposed;

public CurrentMessagesViewModel(
Expand All @@ -28,7 +35,8 @@ public CurrentMessagesViewModel(
IGuiInvoker guiInvoker,
IErrorReporter errorReporter,
IWindowHandle windowHandle,
IJurisdictionChecker jurisdictionChecker)
IJurisdictionChecker jurisdictionChecker,
bool closeWhenEmpty)
{
_configuration = configuration;
_dialogueStore = dialogueStore;
Expand All @@ -37,8 +45,12 @@ public CurrentMessagesViewModel(
_errorReporter = errorReporter;
_windowHandle = windowHandle;
_jurisdictionChecker = jurisdictionChecker;
_closeWhenEmpty = closeWhenEmpty;

WeakReferenceMessenger.Default.Register(this);
WeakReferenceMessenger.Default.Register<DialogueChangedNotification>(this);
WeakReferenceMessenger.Default.Register<FdrOwnerChanged>(this);
WeakReferenceMessenger.Default.Register<ConnectedNotification>(this);
WeakReferenceMessenger.Default.Register<OpenCurrentMessagesWindowRequest>(this);

// Initial load. The window stays open when there are no messages, so that it can open empty.
_ = LoadDialoguesAsync(closeIfEmpty: false);
Expand All @@ -50,6 +62,9 @@ public void Dispose()
return;

WeakReferenceMessenger.Default.Unregister<DialogueChangedNotification>(this);
WeakReferenceMessenger.Default.Unregister<FdrOwnerChanged>(this);
WeakReferenceMessenger.Default.Unregister<ConnectedNotification>(this);
WeakReferenceMessenger.Default.Unregister<OpenCurrentMessagesWindowRequest>(this);
_disposed = true;
}

Expand Down Expand Up @@ -557,8 +572,9 @@ async Task LoadDialoguesAsync(bool closeIfEmpty)
Dialogues.Add(dialogueViewModel);
}

// Close the window if there are no messages left
if (closeIfEmpty && Dialogues.Count == 0)
// Close the window if there are no messages left.
// Only a window that opened automatically closes automatically.
if (closeIfEmpty && _closeWhenEmpty && Dialogues.Count == 0)
{
_windowHandle.Close();
}
Expand All @@ -583,6 +599,45 @@ public void Receive(DialogueChangedNotification message)
});
}

// The jurisdiction of a flight can change, so reload the dialogues that we must show
public void Receive(FdrOwnerChanged message)
{
if (_disposed)
return;

_guiInvoker.InvokeOnGUI(async _ =>
{
if (_disposed)
return;

await LoadDialoguesAsync(closeIfEmpty: true);
});
}

// The dialogues are loaded when we connect, so reload the dialogues that we must show
public void Receive(ConnectedNotification message)
{
if (_disposed)
return;

_guiInvoker.InvokeOnGUI(async _ =>
{
if (_disposed)
return;

await LoadDialoguesAsync(closeIfEmpty: true);
});
}

// The window must not close automatically after the controller opens it manually
public void Receive(OpenCurrentMessagesWindowRequest message)
{
if (_disposed || message.OpenedAutomatically)
return;

_guiInvoker.InvokeOnGUI(_ => _closeWhenEmpty = false);
}

[RelayCommand]
async Task SendStandby(CurrentMessageViewModel currentMessageViewModel)
{
Expand Down
Loading