Skip to content

Commit

Permalink
[feature|optimize|fix] Supports setting the max cache size and max ba…
Browse files Browse the repository at this point in the history
…ck cache size of the player; Search, Article screen support show empty tips; fix long title on media library screen; optimize code
  • Loading branch information
SkyD666 committed Aug 18, 2024
1 parent ebe58b0 commit 9e3178c
Show file tree
Hide file tree
Showing 23 changed files with 491 additions and 180 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = 22
versionName = "2.1-alpha15"
versionName = "2.1-alpha16"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/skyd/anivu/ext/CollectionExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ fun <K, V> snapshotStateMapSaver() = Saver<SnapshotStateMap<K, V>, Any>(
@Suppress("UNCHECKED_CAST")
(value as? List<Pair<K, V>>)?.toMutableStateMap() ?: mutableStateMapOf()
}
)
)

fun calculateHashMapInitialCapacity(
initialCapacity: Int,
loadFactor: Float = 0.75f,
): Int = (initialCapacity / loadFactor).toInt() + 1
2 changes: 1 addition & 1 deletion app/src/main/java/com/skyd/anivu/ext/NumberExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.text.format.Formatter
import android.util.TypedValue

fun Long.fileSize(context: Context): String =
Formatter.formatShortFileSize(context, this)
Formatter.formatFileSize(context, this)

