using System.Linq; namespace NexusReader.UI.Shared.Services; public class ReaderNavigationService : IReaderNavigationService { public int CurrentChapterIndex { get; private set; } = 0; public int TotalChapters { get; private set; } = 1; public string ChapterTitle { get; private set; } = "Loading..."; public event Func? OnNavigationChanged; public async Task GoToChapter(int index) { if (index < 0 || index >= TotalChapters) return; CurrentChapterIndex = index; await NotifyNavigationChangedAsync(); } public async Task GoToNextChapter() { if (CurrentChapterIndex < TotalChapters - 1) { await GoToChapter(CurrentChapterIndex + 1); } } public async Task GoToPreviousChapter() { if (CurrentChapterIndex > 0) { await GoToChapter(CurrentChapterIndex - 1); } } public void UpdateMetadata(int currentIndex, int totalChapters, string title) { bool changed = false; if (CurrentChapterIndex != currentIndex) { CurrentChapterIndex = currentIndex; changed = true; } if (TotalChapters != totalChapters) { TotalChapters = totalChapters; changed = true; } if (ChapterTitle != title) { ChapterTitle = title; changed = true; } if (changed) { // Note: UpdateMetadata remains void, so we trigger notification fire-and-forget here // but usually this is called during a render cycle where metadata is updated from a load. _ = NotifyNavigationChangedAsync(); } } private async Task NotifyNavigationChangedAsync() { var handlers = OnNavigationChanged?.GetInvocationList(); if (handlers != null) { foreach (var handler in handlers.Cast>()) { await handler(); } } } }