diff --git a/src/database/queries/LibraryQueries.js b/src/database/queries/LibraryQueries.js index 8e5efbda..b7f5b708 100644 --- a/src/database/queries/LibraryQueries.js +++ b/src/database/queries/LibraryQueries.js @@ -2,7 +2,7 @@ import * as SQLite from 'expo-sqlite'; const db = SQLite.openDatabase('lnreader.db'); const getLibraryQuery = (sort, filter) => ` - SELECT novels.*, C.chaptersUnread, D.chaptersDownloaded + SELECT novels.*, C.chaptersUnread, D.chaptersDownloaded, H.lastReadAt FROM novels LEFT JOIN ( SELECT chapters.novelId, COUNT(*) AS chaptersUnread @@ -18,6 +18,14 @@ const getLibraryQuery = (sort, filter) => ` GROUP BY chapters.novelId ) AS D ON novels.novelId = D.novelId + LEFT JOIN ( + SELECT history.historyNovelId as novelId, historyTimeRead AS lastReadAt + FROM history + GROUP BY history.historyNovelId + HAVING history.historyTimeRead = MAX(history.historyTimeRead) + ORDER BY history.historyTimeRead DESC + ) AS H + ON novels.novelId = H.novelId WHERE novels.followed = 1 ${filter ? 'AND ' + filter : ''} ${sort ? 'ORDER BY ' + sort : ''} `; @@ -29,7 +37,7 @@ export const getLibrary = (sort, filter) => { getLibraryQuery(sort, filter), null, (txObj, { rows: { _array } }) => { - // console.log(_array); + // console.log(JSON.stringify(_array, null, 2)); resolve(_array); }, (txObj, error) => { @@ -42,7 +50,7 @@ export const getLibrary = (sort, filter) => { const searchLibraryQuery = (searchText, sort, filter) => ` - SELECT novels.*, C.chaptersUnread, D.chaptersDownloaded + SELECT novels.*, C.chaptersUnread, D.chaptersDownloaded, H.lastReadAt FROM novels LEFT JOIN ( SELECT chapters.novelId, COUNT(*) AS chaptersUnread @@ -58,6 +66,14 @@ const searchLibraryQuery = (searchText, sort, filter) => GROUP BY chapters.novelId ) AS D ON novels.novelId = D.novelId + LEFT JOIN ( + SELECT history.historyNovelId as novelId, historyTimeRead AS lastReadAt + FROM history + GROUP BY history.historyNovelId + HAVING history.historyTimeRead = MAX(history.historyTimeRead) + ORDER BY history.historyTimeRead DESC + ) AS H + ON novels.novelId = H.novelId WHERE novels.followed = 1 AND novelName LIKE '%${searchText}%' ${ filter ? 'AND ' + filter : '' } diff --git a/src/screens/library/components/LibraryBottomSheet.js b/src/screens/library/components/LibraryBottomSheet.js index 7b822491..110a7da4 100644 --- a/src/screens/library/components/LibraryBottomSheet.js +++ b/src/screens/library/components/LibraryBottomSheet.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useMemo, useState } from 'react'; import { StyleSheet, View, @@ -68,33 +68,41 @@ const LibrarySheet = ({ ); - const sortOrders = [ - { - label: 'Date Added', - ASC: 'novels.novelId ASC', - DESC: 'novels.novelId DESC', - }, - { - label: 'Alphabetically', - ASC: 'novels.novelName ASC', - DESC: 'novels.novelName DESC', - }, - { - label: 'Unread', - ASC: 'novels.unread ASC', - DESC: 'novels.unread DESC', - }, - { - label: 'Downloaded', - ASC: 'chaptersDownloaded ASC', - DESC: 'chaptersDownloaded DESC', - }, - { - label: 'Total Chapters', - ASC: 'chaptersUnread ASC', - DESC: 'chaptersUnread DESC', - }, - ]; + const sortOrders = useMemo( + () => [ + { + label: 'Last Read', + ASC: 'lastReadAt ASC', + DESC: 'lastReadAt DESC', + }, + { + label: 'Alphabetically', + ASC: 'novels.novelName ASC', + DESC: 'novels.novelName DESC', + }, + { + label: 'Unread', + ASC: 'novels.unread ASC', + DESC: 'novels.unread DESC', + }, + { + label: 'Downloaded', + ASC: 'chaptersDownloaded ASC', + DESC: 'chaptersDownloaded DESC', + }, + { + label: 'Total Chapters', + ASC: 'chaptersUnread ASC', + DESC: 'chaptersUnread DESC', + }, + { + label: 'Date Added', + ASC: 'novels.novelId ASC', + DESC: 'novels.novelId DESC', + }, + ], + [], + ); const SecondRoute = () => (