- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
78 lines
2.2 KiB
TypeScript
78 lines
2.2 KiB
TypeScript
import {computed, Injectable, signal} from '@angular/core';
|
|
|
|
export interface BookNavigationState {
|
|
bookIds: number[];
|
|
currentIndex: number;
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class BookNavigationService {
|
|
private readonly _navigationState = signal<BookNavigationState | null>(null);
|
|
readonly navigationState = this._navigationState.asReadonly();
|
|
|
|
private readonly _availableBookIds = signal<number[]>([]);
|
|
readonly availableBookIds = this._availableBookIds.asReadonly();
|
|
|
|
readonly canNavigatePrevious = computed(() => {
|
|
const state = this.navigationState();
|
|
return state !== null && state.currentIndex > 0;
|
|
});
|
|
|
|
readonly canNavigateNext = computed(() => {
|
|
const state = this.navigationState();
|
|
return state !== null && state.currentIndex < state.bookIds.length - 1;
|
|
});
|
|
|
|
readonly previousBookId = computed<number | null>(() => {
|
|
const state = this.navigationState();
|
|
return state && state.currentIndex > 0
|
|
? state.bookIds[state.currentIndex - 1]
|
|
: null;
|
|
});
|
|
|
|
readonly nextBookId = computed<number | null>(() => {
|
|
const state = this.navigationState();
|
|
return state && state.currentIndex < state.bookIds.length - 1
|
|
? state.bookIds[state.currentIndex + 1]
|
|
: null;
|
|
});
|
|
|
|
readonly currentPosition = computed<{ current: number; total: number } | null>(() => {
|
|
const state = this.navigationState();
|
|
return state
|
|
? {
|
|
current: state.currentIndex + 1,
|
|
total: state.bookIds.length
|
|
}
|
|
: null;
|
|
});
|
|
|
|
setAvailableBookIds(bookIds: number[]): void {
|
|
this._availableBookIds.set(bookIds);
|
|
}
|
|
|
|
setNavigationContext(bookIds: number[], currentBookId: number): void {
|
|
const currentIndex = bookIds.indexOf(currentBookId);
|
|
if (currentIndex !== -1) {
|
|
this._navigationState.set({bookIds, currentIndex});
|
|
} else {
|
|
this._navigationState.set(null);
|
|
}
|
|
}
|
|
|
|
updateCurrentBook(bookId: number): void {
|
|
const state = this.navigationState();
|
|
if (state) {
|
|
const newIndex = state.bookIds.indexOf(bookId);
|
|
if (newIndex !== -1) {
|
|
this._navigationState.set({
|
|
...state,
|
|
currentIndex: newIndex
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|