diff --git a/app/build.gradle b/app/build.gradle index 4891fcbfd..89c0b7d53 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,6 +3,10 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' +androidExtensions { + experimental = true +} + android { compileSdkVersion 28 defaultConfig { @@ -19,6 +23,19 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + + compileOptions { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' + } + +} + + +kapt { + arguments { + arg("room.schemaLocation", "$projectDir/schemas") + } } dependencies { diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt new file mode 100644 index 000000000..71cb9dbb1 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -0,0 +1,52 @@ +package io.legado.app.data + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase +import io.legado.app.data.dao.BookDao +import io.legado.app.data.entities.Book + + +@Database(entities = [Book::class], version = 1, exportSchema = true) +// @TypeConverters(Converters::class) +abstract class AppDatabase : RoomDatabase() { + + companion object { + private const val DATABASE_NAME = "legado.db" + + + + private val MIGRATION_1_2: Migration = object : Migration(1, 2) { + override fun migrate(database: SupportSQLiteDatabase) { + database.run { + // execSQL("ALTER TABLE parsers ADD COLUMN fulltextScript TEXT") + // execSQL("ALTER TABLE feeds ADD COLUMN lastUpdateTime INTEGER NOT NULL DEFAULT 0") + // execSQL("DELETE FROM entries WHERE rowid NOT IN (SELECT MIN(rowid) FROM entries GROUP BY link)") + // execSQL("CREATE UNIQUE INDEX index_entries_link ON entries(link)") + } + } + } + + fun createDatabase(context: Context): AppDatabase { + return Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, DATABASE_NAME) + // .addMigrations(MIGRATION_1_2) + // .addMigrations(MIGRATION_2_3) + // .addMigrations(MIGRATION_3_4) + // .addMigrations(MIGRATION_4_5) + // .addMigrations(MIGRATION_5_6) + .addCallback(object : Callback() { + override fun onCreate(db: SupportSQLiteDatabase) { + super.onCreate(db) + } + }) + .build() + } + + } + + abstract fun bookDao(): BookDao + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt new file mode 100644 index 000000000..b6ad0ba40 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -0,0 +1,19 @@ +package io.legado.app.data.dao + +import androidx.lifecycle.LiveData +import androidx.paging.DataSource +import androidx.room.Dao +import androidx.room.Query +import io.legado.app.data.entities.Book + + +@Dao +interface BookDao { + + @Query("SELECT * FROM books WHERE `group` = :group") + fun observeByGroup(group: Int): DataSource.Factory + + @Query("SELECT url FROM books WHERE `group` = :group") + fun observeUrlsByGroup(group: Int): LiveData> + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/Book.kt b/app/src/main/java/io/legado/app/data/entities/Book.kt index 8082605ab..800ea42d9 100644 --- a/app/src/main/java/io/legado/app/data/entities/Book.kt +++ b/app/src/main/java/io/legado/app/data/entities/Book.kt @@ -1,9 +1,9 @@ package io.legado.app.data.entities +import android.annotation.SuppressLint import android.os.Parcelable import androidx.room.* import kotlinx.android.parcel.Parcelize -import java.util.* @Parcelize @Entity(tableName = "books", @@ -18,9 +18,10 @@ data class Book(@PrimaryKey var introduction: String? = null, var charset: String? = null, var type: Int = 0, // 0: text, 1: audio + var group: Int = 0, // fenqu var latestChapterName: String? = null, - var lastUpdateTime: Date? = null, - var latestChapterTime: Date? = null, + var lastUpdateTime: Long? = null, + var latestChapterTime: Long? = null, var durChapterIndex: Int = 0, var durChapterPage: Int = 0, var totalChapterNum: Int = 0,