Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions app/schemas/com.starry.myne.database.MyneDatabase/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "ac3767d85a290abc83ef67a8ee50796f",
"entities": [
{
"tableName": "book_library",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`book_id` INTEGER NOT NULL, `title` TEXT NOT NULL, `authors` TEXT NOT NULL, `file_path` TEXT NOT NULL, `created_at` INTEGER NOT NULL, `is_external_book` INTEGER NOT NULL DEFAULT false, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "bookId",
"columnName": "book_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "authors",
"columnName": "authors",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "filePath",
"columnName": "file_path",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "createdAt",
"columnName": "created_at",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isImported",
"columnName": "is_external_book",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
},
{
"tableName": "reader_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`library_item_id` INTEGER NOT NULL, `last_chapter_index` INTEGER NOT NULL, `last_chapter_offset` INTEGER NOT NULL, `last_read_time` INTEGER NOT NULL DEFAULT 0, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "libraryItemId",
"columnName": "library_item_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "lastChapterIndex",
"columnName": "last_chapter_index",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "lastChapterOffset",
"columnName": "last_chapter_offset",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "lastReadTime",
"columnName": "last_read_time",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
},
{
"tableName": "bookmarks_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`book_id` INTEGER NOT NULL, `chapter_index` INTEGER NOT NULL, `chapter_title` TEXT NOT NULL, `chapter_offset` INTEGER NOT NULL, `created_at` INTEGER NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)",
"fields": [
{
"fieldPath": "bookId",
"columnName": "book_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "chapterIndex",
"columnName": "chapter_index",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "chapterTitle",
"columnName": "chapter_title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "chapterOffset",
"columnName": "chapter_offset",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "createdAt",
"columnName": "created_at",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
}
],
"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, 'ac3767d85a290abc83ef67a8ee50796f')"
]
}
}
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.progress.ProgressDao
import com.starry.myne.database.reader.ReaderDao
import com.starry.myne.helpers.PreferenceUtil
import com.starry.myne.ui.navigation.BottomBarScreen
import com.starry.myne.ui.navigation.Screens
Expand All @@ -44,7 +44,7 @@ import javax.inject.Inject
class MainViewModel @Inject constructor(
private val welcomeDataStore: WelcomeDataStore,
private val libraryDao: LibraryDao,
private val progressDao: ProgressDao,
private val readerDao: ReaderDao,
private val preferenceUtil: PreferenceUtil
) :
ViewModel() {
Expand Down Expand Up @@ -94,7 +94,7 @@ class MainViewModel @Inject constructor(
onComplete: (List<ShortcutInfo>) -> Unit
) {
viewModelScope.launch(Dispatchers.IO) {
val libraryItems = progressDao.getAllReaderItems()
val libraryItems = readerDao.getAllReaderItems()
.sortedByDescending { it.lastReadTime }
.take(limit - 1).mapNotNull {
libraryDao.getItemById(it.libraryItemId)
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/starry/myne/database/MyneDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ 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.progress.ProgressDao
import com.starry.myne.database.progress.ProgressData
import com.starry.myne.database.reader.ReaderProgress
import com.starry.myne.database.reader.ReaderBookmark
import com.starry.myne.database.reader.ReaderDao
import com.starry.myne.helpers.Constants

@Database(
entities = [LibraryItem::class, ProgressData::class],
version = 5,
entities = [LibraryItem::class, ReaderProgress::class, ReaderBookmark::class],
version = 6,
exportSchema = true,
autoMigrations = [
AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3),
AutoMigration(from = 4, to = 5),
AutoMigration(from = 5, to = 6),
]
)
abstract class MyneDatabase : RoomDatabase() {

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

companion object {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) [2022 - Present] Stɑrry Shivɑm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.starry.myne.database.reader

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "bookmarks_table")
data class ReaderBookmark(
@ColumnInfo(name = "book_id") val bookId: Int,
@ColumnInfo(name = "chapter_index") val chapterIndex: Int,
@ColumnInfo(name = "chapter_title") val chapterTitle: String,
@ColumnInfo(name = "chapter_offset") val chapterOffset: Int,
@ColumnInfo(name = "created_at") val createdAt: Long = System.currentTimeMillis()
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/


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

import androidx.room.Dao
import androidx.room.Insert
Expand All @@ -24,25 +23,37 @@ import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow


@Dao
interface ProgressDao {
interface ReaderDao {

// --- Progress related methods (moved from ProgressDao) ---

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(progressData: ProgressData)
fun insert(readerProgress: ReaderProgress)

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

@Update
fun update(progressData: ProgressData)
fun update(readerProgress: ReaderProgress)

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

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

@Query("SELECT * FROM reader_table WHERE library_item_id = :libraryItemId")
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ProgressData?>
}
fun getReaderDataAsFlow(libraryItemId: Int): Flow<ReaderProgress?>

// --- Bookmark related methods ---

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertBookmark(bookmark: ReaderBookmark)

@Query("DELETE FROM bookmarks_table WHERE id = :id")
suspend fun deleteBookmark(id: Int)

@Query("SELECT * FROM bookmarks_table WHERE book_id = :bookId ORDER BY created_at DESC")
fun getBookmarksForBook(bookId: Int): Flow<List<ReaderBookmark>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/


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

import androidx.room.ColumnInfo
import androidx.room.Entity
Expand All @@ -31,7 +31,7 @@ import java.util.Locale
* @param lastReadTime The time when the last chapter was read.
*/
@Entity(tableName = "reader_table")
data class ProgressData(
data class ReaderProgress(
@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
Loading
Loading