namespace VideoBrowser.Controls.CefSharpBrowser.ViewModels
{
using CefSharp;
using System;
using System.Collections.Generic;
using System.Windows.Input;
using System.Windows.Media;
using VideoBrowser.Common;
using VideoBrowser.Controls.CefSharpBrowser.Helpers;
using VideoBrowser.Core;
using VideoBrowser.Extensions;
using VideoBrowser.ViewModels;
///
/// Defines the .
///
public class VideoBrowserViewModel : NotifyPropertyChanged, IDisposable
{
#region Fields
private ICommand _backwardCommand;
private bool _canBackward;
private bool _canForward;
private CefWindowData _cefWindowData;
private ICommand _forwardCommand;
private string _header = "New Tab";
private string _navigateUrl = "youtube.com";
private ICommand _reloadCommand;
private IWebBrowser _webBrowser;
#endregion Fields
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The globalBrowserData.
/// The windowData.
internal VideoBrowserViewModel(GlobalBrowserData globalBrowserData, CefWindowData windowData)
{
this.GlobalBrowserData = globalBrowserData;
this.CefWindowData = windowData;
// BackwardCommand and ForwardCommand are set by the View.
this.DownloadCommand = new RelayCommand(this.OnDownload, "Download", (o) => this.UrlReader.IsDownloadable);
this.HomeCommand = new RelayCommand(this.OnHome, "Home");
this.NavigateUrlCommand = new RelayCommand(this.OnNavigateUrl, "NavigateUrl");
this.OpenOutputFolderCommand = new RelayCommand(this.OnOpenOutputFolder, "Open output folder");
this.IndicatorColor = new SolidColorBrush(Colors.DarkBlue);
this.UrlEditor = new UrlEditorViewModel(this.UrlReader, this.GlobalBrowserData.Settings)
{
NavigateUrlCommand = this.NavigateUrlCommand,
ShowMessageAsyncAction = this.ShowMessageAsync
};
this.UrlEditor.PropertyChanged += this.OnUrlEditor_PropertyChanged;
this.PropertyChanged += this.OnPropertyChanged;
this.OnHome(null);
}
#endregion Constructors
#region Properties
///
/// Gets the AddInButtons.
///
public ICollection AddInButtons => this.GlobalBrowserData.AddInButtons;
///
/// Gets or sets the BackwardCommand.
///
public ICommand BackwardCommand { get => this._backwardCommand; set => this.Set(this.PropertyChangedHandler, ref this._backwardCommand, value); }
///
/// Gets or sets a value indicating whether CanBackward.
///
public bool CanBackward { get => this._canBackward; set => this.Set(this.PropertyChangedHandler, ref this._canBackward, value); }
///
/// Gets or sets a value indicating whether CanForward.
///
public bool CanForward { get => this._canForward; set => this.Set(this.PropertyChangedHandler, ref this._canForward, value); }
///
/// Gets or sets the CefWindowData.
///
public CefWindowData CefWindowData
{
get => _cefWindowData;
internal set
{
if (!this.Set(this.PropertyChangedHandler, ref _cefWindowData, value))
{
return;
};
this.InitializeHandlers();
}
}
///
/// Gets the DownloadCommand.
///
public ICommand DownloadCommand { get; }
///
/// Gets or sets the ForwardCommand.
///
public ICommand ForwardCommand { get => this._forwardCommand; set => this.Set(this.PropertyChangedHandler, ref this._forwardCommand, value); }
///
/// Gets the GlobalBrowserData.
///
public GlobalBrowserData GlobalBrowserData { get; }
///
/// Gets or sets the Header.
///
public string Header { get => _header; set => this.Set(this.PropertyChangedHandler, ref _header, value); }
///
/// Gets the HomeCommand.
///
public ICommand HomeCommand { get; }
///
/// Gets the IndicatorColor.
///
public Brush IndicatorColor { get; private set; }
///
/// Sets the IsSuccessful.
///
public bool? IsSuccessful
{
set
{
if (value == null)
IndicatorColor = new SolidColorBrush(Colors.LightBlue);
else if (value == false)
IndicatorColor = new SolidColorBrush(Colors.Red);
else
IndicatorColor = new SolidColorBrush(Colors.Green);
OnPropertyChanged(nameof(IndicatorColor));
}
}
///
/// Gets or sets the NavigateUrl.
/// The current valid Url that is currently opened,
/// it is set by Url property if the Return key is pressed or link is clicked...
///
public string NavigateUrl { get => this._navigateUrl; set => this.Set(this.PropertyChangedHandler, ref this._navigateUrl, value); }
///
/// Gets the NavigateUrlCommand.
///
public ICommand NavigateUrlCommand { get; }
///
/// Gets the OpenOutputFolderCommand.
///
public ICommand OpenOutputFolderCommand { get; }
///
/// Gets or sets the ReloadCommand.
///
public ICommand ReloadCommand { get => this._reloadCommand; set => this.Set(this.PropertyChangedHandler, ref this._reloadCommand, value); }
///
/// Gets or sets the Url.
/// It is typed in the TextBox.
///
public string Url { get => this.UrlEditor.Url; set => this.UrlEditor.Url = value; }
///
/// Gets the UrlEditor.
///
public UrlEditorViewModel UrlEditor { get; }
///
/// Gets the UrlReader.
///
public UrlReader UrlReader { get; } = new UrlReader();
///
/// Gets or sets the WebBrowser.
///
public IWebBrowser WebBrowser
{
get => _webBrowser;
set
{
if (this.WebBrowser == value)
{
return;
}
_webBrowser = value;
this.InitializeHandlers();
}
}
///
/// Gets or sets the DownloadAction.
///
internal Action DownloadAction { get => this.UrlEditor.DownloadAction; set => this.UrlEditor.DownloadAction = value; }
#endregion Properties
#region Methods
///
/// The Dispose.
///
public void Dispose()
{
this.UrlEditor.Dispose();
this.UrlEditor.PropertyChanged -= this.OnUrlEditor_PropertyChanged;
this.UrlReader.Dispose();
}
///
/// The InitializeHandlers.
///
private void InitializeHandlers()
{
if (this.WebBrowser != null)
{
this.WebBrowser.DownloadHandler = this.GlobalBrowserData.DownloadHandler;
this.WebBrowser.MenuHandler = this.CefWindowData.CefContextMenuHandler;
this.WebBrowser.RequestHandler = this.CefWindowData.CefRequestHandler;
}
}
///
/// The IsUrlValid.
///
/// The .
private bool IsUrlValid()
{
return true;
}
///
/// The OnDownload.
///
/// The obj.
private void OnDownload(object obj)
{
if (!this.IsUrlValid() || !this.UrlEditor.DownloadCommand.CanExecute(null))
{
return;
}
this.UrlEditor.DownloadCommand.Execute(null);
}
///
/// The OnHome.
///
/// The obj.
private void OnHome(object obj)
{
this.Url = AppEnvironment.HomeUrl;
this.NavigateUrl = this.Url;
}
///
/// The OnNavigateUrl called from Button.
///
/// The obj.
private void OnNavigateUrl(object obj)
{
this.Url = VideoBrowser.Helpers.UrlHelper.GetValidUrl(this.Url);
this.NavigateUrl = this.Url;
this.CefWindowData.IsAirspaceVisible = false;
}
///
/// The OnOpenOutputFolder.
///
/// The obj.
private void OnOpenOutputFolder(object obj)
{
ProcessHelper.Start(this.GlobalBrowserData.Settings.OutputFolder);
}
///
/// The OnPropertyChanged.
///
/// The sender.
/// The e.
private void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(this.Url):
this.UrlReader.Url = this.Url;
break;
case nameof(this.NavigateUrl):
this.Url = this.NavigateUrl;
this.UrlEditor.NavigateUrl = this.NavigateUrl;
break;
default:
break;
}
}
///
/// The OnUrlEditor_PropertyChanged.
///
/// The sender.
/// The e.
private void OnUrlEditor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.IsMatch(nameof(this.UrlEditor.Url)))
{
this.OnPropertyChanged(nameof(this.Url));
}
}
///
/// The ShowMessage.
///
/// The title.
/// The message.
private void ShowMessageAsync(string title, string message)
{
this.CefWindowData.ShowMessageAsync(title, message);
}
#endregion Methods
}
}