LANCommander/LANCommander.Launcher/UI/Components/KeepAliveContainer.razor

77 lines
No EOL
2.4 KiB
Text

@implements IDisposable
@inject KeepAliveService KeepAliveService
@inject IMessageService MessageService
@code {
string ConnectionMessageKey = Guid.NewGuid().ToString();
protected override void OnInitialized()
{
KeepAliveService.ConnectionSevered += OnConnectionSevered;
KeepAliveService.ConnectionRetryNext += OnConnectionRetryNext;
KeepAliveService.ConnectionLostPermanently += OnConnectionLostPermanently;
KeepAliveService.ConnectionEstablished += OnConnectionEstablished;
}
void OnConnectionSevered(object? sender, EventArgs e)
{
var messageConfig = new MessageConfig
{
Key = ConnectionMessageKey,
Type = MessageType.Loading,
Content = "Lost connection, retrying...",
Duration = 0,
};
MessageService.Open(messageConfig);
}
void OnConnectionRetryNext(object? sender, EventArgs e)
{
// show same lost-connection-message by replacing the message by its key
var retry = KeepAliveService.GetRetryCount();
var messageConfig = new MessageConfig
{
Key = ConnectionMessageKey,
Type = MessageType.Loading,
Content = $"Lost connection, retrying ({retry.current}/{retry.total}) ...",
Duration = 0,
};
MessageService.Open(messageConfig);
}
void OnConnectionLostPermanently(object? sender, EventArgs e)
{
var messageConfig = new MessageConfig
{
Key = ConnectionMessageKey,
Type = MessageType.Error,
Content = "Server unavailable, enabling offline mode",
Duration = 2.5,
};
MessageService.Open(messageConfig);
}
void OnConnectionEstablished(object? sender, EventArgs e)
{
var messageConfig = new MessageConfig
{
Key = ConnectionMessageKey,
Type = MessageType.Success,
Content = "Connection established!",
Duration = 2.5,
};
MessageService.Open(messageConfig);
}
public void Dispose()
{
KeepAliveService.ConnectionSevered -= OnConnectionSevered;
KeepAliveService.ConnectionRetryNext -= OnConnectionRetryNext;
KeepAliveService.ConnectionLostPermanently -= OnConnectionLostPermanently;
KeepAliveService.ConnectionEstablished -= OnConnectionEstablished;
}
}