Skip to content

Commit

Permalink
Test new BrowserService
Browse files Browse the repository at this point in the history
  • Loading branch information
Volker Hänsel committed Oct 2, 2023
1 parent 5a9c588 commit 22dc24f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@
using System.Diagnostics;
using tuya_mqtt.net.Data;

namespace tuya_mqtt.net.Services
namespace tuya_mqtt.net.Helper
{
public class BoundingClientRect
public class BrowserService
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Top { get; set; }
public double Right { get; set; }
public double Bottom { get; set; }
public double Left { get; set; }
}
public interface IBrowserService
{

public Task InitAsync(IJSRuntime js);
public event EventHandler<WindowSize>? Resize;
public Task<BoundingClientRect> GetElementClientRectAsync(ElementReference element);
public Task ScrollClassIntoView(string classSearchString, int index);
public int BrowserWidth { get; }
public int BrowserHeight { get; }
public bool IsInitialized { get; }
}
public class BrowserService : IBrowserService
{

public class BoundingClientRect
{
public double X { get; set; }
public double Y { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Top { get; set; }
public double Right { get; set; }
public double Bottom { get; set; }
public double Left { get; set; }
}
// ReSharper disable once InconsistentNaming
// ReSharper disable once RedundantDefaultMemberInitializer
private IJSRuntime? JS = null;
Expand All @@ -38,7 +26,7 @@ public class BrowserService : IBrowserService
public event EventHandler<WindowSize>? Resize;
private int _browserWidth;
private int _browserHeight;
private readonly SemaphoreSlim _initLocker = new SemaphoreSlim(1,1);

public BrowserService(ILogger<BrowserService> logger)
{
_logger = logger;
Expand All @@ -48,13 +36,12 @@ public BrowserService(ILogger<BrowserService> logger)

public async Task InitAsync(IJSRuntime js)
{
await _initLocker.WaitAsync().ConfigureAwait(false);
try
{
// enforce single invocation
if (JS == null)
{
this.JS = js;
JS = js;
try
{
_jsModule = await JS.InvokeAsync<IJSObjectReference>("import", "/lib/BrowserService.js");
Expand Down Expand Up @@ -88,16 +75,15 @@ await _jsModule.InvokeAsync<string>("resizeListener", DotNetObjectReference.Crea
}
finally
{

_initLocker.Release();


}

}

private async Task GetViewPortSize()
{
//await _initLocker.WaitAsync().ConfigureAwait(false);

try
{
if (_jsModule == null)
Expand All @@ -120,7 +106,7 @@ private async Task GetViewPortSize()
// ReSharper disable once RedundantEmptyFinallyBlock
finally
{
// _initLocker.Release();


}
}
Expand All @@ -133,7 +119,7 @@ public void SetBrowserDimensions(int jsBrowserWidth, int jsBrowserHeight)
_browserWidth = jsBrowserWidth;
_browserHeight = jsBrowserHeight;
_logger.LogDebug($"BrowserService window new size ({_browserWidth},{_browserHeight})");
this.Resize?.Invoke(this, new WindowSize() { Width = _browserWidth, Height = _browserHeight });
Resize?.Invoke(this, new WindowSize() { Width = _browserWidth, Height = _browserHeight });
}
catch (Exception e)
{
Expand All @@ -147,7 +133,7 @@ public int BrowserWidth
{
if (_browserWidth == 0)
{
_logger.LogError( "browserWidth == 0");
_logger.LogError("browserWidth == 0");
throw new InvalidOperationException("InitAsync seems not called or not finished yet.");
}
return _browserWidth;
Expand All @@ -167,13 +153,13 @@ public int BrowserHeight
}
}

public async Task<Services.BoundingClientRect> GetElementClientRectAsync(ElementReference element)
public async Task<BoundingClientRect> GetElementClientRectAsync(ElementReference element)
{
if (_jsModule == null)
{
throw new InvalidOperationException("BrowserService is not initialized. run Init() before.");
}
var result = await _jsModule.InvokeAsync<Services.BoundingClientRect>("MyGetBoundingClientRect", element);
var result = await _jsModule.InvokeAsync<BoundingClientRect>("MyGetBoundingClientRect", element);
_logger.LogDebug($"GetBoundingClientRect ({result.Width},{result.Height})");
return result;
}
Expand All @@ -185,9 +171,9 @@ public async Task ScrollToEnd(ElementReference element)
_logger.LogError("ScrollToEnd - no JSModule");
throw new InvalidOperationException("BrowserService is not initialized. run Init() before.");
}

await _jsModule.InvokeVoidAsync("scrollToEnd", element);

}

public async Task ScrollClassIntoView(string classSearchString, int index)
Expand Down
5 changes: 4 additions & 1 deletion tuya_mqtt.net/Pages/Logger.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@page "/logger"
@using tuya_mqtt.net.Data;
@using tuya_mqtt.net.Services;
@using tuya_mqtt.net.Helper;
@inject IJSRuntime JS
@inject ILogger<Logger> LocalLogger
@inject ILoggerFactory LoggerFactory
Expand Down Expand Up @@ -49,7 +50,9 @@
{
try
{
#if DEBUG
LocalLogger.LogInformation($"OnResize {_myElementReference}", _myElementReference);
#endif
var rect = await Browser.GetElementClientRectAsync(_myElementReference);

CalculateNewLogComponentHeight(newSize, rect);
Expand All @@ -66,7 +69,7 @@
}
}

private void CalculateNewLogComponentHeight(WindowSize viewportSize, BoundingClientRect logComponentClientRect)
private void CalculateNewLogComponentHeight(WindowSize viewportSize, BrowserService.BoundingClientRect logComponentClientRect)
{
#warning TODO - find out where this offset of ~66 come from
LogWindowHeight = viewportSize.Height - (int)logComponentClientRect.Top - 66;
Expand Down
14 changes: 12 additions & 2 deletions tuya_mqtt.net/Pages/TuyaDevices.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
@using com.clusterrr.TuyaNet;
@using tuya_mqtt.net.Data
@using tuya_mqtt.net.Services;
@using tuya_mqtt.net.Helper;
@inject TuyaCommunicatorService TuyaService;
@inject TuyaConnectedDeviceService TuyaConnectedDevices;
@inject NavigationManager NavManager
@inject BrowserService Browser;
@inject IJSRuntime JS;
@inject ISnackbar Snackbar;
@inject ILogger<TuyaDevices> Logger;
@inject ILogger<BrowserService> BrowserServiceLogger;


<PageTitle>Tuya Devices</PageTitle>
Expand Down Expand Up @@ -74,6 +75,7 @@
</MudExpansionPanels>

@code {
private BrowserService Browser;
private bool _isInWait = false;
private MudExpansionPanel? _expansionList = null;
private MudExpansionPanel? _expansionAdd = null;
Expand Down Expand Up @@ -115,13 +117,21 @@

protected override async Task OnInitializedAsync()
{
Browser = new BrowserService(BrowserServiceLogger);
Snackbar.Clear();
_tuyaScannedDevices = TuyaService.GetTuyaScanDevices();
_tuyaMonitoredDevices = TuyaConnectedDevices.GetMonitoredDevices();
_tuyaMonitoredData = TuyaConnectedDevices.GetMonitoredData();
TuyaConnectedDevices.OnDataUpdate += OnTuyaDataUpdate;
TuyaService.OnTuyaScannerUpdate += OnTuyaScannerUpdate;
await Browser.InitAsync(JS);
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await Browser.InitAsync(JS);
}
}

public void GoSettings()
Expand Down
1 change: 0 additions & 1 deletion tuya_mqtt.net/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static void Main(string[] args)
builder.Services.AddSingleton<MqttClientService>();
builder.Services.AddSingleton<MqttSubscriptionService>();
builder.Services.AddSingleton<LogNotificationService>();
//builder.Services.AddTransient<IBrowserService,BrowserService>();

builder.Services.Configure<TuyaCommunicatorOptions>(
builder.Configuration.GetSection("Tuya"));
Expand Down

0 comments on commit 22dc24f

Please sign in to comment.