Skip to content

Commit

Permalink
refactor: Cleanup and improve state management logic in reader (#228)
Browse files Browse the repository at this point in the history
* Remove the existing reader bottom sheet menu for now, to add a better option later.
* Change reader data to progress data
* improve state management logic in reader detail screen
---------

Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Sep 27, 2024
1 parent 4ad8b45 commit b62d804
Show file tree
Hide file tree
Showing 19 changed files with 638 additions and 833 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

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

2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</activity>

<activity
android:name=".ui.screens.reader.activities.ReaderActivity"
android:name=".ui.screens.reader.main.activities.ReaderActivity"
android:configChanges="orientation|screenSize|uiMode|locale|layoutDirection|screenLayout"
android:exported="true">
<intent-filter>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/starry/myne/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.starry.myne.database.library.LibraryDao
import com.starry.myne.database.reader.ReaderDao
import com.starry.myne.database.progress.ProgressDao
import com.starry.myne.ui.navigation.BottomBarScreen
import com.starry.myne.ui.navigation.Screens
import com.starry.myne.ui.screens.welcome.viewmodels.WelcomeDataStore
Expand All @@ -43,7 +43,7 @@ import javax.inject.Inject
class MainViewModel @Inject constructor(
private val welcomeDataStore: WelcomeDataStore,
private val libraryDao: LibraryDao,
private val readerDao: ReaderDao
private val progressDao: ProgressDao
) :
ViewModel() {
private val _isLoading: MutableState<Boolean> = mutableStateOf(true)
Expand Down Expand Up @@ -86,7 +86,7 @@ class MainViewModel @Inject constructor(
onComplete: (List<ShortcutInfo>) -> Unit
) {
viewModelScope.launch(Dispatchers.IO) {
val libraryItems = readerDao.getAllReaderItems()
val libraryItems = progressDao.getAllReaderItems()
.sortedByDescending { it.lastReadTime }
.take(limit - 1).mapNotNull {
libraryDao.getItemById(it.libraryItemId)
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/starry/myne/database/MyneDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import com.starry.myne.database.library.LibraryDao
import com.starry.myne.database.library.LibraryItem
import com.starry.myne.database.reader.ReaderDao
import com.starry.myne.database.reader.ReaderData
import com.starry.myne.database.progress.ProgressDao
import com.starry.myne.database.progress.ProgressData
import com.starry.myne.helpers.Constants

@Database(
entities = [LibraryItem::class, ReaderData::class],
entities = [LibraryItem::class, ProgressData::class],
version = 5,
exportSchema = true,
autoMigrations = [
Expand All @@ -41,7 +41,7 @@ import com.starry.myne.helpers.Constants
abstract class MyneDatabase : RoomDatabase() {

abstract fun getLibraryDao(): LibraryDao
abstract fun getReaderDao(): ReaderDao
abstract fun getReaderDao(): ProgressDao

companion object {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ import java.text.StringCharacterIterator
import java.util.Date
import java.util.Locale

/**
* Data class for storing the library items (books).
*
* @param bookId The ID of the book.
* @param title The title of the book.
* @param authors The authors of the book.
* @param filePath The path to the book file.
* @param createdAt The time when the book was added to the library.
*/
@Entity(tableName = "book_library")
data class LibraryItem(
@ColumnInfo(name = "book_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/


package com.starry.myne.database.reader
package com.starry.myne.database.progress

import androidx.room.Dao
import androidx.room.Insert
Expand All @@ -26,23 +26,23 @@ import kotlinx.coroutines.flow.Flow


@Dao
interface ReaderDao {
interface ProgressDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(readerData: ReaderData)
fun insert(progressData: ProgressData)

@Query("DELETE FROM reader_table WHERE library_item_id = :libraryItemId")
fun delete(libraryItemId: Int)

@Update
fun update(readerData: ReaderData)
fun update(progressData: ProgressData)

@Query("SELECT * FROM reader_table WHERE library_item_id = :libraryItemId")
fun getReaderData(libraryItemId: Int): ReaderData?
fun getReaderData(libraryItemId: Int): ProgressData?

@Query("SELECT * FROM reader_table")
fun getAllReaderItems(): List<ReaderData>
fun getAllReaderItems(): List<ProgressData>

@Query("SELECT * FROM reader_table WHERE library_item_id = :libraryItemId")
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ReaderData>?
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ProgressData>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
*/


package com.starry.myne.database.reader
package com.starry.myne.database.progress

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.Locale

/**
* Data class for storing the reading progress of a library item (book).
*
* @param libraryItemId The ID of the library item.
* @param lastChapterIndex The index of the last chapter read.
* @param lastChapterOffset The offset of the last chapter read.
* @param lastReadTime The time when the last chapter was read.
*/
@Entity(tableName = "reader_table")
data class ReaderData(
data class ProgressData(
@ColumnInfo(name = "library_item_id") val libraryItemId: Int,
@ColumnInfo(name = "last_chapter_index") val lastChapterIndex: Int,
@ColumnInfo(name = "last_chapter_offset") val lastChapterOffset: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fun BookDetailTopUI(
color = MaterialTheme.colorScheme.onBackground,
)

progressPercent?.let {
if (!progressPercent.isNullOrBlank()) {
Text(
text = "$progressPercent% Completed",
modifier = Modifier.padding(start = 12.dp, end = 8.dp),
Expand All @@ -167,7 +167,6 @@ fun BookDetailTopUI(
color = MaterialTheme.colorScheme.onBackground,
)
}

Spacer(modifier = Modifier.height(50.dp))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.starry.myne.ui.screens.categories.composables.CategoryDetailScreen
import com.starry.myne.ui.screens.detail.composables.BookDetailScreen
import com.starry.myne.ui.screens.home.composables.HomeScreen
import com.starry.myne.ui.screens.library.composables.LibraryScreen
import com.starry.myne.ui.screens.reader.composables.ReaderDetailScreen
import com.starry.myne.ui.screens.reader.detail.ReaderDetailScreen
import com.starry.myne.ui.screens.settings.composables.AboutScreen
import com.starry.myne.ui.screens.settings.composables.OSLScreen
import com.starry.myne.ui.screens.settings.composables.SettingsScreen
Expand Down
Loading

0 comments on commit b62d804

Please sign in to comment.