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 all commits
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
32 changes: 32 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,37 @@
</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 VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="13">
<Hyperlink Name="TitleHyperlink"
RequestNavigate="TitleLink_RequestNavigate">
<TextBlock Name="QuoteTitleTextBlock">
Pleasure of Learning
</TextBlock>
</Hyperlink>
</TextBlock>
</StackPanel>
</Grid>

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

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

Loaded += CollectionSelectionWindow_Loaded;
Loaded += ShowQuoteOfTheDay;
}

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

private void TitleLink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}

private void ShowQuoteOfTheDay(object sender, RoutedEventArgs e)
{

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

if (QuoteFile.Exists())
{
try
{
var Lines = File.ReadAllLines(QuoteFile.FullPath);

// Line 1 is .tsv heading
if (Lines.Length <= 1)
{
return;
}

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

// Tab separated file
// Field 0: Quote
// Field 1: Author
// Field 2: Url
// Field 3: Title
if (!SplitQuoteLine[0].EndsWith(".")
Copy link
Member

@alexis- alexis- Feb 16, 2020

Choose a reason for hiding this comment

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

Just nitpicking, but you could implement the condition this way to make the code more easy to read and maintain:

private static readonly string[] SentenceEndingPunctuation = new[] { ".", "!", "?" };
[...]
if (SentenceEndingPunctuation[0].None(p => SplitLineQuoteLine.EndsWith(p)))

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

QuoteBodyTextBlock.Text = "\"" + SplitQuoteLine[0] + "\"";
Copy link
Member

Choose a reason for hiding this comment

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

FYI, there is a neat feature in C# called string interpolation. It's not very useful in this case, but often it will make it easier to read strings with embedded variables. Either:

  • $"\"{SplitQuoteLine[0]}\""
  • $@"""{SplitQuoteLine[0]}""" (double "" equals escaped ")

QuoteAuthorTextBlock.Text = SplitQuoteLine[1];
Copy link
Member

Choose a reason for hiding this comment

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

That and subsequent array accesses [2] [3] will throw an exception if QuoteLine isn't correctly formatted (i.e. if it isn't a \t separated line as you expect).

It is very important to always assume your inputs are going to be somehow faulty (or even malicious), and to add the necessary code to deal with that.

TitleHyperlink.NavigateUri = new Uri(SplitQuoteLine[2]);
QuoteTitleTextBlock.Text = SplitQuoteLine[3];
}
catch (IOException ex)
{
LogTo.Warning(ex, $"IOException when trying to open {QuoteFile.FullPath}");
}
catch (Exception ex)
{
LogTo.Error(ex, $"Exception caught while opening file {QuoteFile.FullPath}");
}
}
}
#endregion
}
}