Skip to content

Commit

Permalink
[optimize] Optimize FeedView SQL; optimize media group name display
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Dec 28, 2024
1 parent d4a5659 commit 9c41fb8
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 47 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.anivu"
minSdk = 24
targetSdk = 35
versionCode = 24
versionName = "2.1-rc02"
versionCode = 25
versionName = "2.1-rc03"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/com/skyd/anivu/model/bean/feed/FeedViewBean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@ const val FEED_VIEW_NAME = "FeedView"
@Parcelize
@Serializable
@DatabaseView(
value = "SELECT $FEED_TABLE_NAME.*, IFNULL(ArticleCount.`count`, 0) AS ${FeedViewBean.ARTICLE_COUNT_COLUMN}, " +
"IFNULL(UnreadArticleCount.`count`, 0) AS ${FeedViewBean.UNREAD_ARTICLE_COUNT_COLUMN} " +
"FROM $FEED_TABLE_NAME LEFT JOIN (SELECT ${ArticleBean.FEED_URL_COLUMN}, COUNT(1) AS `count` " +
"FROM $ARTICLE_TABLE_NAME GROUP BY ${ArticleBean.FEED_URL_COLUMN}) AS ArticleCount " +
"ON $FEED_TABLE_NAME.${FeedBean.URL_COLUMN} = ArticleCount.${ArticleBean.FEED_URL_COLUMN} " +

"LEFT JOIN (SELECT ${ArticleBean.FEED_URL_COLUMN}, COUNT(1) AS `count` " +
"FROM $ARTICLE_TABLE_NAME WHERE ${ArticleBean.IS_READ_COLUMN} = 0 " +
"GROUP BY ${ArticleBean.FEED_URL_COLUMN}) AS UnreadArticleCount " +
"ON $FEED_TABLE_NAME.${FeedBean.URL_COLUMN} = UnreadArticleCount.${ArticleBean.FEED_URL_COLUMN}",
value = "SELECT " +
" $FEED_TABLE_NAME.*, " +
" IFNULL(ArticleCount.totalCount, 0) AS ${FeedViewBean.ARTICLE_COUNT_COLUMN}, " +
" IFNULL(ArticleCount.unreadCount, 0) AS ${FeedViewBean.UNREAD_ARTICLE_COUNT_COLUMN} " +
"FROM " +
" $FEED_TABLE_NAME " +
"LEFT JOIN (" +
" SELECT " +
" ${ArticleBean.FEED_URL_COLUMN}, " +
" COUNT(1) AS totalCount, " +
" COUNT(CASE WHEN ${ArticleBean.IS_READ_COLUMN} = 0 THEN 1 END) AS unreadCount " +
" FROM " +
" $ARTICLE_TABLE_NAME " +
" GROUP BY " +
" ${ArticleBean.FEED_URL_COLUMN}" +
") AS ArticleCount " +
" ON $FEED_TABLE_NAME.${FeedBean.URL_COLUMN} = ArticleCount.${ArticleBean.FEED_URL_COLUMN}",
viewName = FEED_VIEW_NAME
)
data class FeedViewBean(
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/skyd/anivu/model/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.skyd.anivu.model.db.migration.Migration11To12
import com.skyd.anivu.model.db.migration.Migration12To13
import com.skyd.anivu.model.db.migration.Migration13To14
import com.skyd.anivu.model.db.migration.Migration14To15
import com.skyd.anivu.model.db.migration.Migration15To16
import com.skyd.anivu.model.db.migration.Migration1To2
import com.skyd.anivu.model.db.migration.Migration2To3
import com.skyd.anivu.model.db.migration.Migration3To4
Expand Down Expand Up @@ -61,7 +62,7 @@ const val APP_DATA_BASE_FILE_NAME = "app.db"
RssMediaBean::class,
],
views = [FeedViewBean::class],
version = 15,
version = 16,
)
@TypeConverters(
value = [CategoriesConverter::class, RequestHeadersConverter::class]
Expand All @@ -86,7 +87,7 @@ abstract class AppDatabase : RoomDatabase() {
Migration1To2(), Migration2To3(), Migration3To4(), Migration4To5(),
Migration5To6(), Migration6To7(), Migration7To8(), Migration8To9(),
Migration9To10(), Migration10To11(), Migration11To12(), Migration12To13(),
Migration13To14(), Migration14To15(),
Migration13To14(), Migration14To15(), Migration15To16(),
)

fun getInstance(context: Context): AppDatabase {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.skyd.anivu.model.db.migration

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.skyd.anivu.model.bean.article.ARTICLE_TABLE_NAME
import com.skyd.anivu.model.bean.article.ArticleBean
import com.skyd.anivu.model.bean.feed.FEED_TABLE_NAME
import com.skyd.anivu.model.bean.feed.FEED_VIEW_NAME
import com.skyd.anivu.model.bean.feed.FeedBean
import com.skyd.anivu.model.bean.feed.FeedViewBean

class Migration15To16 : Migration(15, 16) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("DROP VIEW IF EXISTS `$FEED_VIEW_NAME`")
db.execSQL(
"CREATE VIEW `$FEED_VIEW_NAME` AS " +
"SELECT " +
" $FEED_TABLE_NAME.*, " +
" IFNULL(ArticleCount.totalCount, 0) AS ${FeedViewBean.ARTICLE_COUNT_COLUMN}, " +
" IFNULL(ArticleCount.unreadCount, 0) AS ${FeedViewBean.UNREAD_ARTICLE_COUNT_COLUMN} " +
"FROM " +
" $FEED_TABLE_NAME " +
"LEFT JOIN (" +
" SELECT " +
" ${ArticleBean.FEED_URL_COLUMN}, " +
" COUNT(1) AS totalCount, " +
" COUNT(CASE WHEN ${ArticleBean.IS_READ_COLUMN} = 0 THEN 1 END) AS unreadCount " +
" FROM " +
" $ARTICLE_TABLE_NAME " +
" GROUP BY " +
" ${ArticleBean.FEED_URL_COLUMN}" +
") AS ArticleCount " +
" ON $FEED_TABLE_NAME.${FeedBean.URL_COLUMN} = ArticleCount.${ArticleBean.FEED_URL_COLUMN}"
)
}
}
16 changes: 8 additions & 8 deletions app/src/main/java/com/skyd/anivu/ui/screen/media/MediaIntent.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.skyd.anivu.ui.screen.media

