Skip to content

Commit

Permalink
Option to share books without practice results
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed May 6, 2024
1 parent b257210 commit 5b8c8fb
Show file tree
Hide file tree
Showing 17 changed files with 856 additions and 595 deletions.
6 changes: 3 additions & 3 deletions src/Vocup/IO/BookFileFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ private static bool StartsWithZipHeader(Stream stream)
return zipHeader;
}

public bool TryWrite(string path, VocabularyBook book, string vhrPath)
public bool TryWrite(string path, VocabularyBook book, string vhrPath, bool includeResults = true)
{
try
{
//await default(HopToThreadPoolAwaitable); // Perform IO operations on a separate thread

using FileStream stream = new(path, FileMode.Create, FileAccess.Write, FileShare.None);
Write(stream, book, vhrPath);
Write(stream, book, vhrPath, includeResults);
return true;
}
catch (Exception ex)
Expand All @@ -90,7 +90,7 @@ public bool TryWrite(string path, VocabularyBook book, string vhrPath)
}
}

public abstract void Write(FileStream stream, VocabularyBook book, string vhrPath);
public abstract void Write(FileStream stream, VocabularyBook book, string vhrPath, bool includeResults);

protected static bool TryDeleteVhrFile(string? vhrCode, string vhrPath)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Vocup/IO/Vhf1Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Read(FileStream stream, VocabularyBook book, string vhrPath)
}
}

public override void Write(FileStream stream, VocabularyBook book, string vhrPath)
public override void Write(FileStream stream, VocabularyBook book, string vhrPath, bool includeResults)
{
StringBuilder content = new();
content.AppendLine("1.0");
Expand All @@ -88,7 +88,7 @@ public override void Write(FileStream stream, VocabularyBook book, string vhrPat

EncryptAndWrite(stream, content.ToString());

if (!string.IsNullOrEmpty(book.VhrCode))
if (includeResults && !string.IsNullOrEmpty(book.VhrCode))
{
// Write results to .vhr file
WriteResults(book, stream.Name, book.VhrCode, vhrPath);
Expand Down
6 changes: 4 additions & 2 deletions src/Vocup/IO/Vhf2Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public bool Read(FileStream stream, VocabularyBook book)
}
}

public override void Write(FileStream stream, VocabularyBook book, string vhrPath)
public override void Write(FileStream stream, VocabularyBook book, string vhrPath, bool includeResults)
{
using (ZipArchive archive = new(stream, ZipArchiveMode.Create, leaveOpen: true))
{
Expand All @@ -100,7 +100,9 @@ public override void Write(FileStream stream, VocabularyBook book, string vhrPat

foreach (VocabularyWord word in book.Words)
{
JsonWord jsonWord = new(word.MotherTongue, word.ForeignLang, word.ForeignLangSynonym, word.PracticeStateNumber, word.PracticeDate);
JsonWord jsonWord = includeResults
? new(word.MotherTongue, word.ForeignLang, word.ForeignLangSynonym, word.PracticeStateNumber, word.PracticeDate)
: new(word.MotherTongue, word.ForeignLang, word.ForeignLangSynonym, 0, DateTime.MinValue);
jsonBook.Words.Add(jsonWord);
}

Expand Down
724 changes: 344 additions & 380 deletions src/Vocup/MainForm.Designer.cs

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/Vocup/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void VocabularyBookHasContent(bool value)
TsbPrint.Enabled = value;

TsmiExport.Enabled = value;
TsmiShare.Enabled = value;
}
public void VocabularyBookPracticable(bool value)
{
Expand Down Expand Up @@ -356,6 +357,38 @@ private async void TsmiUpdate_Click(object sender, EventArgs e)
private void TsmiSaveAs_Click(object sender, EventArgs e) => SaveFile(true);
private void TsbSave_Click(object sender, EventArgs e) => SaveFile(false);

private void TsmiShare_Click(object sender, EventArgs e)
{
using SaveFileDialog save = new()
{
Title = Words.ShareVocabularyBook,
FileName = CurrentBook.MotherTongue + " - " + CurrentBook.ForeignLang,
InitialDirectory = Program.Settings.VhfPath,
Filter = $"{Words.FileFormatVhf2} (*.vhf)|*.vhf|{Words.FileFormatVhf1} (*.vhf)|*.vhf"
};
if (save.ShowDialog() == DialogResult.OK)
{
BookFileFormat format = save.FilterIndex == 2 ? BookFileFormat.Vhf1 : BookFileFormat.Vhf2;

if (format.TryWrite(save.FileName, CurrentBook, Program.Settings.VhrPath, includeResults: false))
{
try
{
string explorer = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe");
Process.Start(explorer, $"/select,\"{save.FileName}\"");
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Messages.OpenInExplorerError, ex), Messages.OpenInExplorerErrorT, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
else
{
return;
}
}

private void BtnPractice_Click(object sender, EventArgs e) => PracticeWords();
private void TsmiPractice_Click(object sender, EventArgs e) => PracticeWords();

Expand Down
3 changes: 3 additions & 0 deletions src/Vocup/MainForm.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,7 @@
<data name="$this.Text" xml:space="preserve">
<value>Vocup</value>
</data>
<data name="TsmiShare.Text" xml:space="preserve">
<value>Teilen ohne Übungsergebnisse</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/Vocup/MainForm.nl.resx
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,7 @@ Open een woordenboek of maak een nieuwe aan.</value>
<data name="$this.Text" xml:space="preserve">
<value>Vocup</value>
</data>
<data name="TsmiShare.Text" xml:space="preserve">
<value>Delen zonder trainingsresultaten</value>
</data>
</root>
Loading

0 comments on commit 5b8c8fb

Please sign in to comment.