diff --git a/AideDeJeu/AideDeJeu/ViewModels/MainViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/MainViewModel.cs index 5c244c2b..d555bd50 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/MainViewModel.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/MainViewModel.cs @@ -200,6 +200,16 @@ namespace AideDeJeu.ViewModels } } + private SpeechViewModel _Speech = null; + public SpeechViewModel Speech + { + get + { + return _Speech ?? (_Speech = new SpeechViewModel()); + } + } + + public MainViewModel() { } diff --git a/AideDeJeu/AideDeJeu/ViewModels/Navigator.cs b/AideDeJeu/AideDeJeu/ViewModels/Navigator.cs index ccc5768c..a55726bc 100644 --- a/AideDeJeu/AideDeJeu/ViewModels/Navigator.cs +++ b/AideDeJeu/AideDeJeu/ViewModels/Navigator.cs @@ -393,19 +393,5 @@ namespace AideDeJeu.ViewModels } - private Command _SpeakItemCommand = null; - public Command SpeakItemCommand - { - get - { - return _SpeakItemCommand ?? (_SpeakItemCommand = new Command(async (item) => await ExecuteSpeakItemommandAsync(item))); - } - } - - public async Task ExecuteSpeakItemommandAsync(Item item) - { - var md = item.Markdown; - await Xamarin.Essentials.TextToSpeech.SpeakAsync(md); - } } } \ No newline at end of file diff --git a/AideDeJeu/AideDeJeu/ViewModels/SpeechViewModel.cs b/AideDeJeu/AideDeJeu/ViewModels/SpeechViewModel.cs new file mode 100644 index 00000000..b30f4a7b --- /dev/null +++ b/AideDeJeu/AideDeJeu/ViewModels/SpeechViewModel.cs @@ -0,0 +1,77 @@ +using AideDeJeuLib; +using System; +using System.Threading; +using System.Threading.Tasks; +using Xamarin.Forms; + +namespace AideDeJeu.ViewModels +{ + public class SpeechViewModel : BaseViewModel + { + private Command _SpeakItemCommand = null; + public Command SpeakItemCommand + { + get + { + return _SpeakItemCommand ?? (_SpeakItemCommand = new Command(async (item) => await ExecuteSpeakItemCommandAsync(item))); + } + } + + private Command _CancelSpeakCommand = null; + public Command CancelSpeakCommand + { + get + { + return _CancelSpeakCommand ?? (_CancelSpeakCommand = new Command(() => ExecuteCancelSpeakCommand())); + } + } + + public bool Speaking + { + get + { + return _CancellationTokenSource != null; + } + } + public bool NotSpeaking + { + get + { + return _CancellationTokenSource == null; + } + } + private CancellationTokenSource _CancellationTokenSource = null; + public async Task ExecuteSpeakItemCommandAsync(Item item) + { + if(Speaking) + { + ExecuteCancelSpeakCommand(); + return; + } + var md = item.Markdown; + try + { + _CancellationTokenSource = new CancellationTokenSource(); + OnPropertyChanged(nameof(Speaking)); + OnPropertyChanged(nameof(NotSpeaking)); + await Xamarin.Essentials.TextToSpeech.SpeakAsync(md, _CancellationTokenSource.Token); + } + catch(Exception ex) + { + + } + finally + { + _CancellationTokenSource?.Dispose(); + _CancellationTokenSource = null; + OnPropertyChanged(nameof(Speaking)); + OnPropertyChanged(nameof(NotSpeaking)); + } + } + + public void ExecuteCancelSpeakCommand() + { + _CancellationTokenSource?.Cancel(); + } + } +} diff --git a/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml b/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml index efab44db..c5490792 100644 --- a/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml +++ b/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml @@ -16,7 +16,7 @@ - + diff --git a/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml.cs b/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml.cs index 1a996906..d2f42edd 100644 --- a/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml.cs +++ b/AideDeJeu/AideDeJeu/Views/Library/ItemDetailPage.xaml.cs @@ -60,6 +60,12 @@ namespace AideDeJeu.Views.Library //BindingContext = viewModel; } + protected override void OnDisappearing() + { + base.OnDisappearing(); + Main.Speech.ExecuteCancelSpeakCommand(); + } + async Task InitDBEngineAsync() { await Task.Delay(1000).ConfigureAwait(false); diff --git a/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml b/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml index 414293f5..a19f7db4 100644 --- a/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml +++ b/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml @@ -19,7 +19,7 @@ - + diff --git a/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml.cs b/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml.cs index 5667ce8c..7842ea77 100644 --- a/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml.cs +++ b/AideDeJeu/AideDeJeu/Views/Library/ItemsPage.xaml.cs @@ -46,5 +46,10 @@ namespace AideDeJeu.Views.Library if (e.Item == null) return; ((ListView)sender).SelectedItem = null; } + protected override void OnDisappearing() + { + base.OnDisappearing(); + Main.Speech.ExecuteCancelSpeakCommand(); + } } } \ No newline at end of file