Skip to content

Commit

Permalink
Replace ClientAppState with stores.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 7, 2023
1 parent 13e3a6a commit f076089
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 200 deletions.
10 changes: 4 additions & 6 deletions Server/Components/Devices/ChatCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Remotely.Server.Hubs;
using Remotely.Server.Models.Messages;
using Remotely.Server.Services;
using Remotely.Server.Services.Stores;
using Remotely.Shared.Enums;
using Remotely.Shared.ViewModels;
using System;
Expand All @@ -22,10 +23,7 @@ public partial class ChatCard : AuthComponentBase, IDisposable
public required ChatSession Session { get; set; }

[Inject]
private IClientAppState AppState { get; init; } = null!;

[Inject]
private IChatSessionCache ChatSessionCache { get; init; } = null!;
private IChatSessionStore ChatSessionStore { get; init; } = null!;

[Inject]
private ICircuitConnection CircuitConnection { get; init; } = null!;
Expand Down Expand Up @@ -63,7 +61,7 @@ private async Task HandleChatMessageReceived(ChatReceivedMessage message)
return;
}

if (!ChatSessionCache.TryGetSession(message.DeviceId, out var session))
if (!ChatSessionStore.TryGetSession(message.DeviceId, out var session))
{
return;
}
Expand Down Expand Up @@ -97,7 +95,7 @@ private async Task HandleChatMessageReceived(ChatReceivedMessage message)

private async Task CloseChatCard()
{
_ = ChatSessionCache.TryRemove($"{Session.DeviceId}", out _);
_ = ChatSessionStore.TryRemove($"{Session.DeviceId}", out _);
var message = new ChatSessionsChangedMessage();
await Messenger.Send(message, CircuitConnection.ConnectionId);
}
Expand Down
6 changes: 3 additions & 3 deletions Server/Components/Devices/ChatFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.AspNetCore.Components.Web;
using Remotely.Server.Hubs;
using Remotely.Server.Models.Messages;
using Remotely.Server.Services;
using Remotely.Server.Services.Stores;
using Remotely.Shared.Entities;
using Remotely.Shared.Enums;
using Remotely.Shared.ViewModels;
Expand All @@ -19,10 +19,10 @@ public partial class ChatFrame : AuthComponentBase, IAsyncDisposable
private ICollection<ChatSession> _chatSessions = Array.Empty<ChatSession>();

[Inject]
private IClientAppState AppState { get; init; } = null!;
private ISelectedCardsStore CardStore { get; init; } = null!;

[Inject]
private IChatSessionCache ChatCache { get; init; } = null!;
private IChatSessionStore ChatCache { get; init; } = null!;

[Inject]
private ICircuitConnection CircuitConnection { get; init; } = null!;
Expand Down
10 changes: 5 additions & 5 deletions Server/Components/Devices/DeviceCard.razor
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
@attribute [Authorize]
@inherits AuthComponentBase

<div @ref="_card" class="card border-secondary my-3 mr-3 device-card @_theme @GetCardStateClass(Device)"
<div @ref="_card" class="card border-secondary my-3 mr-3 device-card @_theme @GetCardStateClass()"
@onclick="ExpandCard"
@onclick:stopPropagation
@oncontextmenu="ContextMenuOpening"
@oncontextmenu:preventDefault="GetCardState() == DeviceCardState.Normal"
@oncontextmenu:stopPropagation="GetCardState() == DeviceCardState.Normal">
@oncontextmenu:preventDefault="_state == DeviceCardState.Normal"
@oncontextmenu:stopPropagation="_state == DeviceCardState.Normal">

<div class="card-header" @onclick="HandleHeaderClick"
@onclick:stopPropagation="GetCardState() == DeviceCardState.Expanded"
@onclick:preventDefault="GetCardState() == DeviceCardState.Expanded">
@onclick:stopPropagation="_state == DeviceCardState.Expanded"
@onclick:preventDefault="_state == DeviceCardState.Expanded">
<div>
@if (Device.IsOnline)
{
Expand Down
99 changes: 47 additions & 52 deletions Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Remotely.Server.Hubs;
using Remotely.Server.Models.Messages;
using Remotely.Server.Services;
using Remotely.Server.Services.Stores;
using Remotely.Shared.Entities;
using Remotely.Shared.Enums;
using Remotely.Shared.Utilities;
Expand All @@ -24,6 +25,7 @@ public partial class DeviceCard : AuthComponentBase, IDisposable
private ElementReference _card;
private Version _currentVersion = new();
private Theme _theme;
private DeviceCardState _state;
private DeviceGroup[] _deviceGroups = Array.Empty<DeviceGroup>();

[Parameter]
Expand All @@ -34,7 +36,7 @@ public partial class DeviceCard : AuthComponentBase, IDisposable


[Inject]
private IClientAppState AppState { get; init; } = null!;
private ISelectedCardsStore SelectedCards { get; init; } = null!;

[Inject]
private IThemeProvider ThemeProvider { get; init; } = null!;
Expand All @@ -46,15 +48,15 @@ public partial class DeviceCard : AuthComponentBase, IDisposable
private IDataService DataService { get; init; } = null!;

[Inject]
private IChatSessionCache ChatCache { get; init; } = null!;
private IChatSessionStore ChatCache { get; init; } = null!;

private bool IsExpanded => GetCardState() == DeviceCardState.Expanded;
private bool IsExpanded => _state == DeviceCardState.Expanded;

private bool IsOutdated =>
Version.TryParse(Device.AgentVersion, out var result) &&
result < _currentVersion;

private bool IsSelected => AppState.DevicesFrameSelectedDevices.Contains(Device.ID);
private bool IsSelected => SelectedCards.SelectedDevices.Contains(Device.ID);

[Inject]
private IJsInterop JsInterop { get; init; } = null!;
Expand All @@ -76,7 +78,6 @@ public partial class DeviceCard : AuthComponentBase, IDisposable

public void Dispose()
{
AppState.PropertyChanged -= AppState_PropertyChanged;
Messenger.Unregister<DeviceStateChangedMessage, string>(this, CircuitConnection.ConnectionId);
GC.SuppressFinalize(this);
}
Expand All @@ -88,13 +89,41 @@ protected override async Task OnInitializedAsync()
_theme = await ThemeProvider.GetEffectiveTheme();
_currentVersion = UpgradeService.GetCurrentVersion();
_deviceGroups = DataService.GetDeviceGroups(UserName);
AppState.PropertyChanged += AppState_PropertyChanged;

await Messenger.Register<DeviceCardStateChangedMessage, string>(
this,
CircuitConnection.ConnectionId,
HandleDeviceCardStateChanged);

await Messenger.Register<DeviceStateChangedMessage, string>(
this,
CircuitConnection.ConnectionId,
HandleDeviceStateChanged);
}

