Skip to content

Commit

Permalink
Merge pull request #111 from block-core/Setting-UI/UX
Browse files Browse the repository at this point in the history
Add NetworkMonitoringService to check Indexers and Relays in background
  • Loading branch information
miladsoft authored Jun 28, 2024
2 parents 4f3753e + 9b39f35 commit e90d822
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 11 deletions.
87 changes: 85 additions & 2 deletions src/Angor/Client/Pages/Browse.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@inject NavigationManager NavigationManager
@inject IRelayService _RelayService
@inject IIndexerService _IndexerService
@inject INetworkService _NetworkService
@inject IJSRuntime JS
@inject ISerializer serializer

Expand Down Expand Up @@ -216,11 +217,93 @@ else
protected override async Task OnInitializedAsync()
{
projects = SessionStorage.GetProjectIndexerData() ?? new List<ProjectIndexerData>();
if (!projects.Any())

if (projects.Any())
{
// Projects exist, initialize the search focus
await JS.InvokeVoidAsync("angor.initSearchFocus", "searchQuery");

// Check for indexer and relay availability in the background
_ = CheckIndexerAndRelayAvailability();
}
else
{
// No projects are cached, fetch new projects after ensuring services are available
await FetchAndEnsureServices();
}
}

private async Task FetchAndEnsureServices()
{
try
{
_NetworkService.AddSettingsIfNotExist();
await _NetworkService.CheckServices(true);

var primaryIndexer = _NetworkService.GetPrimaryIndexer();
var primaryRelay = _NetworkService.GetPrimaryRelay();

if (primaryIndexer == null || primaryRelay == null)
{
notificationComponent.ShowNotificationMessage("No primary indexer or relay found. Please configure them in settings.");
return;
}

await GetAllProjects();
}
await JS.InvokeVoidAsync("angor.initSearchFocus", "searchQuery");
catch (Exception ex)
{
notificationComponent.ShowNotificationMessage($"Initialization failed: {ex.Message}");
}
}

private async Task CheckIndexerAndRelayAvailability()
{
try
{
// Attempt to retrieve primary indexer and relay
var primaryIndexer = _NetworkService.GetPrimaryIndexer();
var primaryRelay = _NetworkService.GetPrimaryRelay();

// Initialize flags to track service availability
bool indexerAvailable = false;
bool relayAvailable = false;

// Check if the primary indexer is available
if (primaryIndexer != null)
{
await _NetworkService.CheckServices(false); // Refresh service status
indexerAvailable = primaryIndexer.Status == UrlStatus.Online;
if (!indexerAvailable)
{
notificationComponent.ShowNotificationMessage("Primary indexer is offline. Please check your network settings.");
}
}
else
{
notificationComponent.ShowNotificationMessage("No primary indexer configured. Please add one in settings.");
}

// Check if the primary relay is available
if (primaryRelay != null)
{
await _NetworkService.CheckServices(false); // Refresh service status
relayAvailable = primaryRelay.Status == UrlStatus.Online;
if (!relayAvailable)
{
notificationComponent.ShowNotificationMessage("Primary relay is offline. Please check your relay settings.");
}
}
else
{
notificationComponent.ShowNotificationMessage("No primary relay configured. Please add one in settings.");
}
}
catch (Exception ex)
{
// General error handling if the service check fails
notificationComponent.ShowNotificationMessage($"Service check failed: {ex.Message}");
}
}

private void ToggleView()
Expand Down
29 changes: 21 additions & 8 deletions src/Angor/Client/Pages/Settings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@
<td>
@if (indexer.IsPrimary)
{
<span role="button">
<span role="button">
<Icon IconName="primary" Height="24" Width="24"></Icon>
</span>
</span>
}
else
{
Expand Down Expand Up @@ -286,7 +286,7 @@
else
{
<span @onclick="() => SetPrimaryRelay(relay)" role="button">
<Icon IconName="set" Height="24" Width="24" ></Icon>
<Icon IconName="set" Height="24" Width="24"></Icon>
</span>
}
</td>
Expand Down Expand Up @@ -362,7 +362,7 @@
</div>
</div>
<div class="modal-footer">
<button class="btn btn-danger" @onclick="WipteAllData">Wipe Storage</button>
<button class="btn btn-danger" @onclick="WipeAllData">Wipe Storage</button>

<button type="button" class="btn btn-secondary" data-bs-dismiss="modal" @onclick="() => showWipeallModal = false">Close</button>
</div>
Expand Down Expand Up @@ -402,12 +402,25 @@

networkType = _networkConfiguration.GetNetwork().Name;

_networkService.OnStatusChanged += UpdateUI;

if (!networkType.ToLower().Contains("test"))
selectedNetwork = "mainnet";

return base.OnInitializedAsync();
}

