-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -72,6 +76,7 @@ public CollectionSelectionWindow(StartupCfg startupCfg) | |
lbCollections.SelectedIndex = 0; | ||
|
||
Loaded += CollectionSelectionWindow_Loaded; | ||
Loaded += ShowQuoteOfTheDay; | ||
} | ||
|
||
#endregion | ||
|
@@ -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(".") | ||
&& !SplitQuoteLine[0].EndsWith("!") | ||
&& !SplitQuoteLine[0].EndsWith("?")) | ||
{ | ||
SplitQuoteLine[0] += "."; | ||
} | ||
|
||
QuoteBodyTextBlock.Text = "\"" + SplitQuoteLine[0] + "\""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
QuoteAuthorTextBlock.Text = SplitQuoteLine[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That and subsequent array accesses [2] [3] will throw an exception if 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 | ||
} | ||
} |
There was a problem hiding this comment.
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: