Skip to content

Commit 5d32de2

Browse files
Joshua-Lester3BenjaminMichaelis
authored andcommitted
Reimplemented it to account for bug
1 parent 103a88e commit 5d32de2

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
namespace EssentialCSharp.Web.Services;
1+
namespace EssentialCSharp.Web.Services;
22

33
public interface ISiteMappingService
44
{
55
IList<SiteMapping> SiteMappings { get; }
66
IEnumerable<SiteMappingDto> GetTocData();
7+
string GetPercentComplete(string currentPageKey);
78
}

EssentialCSharp.Web/Services/SiteMappingService.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using EssentialCSharp.Web.Models;
1+
using System.Globalization;
2+
using EssentialCSharp.Common;
3+
using EssentialCSharp.Web.Models;
24

35
namespace EssentialCSharp.Web.Services;
46

@@ -12,7 +14,6 @@ public SiteMappingService(IWebHostEnvironment webHostEnvironment)
1214
List<SiteMapping>? siteMappings = System.Text.Json.JsonSerializer.Deserialize<List<SiteMapping>>(File.OpenRead(path)) ?? throw new InvalidOperationException("No table of contents found");
1315
SiteMappings = siteMappings;
1416
}
15-
1617
public IEnumerable<SiteMappingDto> GetTocData()
1718
{
1819
return SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key).Select(x =>
@@ -50,4 +51,33 @@ private static IEnumerable<SiteMappingDto> GetItems(IEnumerable<SiteMapping> cha
5051
Items = GetItems(chapterItems.SkipWhile(q => i.Keys.First() != q.Keys.First()).Skip(1), indentLevel + 1)
5152
});
5253
}
54+
55+
public string GetPercentComplete(string currentPageKey)
56+
{
57+
int currentMappingCount = 1;
58+
int overallMappingCount = 1;
59+
bool currentPageFound = false;
60+
IEnumerable<IGrouping<int, SiteMapping>> chapterGroupings = SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key);
61+
foreach (IGrouping<int, SiteMapping> chapterGrouping in chapterGroupings)
62+
{
63+
IEnumerable<IGrouping<int, SiteMapping>> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key);
64+
foreach (IGrouping<int, SiteMapping> pageGrouping in pageGroupings)
65+
{
66+
foreach (SiteMapping siteMapping in pageGrouping)
67+
{
68+
if (siteMapping.Key == currentPageKey)
69+
{
70+
currentPageFound = true;
71+
}
72+
if (!currentPageFound)
73+
{
74+
currentMappingCount++;
75+
}
76+
overallMappingCount++;
77+
}
78+
}
79+
}
80+
double result = (double)currentMappingCount / overallMappingCount * 100;
81+
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
82+
}
5383
}

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@
124124
<a v-if="chapterParentPage" :href="chapterParentPage.href" class="menu-chapter-title text-light">
125125
<span v-cloak>{{chapterParentPage.title}}</span>
126126
</a>
127-
<div class="menu-chapter-title text-light">
128-
<span v-cloak>{{currentPageCount}} / {{totalPageCount}}</span>
127+
<div class="menu-chapter-title text-light" v-if="isContentPage">
128+
<span v-cloak>Progress: {{percentComplete}}%</span>
129129
</div>
130130

131131
<div class="d-flex align-items-center">
@@ -271,10 +271,9 @@
271271
<script>
272272
@{
273273
var tocData = _SiteMappings.GetTocData();
274+
var percentComplete = _SiteMappings.GetPercentComplete(ViewBag.CurrentPageKey);
274275
}
275-
CURRENT_PAGE_COUNT = @Json.Serialize(currentPageCount)
276-
TOTAL_PAGE_COUNT = @Json.Serialize(overallCount)
277-
KEY_LIST = @Json.Serialize(keyList)
276+
PERCENT_COMPLETE = @Json.Serialize(percentComplete);
278277
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)
279278
NEXT_PAGE = @Json.Serialize(ViewBag.NextPage)
280279
TOC_DATA = @Json.Serialize(tocData)

EssentialCSharp.Web/wwwroot/js/site.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { useWindowSize } from "vue-window-size";
1818
* @prop {TocItem[]} [items]
1919
*/
2020
/** @type {TocItem} */
21-
debugger;
2221
const tocData = markRaw(TOC_DATA);
2322

2423
//Add new content or features here:
@@ -211,9 +210,7 @@ const app = createApp({
211210

212211
const currentPage = findCurrentPage([], tocData) ?? [];
213212

214-
const currentPageCount = CURRENT_PAGE_COUNT;
215-
const totalPageCount = TOTAL_PAGE_COUNT;
216-
const keyList = KEY_LIST
213+
const percentComplete = ref(PERCENT_COMPLETE);
217214

218215
const chapterParentPage = currentPage.find((parent) => parent.level === 0);
219216

@@ -282,6 +279,11 @@ const app = createApp({
282279
return tocData.filter(item => filterItem(item, query));
283280
});
284281

282+
const isContentPage = computed(() => {
283+
let path = window.location.pathname;
284+
return path !== '/home' && path !== '/guidelines' && path !== '/about' && path !== '/announcements';
285+
});
286+
285287
function filterItem(item, query) {
286288
let matches = normalizeString(item.title).includes(query);
287289
if (item.items && item.items.length) {
@@ -339,14 +341,13 @@ const app = createApp({
339341
tocData,
340342
expandedTocs,
341343
currentPage,
342-
currentPageCount,
343-
totalPageCount,
344-
keyList,
344+
percentComplete,
345345
chapterParentPage,
346346

347347
searchQuery,
348348
filteredTocData,
349-
enableTocFilter
349+
enableTocFilter,
350+
isContentPage
350351
};
351352
},
352353
});

0 commit comments

Comments
 (0)