From 79e6ef518515162a61580fe079ddad9aecc825ef Mon Sep 17 00:00:00 2001 From: Efrain Date: Fri, 25 Jun 2021 23:43:44 -0500 Subject: [PATCH] [CastIt] Added a vm to change the server url --- .../Dialogs/AboutDialogViewModel.cs | 9 +- .../Dialogs/ChangeServerUrlDialogViewModel.cs | 101 ++++++++++++++++++ 2 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs diff --git a/CastIt/ViewModels/Dialogs/AboutDialogViewModel.cs b/CastIt/ViewModels/Dialogs/AboutDialogViewModel.cs index 746c3fc2..04322c2b 100644 --- a/CastIt/ViewModels/Dialogs/AboutDialogViewModel.cs +++ b/CastIt/ViewModels/Dialogs/AboutDialogViewModel.cs @@ -1,5 +1,4 @@ using CastIt.Interfaces; -using CastIt.Server.Interfaces; using Microsoft.Extensions.Logging; using MvvmCross.Commands; using MvvmCross.Navigation; @@ -10,21 +9,21 @@ namespace CastIt.ViewModels.Dialogs public class AboutDialogViewModel : BaseDialogViewModel { private readonly IMvxNavigationService _navigationService; - private readonly IAppWebServer _appWebServer; + private readonly ICastItHubClientService _castItHubClientService; public string CastItServerUrl - => GetText("ServerUrl", _appWebServer.BaseUrl); + => GetText("ServerUrl", _castItHubClientService.IpAddress ?? "N/A"); public AboutDialogViewModel( ITextProvider textProvider, IMvxMessenger messenger, ILogger logger, IMvxNavigationService navigationService, - IAppWebServer appWebServer) + ICastItHubClientService castItHubClientService) : base(textProvider, messenger, logger) { _navigationService = navigationService; - _appWebServer = appWebServer; + _castItHubClientService = castItHubClientService; } public override void Prepare() diff --git a/CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs b/CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs new file mode 100644 index 00000000..0a46fa36 --- /dev/null +++ b/CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs @@ -0,0 +1,101 @@ +using CastIt.Interfaces; +using Microsoft.Extensions.Logging; +using MvvmCross.Commands; +using MvvmCross.Navigation; +using MvvmCross.Plugin.Messenger; +using System; +using System.Threading.Tasks; + +namespace CastIt.ViewModels.Dialogs +{ + public class ChangeServerUrlDialogViewModel : BaseDialogViewModelResult + { + private readonly IMvxNavigationService _navigationService; + private readonly ICastItHubClientService _castItHub; + + private bool _isBusy; + private string _newServerIpAddress; + + public bool IsBusy + { + get => _isBusy; + set => SetProperty(ref _isBusy, value); + } + + public bool IsNewServerIpAddressValid + => !string.IsNullOrWhiteSpace(NewServerIpAddress) && Uri.TryCreate(NewServerIpAddress, UriKind.Absolute, out _); + + public string NewServerIpAddress + { + get => _newServerIpAddress; + set + { + SetProperty(ref _newServerIpAddress, value); + RaisePropertyChanged(() => IsNewServerIpAddressValid); + } + } + + public string CurrentServerIpAddress + => _castItHub.IpAddress; + + public IMvxAsyncCommand SaveUrlCommand { get; private set; } + + public ChangeServerUrlDialogViewModel( + ITextProvider textProvider, + IMvxMessenger messenger, + ILogger logger, + IMvxNavigationService navigationService, + ICastItHubClientService castItHub) + : base(textProvider, messenger, logger) + { + _castItHub = castItHub; + _navigationService = navigationService; + } + + public override Task Initialize() + { + Title = GetText("ChangeServerUrl"); + NewServerIpAddress = CurrentServerIpAddress; + return base.Initialize(); + } + + public override void SetCommands() + { + base.SetCommands(); + + SaveUrlCommand = new MvxAsyncCommand(ChangeServerUrl); + + CloseCommand = new MvxAsyncCommand(async () => await _navigationService.Close(this, false)); + } + + private async Task ChangeServerUrl(string url) + { + if (!IsNewServerIpAddressValid) + { + return; + } + + IsBusy = true; + ContentText = null; + try + { + bool connected = await _castItHub.Init(url).ConfigureAwait(false); + if (!connected) + { + ContentText = GetText("ConnectionCouldNotBeEstablished"); + return; + } + + await _navigationService.Close(this, true); + } + catch (Exception e) + { + Logger.LogError(e, $"{nameof(ChangeServerUrl)}: Unknown error"); + } + finally + { + IsBusy = false; + } + } + } +}