Skip to content

Commit

Permalink
[feature|fix] Support statistics of recently shared stickers; fix a d…
Browse files Browse the repository at this point in the history
…atabase singleton code bug; fix sticker color theme bug
  • Loading branch information
SkyD666 committed Apr 11, 2024
1 parent 960611a commit 27e75b8
Show file tree
Hide file tree
Showing 27 changed files with 331 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
applicationId = "com.skyd.rays"
minSdk = 24
targetSdk = 34
versionCode = 62
versionName = "2.2-beta02"
versionCode = 63
versionName = "2.2-beta03"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/skyd/rays/di/DatabaseModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.skyd.rays.model.db.dao.ApiGrantPackageDao
import com.skyd.rays.model.db.dao.SearchDomainDao
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.db.dao.UriStringSharePackageDao
import com.skyd.rays.model.db.dao.cache.StickerShareTimeDao
import com.skyd.rays.model.db.dao.sticker.MimeTypeDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import dagger.Module
Expand Down Expand Up @@ -48,6 +49,10 @@ object DatabaseModule {

@Provides
@Singleton
fun provideMimeTypeDao(database: AppDatabase): MimeTypeDao =
database.mimeTypeDao()
fun provideMimeTypeDao(database: AppDatabase): MimeTypeDao = database.mimeTypeDao()

@Provides
@Singleton
fun provideStickerShareTimeDao(database: AppDatabase): StickerShareTimeDao =
database.stickerShareTimeDao()
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/skyd/rays/model/bean/StickerShareTimeBean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.skyd.rays.model.bean

import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import com.skyd.rays.base.BaseBean
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Serializable

const val STICKER_SHARE_TIME_TABLE_NAME = "StickerShareTime"

@Parcelize
@Serializable
@Entity(
tableName = STICKER_SHARE_TIME_TABLE_NAME,
primaryKeys = [
StickerShareTimeBean.STICKER_UUID_COLUMN,
StickerShareTimeBean.SHARE_TIME_COLUMN
],
foreignKeys = [
ForeignKey(
entity = StickerBean::class,
parentColumns = [StickerBean.UUID_COLUMN],
childColumns = [StickerShareTimeBean.STICKER_UUID_COLUMN],
onDelete = ForeignKey.CASCADE
)
]
)
data class StickerShareTimeBean(
@ColumnInfo(name = STICKER_UUID_COLUMN)
var stickerUuid: String,
@ColumnInfo(name = SHARE_TIME_COLUMN)
var shareTime: Long,
) : BaseBean, Parcelable {
companion object {
const val STICKER_UUID_COLUMN = "stickerUuid"
const val SHARE_TIME_COLUMN = "shareTime"
}
}

28 changes: 19 additions & 9 deletions app/src/main/java/com/skyd/rays/model/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import com.skyd.rays.model.bean.ApiGrantPackageBean
import com.skyd.rays.model.bean.MimeTypeBean
import com.skyd.rays.model.bean.SearchDomainBean
import com.skyd.rays.model.bean.StickerBean
import com.skyd.rays.model.bean.StickerShareTimeBean
import com.skyd.rays.model.bean.TagBean
import com.skyd.rays.model.bean.UriStringSharePackageBean
import com.skyd.rays.model.db.dao.ApiGrantPackageDao
import com.skyd.rays.model.db.dao.SearchDomainDao
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.db.dao.UriStringSharePackageDao
import com.skyd.rays.model.db.dao.cache.StickerShareTimeDao
import com.skyd.rays.model.db.dao.sticker.MimeTypeDao
import com.skyd.rays.model.db.dao.sticker.StickerDao
import com.skyd.rays.model.db.migration.Migration1To2
Expand All @@ -23,6 +25,7 @@ import com.skyd.rays.model.db.migration.Migration3To4
import com.skyd.rays.model.db.migration.Migration4To5
import com.skyd.rays.model.db.migration.Migration5To6
import com.skyd.rays.model.db.migration.Migration6To7
import com.skyd.rays.model.db.migration.Migration7To8

const val APP_DATA_BASE_FILE_NAME = "app.db"

Expand All @@ -34,8 +37,9 @@ const val APP_DATA_BASE_FILE_NAME = "app.db"
UriStringSharePackageBean::class,
ApiGrantPackageBean::class,
MimeTypeBean::class,
StickerShareTimeBean::class,
],
version = 7
version = 8
)
@TypeConverters(
value = []
Expand All @@ -48,26 +52,32 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun uriStringSharePackageDao(): UriStringSharePackageDao
abstract fun apiGrantPackageDao(): ApiGrantPackageDao
abstract fun mimeTypeDao(): MimeTypeDao
abstract fun stickerShareTimeDao(): StickerShareTimeDao

companion object {
@Volatile
private var instance: AppDatabase? = null

private val migrations = arrayOf(
Migration1To2(), Migration2To3(), Migration3To4(), Migration4To5(), Migration5To6(),
Migration6To7(),
Migration6To7(), Migration7To8(),
)

fun getInstance(context: Context): AppDatabase {
return if (instance == null) {
synchronized(this) {
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
APP_DATA_BASE_FILE_NAME
)
.addMigrations(*migrations)
.build()
if (instance == null) {
Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
APP_DATA_BASE_FILE_NAME
)
.addMigrations(*migrations)
.build()
.apply { instance = this }
} else {
instance as AppDatabase
}
}
} else {
instance as AppDatabase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.skyd.rays.model.db.dao.cache

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.skyd.rays.model.bean.STICKER_SHARE_TIME_TABLE_NAME
import com.skyd.rays.model.bean.StickerShareTimeBean

@Dao
interface StickerShareTimeDao {
@Transaction
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun updateShareTime(stickerShareTimeBean: StickerShareTimeBean)

@Transaction
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun updateShareTime(stickerShareTimeList: List<StickerShareTimeBean>)

@Transaction
@Query(
"""
SELECT ${StickerShareTimeBean.SHARE_TIME_COLUMN} FROM $STICKER_SHARE_TIME_TABLE_NAME
WHERE ${StickerShareTimeBean.STICKER_UUID_COLUMN} LIKE :uuid
"""
)
fun getShareTimeByUuid(uuid: String): List<Long>

@Transaction
@Query("DELETE FROM $STICKER_SHARE_TIME_TABLE_NAME")
fun deleteAll(): Int

@Transaction
@Query(
"""
DELETE FROM $STICKER_SHARE_TIME_TABLE_NAME
WHERE ${StickerShareTimeBean.STICKER_UUID_COLUMN} LIKE :uuid
"""
)
fun deleteShareTimeByUuid(uuid: String): Int

@Transaction
@Query(
"""
DELETE FROM $STICKER_SHARE_TIME_TABLE_NAME
WHERE ${StickerShareTimeBean.SHARE_TIME_COLUMN} < :timestamp
"""
)
fun deleteShareTimeBefore(timestamp: Long): Int
}
38 changes: 35 additions & 3 deletions app/src/main/java/com/skyd/rays/model/db/dao/sticker/StickerDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.skyd.rays.appContext
import com.skyd.rays.config.STICKER_DIR
import com.skyd.rays.ext.dataStore
import com.skyd.rays.ext.getOrDefault
import com.skyd.rays.model.bean.STICKER_SHARE_TIME_TABLE_NAME
import com.skyd.rays.model.bean.STICKER_TABLE_NAME
import com.skyd.rays.model.bean.StickerBean
import com.skyd.rays.model.bean.StickerBean.Companion.CLICK_COUNT_COLUMN
Expand All @@ -20,10 +21,12 @@ import com.skyd.rays.model.bean.StickerBean.Companion.MODIFY_TIME_COLUMN
import com.skyd.rays.model.bean.StickerBean.Companion.SHARE_COUNT_COLUMN
import com.skyd.rays.model.bean.StickerBean.Companion.STICKER_MD5_COLUMN
import com.skyd.rays.model.bean.StickerBean.Companion.UUID_COLUMN
import com.skyd.rays.model.bean.StickerShareTimeBean
import com.skyd.rays.model.bean.StickerWithTags
import com.skyd.rays.model.bean.StickerWithTagsAndFile
import com.skyd.rays.model.bean.TagBean
import com.skyd.rays.model.db.dao.TagDao
import com.skyd.rays.model.db.dao.cache.StickerShareTimeDao
import com.skyd.rays.model.preference.CurrentStickerUuidPreference
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
Expand All @@ -41,6 +44,7 @@ interface StickerDao {
@InstallIn(SingletonComponent::class)
interface StickerDaoEntryPoint {
val tagDao: TagDao
val stickerShareTimeDao: StickerShareTimeDao
}

@Transaction
Expand Down Expand Up @@ -105,14 +109,31 @@ interface StickerDao {

@Transaction
@Query(
"""SELECT *
"""
SELECT *
FROM $STICKER_TABLE_NAME
ORDER BY $SHARE_COUNT_COLUMN DESC
LIMIT :count
"""
)
fun getMostSharedStickersList(count: Int): Flow<List<StickerWithTags>>

@Transaction
@Query(
"""
SELECT * FROM $STICKER_TABLE_NAME LEFT JOIN (
SELECT *, MAX(${StickerShareTimeBean.SHARE_TIME_COLUMN})
FROM $STICKER_SHARE_TIME_TABLE_NAME
GROUP BY ${StickerShareTimeBean.STICKER_UUID_COLUMN}
ORDER BY ${StickerShareTimeBean.SHARE_TIME_COLUMN} DESC
LIMIT :count
) AS shareTime
WHERE $UUID_COLUMN = shareTime.${StickerShareTimeBean.STICKER_UUID_COLUMN}
ORDER BY ${StickerShareTimeBean.SHARE_TIME_COLUMN} DESC
"""
)
fun getRecentSharedStickers(count: Int): Flow<List<StickerWithTags>>

@Transaction
@Query("SELECT $UUID_COLUMN FROM $STICKER_TABLE_NAME WHERE $STICKER_MD5_COLUMN LIKE :stickerMd5")
fun containsByMd5(stickerMd5: String): String?
Expand All @@ -137,6 +158,17 @@ interface StickerDao {
)
fun addShareCount(uuids: List<String>, count: Int = 1): Int

@Transaction
fun shareStickers(uuids: List<String>, count: Int = 1) {
val hiltEntryPoint = EntryPointAccessors
.fromApplication(appContext, StickerDaoEntryPoint::class.java)
val currentTimeMillis = System.currentTimeMillis()
addShareCount(uuids, count)
hiltEntryPoint.stickerShareTimeDao.updateShareTime(uuids.map { stickerUuid ->
StickerShareTimeBean(stickerUuid, currentTimeMillis)
})
}

@Transaction
@Query("SELECT * FROM $STICKER_TABLE_NAME ORDER BY $SHARE_COUNT_COLUMN DESC LIMIT :count")
fun getPopularStickersList(count: Int = 15): Flow<List<StickerWithTags>>
Expand All @@ -150,8 +182,8 @@ interface StickerDao {
if (updateModifyTime) {
stickerWithTags.sticker.modifyTime = System.currentTimeMillis()
}
val hiltEntryPoint =
EntryPointAccessors.fromApplication(appContext, StickerDaoEntryPoint::class.java)
val hiltEntryPoint = EntryPointAccessors
.fromApplication(appContext, StickerDaoEntryPoint::class.java)
var stickerUuid = stickerWithTags.sticker.uuid
runCatching {
UUID.fromString(stickerUuid)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.skyd.rays.model.db.migration

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.skyd.rays.model.bean.STICKER_SHARE_TIME_TABLE_NAME
import com.skyd.rays.model.bean.STICKER_TABLE_NAME
import com.skyd.rays.model.bean.StickerBean
import com.skyd.rays.model.bean.StickerShareTimeBean

class Migration7To8 : Migration(7, 8) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"""
CREATE TABLE $STICKER_SHARE_TIME_TABLE_NAME (
${StickerShareTimeBean.STICKER_UUID_COLUMN} TEXT NOT NULL,
${StickerShareTimeBean.SHARE_TIME_COLUMN} INTEGER NOT NULL,
PRIMARY KEY (${StickerShareTimeBean.STICKER_UUID_COLUMN}, ${StickerShareTimeBean.SHARE_TIME_COLUMN})
FOREIGN KEY (${StickerShareTimeBean.STICKER_UUID_COLUMN})
REFERENCES $STICKER_TABLE_NAME(${StickerBean.UUID_COLUMN})
ON DELETE CASCADE
)
"""
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlinx.coroutines.launch

object CustomPrimaryColorPreference : BasePreference<String> {
private const val CUSTOM_PRIMARY_COLOR = "customPrimaryColor"
override val default = ""
override val default = "62539F"

val key = stringPreferencesKey(CUSTOM_PRIMARY_COLOR)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.skyd.rays.model.preference.theme
import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import com.skyd.rays.ext.dataStore
import com.skyd.rays.ext.put
import com.skyd.rays.model.preference.BasePreference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -18,7 +18,10 @@ object StickerColorThemePreference : BasePreference<Boolean> {

fun put(context: Context, scope: CoroutineScope, value: Boolean) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
context.dataStore.edit { pref ->
pref[key] = value
if (value) pref[ThemeNamePreference.key] = ThemeNamePreference.CUSTOM_THEME_NAME
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.skyd.rays.model.preference.theme
import android.content.Context
import androidx.compose.ui.graphics.Color
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.skyd.rays.ext.dataStore
import com.skyd.rays.ext.getOrDefault
import com.skyd.rays.ext.put
import com.skyd.rays.model.preference.BasePreference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,7 +19,7 @@ object ThemeNamePreference : BasePreference<String> {
val values: List<ThemeItem> = mutableListOf(
ThemeItem(
name = "Purple",
keyColor = Color(0xFF62539f)
keyColor = Color(0xFF62539F)
),
ThemeItem(
name = "Blue",
Expand All @@ -45,7 +45,10 @@ object ThemeNamePreference : BasePreference<String> {

fun put(context: Context, scope: CoroutineScope, value: String) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
context.dataStore.edit { pref ->
pref[key] = value
pref[StickerColorThemePreference.key] = false
}
}
}

Expand Down
Loading

0 comments on commit 27e75b8

Please sign in to comment.