private void UpdateUI()
{
settingsInfo = _clientStorage.GetSettingsInfo();
StateHasChanged();
}

public void Dispose()
{
_networkService.OnStatusChanged -= UpdateUI;
}

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
Expand Down Expand Up @@ -565,21 +578,21 @@
try
{
await _networkService.CheckServices(force);

settingsInfo = _clientStorage.GetSettingsInfo();
}
catch (Exception e)
{
Logger.LogError(e, e.Message);
notificationComponent.ShowErrorMessage(e.Message);
Logger.LogError(e, $"Error during Refresh: {e.Message}");
notificationComponent.ShowErrorMessage("Failed to refresh settings: " + e.Message);
}
finally
{
refreshSpinner = false;
StateHasChanged();
}
}

private void WipteAllData()
private void WipeAllData()
{
if (!confirmWipe)
{
Expand Down
9 changes: 8 additions & 1 deletion src/Angor/Client/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@inject INetworkService _networkService
@inject NavigationManager _navManager
@inject NavMenuState NavMenuState;

<div class="@($"{(isDarkTheme ? "dark" : "")}") sidenav-show bg-gray-100 @sidenavClass ">
<aside class="sidenav navbar navbar-vertical navbar-expand-xs border-0 border-radius-xl my-3 fixed-start ms-3 b-shadow overflow-x-hidden" id="sidenav-main">
<NavMenu ToggleSidenavCallback="ToggleSidenavMobile" ToggleSidenavTextCallback="ToggleSidenav" ToggleleLockMenuCallback="ToggleLockMenu" />
Expand Down Expand Up @@ -85,6 +85,10 @@
{
InitializeAppVersion();
InitializeNetworkConfiguration();

_networkService.AddSettingsIfNotExist();
await _networkService.CheckServices(true);

await LoadThemeFromStorage();

await UpdateThemeIcon(isDarkTheme);
Expand All @@ -100,6 +104,9 @@
if (version != null)
_softwareVersion = version.ToString();
}




private void InitializeNetworkConfiguration()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Angor/Server/FaucetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private AddressInfo GenerateAddressFromPubKey(int scanIndex, Network network, bo

public class NetworkServiceMock : INetworkService
{
public event Action OnStatusChanged;

public Task CheckServices(bool force = false)
{
throw new NotImplementedException();
Expand Down
2 changes: 2 additions & 0 deletions src/Angor/Shared/Services/INetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface INetworkService
void CheckAndHandleError(HttpResponseMessage httpResponseMessage);
void HandleException(Exception exception);
void CheckAndSetNetwork(string url, string? setNetwork = null);
event Action OnStatusChanged;

}
3 changes: 3 additions & 0 deletions src/Angor/Shared/Services/NetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class NetworkService : INetworkService
private readonly HttpClient _httpClient;
private readonly ILogger<NetworkService> _logger;
private readonly INetworkConfiguration _networkConfiguration;
public event Action OnStatusChanged;


public NetworkService(INetworkStorage networkStorage, HttpClient httpClient, ILogger<NetworkService> logger, INetworkConfiguration networkConfiguration)
{
Expand Down Expand Up @@ -104,6 +106,7 @@ public async Task CheckServices(bool force = false)
{
_logger.LogError($"Failed to check indexer status url = {indexerUrl.Url}, StatusCode = {response.StatusCode}");
}
OnStatusChanged?.Invoke();
}
catch (Exception ex)
{
Expand Down

0 comments on commit e90d822

Please sign in to comment.