Speech, auto avoid, bug fixes, iu improments
This commit is contained in:
parent
bc45154b70
commit
bb125e7c7d
8 changed files with 164 additions and 22 deletions
|
@ -6,7 +6,7 @@
|
|||
@inject Blazored.LocalStorage.ILocalStorageService localStorage
|
||||
@inject HttpClient httpClient
|
||||
@inject ISnackbar Snackbar
|
||||
|
||||
@inject SpeechSynthesis SpeechSynthesis
|
||||
|
||||
<style>
|
||||
.LargeCharecter{
|
||||
|
@ -27,9 +27,13 @@
|
|||
<MudContainer Class="justify-center d-flex ma-0" Style="height: 100%;">
|
||||
<MudStack Class="d-flex">
|
||||
<MudStack Row=true Class="pt-2 d-flex justify-center">
|
||||
<MudButton Color="Color.Default" OnClick="RemoveCCharFromSelection" Variant="Variant.Outlined">Avoid</MudButton>
|
||||
@if (!Answers.Any(x => x == null))
|
||||
{
|
||||
@*Color="@(selectedCorrect ? Color.Success : Color.Error)"*@
|
||||
<MudButton Disabled="@(!(Answers.Any(x=>x.isSelected)))" OnClick="()=>RemoveCCharFromSelection()" Variant="Variant.Outlined">Avoid</MudButton>
|
||||
}
|
||||
<MudButton Color="Color.Default" Variant="Variant.Outlined" OnClick="ShowMeaning">Meaning</MudButton>
|
||||
<MudButton Color="Color.Default" Variant="Variant.Outlined" OnClick="ShowPinyin">Pinyin</MudButton>
|
||||
<MudButton Color="Color.Default" Variant="Variant.Outlined" OnClick="SayCorrectChar">Speech</MudButton>
|
||||
</MudStack>
|
||||
<MudContainer>
|
||||
<MudPaper Class="pa-16 ma-2 rounded-xl mud-dark" Elevation="1">
|
||||
|
@ -71,13 +75,11 @@
|
|||
</MudStack>
|
||||
</MudContainer>
|
||||
|
||||
<MudOverlay @bind-Visible="ShowOverlay" DarkBackground="true" AutoClose="true" />
|
||||
|
||||
@code {
|
||||
public bool ShowOverlay = false;
|
||||
|
||||
bool isSavedLocally = false;
|
||||
|
||||
bool selectedCorrect = false;
|
||||
|
||||
public Answer[] Answers = new Answer[4];
|
||||
public void SelectButton(int selectedIndex)
|
||||
{
|
||||
|
@ -85,6 +87,8 @@
|
|||
{
|
||||
Answers[i].isSelected = (i == selectedIndex);
|
||||
}
|
||||
|
||||
selectedCorrect = Answers[selectedIndex].isCorrect;
|
||||
}
|
||||
|
||||
private CChar[]? _charecters;
|
||||
|
@ -99,7 +103,8 @@
|
|||
set { _charecters = value; }
|
||||
}
|
||||
|
||||
public List<CChar>? DontSkipTheseCChar;
|
||||
|
||||
public List<CCharStats>? DontSkipTheseCChar;
|
||||
|
||||
private async Task<string> GetFileContentsAsync(string filePath)
|
||||
{
|
||||
|
@ -123,7 +128,7 @@
|
|||
Charecters = JsonConvert.DeserializeObject<CChar[]>(json);
|
||||
}
|
||||
|
||||
DontSkipTheseCChar = Charecters.ToList();
|
||||
DontSkipTheseCChar = Charecters.Select(x=>new CCharStats(x)).ToList();
|
||||
Program.CCharsLeft = DontSkipTheseCChar.Count;
|
||||
Program.InvokeUiUpdate();
|
||||
|
||||
|
@ -148,11 +153,21 @@
|
|||
CChar randomCChar;
|
||||
|
||||
repickRandomCChar:
|
||||
randomCChar = DontSkipTheseCChar[Random.Shared.Next(0, DontSkipTheseCChar.Count)];
|
||||
randomCChar = DontSkipTheseCChar[Random.Shared.Next(0, DontSkipTheseCChar.Count)].cchar;
|
||||
if (Answers.Any(x =>x != null && x.cchar == randomCChar)) goto repickRandomCChar;
|
||||
|
||||
Answers[i] = new Answer(randomCChar, isCorrect);
|
||||
}
|
||||
|
||||
string correctPinyin = GetCorrectCChar().pinyin;
|
||||
|
||||
for (int i = 0; i < Answers.Length; i++)
|
||||
{
|
||||
if (Answers[i].isCorrect) continue;
|
||||
if (Answers[i].cchar.pinyin != correctPinyin) continue;
|
||||
|
||||
Answers[i].cchar.pinyin += "_";
|
||||
}
|
||||
}
|
||||
|
||||
void FinishAndKickBackToLearn()
|
||||
|
@ -171,11 +186,13 @@
|
|||
async void Submit()
|
||||
{
|
||||
bool isCorrect = Answers.Any(x => x.isCorrect && x.isSelected);
|
||||
CChar correctCChar = GetCorrectCChar();
|
||||
CCharStats correctCCharStats = GetCorrectCCharStats();
|
||||
CChar correctCChar = correctCCharStats.cchar;
|
||||
|
||||
if (isCorrect)
|
||||
{
|
||||
Snackbar.Add($"<b>Definition:</b> {correctCChar.definition}", Severity.Success, config => { config.VisibleStateDuration = 1000; });
|
||||
increaseCCharStat(GetCorrectCCharStats(), StatType.NumOfCorrects);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -187,8 +204,19 @@
|
|||
<li>Correct answer: @correctCChar.pinyin.ToTitleCase()</li>
|
||||
<li>Meaning: @correctCChar.definition</li>
|
||||
</ul>
|
||||
</div>
|
||||
, Severity.Error);
|
||||
</div>
|
||||
, Severity.Error);
|
||||
increaseCCharStat(GetCorrectCCharStats(), StatType.NumOfWrongs);
|
||||
}
|
||||
|
||||
if (correctCCharStats.TotalAnswers >= 3)
|
||||
{
|
||||
if (correctCCharStats.Accuracy > 0.7f)
|
||||
{
|
||||
RemoveCCharFromSelection(true);
|
||||
|
||||
Snackbar.Add($"CChar '{correctCChar.charcter}' compleated!", Severity.Success);
|
||||
}
|
||||
}
|
||||
|
||||
GenerateQuestion();
|
||||
|
@ -224,10 +252,43 @@
|
|||
return cchar;
|
||||
}
|
||||
|
||||
public void RemoveCCharFromSelection()
|
||||
public CCharStats GetCorrectCCharStats()
|
||||
{
|
||||
CChar correctChar = GetCorrectCChar();
|
||||
DontSkipTheseCChar.Remove(correctChar);
|
||||
CCharStats correctCCharStats = DontSkipTheseCChar.First(x => x.cchar == correctChar);
|
||||
return correctCCharStats;
|
||||
}
|
||||
public void increaseCCharStat(CCharStats stats, StatType statType)
|
||||
{
|
||||
int correctStatsIndex = DontSkipTheseCChar.IndexOf(stats);
|
||||
switch (statType)
|
||||
{
|
||||
case StatType.NumOfCorrects:
|
||||
DontSkipTheseCChar[correctStatsIndex].NumOfCorrects++;
|
||||
break;
|
||||
|
||||
case StatType.NumOfWrongs:
|
||||
DontSkipTheseCChar[correctStatsIndex].NumOfWrongs++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveCCharFromSelection(bool ignoreSelection = false)
|
||||
{
|
||||
CCharStats correctCCharStats = GetCorrectCCharStats();
|
||||
|
||||
|
||||
if (!selectedCorrect && !ignoreSelection)
|
||||
{
|
||||
Snackbar.Add("Selected is wrong. Try again!", Severity.Error);
|
||||
//return;
|
||||
}
|
||||
else if (!ignoreSelection)// Hacky way to only display this snackbar when is manually removed
|
||||
{
|
||||
Snackbar.Add($"Removed '{correctCCharStats.cchar.charcter}' from selection", Severity.Info, config => config.VisibleStateDuration = 3000);
|
||||
}
|
||||
|
||||
DontSkipTheseCChar.Remove(correctCCharStats);
|
||||
Console.WriteLine("Remaining CChars: " + DontSkipTheseCChar);
|
||||
Program.CCharsLeft = DontSkipTheseCChar.Count;
|
||||
Program.InvokeUiUpdate();
|
||||
|
@ -243,4 +304,34 @@
|
|||
{
|
||||
Snackbar.Add($"<b>Pinyin:</b> {GetCorrectCChar().pinyin.ToTitleCase()}", Severity.Info);
|
||||
}
|
||||
|
||||
SpeechSynthesisVoice? SpeechVoice;
|
||||
|
||||
protected async override Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
// Gets chinese voice on windows or iphone.
|
||||
this.SpeechVoice = ((IEnumerable<SpeechSynthesisVoice>)(await this.SpeechSynthesis.GetVoicesAsync())).FirstOrDefault(v => v.Name.Contains("Yaoyao") || v.Name.Contains("Ting-Ting"));
|
||||
this.StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
async Task SayCorrectChar()
|
||||
{
|
||||
if (SpeechVoice == null)
|
||||
{
|
||||
Snackbar.Add("Couldn't play sound", Severity.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
var utterancet = new SpeechSynthesisUtterance
|
||||
{
|
||||
Text = this.GetCorrectCChar().charcter.ToString(),
|
||||
Voice = this.SpeechVoice,
|
||||
Rate = 0.75f
|
||||
};
|
||||
|
||||
await this.SpeechSynthesis.SpeakAsync(utterancet); // 👈 Speak with "Haruka"'s voice!
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue