Skip to content

Commit

Permalink
Refactor a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
taurit committed Aug 1, 2024
1 parent ca43523 commit 97e847d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
9 changes: 1 addition & 8 deletions AnkiStoryGenerator/AnkiStoryGenerator/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<ComboBox
Name="Genre"
Margin="0,10,0,0"
SelectionChanged="Genre_OnSelectionChanged"
Text="{Binding Genre, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem IsSelected="True">crime</ComboBoxItem>
<ComboBoxItem>fantasy</ComboBoxItem>
Expand All @@ -93,14 +94,6 @@
<ComboBoxItem>comedy</ComboBoxItem>
</ComboBox>

<Button
Name="LoadFlashcards"
Margin="0,10,0,0"
Padding="5"
Click="LoadFlashcards_OnClick">
Load flashcards and prepare prompt
</Button>

</StackPanel>
<DockPanel
Grid.Row="1"
Expand Down
34 changes: 20 additions & 14 deletions AnkiStoryGenerator/AnkiStoryGenerator/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace AnkiStoryGenerator;

Expand All @@ -19,12 +20,12 @@ namespace AnkiStoryGenerator;
public partial class MainWindow : Window
{
private readonly MainWindowViewModel _viewModel = new();
private string? _latestStoryHtml;

public MainWindow()
{
DataContext = _viewModel;
InitializeComponent();
LoadFlashcards();
}

private static async Task SetPreviewWindowHtml(WebView2 webViewControl, string htmlContentToSet)
Expand All @@ -40,7 +41,7 @@ private static async Task SetPreviewWindowHtml(WebView2 webViewControl, string h
webViewControl.NavigateToString(htmlContentToSet);
}

private async void LoadFlashcards_OnClick(object sender, RoutedEventArgs e)
private async void LoadFlashcards()
{
var flashcards =
AnkiHelpers.GetRecentlyReviewedCardsFromSpecificDeck(Settings.AnkiDatabaseFilePath, _viewModel.DeckName, _viewModel.NumRecentFlashcardsToUse);
Expand All @@ -59,15 +60,24 @@ private async void LoadFlashcards_OnClick(object sender, RoutedEventArgs e)

private async void GenerateStory_OnClick(object sender, RoutedEventArgs e)
{
// generate story
var story = await GenerateStoryUsingChatGptApi();

this._latestStoryHtml = story.OriginalHtml;
this._viewModel.LatestStoryHtml = story.OriginalHtml;

var storyHtmlWithTooltips = TooltipsHelper.AddInteractiveTooltipsMarkupToTheStory(story.OriginalHtml, _viewModel.Flashcards);
var translationHtmlWithTooltips = TooltipsHelper.AddInteractiveTooltipsMarkupToTheStory(story.TranslatedHtml, _viewModel.Flashcards);

await SetPreviewWindowHtml(WebViewControlOriginal, storyHtmlWithTooltips);
await SetPreviewWindowHtml(WebViewControlTranslation, translationHtmlWithTooltips);

// generate audio

if (!File.Exists(_viewModel.LatestStoryAudioFileName))
{
var ttsAudio = await TextToSpeechHelpers.SynthesizeTextToSpeech(_viewModel.LatestStoryPlainText);
await File.WriteAllBytesAsync(_viewModel.LatestStoryAudioFileName, ttsAudio);
}
}

private async Task<GeneratedStory> GenerateStoryUsingChatGptApi()
Expand Down Expand Up @@ -111,27 +121,23 @@ private async Task<GeneratedStory> GenerateStoryUsingChatGptApi()

private async void PlayStory_OnClick(object sender, RoutedEventArgs e)

Check warning on line 122 in AnkiStoryGenerator/AnkiStoryGenerator/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 122 in AnkiStoryGenerator/AnkiStoryGenerator/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 122 in AnkiStoryGenerator/AnkiStoryGenerator/MainWindow.xaml.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (this._latestStoryHtml is null)
if (this._viewModel.LatestStoryHtml is null)
{
MessageBox.Show("Please generate a story first.");
return;
}

var storyInPlainText = HtmlHelpers.ConvertToPlainText(this._latestStoryHtml);

var cacheFileName = Path.Combine(Settings.AudioFilesCacheDirectory, storyInPlainText.GetHashCodeStable() + ".mp3");
if (!File.Exists(cacheFileName))
{
var ttsAudio = await TextToSpeechHelpers.SynthesizeTextToSpeech(storyInPlainText);
await File.WriteAllBytesAsync(cacheFileName, ttsAudio);
}

var startInfo = new ProcessStartInfo(cacheFileName) { UseShellExecute = true };
var startInfo = new ProcessStartInfo(_viewModel.LatestStoryAudioFileName) { UseShellExecute = true };
Process.Start(startInfo);
}

private void PublishToRssFeed_OnClick(object sender, RoutedEventArgs e)
{
new RssHelper().CreateFeed();
}

private void Genre_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
LoadFlashcards();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ public static class HtmlHelpers
/// </summary>
/// <param name="html">The HTML.</param>
/// <returns></returns>
public static string ConvertToPlainText(string html)
public static string ConvertToPlainText(string? html)
{
if (html is null)
{
return string.Empty;
}

HtmlDocument doc = new();
doc.LoadHtml(html);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using PropertyChanged;
using AnkiStoryGenerator.Utilities;
using PropertyChanged;
using System.Collections.ObjectModel;
using System.IO;

namespace AnkiStoryGenerator.ViewModels;

Expand All @@ -16,6 +18,14 @@ public class MainWindowViewModel

public ObservableCollection<FlashcardViewModel> Flashcards { get; set; } = [];
public string? ChatGptPrompt { get; set; }

public string? LatestStoryHtml { get; set; }

[DependsOn(nameof(LatestStoryHtml))]
public string LatestStoryPlainText => HtmlHelpers.ConvertToPlainText(LatestStoryHtml);

[DependsOn(nameof(LatestStoryPlainText))]
public string LatestStoryAudioFileName => Path.Combine(Settings.AudioFilesCacheDirectory, LatestStoryPlainText.GetHashCodeStable() + ".mp3");
}

[AddINotifyPropertyChangedInterface]
Expand Down

0 comments on commit 97e847d

Please sign in to comment.