Skip to content

Commit

Permalink
Minor fixes for settings #201
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hofmann <[email protected]>
  • Loading branch information
hoffe86 committed Jul 28, 2021
1 parent 2208ff1 commit ed9e0db
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 88 deletions.
6 changes: 3 additions & 3 deletions src/openHAB.Windows/Controls/ConnectionDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@
<TextBlock x:Uid="VersionNumber"
Margin="0,0,5,0"
VerticalAlignment="Center" />
<TextBlock Text="{Binding RuntimeVersion, Mode=OneWay}" />
<TextBlock Text="{Binding Status.RuntimeVersion, Mode=OneWay}" />
</StackPanel>
<StackPanel Grid.Row="2" Margin="5,0,0,0" Orientation="Horizontal">
<TextBlock x:Uid="BuildNumber"
Margin="0,0,5,0"
VerticalAlignment="Center" />
<TextBlock Text="{Binding Build, Mode=OneWay}" />
<TextBlock Text="{Binding Status.Build, Mode=OneWay}" />
</StackPanel>
<StackPanel Grid.Row="3" Margin="5,0,0,0" Orientation="Horizontal">
<TextBlock x:Uid="ConnectionCheck"
Expand All @@ -112,7 +112,7 @@
VerticalAlignment="Top"
FontSize="32"
FontFamily="{StaticResource SymbolThemeFontFamily}"
Glyph="{Binding State, Converter={StaticResource OpenHabUrlStateToGlyph}, Mode=TwoWay}" />
Glyph="{Binding Status.State, Converter={StaticResource OpenHabUrlStateToGlyph}, Mode=TwoWay}" />
</StackPanel>
</Grid>
</StackPanel>
Expand Down
9 changes: 0 additions & 9 deletions src/openHAB.Windows/View/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ protected override async void OnNavigatedTo(NavigationEventArgs e)
Messenger.Default.Register<SettingsUpdatedMessage>(this, msg => HandleSettingsUpdate(msg), true);
Messenger.Default.Register<SettingsValidationMessage>(this, msg => NotificationSettingsValidation(msg), true);

var autostartEnabled = await _appManager.IsStartupEnabled().ConfigureAwait(false);
var canAppAutostartEnabled = await _appManager.CanEnableAutostart().ConfigureAwait(false);

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
_settingsViewModel.Settings.IsAppAutostartEnabled = autostartEnabled;
_settingsViewModel.Settings.CanAppAutostartEnabled = canAppAutostartEnabled;
});

AppAutostartSwitch.Toggled += AppAutostartSwitch_Toggled;
}

Expand Down
34 changes: 29 additions & 5 deletions src/openHAB.Windows/ViewModel/ConfigurationViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OpenHAB.Core.Contracts.Services;
using OpenHAB.Core.Model;
using OpenHAB.Core.Model.Connection;
using OpenHAB.Core.SDK;
using OpenHAB.Core.Services;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;

namespace OpenHAB.Windows.ViewModel
{
Expand All @@ -17,6 +20,7 @@ public class ConfigurationViewModel : ViewModelBase<object>
{
private readonly ILogger<ConfigurationViewModel> _logger;
private readonly Settings _settings;
private readonly IAppManager _appManager;
private readonly ISettingsService _settingsService;
private List<LanguageViewModel> _appLanguages;
private bool? _canAppAutostartEnabled;
Expand All @@ -34,12 +38,13 @@ public class ConfigurationViewModel : ViewModelBase<object>
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationViewModel"/> class.
/// </summary>
public ConfigurationViewModel(ISettingsService settingsService, IOpenHAB openHabsdk, ILogger<ConfigurationViewModel> logger, Settings settings)
public ConfigurationViewModel(ISettingsService settingsService, IAppManager appManager, IOpenHAB openHabsdk, ILogger<ConfigurationViewModel> logger, Settings settings)
: base(new object())
{
_settingsService = settingsService;
_logger = logger;
_settings = settings;
_appManager = appManager;

_localConnection = new ConnectionDialogViewModel(_settings.LocalConnection, openHabsdk, OpenHABHttpClientType.Local);
_localConnection.PropertyChanged += ConnectionPropertyChanged;
Expand All @@ -53,6 +58,25 @@ public ConfigurationViewModel(ISettingsService settingsService, IOpenHAB openHab
_startAppMinimized = _settings.StartAppMinimized;
_notificationsEnable = _settings.NotificationsEnable;

CoreDispatcher dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
Task<bool> taskCanEnableAutostart = _appManager.CanEnableAutostart();
taskCanEnableAutostart.ContinueWith(result =>
{
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
CanAppAutostartEnabled = result.Result;
});
});

Task<bool> taskStartupEnbabled = _appManager.IsStartupEnabled();
taskStartupEnbabled.ContinueWith(result =>
{
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
IsAppAutostartEnabled = result.Result;
});
});

