Skip to content

Commit

Permalink
Renamed database references and added database sort in LoadDatabases
Browse files Browse the repository at this point in the history
  • Loading branch information
jschick04 committed May 30, 2023
1 parent cce8dc9 commit f3cb3c5
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/EventLogExpert.Library/Models/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record SettingsModel
[JsonIgnore]
public TimeZoneInfo TimeZoneInfo => TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId);

public IList<string>? DisabledProviders { get; set; }
public IList<string>? DisabledDatabases { get; set; }

public bool IsPrereleaseEnabled { get; set; }
}
5 changes: 5 additions & 0 deletions src/EventLogExpert.Test/EventResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public DisplayEventModel Resolve(EventRecord eventRecord, string OwningLog)
{
return ResolveFromProviderDetails(eventRecord, eventRecord.Properties, _providerDetailsList[0], OwningLog);
}

public void SetActiveDatabases(IEnumerable<string> databaseNames)
{
return;
}
}

private readonly ITestOutputHelper _outputHelper;
Expand Down
4 changes: 2 additions & 2 deletions src/EventLogExpert/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public App(IDispatcher fluxorDispatcher,
IStateSelection<EventLogState, bool> continuouslyUpdateState,
IStateSelection<SettingsState, bool> showLogState,
IStateSelection<SettingsState, bool> showComputerState,
IStateSelection<SettingsState, IEnumerable<string>> loadedProvidersState)
IStateSelection<SettingsState, IEnumerable<string>> loadedDatabasesState)
{
InitializeComponent();

Expand All @@ -31,7 +31,7 @@ public App(IDispatcher fluxorDispatcher,
continuouslyUpdateState,
showLogState,
showComputerState,
loadedProvidersState));
loadedDatabasesState));
}

