Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Jul 30, 2024
1 parent 6949340 commit e21ae03
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ object BangumiCommentSticker {
return STICKER_RES[id]
}

fun <R> map(block: (Pair<Int, DrawableResource>) -> R): List<R> {
return STICKER_RES.entries.map { (k, v) -> block(k to v) }
}

private val STICKER_RES: Map<Int, DrawableResource> = mapOf(
1 to Res.drawable.bgm_01,
2 to Res.drawable.bgm_02,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.him188.ani.app.ui.subject.components.comment
import androidx.compose.animation.Crossfade
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.Indication
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.MutableInteractionSource
Expand Down Expand Up @@ -55,7 +56,6 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand All @@ -68,7 +68,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextRange
Expand Down Expand Up @@ -137,6 +136,8 @@ class EditCommentState(
editor.override(TextFieldValue(""))
}
currentSendTarget = id
previewer.closePreview()
_editExpanded.value = false
}

fun setEditExpanded(value: Boolean) {
Expand Down Expand Up @@ -210,7 +211,6 @@ fun EditComment(
stickerPanelHeight: Dp = EditCommentDefaults.MinStickerHeight.dp,
onStickerPanelStateChanged: (Boolean) -> Unit = { },
) {
val density = LocalDensity.current
val keyboard = if (!controlSoftwareKeyboard) null else LocalSoftwareKeyboardController.current

val textFieldInteractionSource = remember { MutableInteractionSource() }
Expand Down Expand Up @@ -277,6 +277,7 @@ fun EditComment(
onClickExpanded = { state.setEditExpanded(it) },
) {
val contentPadding = remember { PaddingValues(horizontal = 12.dp, vertical = 12.dp) }

Crossfade(
targetState = state.previewing,
modifier = Modifier.weight(1.0f, fill = false),
Expand All @@ -286,7 +287,10 @@ fun EditComment(
val richText by state.previewContent.collectAsState()
EditCommentDefaults.Preview(
content = richText,
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.ifThen(state.editExpanded) { fillMaxHeight() }
.animateContentSize(),
contentPadding = contentPadding,
)
} else {
Expand All @@ -311,16 +315,6 @@ fun EditComment(
}
}

@Composable
private fun rememberEditCommentPreviewer(
initialPreviewing: Boolean = false
): EditCommentPreviewerState {
val scope = rememberCoroutineScope()
return remember(scope) {
EditCommentPreviewerState(initialPreviewing, scope)
}
}

/**
* 评论编辑 Scaffold
*
Expand Down Expand Up @@ -796,9 +790,17 @@ object EditCommentDefaults {
list.forEach { sticker ->
IconButton(onClick = { onClickItem(sticker.id) }) {
if (previewing || sticker.drawableRes == null) {
Icon(Icons.Outlined.SentimentSatisfied, contentDescription = null)
Icon(
Icons.Outlined.SentimentSatisfied,
contentDescription = null,
modifier = Modifier.size(24.dp),
)
} else {
Icon(painterResource(sticker.drawableRes), contentDescription = null)
Image(
painterResource(sticker.drawableRes),
contentDescription = null,
modifier = Modifier.size(24.dp),
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import me.him188.ani.app.data.models.subject.subjectInfoFlow
import me.him188.ani.app.data.repository.EpisodePreferencesRepository
import me.him188.ani.app.data.repository.EpisodeRevisionRepository
import me.him188.ani.app.data.repository.SettingsRepository
import me.him188.ani.app.data.source.BangumiCommentSticker
import me.him188.ani.app.data.source.danmaku.DanmakuManager
import me.him188.ani.app.data.source.media.EpisodeCacheStatus
import me.him188.ani.app.data.source.media.MediaCacheManager
Expand Down Expand Up @@ -528,12 +529,7 @@ private class EpisodeViewModelImpl(
showExpandEditCommentButton = true,
initialExpandEditComment = false,
panelTitle = "评论:${episodeDetailsState.subjectTitle}",
stickerProvider = {
generateSequence(1) { it + 1 }
.take(64)
.map { EditCommentSticker(it, null) }
.toList()
},
stickerProvider = { BangumiCommentSticker.map { EditCommentSticker(it.first, it.second) } },
onSend = { delay(3000) },
backgroundScope = backgroundScope,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.him188.ani.app.ui.subject.episode.comments

import androidx.compose.animation.core.TweenSpec
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.clickable
Expand All @@ -17,6 +18,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand Down Expand Up @@ -73,6 +75,9 @@ fun EpisodeEditCommentSheet(
var sheetHeight by remember { mutableStateOf(0) }
val sheetOffset by derivedStateOf { sheetHeight * (1 - scrimAlpha) }

// CornerExtraLargeTop = RoundedCornerShape(28.0.dp)
val shapeDp by animateDpAsState(if (state.editExpanded) 0.dp else 28.dp)

val animateToDismiss: () -> Unit = {
focusManager.clearFocus(force = true)
visible = false
Expand Down Expand Up @@ -134,7 +139,7 @@ fun EpisodeEditCommentSheet(
.fillMaxWidth()
.align(Alignment.BottomCenter)
.graphicsLayer { translationY = sheetOffset },
shape = BottomSheetDefaults.ExpandedShape,
shape = RoundedCornerShape(topStart = shapeDp, topEnd = shapeDp),
color = BottomSheetDefaults.ContainerColor,
contentColor = contentColorFor(BottomSheetDefaults.ContainerColor),
tonalElevation = BottomSheetDefaults.Elevation,
Expand Down

0 comments on commit e21ae03

Please sign in to comment.