Skip to content

Commit

Permalink
Merge pull request #21 from paulcoding810/feat/history
Browse files Browse the repository at this point in the history
History feature
  • Loading branch information
paulcoding810 authored Dec 26, 2024
2 parents 8543bdf + c9d362c commit c24867c
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 20 deletions.
72 changes: 70 additions & 2 deletions app/schemas/com.paulcoding.hviewer.database.AppDatabase/4.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 4,
"identityHash": "9cae14220c3b54c73ec622e03a0d6f08",
"identityHash": "d30eb7c5c1c1735782bd48febc6f9594",
"entities": [
{
"tableName": "favorite_posts",
Expand Down Expand Up @@ -71,12 +71,80 @@
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "history",
"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`))",
"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": "site",
"columnName": "site",
"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
}
],
"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, '9cae14220c3b54c73ec622e03a0d6f08')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd30eb7c5c1c1735782bd48febc6f9594')"
]
}
}
150 changes: 150 additions & 0 deletions app/schemas/com.paulcoding.hviewer.database.AppDatabase/5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "d30eb7c5c1c1735782bd48febc6f9594",
"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`))",
"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": "site",
"columnName": "site",
"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
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"url"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "history",
"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`))",
"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": "site",
"columnName": "site",
"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
}
],
"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, 'd30eb7c5c1c1735782bd48febc6f9594')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ 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], version = 4, exportSchema = true)
@Database(entities = [PostItem::class, PostHistory::class], version = 5, exportSchema = true)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun favoritePostDao(): FavoritePostDao
abstract fun historyDao(): HistoryDao
}
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)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5)
.build()
}
return db!!
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/paulcoding/hviewer/database/HistoryDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.paulcoding.hviewer.database

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

@Dao
interface HistoryDao {
@Query("SELECT * FROM history ORDER BY createdAt DESC")
fun getAll(): Flow<List<PostHistory>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(history: PostHistory)

@Delete
suspend fun delete(history: PostHistory)

@Query("DELETE FROM history WHERE url = (SELECT url FROM history ORDER BY createdAt ASC LIMIT 1)")
suspend fun deleteOldest()
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ val MIGRATION_3_4 = object : Migration(3, 4) {
db.execSQL("ALTER TABLE favorite_posts ADD COLUMN views INTEGER")
db.execSQL("ALTER TABLE favorite_posts ADD COLUMN quantity INTEGER")
}
}

val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"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`))"
)
}
}
28 changes: 28 additions & 0 deletions app/src/main/java/com/paulcoding/hviewer/model/PostModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ data class PostItem(
val quantity: Int? = null,
)

// 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.paulcoding.hviewer.model.Tag
import com.paulcoding.hviewer.ui.component.HBackIcon
import com.paulcoding.hviewer.ui.component.HEmpty
import com.paulcoding.hviewer.ui.page.AppViewModel
import com.paulcoding.hviewer.ui.page.posts.PostCard
import com.paulcoding.hviewer.ui.page.posts.FavoriteCard
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -90,7 +90,7 @@ fun FavoriteItem(
onTagClick: (Tag) -> Unit,
deleteFavorite: () -> Unit
) {
PostCard(
FavoriteCard(
post, isFavorite = true,
setFavorite = {
deleteFavorite()
Expand Down
Loading

0 comments on commit c24867c

Please sign in to comment.