mirror of
https://github.com/Nioux/AideDeJeu.git
synced 2025-10-29 14:35:45 +00:00
Cancel speak
This commit is contained in:
parent
4ac5369383
commit
dc64a1f3da
7 changed files with 100 additions and 16 deletions
|
|
@ -200,6 +200,16 @@ namespace AideDeJeu.ViewModels
|
|||
}
|
||||
}
|
||||
|
||||
private SpeechViewModel _Speech = null;
|
||||
public SpeechViewModel Speech
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Speech ?? (_Speech = new SpeechViewModel());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -393,19 +393,5 @@ namespace AideDeJeu.ViewModels
|
|||
|
||||
}
|
||||
|
||||
private Command<Item> _SpeakItemCommand = null;
|
||||
public Command<Item> SpeakItemCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _SpeakItemCommand ?? (_SpeakItemCommand = new Command<Item>(async (item) => await ExecuteSpeakItemommandAsync(item)));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ExecuteSpeakItemommandAsync(Item item)
|
||||
{
|
||||
var md = item.Markdown;
|
||||
await Xamarin.Essentials.TextToSpeech.SpeakAsync(md);
|
||||
}
|
||||
}
|
||||
}
|
||||
77
AideDeJeu/AideDeJeu/ViewModels/SpeechViewModel.cs
Normal file
77
AideDeJeu/AideDeJeu/ViewModels/SpeechViewModel.cs
Normal file
|
|
@ -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<Item> _SpeakItemCommand = null;
|
||||
public Command<Item> SpeakItemCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return _SpeakItemCommand ?? (_SpeakItemCommand = new Command<Item>(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
<ContentPage.ToolbarItems>
|
||||
<ToolbarItem Name="AddToFavorites" Text="Ajouter aux favoris" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.AddToFavoritesCommand}" />
|
||||
<ToolbarItem Name="Print" Text="Générer un PDF" Order="Primary" Icon="scroll_unfurled.png" Command="{Binding Main.Navigator.GeneratePDFCommand}" CommandParameter="{Binding Item.Markdown}" />
|
||||
<ToolbarItem Name="Speak" Text="Écouter" Order="Secondary" Command="{Binding Main.Navigator.SpeakItemCommand}" CommandParameter="{Binding Item}" />
|
||||
<ToolbarItem Name="Speak" Text="Écouter / Arrêter" Order="Secondary" Command="{Binding Main.Speech.SpeakItemCommand}" CommandParameter="{Binding Item}" />
|
||||
</ContentPage.ToolbarItems>
|
||||
<Grid>
|
||||
<ScrollView Orientation="Vertical" BackgroundColor="{StaticResource HDWhite}">
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@
|
|||
<ContentPage.ToolbarItems>
|
||||
<ToolbarItem Name="AddToFavorites" Text="Ajouter aux favoris" Order="Primary" Icon="round_star.png" Command="{Binding Main.Navigator.AddToFavoritesCommand}" />
|
||||
<ToolbarItem Name="Print" Text="Générer un PDF" Order="Primary" Icon="scroll_unfurled.png" Command="{Binding Main.Navigator.GeneratePDFCommand}" CommandParameter="{Binding BindingContext.Items.Markdown, Source={x:Reference This}}" />
|
||||
<ToolbarItem Name="Speak" Text="Écouter" Order="Secondary" Command="{Binding Main.Navigator.SpeakItemCommand}" CommandParameter="{Binding BindingContext.Items, Source={x:Reference This}}" />
|
||||
<ToolbarItem Name="Speak" Text="Écouter / Arrêter" Order="Secondary" Command="{Binding Main.Speech.SpeakItemCommand}" CommandParameter="{Binding BindingContext.Items, Source={x:Reference This}}" />
|
||||
</ContentPage.ToolbarItems>
|
||||
<Grid>
|
||||
<ScrollView Orientation="Vertical">
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue