Skip to content

Commit 1e3069b

Browse files
committed
Conform to revised J4JHost API
1 parent cfd1e64 commit 1e3069b

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

GeoProcessorWPF/ViewModelLocator.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ViewModelLocator
2121
public const string AppConfigFile = "appConfig.json";
2222
public const string UserConfigFile = "userConfig.json";
2323

24-
private readonly IHost _host;
24+
private readonly IJ4JHost _host;
2525

2626
private IJ4JLogger? _buildLogger;
2727

@@ -36,7 +36,7 @@ public ViewModelLocator()
3636
public RouteDisplayVM RouteDisplayVM => _host!.Services.GetRequiredService<RouteDisplayVM>();
3737
public RouteEnginesVM RouteEnginesVM => _host!.Services.GetRequiredService<RouteEnginesVM>();
3838

39-
private IHost CreateHost()
39+
private IJ4JHost CreateHost()
4040
{
4141
var hostConfig = new J4JHostConfiguration()
4242
.Publisher( "J4JSoftware" )
@@ -55,12 +55,7 @@ private IHost CreateHost()
5555
throw new ArgumentException(
5656
$"Could not create IHost. The following requirements were not met: {hostConfig.MissingRequirements.ToText()}" );
5757

58-
var builder = hostConfig.CreateHostBuilder();
59-
60-
if( builder == null )
61-
throw new ArgumentException( "Failed to create host builder." );
62-
63-
var retVal = builder.Build();
58+
var retVal = hostConfig.Build();
6459

6560
if( retVal == null )
6661
throw new ArgumentException( "Failed to build host" );
@@ -126,6 +121,10 @@ private void SetupDependencyInjection( HostBuilderContext hbc, ContainerBuilder
126121
builder.RegisterType<ProcessorVM>()
127122
.AsSelf();
128123

124+
//builder.Register( c => _host )
125+
// .As<IJ4JHost>()
126+
// .SingleInstance();
127+
129128
builder.RegisterModule<AutofacGeoProcessorModule>();
130129
}
131130

GeoProcessorWPF/config/APIKey.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ namespace J4JSoftware.GeoProcessor
2525
{
2626
public class APIKey
2727
{
28-
private string? _encryptedKey;
29-
private string? _key;
28+
private string _encryptedKey = string.Empty;
29+
private string _key = string.Empty;
3030

3131
internal IJ4JProtection? Protection { get; private set; }
3232

@@ -36,52 +36,56 @@ public string EncryptedValue
3636
{
3737
get
3838
{
39-
if( _encryptedKey != null )
40-
return _encryptedKey;
41-
42-
// abort if we haven't been initialized or the plain text key is undefined/not yet set
43-
if( Protection == null || string.IsNullOrEmpty( _key ) )
39+
if( Protection == null )
4440
return string.Empty;
4541

46-
if( Protection == null )
47-
throw new NullReferenceException(
48-
$"{nameof(APIKey)} is not initialized. {nameof(Initialize)}() must be called before use." );
42+
if( !string.IsNullOrEmpty( _encryptedKey ) )
43+
return _encryptedKey;
4944

5045
if( !Protection.Protect( _key!, out var encrypted ) )
5146
return string.Empty;
5247

5348
_encryptedKey = encrypted!;
49+
5450
return _encryptedKey;
5551
}
5652

57-
set => _encryptedKey = value;
53+
set
54+
{
55+
_encryptedKey = value;
56+
57+
// force decryption if the cryption system is initialized
58+
if( Protection != null )
59+
_key = string.Empty;
60+
}
5861
}
5962

6063
[ JsonIgnore ]
6164
public string Value
6265
{
6366
get
6467
{
65-
if( _key != null )
66-
return _key;
67-
68-
// abort if we haven't been initialized or the encrypted key is undefined/not yet set
69-
if( Protection == null || string.IsNullOrEmpty( _encryptedKey ) )
68+
if (Protection == null)
7069
return string.Empty;
7170

72-
if( !Protection.Unprotect( _encryptedKey!, out var decrypted ) )
71+
if (!string.IsNullOrEmpty(_key))
72+
return _key;
73+
74+
if ( !Protection.Unprotect( _encryptedKey!, out var decrypted ) )
7375
return string.Empty;
7476

7577
_key = decrypted!;
78+
7679
return _key;
7780
}
7881

7982
set
8083
{
8184
_key = value;
8285

83-
// force re-encryption
84-
_encryptedKey = null;
86+
// force encryption if the cryption system is initialized
87+
if( Protection != null )
88+
_encryptedKey = string.Empty;
8589
}
8690
}
8791

GeoProcessorWPF/ui/optionswin/vm/RouteEnginesVM.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Collections.ObjectModel;
2222
using System.Linq;
2323
using System.Windows;
24+
using J4JSoftware.Logging;
2425
using Microsoft.Toolkit.Mvvm.ComponentModel;
2526

2627
namespace J4JSoftware.GeoProcessor
@@ -29,6 +30,7 @@ public class RouteEnginesVM : ObservableRecipient
2930
{
3031
private readonly IAppConfig _appConfig;
3132
private readonly IUserConfig _userConfig;
33+
3234
private string _apiKey = string.Empty;
3335

3436
private Visibility _apiKeyVisibility = Visibility.Collapsed;
@@ -41,7 +43,8 @@ public class RouteEnginesVM : ObservableRecipient
4143

4244
public RouteEnginesVM(
4345
IAppConfig appConfig,
44-
IUserConfig userConfig )
46+
IUserConfig userConfig
47+
)
4548
{
4649
_appConfig = appConfig;
4750
_userConfig = userConfig;
@@ -50,6 +53,10 @@ public RouteEnginesVM(
5053
.Where( x => x != ProcessorType.None ) );
5154

5255
UnitTypes = new ObservableCollection<UnitTypes>( Enum.GetValues<UnitTypes>() );
56+
57+
// go live for messages
58+
IsActive = true;
59+
5360
SelectedProcessorType = ProcessorTypes.FirstOrDefault( x => x != ProcessorType.Distance );
5461

5562
_setState = PropertySettingState.Normal;
@@ -137,14 +144,17 @@ public string APIKey
137144
set
138145
{
139146
SetProperty( ref _apiKey, value );
140-
OnSettingsChanged();
141147

142148
if( _setState != PropertySettingState.Normal
143149
|| !_userConfig.APIKeys.TryGetValue( SelectedProcessorType, out var temp ) )
150+
{
144151
return;
152+
}
145153

146154
temp.Value = value;
147155
EncryptedAPIKey = temp.EncryptedValue;
156+
157+
OnSettingsChanged();
148158
}
149159
}
150160

0 commit comments

Comments
 (0)