Skip to content

Commit

Permalink
Add PageCount/WordCount display settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Videogamers0 committed Aug 4, 2023
1 parent a2702f5 commit f2dec3a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 21 deletions.
17 changes: 16 additions & 1 deletion UI/StoriesList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
Expand Down Expand Up @@ -158,8 +159,22 @@
Visibility="{Binding MVM.Settings.DisplaySettings.ShowOverallRating, Mode=OneWay, Converter={StaticResource InverseBooleanToVisibilityConverter}, FallbackValue=Collapsed}" />
</Grid>

<!-- Page Count / Word Count -->
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" TextElement.FontSize="10" TextElement.FontStyle="Italic" TextElement.Foreground="Gray">
<TextBlock VerticalAlignment="Center" Margin="0,0,8,0" d:Visibility="Visible"
Visibility="{Binding MVM.Settings.DisplaySettings.ShowPageCount, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
<Run FontWeight="SemiBold" Text="{Binding Summary.PageCount, Mode=OneWay, FallbackValue=1}" d:Text="1" />
<Run Text="page(s)" />
</TextBlock>
<TextBlock Margin="0,0,8,0" VerticalAlignment="Center" d:Visibility="Visible"
Visibility="{Binding MVM.Settings.DisplaySettings.ShowWordCount, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
<Run FontWeight="SemiBold" Text="{Binding WordCount, Mode=OneTime, FallbackValue=0}" d:Text="999" />
<Run Text="words" />
</TextBlock>
</StackPanel>

<!-- User Rating / Download date -->
<DockPanel Grid.Row="2" Grid.Column="0">
<DockPanel Grid.Row="3" Grid.Column="0">
<local:RatingControl DockPanel.Dock="Left" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,1,0,0" Width="60" Height="16"
Value="{Binding BindableUserRating, Mode=TwoWay, FallbackValue=0}" d:Value="4" d:Visibility="Visible"
Visibility="{Binding MVM.Settings.DisplaySettings.ShowUserRating, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed}" />
Expand Down
4 changes: 4 additions & 0 deletions UI/StoryFilterContent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
<Border Height="{StaticResource VerticalPadding1}" />
<CheckBox Content="Title" IsChecked="True" IsEnabled="False" />
<Border Height="{StaticResource VerticalPadding1}" />
<CheckBox Content="Page Count" IsChecked="{Binding ShowPageCount, Mode=TwoWay}" />
<Border Height="{StaticResource VerticalPadding1}" />
<CheckBox Content="Word Count" IsChecked="{Binding ShowWordCount, Mode=TwoWay}" />
<Border Height="{StaticResource VerticalPadding1}" />
<CheckBox Content="Your Rating" IsChecked="{Binding ShowUserRating, Mode=TwoWay}" />
<Border Height="{StaticResource VerticalPadding1}" />
<CheckBox Content="Downloaded Date" IsChecked="{Binding ShowDateDownloaded, Mode=TwoWay}" />
Expand Down
32 changes: 32 additions & 0 deletions VM/DisplaySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,34 @@ public bool ShowOverallRating
}
}

private bool _ShowPageCount;
public bool ShowPageCount
{
get => _ShowPageCount;
set
{
if (_ShowPageCount != value)
{
_ShowPageCount = value;
NPC(nameof(ShowPageCount));
}
}
}

private bool _ShowWordCount;
public bool ShowWordCount
{
get => _ShowWordCount;
set
{
if (_ShowWordCount != value)
{
_ShowWordCount = value;
NPC(nameof(ShowWordCount));
}
}
}

