Skip to content

Commit

Permalink
saving scroll position in reader screen
Browse files Browse the repository at this point in the history
  • Loading branch information
kazemcodes committed Apr 1, 2022
1 parent 69c3d7d commit c51a207
Show file tree
Hide file tree
Showing 34 changed files with 386 additions and 459 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class AppPreferences @Inject constructor(
const val SAVED_PARAGRAPH_INDENT = "paragraph_indent"
const val SAVED_ORIENTATION = "orientation_reader"
const val SORT_LIBRARY_SCREEN = "sort_library_screen"
const val FILTER_LIBRARY_SCREEN = "filter_library_screen"
const val SCROLL_MODE = "scroll_mode"
const val AUTO_SCROLL_MODE_INTERVAL = "auto_scroll_mode_interval"
const val AUTO_SCROLL_MODE_OFFSET = "auto_scroll_mode_offset"
Expand Down Expand Up @@ -122,9 +121,6 @@ class AppPreferences @Inject constructor(
return preferenceStore.getInt(SORT_LIBRARY_SCREEN, 0)
}

fun filterLibraryScreen(): Preference<Int> {
return preferenceStore.getInt(FILTER_LIBRARY_SCREEN, 0)
}

fun lastUpdateCheck(): Preference<Long> {
return preferenceStore.getLong(Last_UPDATE_CHECK, 0)
Expand Down
51 changes: 23 additions & 28 deletions data/src/main/java/org/ireader/data/local/dao/LibraryBookDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,49 @@ import org.ireader.domain.models.entities.Book
@Dao
interface LibraryBookDao {

// @RewriteQueriesToDropUnusedColumns
// @Query("""SELECT library.*, MAX(history.readAt) as lastRead
// FROM library
// LEFT JOIN history ON library.id = history.bookId AND readAt = (SELECT * FROM history WHERE history.bookId = library.id ORDER BY history.readAt ASC LIMIT 1)
// GROUP BY library.id
// HAVING library.favorite = 1 AND (CASE WHEN :unread= 1 THEN lastRead is NULL ELSE 1 END) AND (CASE WHEN :sortByLastRead = 1 THEN history.readAt = lastRead ELSE 1 END)
// 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
//""")
// fun subscribeAllLocalBooksForPagingSortedBySort(
// sortByAbs: Boolean = false,
// sortByDateAdded: Boolean = false,
// sortByLastRead: Boolean = false,
// unread: Boolean = false,
// isAsc: Boolean = false,
// ): PagingSource<Int, Book>

@RewriteQueriesToDropUnusedColumns
@Query("""SELECT * FROM library WHERE favorite = 1 """)
fun subscribeAllLocalBooks(): Flow<List<Book>>

@RewriteQueriesToDropUnusedColumns
@Query("""SELECT library.*, MAX(history.readAt) as lastRead,COUNT(DISTINCT chapter.id) AS total_chapter
@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
FROM library
LEFT JOIN history ON library.id = history.bookId
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)
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)
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 :sortByTotalDownload = 1 THEN total_chapter END ASC,
CASE WHEN :isAsc = 0 AND :sortByTotalDownload = 1 THEN total_chapter 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 = 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
""")
fun subscribeAllInLibraryBooks(
sortByAbs: Boolean = false,
sortByDateAdded: Boolean = false,
sortByLastRead: Boolean = false,
sortByTotalDownload: Boolean = false,
sortByTotalChapter: Boolean = false,
dateFetched: Boolean = false,
dateAdded: Boolean = false,
latestChapter: Boolean = false,
unread: Boolean = false,
downloaded: Boolean = false,
complete: Boolean = false,
isAsc: Boolean = false,
): Flow<List<Book>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class LocalBookRepositoryImpl(
private val remoteKeysDao: RemoteKeysDao,
) : LocalBookRepository {

/*****GET********************************/

override fun subscribeBookById(id: Long): Flow<Book?> = flow {
Timber.d("Timber: GetExploreBookByIdUseCase was Called")
bookDao.subscribeBookById(bookId = id)
Expand All @@ -48,56 +46,31 @@ class LocalBookRepositoryImpl(
sortByAbs: Boolean,
sortByDateAdded: Boolean,
sortByLastRead: Boolean,
sortByTotalChapter: Boolean,
dateFetched: Boolean,
sortByTotalChapters: Boolean,
dateAdded: Boolean,
latestChapter: Boolean,
unread: Boolean,
downloaded: Boolean,
complete: Boolean,
isAsc: Boolean,
): Flow<List<Book>> {
return bookDao.subscribeAllInLibraryBooks(
sortByAbs = sortByAbs,
sortByDateAdded = sortByDateAdded,
sortByLastRead = sortByLastRead,
sortByTotalDownload = sortByTotalChapter,
unread = unread,
isAsc = isAsc
isAsc = isAsc,
latestChapter = latestChapter,
downloaded = downloaded,
dateFetched = dateFetched,
dateAdded = dateAdded,
complete = complete,
sortByTotalChapter = sortByTotalChapters
)

}

override fun subscribeAllInLibraryBooks(
sortType: SortType,
isAsc: Boolean,
unreadFilter: Boolean,
): Flow<List<Book>> {
return when (sortType) {
is SortType.Alphabetically -> {
bookDao.subscribeAllInLibraryBooks(sortByAbs = true,
isAsc = isAsc,
unread = unreadFilter
)

}
is SortType.DateAdded -> {
bookDao.subscribeAllInLibraryBooks(sortByDateAdded = true,
isAsc = isAsc,
unread = unreadFilter
)
}
is SortType.LastRead -> {
bookDao.subscribeAllInLibraryBooks(
sortByLastRead = true,
isAsc = isAsc,
unread = unreadFilter
)
}
is SortType.TotalChapter -> {
bookDao.subscribeAllInLibraryBooks(
sortByTotalDownload = true,
isAsc = isAsc,
unread = unreadFilter
)
}
}
}

override suspend fun findAllInLibraryBooks(
sortType: SortType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class NotificationWorker @AssistedInject constructor(
}

private fun getNotifications() = flow {
val books = bookRepo.subscribeAllInLibraryBooks().first()
val books = bookRepo.findAllInLibraryBooks()

progressNotification(books) { book ->
try {
Expand Down
14 changes: 10 additions & 4 deletions domain/src/main/java/org/ireader/domain/models/SortType.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.ireader.domain.models

sealed class SortType(val name: String, val index: Int) {
object DateAdded : SortType("Date Added", 0)
object Alphabetically : SortType("Alphabetically", 1)
object LastRead : SortType("Last Read", 2)
object TotalChapter : SortType("TotalChapter", 3)
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)
}

sealed class FilterType(val name: String, val index: Int) {
object Disable : FilterType("Disable", 0)
object Unread : FilterType("Unread", 1)
object Downloaded : FilterType("Downloaded", 2)
object Completed : FilterType("Completed", 3)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ interface LocalBookRepository {
fun subscribeBookById(id: Long): Flow<Book?>
suspend fun findBookById(id: Long): Book?

fun subscribeAllInLibraryBooks(
sortType: SortType = SortType.LastRead,
isAsc: Boolean = false,
unreadFilter: Boolean = false,
): Flow<List<Book>>


suspend fun findAllInLibraryBooks(
sortType: SortType = SortType.LastRead,
Expand All @@ -34,8 +30,13 @@ interface LocalBookRepository {
sortByAbs: Boolean,
sortByDateAdded: Boolean,
sortByLastRead: Boolean,
sortByTotalChapter: Boolean,
dateFetched: Boolean,
sortByTotalChapters: Boolean,
dateAdded: Boolean,
latestChapter: Boolean,
unread: Boolean,
downloaded: Boolean,
complete: Boolean,
isAsc: Boolean,
): Flow<List<Book>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ data class LocalGetBookUseCases @Inject constructor(
val subscribeBookById: SubscribeBookById,
val findBookById: FindBookById,
val SubscribeInLibraryBooks: SubscribeInLibraryBooks,
val subscribeAllInLibraryBooks: SubscribeAllInLibraryBooks,
val findAllInLibraryBooks: FindAllInLibraryBooks,
val getBooksByQueryByPagination: GetBooksByQueryByPagination,
val getBooksByQueryPagingSource: GetBooksByQueryPagingSource,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
package org.ireader.domain.use_cases.local.book_usecases


import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import org.ireader.core.utils.UiText
import org.ireader.domain.models.SortType
import org.ireader.domain.models.entities.Book
import org.ireader.domain.repository.LocalBookRepository
import org.ireader.domain.utils.Resource
import retrofit2.HttpException
import java.io.IOException
import javax.inject.Inject

/**
* get All books that inLibrary field is true
* note: when there is no book with that id it return a error resource
*/
class SubscribeAllInLibraryBooks @Inject constructor(private val localBookRepository: LocalBookRepository) {
operator fun invoke(): Flow<List<Book>> = flow {
try {
localBookRepository.subscribeAllInLibraryBooks(sortType = SortType.LastRead,
isAsc = false,
unreadFilter = false).first { books ->
emit(books)
true
}
} catch (e: IOException) {
Resource.Error<Resource<List<Book>>>(
uiText = UiText.ExceptionString(e)
)
} catch (e: HttpException) {
Resource.Error<Resource<List<Book>>>(
uiText = UiText.ExceptionString(e)
)
} catch (e: Exception) {
Resource.Error<Resource<List<Book>>>(
uiText = UiText.ExceptionString(e)
)
}


}
}

class FindAllInLibraryBooks @Inject constructor(private val localBookRepository: LocalBookRepository) {
suspend operator fun invoke(): List<Book> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class SubscribeInLibraryBooks @Inject constructor(private val localBookRepositor
query: String,
sortType: SortType,
isAsc: Boolean,
unreadFilter: FilterType,
filter: List<FilterType>,
): Flow<List<Book>> = flow {
localBookRepository.subscribeAllInLibrary(
sortByLastRead = sortType == SortType.LastRead,
sortByAbs = sortType == SortType.Alphabetically,
sortByDateAdded = sortType == SortType.DateAdded,
sortByTotalChapter = sortType == SortType.TotalChapter,
unread = FilterType.Unread == unreadFilter,
isAsc = isAsc
sortByTotalChapters = sortType == SortType.TotalChapters,
unread = FilterType.Unread in filter,
isAsc = isAsc,
complete = FilterType.Completed in filter,
dateAdded = sortType == SortType.DateAdded,
dateFetched = sortType == SortType.DateFetched,
downloaded = FilterType.Downloaded in filter,
latestChapter = sortType == SortType.LatestChapter,
).collect { books ->
emit(books.filter { it.title.contains(query, true) })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ class OrientationUseCase @Inject constructor(
}


class FiltersUseCase @Inject constructor(
private val appPreferences: AppPreferences,
) {
fun save(value: Int) {
appPreferences.filterLibraryScreen().set(value)
}

fun read(): FilterType {
return mapFilterType(appPreferences.filterLibraryScreen().get())
}
}

class SortersUseCase @Inject constructor(
private val appPreferences: AppPreferences,
) {
Expand All @@ -48,16 +36,31 @@ class SortersUseCase @Inject constructor(
fun mapSortType(input: Int): SortType {
return when (input) {
0 -> {
SortType.DateAdded
SortType.Alphabetically
}
1 -> {
SortType.Alphabetically
SortType.LastRead
}
2 -> {
SortType.LastRead
SortType.LastChecked
}
3 -> {
SortType.Unread
}
4 -> {
SortType.TotalChapters
}
5 -> {
SortType.LatestChapter
}
6 -> {
SortType.DateFetched
}
7 -> {
SortType.DateAdded
}
else -> {
SortType.TotalChapter
SortType.LastRead
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/compose.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
compose = "1.2.0-alpha04"
compose = "1.2.0-alpha06"
activity = "1.5.0-alpha02"
lifecycle = "2.5.0-alpha02"

Expand Down
Loading

0 comments on commit c51a207

Please sign in to comment.