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
32 changes: 28 additions & 4 deletions app/schemas/com.paulcoding.hviewer.database.AppDatabase/5.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "d30eb7c5c1c1735782bd48febc6f9594",
"identityHash": "4e5007c1daa16a91f4f17cc9716ee605",
"entities": [
{
"tableName": "favorite_posts",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`url` TEXT NOT NULL, `name` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `site` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `tags` TEXT, `size` INTEGER, `views` INTEGER, `quantity` INTEGER, PRIMARY KEY(`url`))",
"tableName": "post_items",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`url` TEXT NOT NULL, `name` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `site` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `tags` TEXT, `size` INTEGER, `views` INTEGER, `quantity` INTEGER, `favorite` INTEGER NOT NULL, `favoriteAt` INTEGER NOT NULL, `viewed` INTEGER NOT NULL, `viewedAt` INTEGER NOT NULL, PRIMARY KEY(`url`))",
"fields": [
{
"fieldPath": "url",
Expand Down Expand Up @@ -61,6 +61,30 @@
"columnName": "quantity",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "favorite",
"columnName": "favorite",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "favoriteAt",
"columnName": "favoriteAt",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "viewed",
"columnName": "viewed",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "viewedAt",
"columnName": "viewedAt",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
Expand Down Expand Up @@ -144,7 +168,7 @@
"views": [],
"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, 'd30eb7c5c1c1735782bd48febc6f9594')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4e5007c1daa16a91f4f17cc9716ee605')"
]
}
}
100 changes: 100 additions & 0 deletions app/schemas/com.paulcoding.hviewer.database.AppDatabase/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "9a533ec4d7da1d93de96fb8ff43d17b9",
"entities": [
{
"tableName": "post_items",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`url` TEXT NOT NULL, `name` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `tags` TEXT, `size` INTEGER, `views` INTEGER, `quantity` INTEGER, `favorite` INTEGER NOT NULL, `favoriteAt` INTEGER NOT NULL, `viewed` INTEGER NOT NULL, `viewedAt` INTEGER NOT NULL, PRIMARY KEY(`url`))",
"fields": [
{
"fieldPath": "url",
"columnName": "url",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "thumbnail",
"columnName": "thumbnail",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "createdAt",
"columnName": "createdAt",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "tags",
"columnName": "tags",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "size",
"columnName": "size",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "views",
"columnName": "views",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "quantity",
"columnName": "quantity",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "favorite",
"columnName": "favorite",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "favoriteAt",
"columnName": "favoriteAt",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "viewed",
"columnName": "viewed",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "viewedAt",
"columnName": "viewedAt",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"url"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"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, '9a533ec4d7da1d93de96fb8ff43d17b9')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package com.paulcoding.hviewer.database
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.paulcoding.hviewer.model.PostHistory
import com.paulcoding.hviewer.model.PostItem

@Database(entities = [PostItem::class, PostHistory::class], version = 5, exportSchema = true)
@Database(entities = [PostItem::class], version = 6, exportSchema = true)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun favoritePostDao(): FavoritePostDao
abstract fun historyDao(): HistoryDao
abstract fun postItemDao(): PostItemDao
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object DatabaseProvider {
appContext,
AppDatabase::class.java, "hviewer_db"
)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6)
.build()
}
return db!!
Expand Down
21 changes: 0 additions & 21 deletions app/src/main/java/com/paulcoding/hviewer/database/FavoriteDao.kt

This file was deleted.

24 changes: 0 additions & 24 deletions app/src/main/java/com/paulcoding/hviewer/database/HistoryDao.kt

This file was deleted.

37 changes: 36 additions & 1 deletion app/src/main/java/com/paulcoding/hviewer/database/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,39 @@ val MIGRATION_4_5 = object : Migration(4, 5) {
"CREATE TABLE IF NOT EXISTS `history` (`url` TEXT NOT NULL, `name` TEXT NOT NULL, `thumbnail` TEXT NOT NULL, `site` TEXT NOT NULL, `createdAt` INTEGER NOT NULL, `tags` TEXT, `size` INTEGER, `views` INTEGER, `quantity` INTEGER, PRIMARY KEY(`url`))"
)
}
}
}

val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(db: SupportSQLiteDatabase) {
// Step 1: Create a new table post_items
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS post_items (
url TEXT NOT NULL,
name TEXT NOT NULL,
thumbnail TEXT NOT NULL,
createdAt INTEGER NOT NULL,
tags TEXT,
size INTEGER,
views INTEGER,
quantity INTEGER,
favorite INTEGER NOT NULL,
favoriteAt INTEGER NOT NULL,
viewed INTEGER NOT NULL,
viewedAt INTEGER NOT NULL,
PRIMARY KEY(`url`)
)
""".trimIndent()
)

// Step 2: Copy data from favorite_posts to post_items
db.execSQL("""
INSERT INTO post_items (url, name, thumbnail, createdAt, tags, size, views, quantity, favorite, favoriteAt, viewed, viewedAt)
SELECT url, name, thumbnail, createdAt, tags, size, views, quantity, 1, createdAt, 1, createdAt FROM favorite_posts
""".trimIndent())

// Step 3: Drop tables
db.execSQL("DROP TABLE favorite_posts")
db.execSQL("DROP TABLE history")
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/paulcoding/hviewer/database/PostItemDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.paulcoding.hviewer.database

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.paulcoding.hviewer.model.PostItem
import kotlinx.coroutines.flow.Flow

@Dao
interface PostItemDao {
@Query("SELECT * FROM post_items WHERE favorite = 1 ORDER BY favoriteAt DESC")
fun getFavoritePosts(): Flow<List<PostItem>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addPost(postItem: PostItem)

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addPosts(postItems: List<PostItem>)

@Query("SELECT * FROM post_items where viewed = 1 ORDER BY viewedAt DESC LIMIT 20")
fun getViewedPosts(): Flow<List<PostItem>>

@Update
suspend fun updatePost(postItem: PostItem)
}
35 changes: 5 additions & 30 deletions app/src/main/java/com/paulcoding/hviewer/model/PostModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ data class PostData(
)


@Entity(tableName = "favorite_posts")
@Entity(tableName = "post_items")
data class PostItem(
@PrimaryKey
val url: String = "",
val name: String = "",
val thumbnail: String = "",
val site: String = "",
val createdAt: Long = 0,
val tags: List<Tag>? = null,
val size: Int? = null,
val views: Int? = null,
val quantity: Int? = null,
val favorite: Boolean = false,
val favoriteAt: Long = 0,
val viewed: Boolean = false,
val viewedAt: Long = 0,
) {
fun getHost(): String {
return url.split("/").getOrNull(2) ?: ""
Expand All @@ -33,34 +36,6 @@ data class PostItem(
}
}

// duplicated?
@Entity(tableName = "history")
data class PostHistory(
@PrimaryKey
val url: String = "",
val name: String = "",
val thumbnail: String = "",
val site: String = "",
val createdAt: Long = 0,
val tags: List<Tag>? = null,
val size: Int? = null,
val views: Int? = null,
val quantity: Int? = null,
) {
fun toPostItem(): PostItem {
return PostItem(
url = url,
name = name,
thumbnail = thumbnail,
site = site,
createdAt = createdAt,
tags = tags,
size = size,
views = views
)
}
}

data class Tag(
val name: String = "",
val url: String = "",
Expand Down
Loading