Skip to content

Commit

Permalink
fix the bug where read was not shown correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
kazemcodes committed Apr 3, 2022
1 parent 7c9528b commit 72cd7e0
Show file tree
Hide file tree
Showing 30 changed files with 299 additions and 239 deletions.
13 changes: 1 addition & 12 deletions core/src/main/java/org/ireader/core/utils/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Constants {
const val CATALOG_REMOTE = "catalog_remote"
const val UPDATE_TABLE = "updates"
const val HISTORY_TABLE = "history"
const val CATEGORY_TABLE = "category_table"
const val CATEGORY_TABLE = "category"

const val EXPLORE_BOOK_TABLE = "explore"

Expand All @@ -38,17 +38,6 @@ object Constants {

const val ARG_HIDE_BOTTOM_BAR = "ARG_HIDE_BOTTOM_BAR"


const val NO_BOOKS_ERROR = "There is no book"
const val NO_BOOK_ERROR = "There is no book with this name"
const val NO_CHAPTER_ERROR = "There is No chapter with this name"
const val NO_CHAPTERS_ERROR = "There are no chapters with this name"

const val CLOUDFLARE_PROTECTION_ERROR =
"Cloudflare site protection is enable, Please Open the WebView."

const val UNKNOWN_ERROR = "Unknown Error Happened"

const val repo_url = "/repos/kazemcodes/IReader/releases/latest"
const val github_api_url = "https://api.github.com"

Expand Down
16 changes: 11 additions & 5 deletions data/schemas/org.ireader.data.local.AppDatabase/13.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 13,
"identityHash": "8bcbb7c5073388da5a0a8fcaf4f0c940",
"identityHash": "8c1420c5d760d01a03ea739d80a6d316",
"entities": [
{
"tableName": "library",
Expand Down Expand Up @@ -189,7 +189,7 @@
"foreignKeys": []
},
{
"tableName": "category_table",
"tableName": "category",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `sort` INTEGER NOT NULL, `updateInterval` INTEGER NOT NULL, `flags` INTEGER NOT NULL)",
"fields": [
{
Expand Down Expand Up @@ -524,11 +524,17 @@
},
{
"tableName": "page_key_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `sourceId` INTEGER NOT NULL, `prevPage` INTEGER, `nextPage` INTEGER, PRIMARY KEY(`id`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `sourceId` INTEGER NOT NULL, `prevPage` INTEGER, `nextPage` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
Expand All @@ -555,7 +561,7 @@
"columnNames": [
"id"
],
"autoGenerate": false
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
Expand All @@ -564,7 +570,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8bcbb7c5073388da5a0a8fcaf4f0c940')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8c1420c5d760d01a03ea739d80a6d316')"
]
}
}
3 changes: 2 additions & 1 deletion data/src/main/java/org/ireader/data/local/dao/HistoryDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ interface HistoryDao {
FROM history
JOIN library ON history.bookId = library.id
JOIN chapter ON history.chapterId = chapter.id
WHERE bookTitle LIKE '%' || :query || '%'
ORDER BY history.readAt DESC""")
fun findHistoriesPaging(): kotlinx.coroutines.flow.Flow<List<HistoryWithRelations>>
fun findHistoriesPaging(query: String): kotlinx.coroutines.flow.Flow<List<HistoryWithRelations>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertHistory(history: History): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface LibraryBookDao {
desc: Boolean = false,
): Flow<List<Book>>


@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*, MAX(history.readAt) AS max
FROM library
Expand All @@ -66,6 +66,7 @@ interface LibraryBookDao {
""")
fun subscribeLatestRead(desc: Boolean): Flow<List<Book>>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*, MAX(chapter.dateUpload) AS max
FROM library
Expand All @@ -78,6 +79,7 @@ interface LibraryBookDao {
""")
fun subscribeLatestChapter(desc: Boolean): Flow<List<Book>>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*, SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread
FROM library
Expand All @@ -91,6 +93,7 @@ interface LibraryBookDao {
""")
fun subscribeTotalChapter(desc: Boolean): Flow<List<Book>>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*,
SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread,
Expand All @@ -103,6 +106,7 @@ interface LibraryBookDao {
""")
suspend fun findUnreadBooks(): List<Book>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*,
SUM(length(chapter.content) > 10) as total_download,
Expand All @@ -115,6 +119,7 @@ interface LibraryBookDao {
""")
suspend fun findCompletedBooks(): List<Book>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT library.*,
SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread,
Expand Down
21 changes: 12 additions & 9 deletions data/src/main/java/org/ireader/data/local/dao/LibraryChapterDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface LibraryChapterDao {
chapterId: Long,
): Chapter?

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT *,library.favorite
FROM chapter
Expand Down Expand Up @@ -64,22 +65,24 @@ WHERE library.favorite = 1
bookId: Int,
): PagingSource<Int, Chapter>


@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT *, history.readAt as lastRead
from chapter
JOIN history ON history.bookId = chapter.bookId
GROUP BY chapterId
HAVING chapter.bookId == :bookId AND lastRead = (SELECT MAX(lastRead) FROM chapter WHERE bookId == :bookId)
SELECT *
from chapter
GROUP BY id
HAVING chapter.bookId == :bookId
ORDER BY readAt DESC
LIMIT 1
""")
fun subscribeLastReadChapter(bookId: Long): Flow<Chapter?>

@RewriteQueriesToDropUnusedColumns
@Query("""
SELECT chapter.* , MAX(chapter.readAt) as lastRead
SELECT *
from chapter
GROUP By chapter.id
HAVING chapter.readAt == lastRead AND chapter.bookId = :bookId
GROUP BY id
HAVING chapter.bookId == :bookId
ORDER BY readAt DESC
LIMIT 1
""")
suspend fun findLastReadChapter(bookId: Long): Chapter?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ interface RemoteKeysDao {
@Query("SELECT * FROM library WHERE id =:id")
fun getExploreBookById(id: Int): Flow<Book?>

@Query("SELECT DISTINCT library.* FROM library JOIN page_key_table ON library.title = page_key_table.id AND library.sourceId = page_key_table.sourceId OR tableId = 1 GROUP BY library.title ORDER BY id")
@Query("""
SELECT DISTINCT library.* FROM library
JOIN page_key_table ON library.title = page_key_table.title AND library.sourceId = page_key_table.sourceId OR tableId = 1
GROUP BY library.title
ORDER BY page_key_table.id
""")
fun getAllExploreBookByPaging(): PagingSource<Int, Book>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class HistoryRepositoryImpl constructor(private val historyDao: HistoryDao) : Hi
}

@OptIn(ExperimentalCoroutinesApi::class)
override fun findHistoriesPaging():
override fun findHistoriesPaging(query: String):
Flow<Map<String, List<HistoryWithRelations>>> {
return historyDao.findHistoriesPaging().mapLatest { histories ->
return historyDao.findHistoriesPaging(query).mapLatest { histories ->
histories.distinctBy { it.bookId }.groupBy { history -> history.date }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ExploreRemoteMediator(
}
val keys = response.mangas.map { book ->
RemoteKeys(
id = book.title,
title = book.title,
prevPage = prevPage,
nextPage = nextPage,
sourceId = source.id
Expand Down
Loading

0 comments on commit 72cd7e0

Please sign in to comment.