_appLanguages = InitalizeAppLanguages();
_selectedAppLanguage =
_appLanguages.FirstOrDefault(x => string.Compare(x.Code, _settings.AppLanguage, StringComparison.InvariantCultureIgnoreCase) == 0);
Expand Down Expand Up @@ -272,8 +296,8 @@ public bool UseSVGIcons
public bool IsConnectionConfigValid()
{
bool validConfig = IsRunningInDemoMode.Value ||
(!string.IsNullOrEmpty(LocalConnection?.Url) && LocalConnection?.State == OpenHABUrlState.OK) ||
(!string.IsNullOrEmpty(RemoteConnection?.Url) && RemoteConnection?.State == OpenHABUrlState.OK);
(!string.IsNullOrEmpty(LocalConnection?.Url) && LocalConnection?.Status.State == OpenHABUrlState.OK) ||
(!string.IsNullOrEmpty(RemoteConnection?.Url) && RemoteConnection?.Status.State == OpenHABUrlState.OK);

_logger.LogInformation($"Valid application configuration: {validConfig}");

Expand All @@ -297,15 +321,15 @@ public bool Save()

private void ConfigurationViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(IsDirty))
if (e.PropertyName != nameof(IsDirty) && e.PropertyName != nameof(CanAppAutostartEnabled) && e.PropertyName != nameof(IsAppAutostartEnabled))
{
OnPropertyChanged(nameof(IsDirty));
}
}

private void ConnectionPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
OnPropertyChanged(nameof(IsDirty));
OnPropertyChanged(nameof(IsDirty));
}

private List<LanguageViewModel> InitalizeAppLanguages()
Expand Down
87 changes: 16 additions & 71 deletions src/openHAB.Windows/ViewModel/ConnectionDialogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using OpenHAB.Core;
Expand All @@ -11,8 +10,6 @@
using OpenHAB.Core.Model;
using OpenHAB.Core.Model.Connection;
using OpenHAB.Core.SDK;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;

namespace OpenHAB.Windows.ViewModel
{
Expand All @@ -21,20 +18,18 @@ namespace OpenHAB.Windows.ViewModel
/// </summary>
public class ConnectionDialogViewModel : ViewModelBase<OpenHABConnection>
{
private readonly IOpenHAB _openHabsdk;
private readonly OpenHABHttpClientType _type;
private string _password;
private ConnectionProfileViewModel _profile;
private ObservableCollection<ConnectionProfileViewModel> _profiles;
private ICommand _selectProfile;
private string _url;
private ICommand _urlCheckCommand;
private OpenHABUrlState _connectionState;
private ConnectionStatusViewModel _connectionStatus;
private string _username;
private bool? _willIgnoreSSLCertificate;
private bool? _willIgnoreSSLHostname;
private string _runtimeVersion;
private string _build;


/// <summary>
/// Initializes a new instance of the <see cref="ConnectionDialogViewModel"/> class.
Expand All @@ -45,9 +40,7 @@ public class ConnectionDialogViewModel : ViewModelBase<OpenHABConnection>
public ConnectionDialogViewModel(OpenHABConnection connectionConfig, IOpenHAB openHabsdk, OpenHABHttpClientType type)
: base(connectionConfig)
{
_openHabsdk = openHabsdk;
_type = type;
_connectionState = OpenHABUrlState.Unknown;

List<ConnectionProfileViewModel> list
= new List<ConnectionProfileViewModel>(Settings.ConnectionProfiles.Where(x => x.Type == _type).OrderBy(x => x.Id).Select(x => new ConnectionProfileViewModel(x)));
Expand All @@ -59,9 +52,10 @@ List<ConnectionProfileViewModel> list
_profile = list.FirstOrDefault(x => x.Id == Model.Profile.Id);
}

_connectionStatus = new ConnectionStatusViewModel(openHabsdk);
if (!string.IsNullOrEmpty(Model?.Url))
{
CheckConnectionSettings(Model.Url);
CheckConnectionSettings(null);
}
}

Expand Down Expand Up @@ -118,6 +112,17 @@ public ConnectionProfileViewModel Profile
}
}

/// <summary>Gets or sets the connection status.</summary>
/// <value>The connection status.</value>
public ConnectionStatusViewModel Status
{
get => _connectionStatus;
set
{
Set(ref _connectionStatus, value);
}
}

/// <summary>Gets or sets the available connection profiles.</summary>
/// <value>The profiles.</value>
public ObservableCollection<ConnectionProfileViewModel> Profiles
Expand Down Expand Up @@ -189,32 +194,6 @@ public string Url
/// <value>The local URL check command.</value>
public ICommand UrlCheckCommand => _urlCheckCommand ?? (_urlCheckCommand = new RelayCommand<object>(CheckConnectionSettings));

/// <summary>
/// Gets or sets the state for OpenHab connection.
/// </summary>
/// <value>The state of the connection.</value>
public OpenHABUrlState State
{
get => _connectionState;
set => Set(ref _connectionState, value);
}

