license import { Injectable } from '@angular/core' ; import { BehaviorSubject , ReplaySubject } from 'rxjs' ; import { map , skip } from 'rxjs/operators' ; import { defaultFuriganaStyle , FuriganaStyle , } from 'src/app/models/furigana-style.model' ; import { BookReaderAvailableKeybind , BookReaderKeybindMap , } from './book-reader/book-reader-keybind' ; import { defaultWritingMode , WritingMode } from './models/writing-mode.model' ; @ Injectable ( { providedIn : 'root' , } ) export class StoreService { theme$ = createStringLocalStorageBehaviorSubject < string > ( 'theme' , 'light-theme' ) ; multiplier$ = createNumberLocalStorageBehaviorSubject ( 'autoScrollMultiplier' , 20 ) ; fontSize$ = createNumberLocalStorageBehaviorSubject ( 'fontSize' , 20 ) ; fontFamilyGroupOne$ = createStringLocalStorageBehaviorSubject ( 'fontFamilyGroupOne' , '' ) ; fontFamilyGroupTwo$ = createStringLocalStorageBehaviorSubject ( 'fontFamilyGroupTwo' , '' ) ; hideSpoilerImage$ = createBooleanLocalStorageBehaviorSubject ( 'hideSpoilerImage' , true ) ; hideFurigana$ = createBooleanLocalStorageBehaviorSubject ( 'hideFurigana' , false ) ; furiganaStyle$ = createStringLocalStorageBehaviorSubject < FuriganaStyle > ( 'furiganaStyle' , defaultFuriganaStyle ) ; writingMode$ = createStringLocalStorageBehaviorSubject < WritingMode > ( 'writingMode' , defaultWritingMode ) ; verticalMode$ = this . writingMode$ . pipe ( map ( ( writingMode ) => writingMode === 'vertical-rl' ) ) ; secondDimensionMaxValue$ = createNumberLocalStorageBehaviorSubject ( 'secondDimensionMaxValue' , 0 ) ; firstDimensionMargin$ = createNumberLocalStorageBehaviorSubject ( 'firstDimensionMargin' , 0 ) ; autoPositionOnResize$ = createBooleanLocalStorageBehaviorSubject ( 'autoPositionOnResize' , true ) ; pinFooter$ = new BehaviorSubject ( true ) ; bookReaderKeybindMap$ = new BehaviorSubject < BookReaderKeybindMap > ( { KeyB : BookReaderAvailableKeybind . BOOKMARK , KeyR : BookReaderAvailableKeybind . JUMP_TO_BOOKMARK , PageDown : BookReaderAvailableKeybind . NEXT_PAGE , PageUp : BookReaderAvailableKeybind . PREV_PAGE , Space : BookReaderAvailableKeybind . AUTO_SCROLL_TOGGLE , KeyA : BookReaderAvailableKeybind . AUTO_SCROLL_INCREASE , KeyD : BookReaderAvailableKeybind . AUTO_SCROLL_DECREASE , } ) ; isImportingBooks$ = new BehaviorSubject ( false ) ; importBookProgress$ = new BehaviorSubject ( 0 ) ; previousUrl$ = new ReplaySubject < string > ( 1 ) ; isVerticalMode ( ) { return this . writingMode$ . getValue ( ) === 'vertical-rl' ; } } function getStoredBooleanOrDefault ( key , defaultVal ) { const storedVal = localStorage . getItem ( key ) ; return storedVal != null ? ! ! + storedVal : defaultVal ; } function getStoredNumberOrDefault ( key , defaultVal ) { const storedVal = localStorage . getItem ( key ) ; return storedVal != null ? + storedVal : defaultVal ; } function getStoredStringOrDefault ( key , defaultVal ) { const storedVal = localStorage . getItem ( key ) ; return storedVal ?? defaultVal ; } function createBooleanLocalStorageBehaviorSubject ( key , defaultVal ) { const initVal = getStoredBooleanOrDefault ( key , defaultVal ) ; const behaviorSubject = new BehaviorSubject ( initVal ) ; behaviorSubject . pipe ( skip ( 1 ) ) . subscribe ( ( updatedVal ) => { localStorage . setItem ( key , updatedVal ? '1' : '0' ) ; } ) ; return behaviorSubject ; } function createNumberLocalStorageBehaviorSubject ( key , defaultVal ) { const initVal = getStoredNumberOrDefault ( key , defaultVal ) ; const behaviorSubject = new BehaviorSubject ( initVal ) ; behaviorSubject . pipe ( skip ( 1 ) ) . subscribe ( ( updatedVal ) => { localStorage . setItem ( key , `${ updatedVal }` ) ; } ) ; return behaviorSubject ; } function createStringLocalStorageBehaviorSubject < T extends string , U extends T = T > ( key , defaultVal ) { const initVal = getStoredStringOrDefault ( key , defaultVal ) as T ; const behaviorSubject = new BehaviorSubject ( initVal ) ; behaviorSubject . pipe ( skip ( 1 ) ) . subscribe ( ( updatedVal ) => { localStorage . setItem ( key , `${ updatedVal }` ) ; } ) ; return behaviorSubject ; }