-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CastIt] Added a vm to change the server url
- Loading branch information
Showing
2 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
CastIt/ViewModels/Dialogs/ChangeServerUrlDialogViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |