Skip to content

Commit bcff469

Browse files
Reimplemented it to account for bug
1 parent 12235d5 commit bcff469

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

EssentialCSharp.Web/Services/ISiteMappingService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
public interface ISiteMappingService
44
{
55
IList<SiteMapping> SiteMappings { get; }
6+
string GetPercentComplete(string currentPageKey);
67
}

EssentialCSharp.Web/Services/SiteMappingService.cs

Lines changed: 32 additions & 1 deletion
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,4 +14,33 @@ 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
}
17+
18+
public string GetPercentComplete(string currentPageKey)
19+
{
20+
int currentMappingCount = 1;
21+
int overallMappingCount = 1;
22+
bool currentPageFound = false;
23+
IEnumerable<IGrouping<int, SiteMapping>> chapterGroupings = SiteMappings.GroupBy(x => x.ChapterNumber).OrderBy(g => g.Key);
24+
foreach (IGrouping<int, SiteMapping> chapterGrouping in chapterGroupings)
25+
{
26+
IEnumerable<IGrouping<int, SiteMapping>> pageGroupings = chapterGrouping.GroupBy(x => x.PageNumber).OrderBy(g => g.Key);
27+
foreach (IGrouping<int, SiteMapping> pageGrouping in pageGroupings)
28+
{
29+
foreach (SiteMapping siteMapping in pageGrouping)
30+
{
31+
if (siteMapping.Key == currentPageKey)
32+
{
33+
currentPageFound = true;
34+
}
35+
if (!currentPageFound)
36+
{
37+
currentMappingCount++;
38+
}
39+
overallMappingCount++;
40+
}
41+
}
42+
}
43+
double result = (double)currentMappingCount / overallMappingCount * 100;
44+
return string.Format(CultureInfo.InvariantCulture, "{0:0.00}", result);
45+
}
1546
}

EssentialCSharp.Web/Views/Shared/_Layout.cshtml

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

130130
<div class="d-flex align-items-center">
@@ -295,35 +295,10 @@
295295
Title = $"Chapter {x.Key}: {x.First().ChapterTitle}",
296296
Items = GetItems(x, 1)
297297
});
298-
var groupedTocData = _SiteMappings.SiteMappings.DistinctBy(x => (x.ChapterNumber, x.PageNumber)).GroupBy(x => x.ChapterNumber).OrderBy(x => x.Key);
299-
int currentPageCount = 1;
300-
int overallCount = 1;
301-
bool currentPageFound = false;
302-
List<string> keyList = new();
303298
304-
// Loop through each SiteMapping and increment the overallCount,
305-
// currentPageFound (until you reach it), and build keyList out
306-
foreach (IGrouping<int, SiteMapping> group in groupedTocData)
307-
{
308-
var orderedGroup = group.OrderBy(x => x.PageNumber);
309-
foreach (SiteMapping siteMapping in orderedGroup)
310-
{
311-
if (siteMapping.Key == ViewBag.CurrentPageKey)
312-
{
313-
currentPageFound = true;
314-
}
315-
if (!currentPageFound)
316-
{
317-
currentPageCount++;
318-
}
319-
keyList.Add(siteMapping.Key);
320-
overallCount++;
321-
}
322-
}
299+
var percentComplete = _SiteMappings.GetPercentComplete(ViewBag.CurrentPageKey);
323300
}
324-
CURRENT_PAGE_COUNT = @Json.Serialize(currentPageCount)
325-
TOTAL_PAGE_COUNT = @Json.Serialize(overallCount)
326-
KEY_LIST = @Json.Serialize(keyList)
301+
PERCENT_COMPLETE = @Json.Serialize(percentComplete);
327302
PREVIOUS_PAGE = @Json.Serialize(ViewBag.PreviousPage)
328303
NEXT_PAGE = @Json.Serialize(ViewBag.NextPage)
329304
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:
@@ -195,9 +194,7 @@ const app = createApp({
195194

196195
const currentPage = findCurrentPage([], tocData) ?? [];
197196

198-
const currentPageCount = CURRENT_PAGE_COUNT;
199-
const totalPageCount = TOTAL_PAGE_COUNT;
200-
const keyList = KEY_LIST
197+
const percentComplete = ref(PERCENT_COMPLETE);
201198

202199
const chapterParentPage = currentPage.find((parent) => parent.level === 0);
203200

@@ -260,6 +257,11 @@ const app = createApp({
260257
return tocData.filter(item => filterItem(item, query));
261258
});
262259

260+
const isContentPage = computed(() => {
261+
let path = window.location.pathname;
262+
return path !== '/home' && path !== '/guidelines' && path !== '/about' && path !== '/announcements';
263+
});
264+
263265
function filterItem(item, query) {
264266
let matches = normalizeString(item.title).includes(query);
265267
if (item.items && item.items.length) {
@@ -316,14 +318,13 @@ const app = createApp({
316318
tocData,
317319
expandedTocs,
318320
currentPage,
319-
currentPageCount,
320-
totalPageCount,
321-
keyList,
321+
percentComplete,
322322
chapterParentPage,
323323

324324
searchQuery,
325325
filteredTocData,
326-
enableTocFilter
326+
enableTocFilter,
327+
isContentPage
327328
};
328329
},
329330
});

0 commit comments

Comments
 (0)