/// <summary>Gets or sets the runtime version.</summary>
/// <value>The runtime version.</value>
public string RuntimeVersion
{
get => _runtimeVersion;
set => Set(ref _runtimeVersion, value, true);
}

/// <summary>Gets or sets the build.</summary>
/// <value>The build.</value>
public string Build
{
get => _build;
set => Set(ref _build, value, true);
}

/// <summary>
/// Gets or sets the username for the local OpenHAB server connection.
/// </summary>
Expand Down Expand Up @@ -256,41 +235,7 @@ public bool? WillIgnoreSSLHostname

private void CheckConnectionSettings(object parameter)
{
if (parameter == null)
{
return;
}

string url = parameter.ToString();

Task<HttpResponseResult<ServerInfo>> result = _openHabsdk.GetOpenHABServerInfo(this.Model);
result.ContinueWith(async (task) =>
{
OpenHABUrlState urlState = OpenHABUrlState.Unknown;
string runtimeVersion = string.Empty;
string build = string.Empty;

if (task.IsCompletedSuccessfully && task.Result.Content != null)
{
ServerInfo serverInfo = task.Result.Content;

runtimeVersion = serverInfo.RuntimeVersion;
build = serverInfo.Build;
urlState = OpenHABUrlState.OK;
}
else
{
urlState = OpenHABUrlState.Failed;
}

CoreDispatcher dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
State = urlState;
RuntimeVersion = runtimeVersion;
Build = build;
});
});
_connectionStatus.CheckConnectionSettings(Model.Url, Model);
}

private void CreateProfile(IConnectionProfile value)
Expand Down
96 changes: 96 additions & 0 deletions src/openHAB.Windows/ViewModel/ConnectionStatusViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Threading.Tasks;
using OpenHAB.Core.Common;
using OpenHAB.Core.Model;
using OpenHAB.Core.Model.Connection;
using OpenHAB.Core.SDK;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;

namespace OpenHAB.Windows.ViewModel
{
/// <summary>
/// ViewModel for OpenHAB connection status.
/// </summary>
public class ConnectionStatusViewModel : ViewModelBase<object>
{
private readonly IOpenHAB _openHabsdk;

private OpenHABUrlState _connectionState;

private string _runtimeVersion;
private string _build;

/// <summary>
/// Initializes a new instance of the <see cref="ConnectionStatusViewModel"/> class.
/// </summary>
/// <param name="openHabsdk">OpenHABSDK class.</param>
public ConnectionStatusViewModel(IOpenHAB openHabsdk)
: base(null)
{
_openHabsdk = openHabsdk;
_connectionState = OpenHABUrlState.Unknown;
}

/// <summary>
/// Gets or sets the state for OpenHab connection.
/// </summary>
/// <value>The state of the connection.</value>
public OpenHABUrlState State
{
get => _connectionState;
set => Set(ref _connectionState, value);
}

/// <summary>Gets or sets the runtime version.</summary>
/// <value>The runtime version.</value>
public string RuntimeVersion
{
get => _runtimeVersion;
set => Set(ref _runtimeVersion, value, true);
}

/// <summary>Gets or sets the build.</summary>
/// <value>The build.</value>
public string Build
{
get => _build;
set => Set(ref _build, value, true);
}

/// <summary>Checks the connection settings.</summary>
/// <param name="url">The URL.</param>
/// <param name="connection">The connection.</param>
public void CheckConnectionSettings(string url, OpenHABConnection connection)
{
Task<HttpResponseResult<ServerInfo>> result = _openHabsdk.GetOpenHABServerInfo(connection);
result.ContinueWith(async (task) =>
{
OpenHABUrlState urlState = OpenHABUrlState.Unknown;
string runtimeVersion = string.Empty;
string build = string.Empty;

if (task.IsCompletedSuccessfully && task.Result.Content != null)
{
ServerInfo serverInfo = task.Result.Content;

runtimeVersion = serverInfo.RuntimeVersion;
build = serverInfo.Build;
urlState = OpenHABUrlState.OK;
}
else
{
urlState = OpenHABUrlState.Failed;
}

CoreDispatcher dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
State = urlState;
RuntimeVersion = runtimeVersion;
Build = build;
});
});
}
}
}
1 change: 1 addition & 0 deletions src/openHAB.Windows/openHAB.Windows.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
<Compile Include="Extensions\ContentDialogExtensions.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="ViewModel\ConfigurationViewModel.cs" />
<Compile Include="ViewModel\ConnectionStatusViewModel.cs" />
<Compile Include="ViewModel\ConnectionDialogViewModel.cs" />
<Compile Include="ViewModel\ConnectionProfileViewModel.cs" />
<Compile Include="ViewModel\IViewModel.cs" />
Expand Down

0 comments on commit ed9e0db

Please sign in to comment.