Skip to content

Commit

Permalink
fix filters bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kazemcodes committed Apr 2, 2022
1 parent c51a207 commit 51c0425
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 92 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions core/src/main/res/drawable/ic_update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M11,8v5l4.25,2.52l0.77,-1.28l-3.52,-2.09V8H11zM21,10V3l-2.64,2.64C16.74,4.01 14.49,3 12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9s9,-4.03 9,-9h-2c0,3.86 -3.14,7 -7,7s-7,-3.14 -7,-7s3.14,-7 7,-7c1.93,0 3.68,0.79 4.95,2.05L14,10H21z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface HistoryDao {


@Query("""SELECT history.*, library.title as bookTitle, library.sourceId, library.cover, library.favorite, chapter.title as chapterTitle,
date(ROUND(history.readAt / 1000), 'unixepoch', 'localtime') AS date
date(ROUND(history.readAt / 1000), 'unixepoch', 'localtime') AS date,chapter.number as chapterNumber
FROM history
JOIN library ON history.bookId = library.id
JOIN chapter ON history.chapterId = chapter.id
Expand Down
125 changes: 99 additions & 26 deletions data/src/main/java/org/ireader/data/local/dao/LibraryBookDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,30 @@ interface LibraryBookDao {
fun subscribeAllLocalBooks(): Flow<List<Book>>

@RewriteQueriesToDropUnusedColumns
@Query("""SELECT library.*, MAX(chapter.readAt) as lastRead,COUNT(DISTINCT chapter.id) AS total_chapter, COUNT(chapter.read) as isRead, COUNT(length(chapter.content) > 10) AS total_download
@Query("""SELECT library.*,
MAX(chapter.readAt) as lastRead,
COUNT(DISTINCT chapter.id) AS totalChapters,
SUM(chapter.read) as isRead,
SUM(length(chapter.content) > 10) AS totalDownload
FROM library
LEFT JOIN chapter ON library.id = chapter.bookId
GROUP BY library.id
HAVING library.favorite = 1
AND (CASE WHEN :unread= 1 THEN lastRead is NULL ELSE 1 END)
AND (CASE WHEN :complete= 1 THEN total_chapter == isRead ELSE 1 END)
AND (CASE WHEN :downloaded= 1 THEN total_chapter == total_download ELSE 1 END)
HAVING library.favorite = 1
ORDER BY
CASE WHEN :isAsc = 1 AND :sortByAbs = 1 THEN library.title END ASC,
CASE WHEN :isAsc = 0 AND :sortByAbs = 1 THEN library.title END DESC,
CASE WHEN :isAsc = 1 AND :sortByDateAdded = 1 THEN library.dataAdded END ASC,
CASE WHEN :isAsc = 0 AND :sortByDateAdded = 1 THEN library.dataAdded END DESC,
CASE WHEN :isAsc = 1 AND :sortByLastRead = 1 THEN lastRead END ASC,
CASE WHEN :isAsc = 0 AND :sortByLastRead = 1 THEN lastRead END DESC,
CASE WHEN :isAsc = 1 AND :dateFetched = 1 THEN dateFetch END ASC,
CASE WHEN :isAsc = 0 AND :dateFetched = 1 THEN dateFetch END DESC,
CASE WHEN :isAsc = 1 AND :dateAdded = 1 THEN dataAdded END ASC,
CASE WHEN :isAsc = 0 AND :dateAdded = 1 THEN dataAdded END DESC,
CASE WHEN :isAsc = 1 AND :latestChapter = 1 THEN dateUpload END ASC,
CASE WHEN :isAsc = 0 AND :latestChapter = 1 THEN dateUpload END DESC,
CASE WHEN :isAsc = 1 AND :unread = 1 THEN lastRead END ASC,
CASE WHEN :isAsc = 0 AND :unread = 1 THEN lastRead END DESC,
CASE WHEN :isAsc = 1 AND :sortByTotalChapter = 1 THEN total_chapter END ASC,
CASE WHEN :isAsc = 0 AND :sortByTotalChapter = 1 THEN total_chapter END DESC
CASE WHEN :desc = 1 AND :sortByAbs = 1 THEN library.title END DESC,
CASE WHEN :desc = 0 AND :sortByAbs = 1 THEN library.title END ASC,
CASE WHEN :desc = 1 AND :sortByDateAdded = 1 THEN library.dataAdded END DESC,
CASE WHEN :desc = 0 AND :sortByDateAdded = 1 THEN library.dataAdded END ASC,
CASE WHEN :desc = 1 AND :sortByLastRead = 1 THEN lastRead END DESC,
CASE WHEN :desc = 0 AND :sortByLastRead = 1 THEN lastRead END ASC,
CASE WHEN :desc = 1 AND :dateFetched = 1 THEN dateFetch END DESC,
CASE WHEN :desc = 0 AND :dateFetched = 1 THEN dateFetch END ASC,
CASE WHEN :desc = 1 AND :dateAdded = 1 THEN dataAdded END DESC,
CASE WHEN :desc = 0 AND :dateAdded = 1 THEN dataAdded END ASC,
CASE WHEN :desc = 1 AND :sortByTotalChapter = 1 THEN totalChapters END DESC,
CASE WHEN :desc = 0 AND :sortByTotalChapter = 1 THEN totalChapters END ASC,
CASE WHEN :desc = 1 AND :lastChecked = 1 THEN lastUpdated END DESC,
CASE WHEN :desc = 0 AND :lastChecked = 1 THEN lastUpdated END ASC
""")
fun subscribeAllInLibraryBooks(
sortByAbs: Boolean = false,
Expand All @@ -47,14 +46,88 @@ interface LibraryBookDao {
sortByTotalChapter: Boolean = false,
dateFetched: Boolean = false,
dateAdded: Boolean = false,
latestChapter: Boolean = false,
unread: Boolean = false,
downloaded: Boolean = false,
complete: Boolean = false,
isAsc: Boolean = false,
lastChecked: Boolean = false,
desc: Boolean = false,
): Flow<List<Book>>


@Query("""
SELECT library.*, MAX(history.readAt) AS max
FROM library
LEFT JOIN chapter
ON library.id = chapter.bookId
LEFT JOIN history
ON chapter.id = history.chapterId
WHERE library.favorite = 1
GROUP BY library.id
ORDER BY
CASE WHEN :desc = 1 THEN max END DESC,
CASE WHEN :desc = 0 THEN max END ASC
""")
fun subscribeLatestRead(desc: Boolean): Flow<List<Book>>

@Query("""
SELECT library.*, MAX(chapter.dateUpload) AS max
FROM library
LEFT JOIN chapter
ON library.id = chapter.bookId
GROUP BY library.id
ORDER by
CASE WHEN :desc = 1 THEN max END DESC,
CASE WHEN :desc = 0 THEN max END ASC
""")
fun subscribeLatestChapter(desc: Boolean): Flow<List<Book>>

@Query("""
SELECT library.*, SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread
FROM library
JOIN chapter
ON library.id = chapter.bookId
GROUP BY library.id
HAVING library.favorite = 1
ORDER by
CASE WHEN :desc = 1 THEN COUNT(*) END DESC,
CASE WHEN :desc = 0 THEN COUNT(*) END ASC
""")
fun subscribeTotalChapter(desc: Boolean): Flow<List<Book>>

@Query("""
SELECT library.*,
SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread,
COUNT(*) AS total
FROM library
LEFT JOIN chapter
ON library.id = chapter.bookId
GROUP BY library.id
HAVING library.favorite = 1 AND unread == total
""")
suspend fun findUnreadBooks(): List<Book>

@Query("""
SELECT library.*,
SUM(length(chapter.content) > 10) as total_download,
COUNT(*) AS total
FROM library
LEFT JOIN chapter
ON library.id = chapter.bookId
GROUP BY library.id
HAVING library.favorite = 1 AND total_download == total
""")
suspend fun findCompletedBooks(): List<Book>

@Query("""
SELECT library.*,
SUM(CASE WHEN chapter.read == 0 THEN 1 ELSE 0 END) AS unread,
COUNT(*) AS total
FROM library
LEFT JOIN chapter
ON library.id = chapter.bookId
GROUP BY library.id
HAVING library.favorite = 1 AND unread == 0
""")
suspend fun findDownloadedBooks(): List<Book>


@Query("SELECT * FROM library WHERE favorite = 1")
suspend fun findAllInLibraryBooks(): List<Book>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class LocalBookRepositoryImpl(
}


override suspend fun findUnreadBooks(): List<Book> {
return bookDao.findUnreadBooks()
}

override suspend fun findCompletedBooks(): List<Book> {
return bookDao.findCompletedBooks()
}

override suspend fun findDownloadedBooks(): List<Book> {
return bookDao.findDownloadedBooks()
}

override fun subscribeAllInLibrary(
sortByAbs: Boolean,
Expand All @@ -50,25 +61,27 @@ class LocalBookRepositoryImpl(
sortByTotalChapters: Boolean,
dateAdded: Boolean,
latestChapter: Boolean,
unread: Boolean,
downloaded: Boolean,
complete: Boolean,
isAsc: Boolean,
lastChecked: Boolean,
desc: Boolean,
): Flow<List<Book>> {
return bookDao.subscribeAllInLibraryBooks(
sortByAbs = sortByAbs,
sortByDateAdded = sortByDateAdded,
sortByLastRead = sortByLastRead,
unread = unread,
isAsc = isAsc,
latestChapter = latestChapter,
downloaded = downloaded,
dateFetched = dateFetched,
dateAdded = dateAdded,
complete = complete,
sortByTotalChapter = sortByTotalChapters
)
return when {
sortByLastRead -> bookDao.subscribeLatestRead(desc)
sortByTotalChapters -> bookDao.subscribeTotalChapter(desc)
latestChapter -> bookDao.subscribeLatestChapter(desc)
else -> {
bookDao.subscribeAllInLibraryBooks(
sortByAbs = sortByAbs,
sortByDateAdded = sortByDateAdded,
sortByLastRead = sortByLastRead,
desc = desc,
dateFetched = dateFetched,
dateAdded = dateAdded,
sortByTotalChapter = sortByTotalChapters,
lastChecked = lastChecked
)
}

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ class LibraryUpdatesService @AssistedInject constructor(
override suspend fun doWork(): Result {
val libraryBooks = getBookUseCases.findAllInLibraryBooks()

var updatedBookSize = 0
val cancelIntent = WorkManager.getInstance(applicationContext)
.createCancelPendingIntent(id)
val builder =
NotificationCompat.Builder(applicationContext,
Notifications.CHANNEL_LIBRARY_PROGRESS).apply {
setContentTitle("Checking Updates")
setSmallIcon(R.drawable.ic_downloading)
setSmallIcon(R.drawable.ic_update)
setOnlyAlertOnce(true)
priority = NotificationCompat.PRIORITY_LOW
setAutoCancel(true)
Expand Down Expand Up @@ -88,6 +89,9 @@ class LibraryUpdatesService @AssistedInject constructor(
}, onError = {})
val newChapters =
remoteChapters.filterNot { chapter -> chapter.title in chapters.map { it.title } }
if (newChapters.isNotEmpty()) {
updatedBookSize += 1
}
withContext(Dispatchers.IO) {
insertUseCases.insertChapters(newChapters.map {
it.copy(
Expand Down Expand Up @@ -115,7 +119,7 @@ class LibraryUpdatesService @AssistedInject constructor(
setContentTitle("Failed to Check Library Updates.")
setSubText(e.localizedMessage)
}
setSmallIcon(R.drawable.ic_downloading)
setSmallIcon(R.drawable.ic_update)
priority = NotificationCompat.PRIORITY_DEFAULT
setAutoCancel(true)
}.build()
Expand All @@ -137,8 +141,8 @@ class LibraryUpdatesService @AssistedInject constructor(
Notifications.ID_LIBRARY_PROGRESS,
NotificationCompat.Builder(applicationContext,
Notifications.CHANNEL_DOWNLOADER_COMPLETE).apply {
setContentTitle("${libraryBooks.size} book was update successfully.")
setSmallIcon(R.drawable.ic_downloading)
setContentTitle("$updatedBookSize book was updated.")
setSmallIcon(R.drawable.ic_update)
priority = NotificationCompat.PRIORITY_DEFAULT
setSubText("It was Updated Successfully")
setAutoCancel(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ data class HistoryWithRelations(
val cover: String,
val favorite: Boolean,
val chapterTitle: String,
val chapterNumber: Int,
val date: String,
)
9 changes: 4 additions & 5 deletions domain/src/main/java/org/ireader/domain/models/SortType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ sealed class SortType(val name: String, val index: Int) {
object Alphabetically : SortType("Alphabetically", 0)
object LastRead : SortType("Last Read", 1)
object LastChecked : SortType("Last Checked", 2)
object Unread : SortType("Unread", 3)
object TotalChapters : SortType("Total Chapters", 4)
object LatestChapter : SortType("Latest Chapter", 5)
object DateFetched : SortType("Date Fetched", 6)
object DateAdded : SortType("Date Added", 7)
object TotalChapters : SortType("Total Chapters", 3)
object LatestChapter : SortType("Latest Chapter", 4)
object DateFetched : SortType("Date Fetched", 5)
object DateAdded : SortType("Date Added", 6)
}

sealed class FilterType(val name: String, val index: Int) {
Expand Down
45 changes: 45 additions & 0 deletions domain/src/main/java/org/ireader/domain/models/entities/Book.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,49 @@ fun MangaInfo.fromBookInfo(sourceId: Long): Book {
description = this.description,
author = this.author,
)
}

data class BookWithInfo(
val id: Long = 0,
val title: String,
val lastRead: Long = 0,
val lastUpdated: Long = 0,
val unread: Boolean = false,
val totalChapters: Int = 0,
val dateUpload: Long = 0,
val dateFetch: Long = 0,
val dataAdded: Long = 0,
val sourceId: Long,
val totalDownload: Int,
val isRead: Int,
val link: String,
val status: Int = 0,
val cover: String = "",
val customCover: String = "",
val favorite: Boolean = false,
val tableId: Long = 0,
val author: String = "",
val description: String = "",
val genres: List<String> = emptyList(),
val viewer: Int = 0,
val flags: Int = 0,
)

fun BookWithInfo.toBook(): Book {
return Book(
id = this.id,
sourceId = sourceId,
customCover = this.cover,
cover = this.cover,
flags = 0,
link = this.link,
dataAdded = 0L,
lastUpdated = 0L,
favorite = false,
title = this.title,
status = this.status,
genres = this.genres,
description = this.description,
author = this.author,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ interface LocalBookRepository {
sortByTotalChapters: Boolean,
dateAdded: Boolean,
latestChapter: Boolean,
unread: Boolean,
downloaded: Boolean,
complete: Boolean,
isAsc: Boolean,
lastChecked: Boolean,
desc: Boolean,
): Flow<List<Book>>

suspend fun findUnreadBooks(): List<Book>

suspend fun findCompletedBooks(): List<Book>

suspend fun findDownloadedBooks(): List<Book>

fun getAllExploreBookPagingSource(): PagingSource<Int, Book>

Expand Down
Loading

0 comments on commit 51c0425

Please sign in to comment.