Skip to content

Commit

Permalink
[CastIt] Added a vm to change the server url
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jun 26, 2021
1 parent 0986534 commit 79e6ef5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 5 deletions.
9 changes: 4 additions & 5 deletions CastIt/ViewModels/Dialogs/AboutDialogViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CastIt.Interfaces;
using CastIt.Server.Interfaces;
using Microsoft.Extensions.Logging;
using MvvmCross.Commands;
using MvvmCross.Navigation;
Expand All @@ -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<AboutDialogViewModel> logger,
IMvxNavigationService navigationService,
IAppWebServer appWebServer)
ICastItHubClientService castItHubClientService)
: base(textProvider, messenger, logger)
{
_navigationService = navigationService;
_appWebServer = appWebServer;
_castItHubClientService = castItHubClientService;
}

public override void Prepare()
Expand Down
101 changes: 101 additions & 0 deletions CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs
Original file line number Diff line number Diff line change
@@ -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<bool>
{
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<string> SaveUrlCommand { get; private set; }

public ChangeServerUrlDialogViewModel(
ITextProvider textProvider,
IMvxMessenger messenger,
ILogger<ChangeServerUrlDialogViewModel> 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<string>(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;
}
}
}
}

0 comments on commit 79e6ef5

Please sign in to comment.