val Float.dp: Float
get() = TypedValue.applyDimension(
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/PreferenceExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import com.skyd.anivu.model.preference.data.medialib.MediaLibLocationPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerAutoPipPreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerMaxBackCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerMaxCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
import com.skyd.anivu.model.preference.proxy.ProxyHostnamePreference
Expand Down Expand Up @@ -97,6 +99,8 @@ fun Preferences.toSettings(): Settings {
playerShowScreenshotButton = PlayerShowScreenshotButtonPreference.fromPreferences(this),
hardwareDecode = HardwareDecodePreference.fromPreferences(this),
playerAutoPip = PlayerAutoPipPreference.fromPreferences(this),
playerMaxCacheSize = PlayerMaxCacheSizePreference.fromPreferences(this),
playerMaxBackCacheSize = PlayerMaxBackCacheSizePreference.fromPreferences(this),

// Data
useAutoDelete = UseAutoDeletePreference.fromPreferences(this),
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/skyd/anivu/model/preference/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.skyd.anivu.model.preference.data.medialib.MediaLibLocationPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerAutoPipPreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerMaxBackCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerMaxCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
import com.skyd.anivu.model.preference.proxy.ProxyHostnamePreference
Expand Down Expand Up @@ -79,6 +81,8 @@ import com.skyd.anivu.ui.local.LocalParseLinkTagAsEnclosure
import com.skyd.anivu.ui.local.LocalPickImageMethod
import com.skyd.anivu.ui.local.LocalPlayerAutoPip
import com.skyd.anivu.ui.local.LocalPlayerDoubleTap
import com.skyd.anivu.ui.local.LocalPlayerMaxBackCacheSize
import com.skyd.anivu.ui.local.LocalPlayerMaxCacheSize
import com.skyd.anivu.ui.local.LocalPlayerShow85sButton
import com.skyd.anivu.ui.local.LocalPlayerShowScreenshotButton
import com.skyd.anivu.ui.local.LocalProxyHostname
Expand Down Expand Up @@ -144,6 +148,8 @@ data class Settings(
val playerShowScreenshotButton: Boolean = PlayerShowScreenshotButtonPreference.default,
val hardwareDecode: Boolean = HardwareDecodePreference.default,
val playerAutoPip: Boolean = PlayerAutoPipPreference.default,
val playerMaxCacheSize: Long = PlayerMaxCacheSizePreference.default,
val playerMaxBackCacheSize: Long = PlayerMaxBackCacheSizePreference.default,
// Data
val useAutoDelete: Boolean = UseAutoDeletePreference.default,
val autoDeleteArticleFrequency: Long = AutoDeleteArticleFrequencyPreference.default,
Expand Down Expand Up @@ -209,6 +215,8 @@ fun SettingsProvider(
LocalPlayerShowScreenshotButton provides settings.playerShowScreenshotButton,
LocalHardwareDecode provides settings.hardwareDecode,
LocalPlayerAutoPip provides settings.playerAutoPip,
LocalPlayerMaxCacheSize provides settings.playerMaxCacheSize,
LocalPlayerMaxBackCacheSize provides settings.playerMaxBackCacheSize,
// Data
LocalUseAutoDelete provides settings.useAutoDelete,
LocalAutoDeleteArticleFrequency provides settings.autoDeleteArticleFrequency,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skyd.anivu.model.preference.player

import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.longPreferencesKey
import com.skyd.anivu.base.BasePreference
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object PlayerMaxBackCacheSizePreference : BasePreference<Long> {
private const val PLAYER_MAX_CACHE_SIZE = "playerMaxBackCacheSize"
override val default = 20L * 1024 * 1024 // 20 MB

val key = longPreferencesKey(PLAYER_MAX_CACHE_SIZE)

fun put(context: Context, scope: CoroutineScope, value: Long) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
}
}

override fun fromPreferences(preferences: Preferences): Long = preferences[key] ?: default
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.skyd.anivu.model.preference.player

import android.content.Context
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.longPreferencesKey
import com.skyd.anivu.base.BasePreference
import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.put
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object PlayerMaxCacheSizePreference : BasePreference<Long> {
private const val PLAYER_MAX_CACHE_SIZE = "playerMaxCacheSize"
override val default = 10L * 1024 * 1024 // 10 MB

val key = longPreferencesKey(PLAYER_MAX_CACHE_SIZE)

fun put(context: Context, scope: CoroutineScope, value: Long) {
scope.launch(Dispatchers.IO) {
context.dataStore.put(key, value)
}
}

override fun fromPreferences(preferences: Preferences): Long = preferences[key] ?: default
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.skyd.anivu.model.repository.feed

import com.skyd.anivu.base.BaseRepository
import com.skyd.anivu.ext.calculateHashMapInitialCapacity
import com.skyd.anivu.model.bean.GroupBean
import com.skyd.anivu.model.bean.GroupVo
import com.skyd.anivu.model.bean.GroupWithFeedBean
Expand All @@ -17,15 +18,19 @@ class ReorderGroupRepository @Inject constructor(
) : BaseRepository() {
private fun sortGroups(groupList: List<GroupBean>): List<GroupBean> {
val groupsMap = groupList.associateBy { it.groupId }
val hasPreviousGroups = mutableSetOf<GroupBean>()
val hasPreviousGroups = LinkedHashSet<GroupBean>(
calculateHashMapInitialCapacity(groupList.size)
)
groupList.forEach { group ->
val nextGroupId = group.nextGroupId
if (nextGroupId != null) {
hasPreviousGroups.add(groupsMap[nextGroupId]!!)
}
}
val noPreviousGroups = (groupList - hasPreviousGroups).sortedBy { it.name }
val sortedList = mutableSetOf<GroupBean>()
val sortedList = LinkedHashSet<GroupBean>(
calculateHashMapInitialCapacity(groupList.size)
)
noPreviousGroups.forEach { group ->
var currentGroup: GroupBean? = group
var currentGroupId: String? = group.groupId
Expand All @@ -44,15 +49,19 @@ class ReorderGroupRepository @Inject constructor(
groupList: List<GroupWithFeedBean>
): List<GroupWithFeedBean> {
val groupsMap = groupList.associateBy { it.group.groupId }
val hasPreviousGroups = mutableSetOf<GroupWithFeedBean>()
val hasPreviousGroups = LinkedHashSet<GroupWithFeedBean>(
calculateHashMapInitialCapacity(groupList.size)
)
groupList.forEach { group ->
val nextGroupId = group.group.nextGroupId
if (nextGroupId != null) {
hasPreviousGroups.add(groupsMap[nextGroupId]!!)
}
}
val noPreviousGroups = (groupList - hasPreviousGroups).sortedBy { it.group.name }
val sortedList = mutableSetOf<GroupWithFeedBean>()
val sortedList = LinkedHashSet<GroupWithFeedBean>(
calculateHashMapInitialCapacity(groupList.size)
)
noPreviousGroups.forEach { group ->
var currentGroup: GroupWithFeedBean? = group
var currentGroupId: String? = group.group.groupId
Expand Down

This file was deleted.

108 changes: 108 additions & 0 deletions app/src/main/java/com/skyd/anivu/ui/component/Placeholder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.skyd.anivu.ui.component

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.skyd.anivu.R

@Composable
fun EmptyPlaceholder(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
AnimatedPlaceholder(
modifier = modifier,
contentPadding = contentPadding,
resId = R.raw.lottie_empty_1,
tip = stringResource(id = R.string.empty_tip_1),
)
}

@Composable
fun AnimatedPlaceholder(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
@androidx.annotation.RawRes resId: Int,
tip: String,
) {
WithTextPlaceholder(
modifier = modifier,
contentPadding = contentPadding,
text = tip,
) {
AniVuLottieAnimation(resId = resId)
}
}

@Composable
fun WithTextPlaceholder(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
text: String,
content: @Composable BoxScope.() -> Unit,
) {
BasePlaceholder(
modifier = modifier,
contentPadding = contentPadding,
) {
Column(
modifier = Modifier.padding(vertical = 20.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Box(
modifier = Modifier.sizeIn(maxHeight = 240.dp, maxWidth = 220.dp),
contentAlignment = Alignment.Center,
content = content,
)
Text(
modifier = Modifier.padding(top = 10.dp),
text = text,
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onBackground,
)
}
}
}

@Composable
fun CircularProgressPlaceholder(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
BasePlaceholder(
modifier = modifier,
contentPadding = contentPadding,
) {
CircularProgressIndicator()
}
}

@Composable
fun BasePlaceholder(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
content: @Composable BoxScope.() -> Unit,
) {
Box(
modifier = Modifier
.fillMaxSize()
.then(modifier)
.padding(contentPadding),
contentAlignment = Alignment.Center,
content = content,
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.skyd.anivu.ui.component.dialog

import androidx.annotation.IntRange
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
Expand All @@ -18,6 +19,7 @@ fun SliderDialog(
value: Float,
onValueChange: (Float) -> Unit,
valueRange: ClosedFloatingPointRange<Float> = 0f..1f,
@IntRange(from = 0) steps: Int = 0,
valueLabel: @Composable (() -> Unit)? = null,
icon: @Composable (() -> Unit)? = {
Icon(
Expand All @@ -43,6 +45,7 @@ fun SliderDialog(
value = value,
onValueChange = onValueChange,
valueRange = valueRange,
steps = steps,
)
}
},
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/skyd/anivu/ui/local/LocalValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import com.skyd.anivu.model.preference.data.medialib.MediaLibLocationPreference
import com.skyd.anivu.model.preference.player.HardwareDecodePreference
import com.skyd.anivu.model.preference.player.PlayerAutoPipPreference
import com.skyd.anivu.model.preference.player.PlayerDoubleTapPreference
import com.skyd.anivu.model.preference.player.PlayerMaxBackCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerMaxCacheSizePreference
import com.skyd.anivu.model.preference.player.PlayerShow85sButtonPreference
import com.skyd.anivu.model.preference.player.PlayerShowScreenshotButtonPreference
import com.skyd.anivu.model.preference.proxy.ProxyHostnamePreference
Expand Down Expand Up @@ -112,6 +114,8 @@ val LocalPlayerShowScreenshotButton =
compositionLocalOf { PlayerShowScreenshotButtonPreference.default }
val LocalHardwareDecode = compositionLocalOf { HardwareDecodePreference.default }
val LocalPlayerAutoPip = compositionLocalOf { PlayerAutoPipPreference.default }
val LocalPlayerMaxCacheSize = compositionLocalOf { PlayerMaxCacheSizePreference.default }
val LocalPlayerMaxBackCacheSize = compositionLocalOf { PlayerMaxBackCacheSizePreference.default }

// Data
val LocalUseAutoDelete = compositionLocalOf { UseAutoDeletePreference.default }
Expand Down
Loading

0 comments on commit 9e3178c

Please sign in to comment.