Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds random quotes to the collection selection menu #68

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/AppHosts/SuperMemoAssistant/CollectionSelectionWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="130" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>

Expand Down Expand Up @@ -91,6 +92,33 @@
</ListBox>

<Grid Grid.Row="1">
<StackPanel>
<TextBlock Name="QuoteBodyTextBlock"
VerticalAlignment="Center"
Margin="3 5 3 5"
HorizontalAlignment="Center"
FontSize="15.5"
TextWrapping="Wrap"
FontStyle="Italic"
>
&quot;Good learning is inherently pleasurable, and without pleasure there is no good learning.&quot;
</TextBlock>
<TextBlock Name="QuoteAuthorTextBlock"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13">
Piotr Wozniak
</TextBlock>
<TextBlock Name="QuoteTitleTextBlock"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13">
Pleasure of Learning
</TextBlock>
</StackPanel>
</Grid>

<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
Expand Down
32 changes: 32 additions & 0 deletions src/AppHosts/SuperMemoAssistant/CollectionSelectionWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
using SuperMemoAssistant.SMA.Configs;
using SuperMemoAssistant.Sys.IO;
using SuperMemoAssistant.Sys.Windows.Input;
using SuperMemoAssistant.Interop;

namespace SuperMemoAssistant
{
Expand Down Expand Up @@ -72,6 +73,7 @@ public CollectionSelectionWindow(StartupCfg startupCfg)
lbCollections.SelectedIndex = 0;

Loaded += CollectionSelectionWindow_Loaded;
Loaded += ShowQuoteOfTheDay;
}

#endregion
Expand Down Expand Up @@ -244,6 +246,36 @@ private void CollectionSelectionWindow_Loaded(object sender,
lbCollections.SelectFirstItem();
}

private void ShowQuoteOfTheDay(object sender, RoutedEventArgs e)
{

var QuoteFile = SMAFileSystem.AppRootDir.CombineFile("quotes.csv");

if (QuoteFile.Exists())
{
var Lines = File.ReadAllLines(QuoteFile.FullPath);
Copy link
Member

@alexis- alexis- Feb 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several scenarios where File.ReadAllLines could throw an exception, can you please:

  • handle FileIOException with a LogTo.Warning,
  • handle Exception (fallback) with a LogTo.Error (this will file a bug report as well as log to file)
  • let the try... catch encompass the whole if code branch (under the if), for safety
  • gracefully continue the code if an exception is thrown (don't rethrow or exit)

var RandInt = new Random();
var RandomLineNumber = RandInt.Next(0, Lines.Length - 1);
var QuoteLine = Lines[RandomLineNumber];
var SplitQuoteLine = QuoteLine.Split('|');

// Pipe | separated file.
// Field 0 = quote text
// Field 1 = quote author
// Field 2 = quote URL
// Field 3 = quote title
if (!SplitQuoteLine[0].EndsWith(".")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause an error if the file is empty

&& !SplitQuoteLine[0].EndsWith("!")
&& !SplitQuoteLine[0].EndsWith("?"))
{
SplitQuoteLine[0] += ".";
}

QuoteBodyTextBlock.Text = "\"" + SplitQuoteLine[0] + "\"";
QuoteAuthorTextBlock.Text = SplitQuoteLine[1];
QuoteTitleTextBlock.Text = SplitQuoteLine[3];
}
}
#endregion
}
}