protected override Window CreateWindow(IActivationState? activationState)
Expand Down
4 changes: 2 additions & 2 deletions src/EventLogExpert/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public MainPage(IDispatcher fluxorDispatcher,
showComputerNameState.SelectedValueChanged += (sender, showComputerName) =>
ShowComputerNameMenuItem.Text = $"Show Computer Name{(showComputerName ? " ✓" : "")}";

loadedProvidersState.Select(s => s.LoadedProviders);
loadedProvidersState.Select(s => s.LoadedDatabases);

loadedProvidersState.SelectedValueChanged += (sender, loadedProviders) =>
_resolver.SetActiveDatabases(loadedProviders);

fluxorDispatcher.Dispatch(new SettingsAction.LoadSettings());
fluxorDispatcher.Dispatch(new SettingsAction.LoadProviders());
fluxorDispatcher.Dispatch(new SettingsAction.LoadDatabases());

PopulateOtherLogsMenu();

Expand Down
34 changes: 17 additions & 17 deletions src/EventLogExpert/Shared/Components/SettingsModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

<div class="tz-row">Time Zone: <TimeZoneSelect @bind-Value="_request.TimeZoneId" /></div>

<div class="provider-row">
<div>Loaded Providers: </div>
<div class="database-row">
<div>Loaded Databases: </div>

<button class="button-primary" type="button" @onclick="ImportProvider">
<button class="button-primary" type="button" @onclick="ImportDatabase">
<i class="bi bi-plus-circle"></i> Add Provider
</button>
</div>

<div class="provider-list">
@if (_providers?.Any() is true)
<div class="database-list">
@if (_databases?.Any() is true)
{
@foreach (var provider in _providers)
@foreach (var database in _databases)
{
<div class="provider-row">
<span>@provider</span>
<button class="button-remove" type="button" @onclick="() => DisableProvider(provider)">
<div class="database-row">
<span>@database</span>
<button class="button-remove" type="button" @onclick="() => DisableDatabase(database)">
<i class="bi bi-dash-circle"></i> Disable
</button>
</div>
Expand All @@ -38,18 +38,18 @@
}
</div>

@if (_request.DisabledProviders?.Any() is true)
@if (_request.DisabledDatabases?.Any() is true)
{
<div class="provider-row">
<div>Disabled Providers: </div>
<div class="database-row">
<div>Disabled Databases: </div>
</div>

<div class="provider-list">
@foreach (var provider in _request.DisabledProviders)
<div class="database-list">
@foreach (var database in _request.DisabledDatabases)
{
<div class="provider-row">
<span>@provider</span>
<button class="button-save" type="button" @onclick="() => EnableProvider(provider)">
<div class="database-row">
<span>@database</span>
<button class="button-save" type="button" @onclick="() => EnableDatabase(database)">
<i class="bi bi-plus-circle"></i> Enable
</button>
</div>
Expand Down
52 changes: 25 additions & 27 deletions src/EventLogExpert/Shared/Components/SettingsModal.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace EventLogExpert.Shared.Components;

public partial class SettingsModal
{
private List<string>? _providers;
private bool _providersHasChanged;
private List<string>? _databases;
private bool _databasesHasChanged;
private SettingsModel _request = new();

[Inject] private IJSRuntime JSRuntime { get; set; } = null!;
Expand All @@ -31,25 +31,25 @@ private async void Close()
ResetSettingsModel();
}

private void DisableProvider(string provider)
private void DisableDatabase(string database)
{
_request.DisabledProviders ??= new List<string>();
_request.DisabledDatabases ??= new List<string>();

_request.DisabledProviders?.Add(provider);
_providers?.Remove(provider);
_request.DisabledDatabases?.Add(database);
_databases?.Remove(database);

_providersHasChanged = true;
_databasesHasChanged = true;
}

private void EnableProvider(string provider)
private void EnableDatabase(string database)
{
_request.DisabledProviders?.Remove(provider);
_providers?.Add(provider);
_request.DisabledDatabases?.Remove(database);
_databases?.Add(database);

_providersHasChanged = true;
_databasesHasChanged = true;
}

private async void ImportProvider()
private async void ImportDatabase()
{
PickOptions options = new()
{
Expand Down Expand Up @@ -88,13 +88,12 @@ private async void ImportProvider()
}

bool answer = await Application.Current!.MainPage!.DisplayAlert("Application Restart Required",
"In order to use these providers, a restart of the application is required. Would you like to restart now?",
"Yes",
"No");
"In order to use these databases, a restart of the application is required. Would you like to restart now?",
"Yes", "No");

if (!answer)
{
Dispatcher.Dispatch(new SettingsAction.LoadProviders());
Dispatcher.Dispatch(new SettingsAction.LoadDatabases());
return;
}

Expand All @@ -103,45 +102,44 @@ private async void ImportProvider()
if (res == 0) { Application.Current.Quit(); }
}

private async void RemoveProvider(string provider)
private async void RemoveDatabase(string database)
{
bool answer = await Application.Current!.MainPage!.DisplayAlert("Remove Provider",
"Are you sure you want to remove this provider?",
"Yes",
"No");
bool answer = await Application.Current!.MainPage!.DisplayAlert("Remove Database",
"Are you sure you want to remove this database?",
"Yes", "No");

if (!answer) { return; }

try
{
var destination = Path.Join(Utils.DatabasePath, provider);
var destination = Path.Join(Utils.DatabasePath, database);
File.Delete(destination);
}
catch
{ // TODO: Log Error
return;
}

Dispatcher.Dispatch(new SettingsAction.LoadProviders());
Dispatcher.Dispatch(new SettingsAction.LoadDatabases());
}

private void ResetSettingsModel()
{
_request = SettingsState.Value.Config with { };

_providers = SettingsState.Value.LoadedProviders.ToList();
_providersHasChanged = false;
_databases = SettingsState.Value.LoadedDatabases.ToList();
_databasesHasChanged = false;

StateHasChanged();
}

private void Save()
{

if (_providersHasChanged)
if (_databasesHasChanged)
{
Dispatcher.Dispatch(new SettingsAction.Save(_request));
Dispatcher.Dispatch(new SettingsAction.LoadProviders());
Dispatcher.Dispatch(new SettingsAction.LoadDatabases());
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/EventLogExpert/Shared/Components/SettingsModal.razor.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ dialog {
margin-bottom: 1rem;
}

.provider-row {
.database-row {
display: flex;
justify-content: space-between;
}

.provider-list {
.database-list {
display: flex;
flex-direction: column;

Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert/Store/Settings/SettingsAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record SettingsAction
{
public record CheckForUpdates : SettingsAction;

public record LoadProviders : SettingsAction;
public record LoadDatabases : SettingsAction;

public record LoadSettings : SettingsAction;

Expand Down
36 changes: 28 additions & 8 deletions src/EventLogExpert/Store/Settings/SettingsReducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
using Fluxor;
using System.Collections.Immutable;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace EventLogExpert.Store.Settings;

public class SettingsReducer
{
private static readonly ReaderWriterLockSlim ConfigSrwLock = new();

[ReducerMethod(typeof(SettingsAction.LoadProviders))]
public static SettingsState ReduceLoadProvider(SettingsState state)
[ReducerMethod(typeof(SettingsAction.LoadDatabases))]
public static SettingsState ReduceLoadDatabases(SettingsState state)
{
List<string> providers = new();
List<string> databases = new();

try
{
if (Directory.Exists(Utils.DatabasePath))
{
foreach (var item in Directory.EnumerateFiles(Utils.DatabasePath, "*.db"))
{
providers.Add(Path.GetFileName(item));
databases.Add(Path.GetFileName(item));
}
}
}
Expand All @@ -32,17 +33,17 @@ public static SettingsState ReduceLoadProvider(SettingsState state)
return state;
}

if (providers.Count <= 0) { return state; }
if (databases.Count <= 0) { return state; }

SettingsModel? config = ReadSettingsConfig();

if (config?.DisabledProviders is not null)
if (config?.DisabledDatabases is not null)
{
providers.RemoveAll(enabled => config.DisabledProviders
databases.RemoveAll(enabled => config.DisabledDatabases
.Any(disabled => string.Equals(enabled, disabled, StringComparison.InvariantCultureIgnoreCase)));
}

return state with { LoadedProviders = providers.ToImmutableList() };
return state with { LoadedDatabases = SortDatabases(databases).ToImmutableList() };
}

[ReducerMethod(typeof(SettingsAction.LoadSettings))]
Expand Down Expand Up @@ -89,6 +90,25 @@ public static SettingsState ReduceToggleShowLogName(SettingsState state, Setting
finally { ConfigSrwLock.ExitReadLock(); }
}

private static IEnumerable<string> SortDatabases(IEnumerable<string> databases)
{
var r = new Regex("^(.+) (\\S+)$");

return databases
.Select(name =>
{
var m = r.Match(name);

return m.Success
? new { FirstPart = m.Groups[1].Value + " ", SecondPart = m.Groups[2].Value }
: new { FirstPart = name, SecondPart = "" };
})
.OrderBy(n => n.FirstPart)
.ThenByDescending(n => n.SecondPart)
.Select(n => n.FirstPart + n.SecondPart)
.ToList();
}

private static bool WriteSettingsConfig(SettingsModel settings)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/EventLogExpert/Store/Settings/SettingsState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record SettingsState
{
public SettingsModel Config { get; init; } = new();

public IImmutableList<string> LoadedProviders { get; init; } = ImmutableList<string>.Empty;
public IImmutableList<string> LoadedDatabases { get; init; } = ImmutableList<string>.Empty;

public bool ShowLogName { get; init; }

Expand Down

0 comments on commit f3cb3c5

Please sign in to comment.