import com.skyd.anivu.base.mvi.MviIntent
import com.skyd.anivu.model.bean.MediaGroupBean
import com.skyd.anivu.model.bean.MediaBean
import com.skyd.anivu.model.bean.MediaGroupBean

sealed interface MediaIntent : MviIntent {
data class Init(val path: String?) : MediaIntent
data class Refresh(val path: String?) : MediaIntent
data class Init(val path: String) : MediaIntent
data class Refresh(val path: String) : MediaIntent
data class ChangeMediaGroup(
val path: String?,
val path: String,
val mediaBean: MediaBean,
val group: MediaGroupBean
) : MediaIntent

data class CreateGroup(val path: String?, val group: MediaGroupBean) : MediaIntent
data class DeleteGroup(val path: String?, val group: MediaGroupBean) : MediaIntent
data class RenameGroup(val path: String?, val group: MediaGroupBean, val newName: String) :
data class CreateGroup(val path: String, val group: MediaGroupBean) : MediaIntent
data class DeleteGroup(val path: String, val group: MediaGroupBean) : MediaIntent
data class RenameGroup(val path: String, val group: MediaGroupBean, val newName: String) :
MediaIntent

data class MoveFilesToGroup(
val path: String?,
val path: String,
val from: MediaGroupBean,
val to: MediaGroupBean
) : MediaIntent
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/skyd/anivu/ui/screen/media/MediaScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,18 @@ fun MediaScreen(path: String, viewModel: MediaViewModel = hiltViewModel()) {
topBar = {
AniVuTopBar(
style = AniVuTopBarStyle.Small,
title = { Text(text = stringResource(R.string.media_screen_name)) },
title = {
val title = stringResource(R.string.media_screen_name)
Text(
modifier = Modifier.basicMarquee(),
text = if (LocalMediaShowGroupTab.current) title else {
val groupName = uiState.groups
.getOrNull(pagerState.currentPage)?.first?.name
if (groupName.isNullOrBlank()) title else "$title - $groupName"
},
maxLines = 1,
)
},
colors = TopAppBarDefaults.topAppBarColors(
navigationIconContentColor = TopAppBarDefaults.topAppBarColors().actionIconContentColor
),
Expand Down
36 changes: 13 additions & 23 deletions app/src/main/java/com/skyd/anivu/ui/screen/media/MediaViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,53 +81,43 @@ class MediaViewModel @Inject constructor(
private fun Flow<MediaIntent>.toMediaPartialStateChangeFlow(): Flow<MediaPartialStateChange> {
return merge(
merge(
filterIsInstance<MediaIntent.Init>().filterNot { it.path.isNullOrBlank() },
filterIsInstance<MediaIntent.Refresh>().filterNot { it.path.isNullOrBlank() },
filterIsInstance<MediaIntent.Init>(),
filterIsInstance<MediaIntent.Refresh>(),
).flatMapConcat { intent ->
val path = if (intent is MediaIntent.Init) intent.path
else (intent as MediaIntent.Refresh).path
mediaRepo.requestGroups(path = path!!).map {
mediaRepo.requestGroups(path = path).map {
MediaPartialStateChange.GroupsResult.Success(groups = it)
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.GroupsResult.Failed(it.message.toString()) }
},
filterIsInstance<MediaIntent.ChangeMediaGroup>().filterNot {
it.path.isNullOrBlank()
}.flatMapConcat { intent ->
mediaRepo.changeMediaGroup(intent.path!!, intent.mediaBean, intent.group)
filterIsInstance<MediaIntent.ChangeMediaGroup>().flatMapConcat { intent ->
mediaRepo.changeMediaGroup(intent.path, intent.mediaBean, intent.group)
.map {
MediaPartialStateChange.ChangeMediaGroup.Success
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.ChangeMediaGroup.Failed(it.message.toString()) }
},
filterIsInstance<MediaIntent.DeleteGroup>().filterNot {
it.path.isNullOrBlank()
}.flatMapConcat { intent ->
mediaRepo.deleteGroup(intent.path!!, intent.group).map {
filterIsInstance<MediaIntent.DeleteGroup>().flatMapConcat { intent ->
mediaRepo.deleteGroup(intent.path, intent.group).map {
MediaPartialStateChange.DeleteGroup.Success(intent.group)
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.DeleteGroup.Failed(it.message.toString()) }
},
filterIsInstance<MediaIntent.CreateGroup>().filterNot {
it.path.isNullOrBlank()
}.flatMapConcat { intent ->
mediaRepo.createGroup(intent.path!!, intent.group).map {
filterIsInstance<MediaIntent.CreateGroup>().flatMapConcat { intent ->
mediaRepo.createGroup(intent.path, intent.group).map {
MediaPartialStateChange.CreateGroup.Success
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.CreateGroup.Failed(it.message.toString()) }
},
filterIsInstance<MediaIntent.MoveFilesToGroup>().filterNot {
it.path.isNullOrBlank()
}.flatMapConcat { intent ->
mediaRepo.moveFilesToGroup(intent.path!!, intent.from, intent.to).map {
filterIsInstance<MediaIntent.MoveFilesToGroup>().flatMapConcat { intent ->
mediaRepo.moveFilesToGroup(intent.path, intent.from, intent.to).map {
MediaPartialStateChange.MoveFilesToGroup.Success
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.MoveFilesToGroup.Failed(it.message.toString()) }
},
filterIsInstance<MediaIntent.RenameGroup>().filterNot {
it.path.isNullOrBlank()
}.flatMapConcat { intent ->
mediaRepo.renameGroup(intent.path!!, intent.group, intent.newName).map {
filterIsInstance<MediaIntent.RenameGroup>().flatMapConcat { intent ->
mediaRepo.renameGroup(intent.path, intent.group, intent.newName).map {
MediaPartialStateChange.EditGroup.Success(it)
}.startWith(MediaPartialStateChange.LoadingDialog.Show)
.catchMap { MediaPartialStateChange.EditGroup.Failed(it.message.toString()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sealed interface MediaListIntent : MviIntent {
data class Init(val path: String, val group: MediaGroupBean?, val version: Long?) :
MediaListIntent

data class Refresh(val path: String?, val group: MediaGroupBean?) : MediaListIntent
data class Refresh(val path: String, val group: MediaGroupBean?) : MediaListIntent
data class DeleteFile(val file: File) : MediaListIntent
data class RenameFile(val file: File, val newName: String) : MediaListIntent
data class SetFileDisplayName(val media: MediaBean, val displayName: String?) : MediaListIntent
Expand Down

0 comments on commit 9c41fb8

Please sign in to comment.