diff --git a/src/components/Context/LibraryContext.tsx b/src/components/Context/LibraryContext.tsx index 16abe82c..82e7df1d 100644 --- a/src/components/Context/LibraryContext.tsx +++ b/src/components/Context/LibraryContext.tsx @@ -4,12 +4,11 @@ import { UseLibraryReturnType, } from '@screens/library/hooks/useLibrary'; import { useLibrarySettings } from '@hooks/persisted'; -import { LibrarySettings } from '@hooks/persisted/useSettings'; // type Library = Category & { novels: LibraryNovelInfo[] }; type LibraryContextType = UseLibraryReturnType & { - settings: LibrarySettings; + settings: ReturnType; }; const defaultValue = {} as LibraryContextType; diff --git a/src/hooks/persisted/useSettings.ts b/src/hooks/persisted/useSettings.ts index b7846eac..f88a0308 100644 --- a/src/hooks/persisted/useSettings.ts +++ b/src/hooks/persisted/useSettings.ts @@ -103,6 +103,8 @@ export interface BrowseSettings { export interface LibrarySettings { /** User-selected category for newly added novels. */ defaultCategoryId?: number; + /** Last category viewed in the library. */ + lastUsedCategoryId?: number; globalUpdateExcludeCategoryIds?: number[]; globalUpdateIncludeCategoryIds?: number[]; sortOrder?: LibrarySortOrder; diff --git a/src/screens/library/LibraryScreen.tsx b/src/screens/library/LibraryScreen.tsx index 749dda6c..7e855b09 100644 --- a/src/screens/library/LibraryScreen.tsx +++ b/src/screens/library/LibraryScreen.tsx @@ -54,6 +54,7 @@ import { ThemeColors } from '@theme/types'; import { useLibraryContext } from '@components/Context/LibraryContext'; import { xor } from 'lodash-es'; import { SelectionContext } from './SelectionContext'; +import { getLibraryCategoryIndex } from './constants/constants'; type State = NavigationState<{ key: string; @@ -86,7 +87,13 @@ const LibraryScreen = ({ navigation }: LibraryScreenProps) => { categories, refetchLibrary, isLoading, - settings: { showNumberOfNovels, downloadedOnlyMode, incognitoMode }, + settings: { + showNumberOfNovels, + downloadedOnlyMode, + incognitoMode, + lastUsedCategoryId, + setLibrarySettings, + }, } = useLibraryContext(); const { importNovel } = useImport(); @@ -98,7 +105,19 @@ const LibraryScreen = ({ navigation }: LibraryScreenProps) => { const bottomSheetRef = useRef(null); - const [index, setIndex] = useState(0); + const [selectedCategoryId, setSelectedCategoryId] = + useState(lastUsedCategoryId); + const index = getLibraryCategoryIndex(categories, selectedCategoryId); + const setIndex = useCallback( + (nextIndex: number) => { + const categoryId = categories[nextIndex]?.id; + if (categoryId !== undefined) { + setSelectedCategoryId(categoryId); + setLibrarySettings({ lastUsedCategoryId: categoryId }); + } + }, + [categories, setLibrarySettings], + ); const { value: setCategoryModalVisible, @@ -483,17 +502,21 @@ const LibraryScreen = ({ navigation }: LibraryScreenProps) => { ) : null} - + {categories.length ? ( + + ) : ( + + )} {useLibraryFAB && diff --git a/src/screens/library/constants/__tests__/constants.test.ts b/src/screens/library/constants/__tests__/constants.test.ts new file mode 100644 index 00000000..567c375f --- /dev/null +++ b/src/screens/library/constants/__tests__/constants.test.ts @@ -0,0 +1,19 @@ +import { getLibraryCategoryIndex } from '../constants'; + +const categories = [{ id: 1 }, { id: 7 }, { id: 12 }]; + +describe('getLibraryCategoryIndex', () => { + it('returns the index of the saved category', () => { + expect(getLibraryCategoryIndex(categories, 7)).toBe(1); + }); + + it('falls back to the first category when the saved category is unavailable', () => { + expect(getLibraryCategoryIndex(categories, 99)).toBe(0); + }); + + it('finds the saved category after categories are reordered', () => { + expect(getLibraryCategoryIndex([{ id: 7 }, { id: 1 }, { id: 12 }], 7)).toBe( + 0, + ); + }); +}); diff --git a/src/screens/library/constants/constants.ts b/src/screens/library/constants/constants.ts index 1e44c70f..f407b853 100644 --- a/src/screens/library/constants/constants.ts +++ b/src/screens/library/constants/constants.ts @@ -1,5 +1,13 @@ import { getString } from '@i18n/translations'; +export const getLibraryCategoryIndex = ( + categories: { id: number }[], + categoryId?: number, +): number => { + const index = categories.findIndex(category => category.id === categoryId); + return index === -1 ? 0 : index; +}; + export enum LibraryFilter { Downloaded = '(chaptersDownloaded > 0 OR isLocal = 1)', Unread = 'lastReadAt IS NULL',