private bool _ShowUserRating;
public bool ShowUserRating
{
Expand Down Expand Up @@ -103,6 +131,8 @@ public DisplaySettings()
ShowDateApproved = false;
ShowReadState = true;
ShowOverallRating = false;
ShowPageCount = false;
ShowWordCount = false;
ShowUserRating = false;
ShowDateDownloaded = false;
}
Expand All @@ -114,6 +144,8 @@ public DisplaySettings(SavedSettings InheritFrom)
ShowDateApproved = InheritFrom.ShowDateApproved;
ShowReadState = InheritFrom.ShowReadState;
ShowOverallRating = InheritFrom.ShowOverallRating;
ShowPageCount = InheritFrom.ShowPageCount;
ShowWordCount = InheritFrom.ShowWordCount;
ShowUserRating = InheritFrom.ShowUserRating;
ShowDateDownloaded = InheritFrom.ShowDateDownloaded;
}
Expand Down
3 changes: 0 additions & 3 deletions VM/Literotica/LiteroticaStory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ private set
{
_Story = value;
NPC(nameof(Story));
NPC(nameof(PageCount));

// Initialize the buttons that are used to navigate to particular chapters/pages
if (Story == null)
Expand Down Expand Up @@ -120,7 +119,6 @@ private set
public string Title => Summary.Title;
public string FullDescription => string.Join("\n", Summary.Chapters.Select((x, index) => $"Ch{(index + 1):00}: {x.Description}"));
public int ChapterCount => Summary.Chapters.Count;
public int PageCount => Story?.Chapters.Sum(x => x.Pages.Count) ?? 0;
public double AverageRating
{
get
Expand All @@ -134,7 +132,6 @@ public double AverageRating
}
public bool HasOverallRating => Summary.Chapters.Any(x => x.Rating.HasValue);


private ReadOnlyCollection<StoryNavigationButton> _ChapterNavButtons;
public ReadOnlyCollection<StoryNavigationButton> ChapterNavButtons
{
Expand Down
20 changes: 17 additions & 3 deletions VM/Literotica/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace StoryManager.VM.Literotica
{
public class SerializableStory
{
public string Version { get; set; } = new Version(1, 0, 0, 0).ToString();
public static Version CurrentVersion = new Version(1, 0, 0, 1);
public string Version { get; set; } = CurrentVersion.ToString();
[JsonIgnore]
public bool IsUpToDate => PageCount != 0 && (!System.Version.TryParse(Version, out Version SchemaVersion) || SchemaVersion >= CurrentVersion);

public LiteroticaAuthor Author { get; set; }
public string Title { get; set; }
public ReadOnlyCollection<SerializableChapter> Chapters { get; set; }
public int PageCount { get; set; }

public DateTime DownloadedAt { get; set; }

Expand All @@ -25,9 +30,10 @@ private SerializableStory() { }

public SerializableStory(LiteroticaPage InitialPage, IEnumerable<SerializableChapter> Chapters)
{
this.Version = MainViewModel.FileVersion.ToString();
this.Version = CurrentVersion.ToString();

this.Chapters = Chapters.ToList().AsReadOnly();
PageCount = Chapters.Sum(x => x.Pages.Count);

Author = InitialPage.submission.author;
Title = InitialPage.submission.series?.meta.title ?? InitialPage.submission.title;
Expand All @@ -41,15 +47,23 @@ public SerializableStory(LiteroticaPage InitialPage, IEnumerable<SerializableCha
/// Intended to be used for performance purposes, so a story's metadata can quickly be loaded without loading everything until user selects the story.</summary>
public SerializableStory AsSummary() => new()
{
Version = Version,
Version = CurrentVersion.ToString(),
Author = Author,
Title = Title,
Chapters = Chapters.Select(x => x.AsSummary()).ToList().AsReadOnly(),
PageCount = PageCount == 0 ? Chapters.Sum(x => x.Pages?.Count ?? 0) : PageCount,
DownloadedAt = DownloadedAt,
IsSummary = true
};

public override string ToString() => $"{nameof(SerializableStory)}: {Title} by {Author.username}";

[OnDeserialized]
private void OnDeserialized(StreamingContext sc)
{
if (PageCount == 0)
PageCount = Chapters.Sum(x => x.Pages?.Count ?? 0);
}
}

public class SerializableChapter
Expand Down
47 changes: 33 additions & 14 deletions VM/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ private async Task LoadStoriesAsync(bool IsInitializing)
{
Stopwatch sw = Stopwatch.StartNew();

Dictionary<string, SerializableStory> CachedStories = Settings.PreviousSessionSettings.StoryMetadata
Dictionary<string, SerializableStory> CachedStories = Settings.PreviousSessionSettings.StoryMetadata.Where(x => x.IsUpToDate)
.DistinctBy(x => GetStoryUniqueKey(x.Author.username, x.Title)).ToDictionary(x => GetStoryUniqueKey(GeneralUtils.ToSafeFilename(x.Author.username), GeneralUtils.ToSafeFilename(x.Title)));

string BaseFolder = Settings.StoriesDirectory;
Expand All @@ -792,14 +792,24 @@ private async Task LoadStoriesAsync(bool IsInitializing)
if (!CachedStories.TryGetValue(Key, out SerializableStory Story))
{
string SummaryFile = Path.Combine(StoryFolder, LiteroticaStory.SummaryFilename);
bool SummaryFileExists = File.Exists(SummaryFile);
string JsonFile = SummaryFileExists ? SummaryFile : StoryFile;

string Json = File.ReadAllText(JsonFile);
Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);
// First try loading the data from the smaller story-metadata.json file
bool Loaded = false;
if (File.Exists(SummaryFile))
{
string Json = File.ReadAllText(SummaryFile);
Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);

// If summary data is from an older version, it must be fully loaded and have its summary data re-written
if (Story.IsUpToDate)
Loaded = true;
}

if (!SummaryFileExists)
// If still not successfully loaded, load the entire story contents from story.json
if (!Loaded)
{
string Json = File.ReadAllText(StoryFile);
Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);
string Summary = GeneralUtils.SerializeJson(Story.AsSummary(), true);
File.WriteAllText(SummaryFile, Summary);
}
Expand All @@ -818,7 +828,7 @@ private async Task LoadStoriesAsync(bool IsInitializing)
{
foreach (string AuthorFolder in Directory.GetDirectories(BaseFolder))
{
Task<List<(SerializableStory, string)>> AuthorStoriesTask = Task.Run(() => LoadStories(new DirectoryInfo(AuthorFolder).Name, AuthorFolder));
Task<List<(SerializableStory, string)>> AuthorStoriesTask = Task.Run(() => LoadStories(new DirectoryInfo(AuthorFolder).Name, AuthorFolder));
StoryTasks.Add(AuthorStoriesTask);
}
}
Expand All @@ -841,17 +851,26 @@ private async Task LoadStoriesAsync(bool IsInitializing)
{
try
{
SerializableStory Story;
string SummaryFile = Path.Combine(StoryFolder, LiteroticaStory.SummaryFilename);
bool SummaryFileExists = File.Exists(SummaryFile);
string JsonFile = SummaryFileExists ? SummaryFile : StoryFile;

string Json = File.ReadAllText(JsonFile);
SerializableStory Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);
LiteroticaStory StoryVM = new(this, Story, StoryFolder);
Stories.Add(StoryVM);
// First try loading the data from the smaller story-metadata.json file
bool Loaded = false;
if (File.Exists(SummaryFile))
{
string Json = File.ReadAllText(SummaryFile);
Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);

// If summary data is from an older version, it must be fully loaded and have its summary data re-written
if (Story.IsUpToDate)
Loaded = true;
}

if (!SummaryFileExists)
// If still not successfully loaded, load the entire story contents from story.json
if (!Loaded)
{
string Json = File.ReadAllText(StoryFile);
Story = GeneralUtils.DeserializeJson<SerializableStory>(Json);
string Summary = GeneralUtils.SerializeJson(Story.AsSummary(), true);
File.WriteAllText(SummaryFile, Summary);
}
Expand Down
6 changes: 6 additions & 0 deletions VM/SavedSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public class SavedSettings
public bool ShowReadState { get; set; }
[DataMember(Name = "ShowOverallRating")]
public bool ShowOverallRating { get; set; }
[DataMember(Name = "ShowPageCount")]
public bool ShowPageCount { get; set; }
[DataMember(Name = "ShowWordCount")]
public bool ShowWordCount { get; set; }
[DataMember(Name = "ShowUserRating")]
public bool ShowUserRating { get; set; }
[DataMember(Name = "ShowDateDownloaded")]
Expand Down Expand Up @@ -144,6 +148,8 @@ private void InitializeDefaults()
ShowDateApproved = false;
ShowReadState = true;
ShowOverallRating = false;
ShowPageCount = false;
ShowWordCount = false;
ShowUserRating = false;
ShowDateDownloaded = false;

Expand Down
2 changes: 2 additions & 0 deletions VM/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ public async Task SaveAsync(bool TryCreateBackup)
ShowDateApproved = DisplaySettings.ShowDateApproved,
ShowReadState = DisplaySettings.ShowReadState,
ShowOverallRating = DisplaySettings.ShowOverallRating,
ShowPageCount = DisplaySettings.ShowPageCount,
ShowWordCount = DisplaySettings.ShowWordCount,
ShowUserRating = DisplaySettings.ShowUserRating,
ShowDateDownloaded = DisplaySettings.ShowDateDownloaded,

Expand Down

0 comments on commit f2dec3a

Please sign in to comment.