From 245bea879d3a92ff4c735607cfe291c971635173 Mon Sep 17 00:00:00 2001 From: Joshua Lester Date: Tue, 22 Oct 2024 15:49:47 -0700 Subject: [PATCH 1/8] Counting mechanism works --- .../Controllers/HomeController.cs | 1 + .../Views/Shared/_Layout.cshtml | 27 +++++++++++++++++-- EssentialCSharp.Web/wwwroot/js/site.js | 6 +++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/EssentialCSharp.Web/Controllers/HomeController.cs b/EssentialCSharp.Web/Controllers/HomeController.cs index f20f0ee0..690d063a 100644 --- a/EssentialCSharp.Web/Controllers/HomeController.cs +++ b/EssentialCSharp.Web/Controllers/HomeController.cs @@ -41,6 +41,7 @@ public IActionResult Index(string key) ViewBag.PageTitle = siteMapping.IndentLevel is 0 ? siteMapping.ChapterTitle + " " + siteMapping.RawHeading : siteMapping.RawHeading; ViewBag.NextPage = FlipPage(siteMapping!.ChapterNumber, siteMapping.PageNumber, true); + ViewBag.CurrentPageKey = siteMapping.Key; ViewBag.PreviousPage = FlipPage(siteMapping.ChapterNumber, siteMapping.PageNumber, false); ViewBag.HeadContents = headHtml; ViewBag.Contents = html; diff --git a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml index 04a9367b..d764c3b5 100644 --- a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml +++ b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml @@ -123,6 +123,9 @@ {{chapterParentPage.title}} +
@@ -292,8 +295,29 @@ Title = $"Chapter {x.Key}: {x.First().ChapterTitle}", Items = GetItems(x, 1) }); + var groupedTocData = _SiteMappings.SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key); + int currentPageCount = 0; + int overallCount = 0; + bool currentPageFound = false; + foreach (IGrouping group in groupedTocData) + { + var orderedGroup = group.OrderBy(x => x.PageNumber); + foreach (SiteMapping siteMapping in orderedGroup) + { + if (siteMapping.Key == ViewBag.CurrentPageKey) + { + currentPageFound = true; + } + if (!currentPageFound) + { + currentPageCount++; + } + overallCount++; + } + } } - + CURRENT_PAGE_COUNT = @Json.Serialize(currentPageCount) + TOTAL_PAGE_COUNT = @Json.Serialize(overallCount) PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage) NEXT_PAGE = @Json.Serialize(ViewBag.NextPage) TOC_DATA = @Json.Serialize(tocData) @@ -343,6 +367,5 @@ - diff --git a/EssentialCSharp.Web/wwwroot/js/site.js b/EssentialCSharp.Web/wwwroot/js/site.js index a3e95e07..4df89c7b 100644 --- a/EssentialCSharp.Web/wwwroot/js/site.js +++ b/EssentialCSharp.Web/wwwroot/js/site.js @@ -18,6 +18,7 @@ import { useWindowSize } from "vue-window-size"; * @prop {TocItem[]} [items] */ /** @type {TocItem} */ +debugger; const tocData = markRaw(TOC_DATA); //Add new content or features here: @@ -194,6 +195,9 @@ const app = createApp({ const currentPage = findCurrentPage([], tocData) ?? []; + const currentPageCount = CURRENT_PAGE_COUNT; + const totalPageCount = TOTAL_PAGE_COUNT; + const chapterParentPage = currentPage.find((parent) => parent.level === 0); const sectionTitle = ref(currentPage?.[0]?.title || "Essential C#"); @@ -311,6 +315,8 @@ const app = createApp({ tocData, expandedTocs, currentPage, + currentPageCount, + totalPageCount, chapterParentPage, searchQuery, From a9cc80907d95091fb82db505e50602c0a4d63085 Mon Sep 17 00:00:00 2001 From: Joshua Lester Date: Wed, 23 Oct 2024 08:59:28 -0700 Subject: [PATCH 2/8] save changes --- EssentialCSharp.Web/Controllers/HomeController.cs | 2 ++ EssentialCSharp.Web/Views/Shared/_Layout.cshtml | 13 ++++++++++--- EssentialCSharp.Web/wwwroot/js/site.js | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/EssentialCSharp.Web/Controllers/HomeController.cs b/EssentialCSharp.Web/Controllers/HomeController.cs index 690d063a..a902fe9e 100644 --- a/EssentialCSharp.Web/Controllers/HomeController.cs +++ b/EssentialCSharp.Web/Controllers/HomeController.cs @@ -53,6 +53,8 @@ public IActionResult Index(string key) } } + + [Route("/TermsOfService", Name = "TermsOfService")] public IActionResult TermsOfService() diff --git a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml index d764c3b5..72849b16 100644 --- a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml +++ b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml @@ -124,7 +124,8 @@ {{chapterParentPage.title}}
@@ -296,9 +297,13 @@ Items = GetItems(x, 1) }); var groupedTocData = _SiteMappings.SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key); - int currentPageCount = 0; - int overallCount = 0; + int currentPageCount = 1; + int overallCount = 1; bool currentPageFound = false; + List keyList = new(); + + // Loop through each SiteMapping and increment the overallCount, + // currentPageFound (until you reach it), and build keyList out foreach (IGrouping group in groupedTocData) { var orderedGroup = group.OrderBy(x => x.PageNumber); @@ -312,12 +317,14 @@ { currentPageCount++; } + keyList.Add(siteMapping.Key); overallCount++; } } } CURRENT_PAGE_COUNT = @Json.Serialize(currentPageCount) TOTAL_PAGE_COUNT = @Json.Serialize(overallCount) + KEY_LIST = @Json.Serialize(keyList) PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage) NEXT_PAGE = @Json.Serialize(ViewBag.NextPage) TOC_DATA = @Json.Serialize(tocData) diff --git a/EssentialCSharp.Web/wwwroot/js/site.js b/EssentialCSharp.Web/wwwroot/js/site.js index 4df89c7b..5a36d672 100644 --- a/EssentialCSharp.Web/wwwroot/js/site.js +++ b/EssentialCSharp.Web/wwwroot/js/site.js @@ -197,6 +197,7 @@ const app = createApp({ const currentPageCount = CURRENT_PAGE_COUNT; const totalPageCount = TOTAL_PAGE_COUNT; + const keyList = KEY_LIST const chapterParentPage = currentPage.find((parent) => parent.level === 0); @@ -317,6 +318,7 @@ const app = createApp({ currentPage, currentPageCount, totalPageCount, + keyList, chapterParentPage, searchQuery, From 12235d5774a4aefd87c843734c3ef13c92d1346f Mon Sep 17 00:00:00 2001 From: Joshua Lester Date: Tue, 12 Nov 2024 12:15:09 -0800 Subject: [PATCH 3/8] save --- EssentialCSharp.Web/Views/Shared/_Layout.cshtml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml index 72849b16..c382e33c 100644 --- a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml +++ b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml @@ -124,7 +124,6 @@ {{chapterParentPage.title}} @@ -296,7 +295,7 @@ Title = $"Chapter {x.Key}: {x.First().ChapterTitle}", Items = GetItems(x, 1) }); - var groupedTocData = _SiteMappings.SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key); + var groupedTocData = _SiteMappings.SiteMappings.DistinctBy(x => (x.ChapterNumber, x.PageNumber)).GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key); int currentPageCount = 1; int overallCount = 1; bool currentPageFound = false; From bcff469e1fa201dc8e02437e59167fb4c820c92e Mon Sep 17 00:00:00 2001 From: Joshua Lester Date: Fri, 22 Nov 2024 10:17:55 -0800 Subject: [PATCH 4/8] Reimplemented it to account for bug --- .../Services/ISiteMappingService.cs | 1 + .../Services/SiteMappingService.cs | 33 ++++++++++++++++++- .../Views/Shared/_Layout.cshtml | 33 +++---------------- EssentialCSharp.Web/wwwroot/js/site.js | 17 +++++----- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/EssentialCSharp.Web/Services/ISiteMappingService.cs b/EssentialCSharp.Web/Services/ISiteMappingService.cs index fe8cfbc7..116821a5 100644 --- a/EssentialCSharp.Web/Services/ISiteMappingService.cs +++ b/EssentialCSharp.Web/Services/ISiteMappingService.cs @@ -3,4 +3,5 @@ public interface ISiteMappingService { IList SiteMappings { get; } + string GetPercentComplete(string currentPageKey); } diff --git a/EssentialCSharp.Web/Services/SiteMappingService.cs b/EssentialCSharp.Web/Services/SiteMappingService.cs index c086596f..cc84ec8f 100644 --- a/EssentialCSharp.Web/Services/SiteMappingService.cs +++ b/EssentialCSharp.Web/Services/SiteMappingService.cs @@ -1,4 +1,6 @@ -using EssentialCSharp.Web.Models; +using System.Globalization; +using EssentialCSharp.Common; +using EssentialCSharp.Web.Models; namespace EssentialCSharp.Web.Services; @@ -12,4 +14,33 @@ public SiteMappingService(IWebHostEnvironment webHostEnvironment) List? siteMappings = System.Text.Json.JsonSerializer.Deserialize>(File.OpenRead(path)) ?? throw new InvalidOperationException("No table of contents found"); SiteMappings = siteMappings; } + + public string GetPercentComplete(string currentPageKey) + { + int currentMappingCount = 1; + int overallMappingCount = 1; + bool currentPageFound = false; + IEnumerable> chapterGroupings = SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key); + foreach (IGrouping chapterGrouping in chapterGroupings) + { + IEnumerable> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key); + foreach (IGrouping pageGrouping in pageGroupings) + { + foreach (SiteMapping siteMapping in pageGrouping) + { + if (siteMapping.Key == currentPageKey) + { + currentPageFound = true; + } + if (!currentPageFound) + { + currentMappingCount++; + } + overallMappingCount++; + } + } + } + double result = (double)currentMappingCount / overallMappingCount * 100; + return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result); + } } diff --git a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml index c382e33c..72d793c4 100644 --- a/EssentialCSharp.Web/Views/Shared/_Layout.cshtml +++ b/EssentialCSharp.Web/Views/Shared/_Layout.cshtml @@ -123,8 +123,8 @@ {{chapterParentPage.title}} - - + {{chapterParentPage.title}} -