using Windows.System;
using Microsoft.Web.WebView2.Core;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace TestProject;
/// <summary>
/// 웹 뷰 모델
/// </summary>
public partial class WebViewModel : ObservableRecipient, INavigationAware
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Prvate
#region Field
/// <summary>
/// 소스
/// </summary>
[ObservableProperty]
private Uri source = new("https://docs.microsoft.com/windows/apps/");
/// <summary>
/// 로딩 여부
/// </summary>
[ObservableProperty]
private bool isLoading = true;
/// <summary>
/// 실패 여부
/// </summary>
[ObservableProperty]
private bool hasFailures;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 웹 뷰 서비스 - WebViewService
/// <summary>
/// 웹 뷰 서비스
/// </summary>
public IWebViewService WebViewService { get; }
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - WebViewModel(webViewService)
/// <summary>
/// 생성자
/// </summary>
/// <param name="webViewService">웹 뷰 서비스</param>
public WebViewModel(IWebViewService webViewService)
{
WebViewService = webViewService;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 탐색 진입시 처리하기 - OnNavigatedTo(parameter)
/// <summary>
/// 탐색 진입시 처리하기
/// </summary>
/// <param name="parameter">매개 변수</param>
public void OnNavigatedTo(object parameter)
{
WebViewService.NavigationCompleted += WebViewService_NavigationCompleted;
}
#endregion
#region 탐색 이탈시 처리하기 - OnNavigatedFrom()
/// <summary>
/// 탐색 이탈시 처리하기
/// </summary>
public void OnNavigatedFrom()
{
WebViewService.UnregisterEvent();
WebViewService.NavigationCompleted -= WebViewService_NavigationCompleted;
}
#endregion
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 웹 뷰 서비스 탐색 완료시 처리하기 - WebViewService_NavigationCompleted(sender, status)
/// <summary>
/// 웹 뷰 서비스 탐색 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="status">코어 웹 뷰 2 웹 에러 상태</param>
private void WebViewService_NavigationCompleted(object sender, CoreWebView2WebErrorStatus status)
{
IsLoading = false;
BrowserBackwardCommand.NotifyCanExecuteChanged();
BrowserForwardCommand.NotifyCanExecuteChanged();
if(status != default)
{
HasFailures = true;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 브라우저에서 열기 - OpenInBrowser()
/// <summary>
/// 브라우저에서 열기
/// </summary>
/// <returns>태스크</returns>
[RelayCommand]
private async Task OpenInBrowser()
{
if(WebViewService.Source != null)
{
await Launcher.LaunchUriAsync(WebViewService.Source);
}
}
#endregion
#region 다시 로드하기 - Reload()
/// <summary>
/// 다시 로드하기
/// </summary>
[RelayCommand]
private void Reload()
{
WebViewService.Reload();
}
#endregion
#region 브라우저 앞으로 가기 가능 여부 구하기 - BrowserCanGoForward()
/// <summary>
/// 브라우저 앞으로 가기 가능 여부 구하기
/// </summary>
/// <returns>브라우저 앞으로 가기 가능 여부</returns>
private bool BrowserCanGoForward()
{
return WebViewService.CanGoForward;
}
#endregion
#region 브라우저 앞으로 가기 - BrowserForward()
/// <summary>
/// 브라우저 앞으로 가기
/// </summary>
[RelayCommand(CanExecute = nameof(BrowserCanGoForward))]
private void BrowserForward()
{
if(WebViewService.CanGoForward)
{
WebViewService.GoForward();
}
}
#endregion
#region 브라우저 뒤로 가기 가능 여부 구하기 - BrowserCanGoBackward()
/// <summary>
/// 브라우저 뒤로 가기 가능 여부 구하기
/// </summary>
/// <returns>브라우저 뒤로 가기 가능 여부</returns>
private bool BrowserCanGoBackward()
{
return WebViewService.CanGoBack;
}
#endregion
#region 브라우저 뒤로 가기 - BrowserBackward()
/// <summary>
/// 브라우저 뒤로 가기
/// </summary>
[RelayCommand(CanExecute = nameof(BrowserCanGoBackward))]
private void BrowserBackward()
{
if(WebViewService.CanGoBack)
{
WebViewService.GoBack();
}
}
#endregion
#region 재시도시 처리하기 - OnRetry()
/// <summary>
/// 재시도시 처리하기
/// </summary>
[RelayCommand]
private void OnRetry()
{
HasFailures = false;
IsLoading = true;
WebViewService?.Reload();
}
#endregion
}