private async Task HandleDeviceCardStateChanged(DeviceCardStateChangedMessage message)
{
if (message.DeviceId == Device.ID)
{
if (message.State == _state)
{
return;
}
_state = message.State;
await InvokeAsync(StateHasChanged);
}
else
{
if (_state == DeviceCardState.Normal)
{
return;
}
_state = DeviceCardState.Normal;
await InvokeAsync(StateHasChanged);
return;
}
}

private async Task HandleDeviceStateChanged(DeviceStateChangedMessage message)
{
if (message.Device.ID != Device.ID)
Expand Down Expand Up @@ -133,60 +162,34 @@ private async Task HandleDeviceStateChanged(DeviceStateChangedMessage message)
await InvokeAsync(StateHasChanged);
}

private void AppState_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(AppState.DevicesFrameFocusedCardState) ||
e.PropertyName == nameof(AppState.DevicesFrameFocusedDevice) ||
e.PropertyName == nameof(AppState.DevicesFrameSelectedDevices))
{
InvokeAsync(StateHasChanged);
}
}

private void ContextMenuOpening(MouseEventArgs args)
{
if (GetCardState() == DeviceCardState.Normal)
if (_state == DeviceCardState.Normal)
{
JsInterop.OpenWindow($"/device-details/{Device.ID}", "_blank");
}
}

private async Task ExpandCard(MouseEventArgs args)
{
if (AppState.DevicesFrameFocusedDevice == Device.ID)
if (_state == DeviceCardState.Expanded)
{
if (AppState.DevicesFrameFocusedCardState == DeviceCardState.Normal)
{
AppState.DevicesFrameFocusedCardState = DeviceCardState.Expanded;
}
return;
}

AppState.DevicesFrameFocusedDevice = Device.ID;
AppState.DevicesFrameFocusedCardState = DeviceCardState.Expanded;
await Messenger.Send(
new DeviceCardStateChangedMessage(Device.ID, DeviceCardState.Expanded),
CircuitConnection.ConnectionId);

JsInterop.ScrollToElement(_card);

await CircuitConnection.TriggerHeartbeat(Device.ID);
}

private DeviceCardState GetCardState()
{
if (AppState.DevicesFrameFocusedDevice == Device.ID)
{
return AppState.DevicesFrameFocusedCardState;
}

return DeviceCardState.Normal;
}

private string GetCardStateClass(Device device)
private string GetCardStateClass()
{
if (AppState.DevicesFrameFocusedDevice == device.ID)
{
return AppState.DevicesFrameFocusedCardState.ToString().ToLower();
}

return string.Empty;
return $"{_state}".ToLower();
}

private string GetProgressMessage(string key)
Expand All @@ -203,7 +206,7 @@ private void HandleHeaderClick()
{
if (IsExpanded)
{
SetCardStateNormal();
_state = DeviceCardState.Normal;
}
}
private async Task HandleValidSubmit()
Expand Down Expand Up @@ -265,12 +268,6 @@ private void OpenDeviceDetails()
JsInterop.OpenWindow($"/device-details/{Device.ID}", "_blank");
}

private void SetCardStateNormal()
{
AppState.DevicesFrameFocusedDevice = null;
AppState.DevicesFrameFocusedCardState = DeviceCardState.Normal;
}

private void ShowAllDisks()
{
var disksString = JsonSerializer.Serialize(Device.Drives, JsonSerializerHelper.IndentedOptions);
Expand Down Expand Up @@ -338,13 +335,12 @@ private void ToggleIsSelected(ChangeEventArgs args)

if (isSelected)
{
AppState.DevicesFrameSelectedDevices.Add(Device.ID);
SelectedCards.Add(Device.ID);
}
else
{
AppState.DevicesFrameSelectedDevices.Remove(Device.ID);
SelectedCards.Remove(Device.ID);
}
AppState.InvokePropertyChanged(nameof(AppState.DevicesFrameSelectedDevices));
}

private async Task UninstallAgent()
Expand All @@ -353,8 +349,7 @@ private async Task UninstallAgent()
if (result)
{
await CircuitConnection.UninstallAgents(new[] { Device.ID });
AppState.DevicesFrameFocusedDevice = null;
AppState.DevicesFrameFocusedCardState = DeviceCardState.Normal;
_state = DeviceCardState.Normal;
await ParentFrame.Refresh();
}
}
Expand Down
Loading

0 comments on commit f076089

Please sign in to comment.