|
| 1 | +package com.ninecraft.booket.feature.detail.book.component |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Column |
| 4 | +import androidx.compose.foundation.layout.Spacer |
| 5 | +import androidx.compose.foundation.layout.height |
| 6 | +import androidx.compose.foundation.layout.padding |
| 7 | +import androidx.compose.material3.Text |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.runtime.remember |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.graphics.Color |
| 12 | +import androidx.compose.ui.text.AnnotatedString |
| 13 | +import androidx.compose.ui.text.SpanStyle |
| 14 | +import androidx.compose.ui.text.TextStyle |
| 15 | +import androidx.compose.ui.text.buildAnnotatedString |
| 16 | +import androidx.compose.ui.text.withStyle |
| 17 | +import androidx.compose.ui.unit.dp |
| 18 | +import com.ninecraft.booket.core.common.util.analyzeEmotions |
| 19 | +import com.ninecraft.booket.core.common.util.EmotionDisplayType |
| 20 | +import com.ninecraft.booket.core.designsystem.ComponentPreview |
| 21 | +import com.ninecraft.booket.core.designsystem.theme.ReedTheme |
| 22 | +import com.ninecraft.booket.core.model.Emotion |
| 23 | +import com.ninecraft.booket.core.model.EmotionModel |
| 24 | +import kotlinx.collections.immutable.ImmutableList |
| 25 | +import kotlinx.collections.immutable.persistentListOf |
| 26 | + |
| 27 | +@Composable |
| 28 | +internal fun EmotionAnalysisResultText( |
| 29 | + emotions: ImmutableList<EmotionModel>, |
| 30 | + brandColor: Color, |
| 31 | + secondaryColor: Color, |
| 32 | + emotionTextStyle: TextStyle, |
| 33 | + regularTextStyle: TextStyle, |
| 34 | +): AnnotatedString { |
| 35 | + val analysisResult = remember(emotions) { analyzeEmotions(emotions) } |
| 36 | + |
| 37 | + return when (analysisResult.displayType) { |
| 38 | + EmotionDisplayType.SINGLE -> { |
| 39 | + val emotion = analysisResult.topEmotions.first() |
| 40 | + buildAnnotatedString { |
| 41 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 42 | + append("이 책에서 ") |
| 43 | + } |
| 44 | + withStyle(SpanStyle(color = brandColor, fontSize = emotionTextStyle.fontSize, fontWeight = emotionTextStyle.fontWeight)) { |
| 45 | + append(emotion.type.displayName) |
| 46 | + } |
| 47 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 48 | + append(" 감정을 많이 느꼈어요") |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + EmotionDisplayType.DUAL -> { |
| 54 | + val emotions = analysisResult.topEmotions |
| 55 | + buildAnnotatedString { |
| 56 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 57 | + append("이 책에서 ") |
| 58 | + } |
| 59 | + emotions.forEachIndexed { index, emotion -> |
| 60 | + if (index > 0) { |
| 61 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 62 | + append(", ") |
| 63 | + } |
| 64 | + } |
| 65 | + withStyle(SpanStyle(color = brandColor, fontSize = emotionTextStyle.fontSize, fontWeight = emotionTextStyle.fontWeight)) { |
| 66 | + append(emotion.type.displayName) |
| 67 | + } |
| 68 | + } |
| 69 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 70 | + append(" 감정을 많이 느꼈어요") |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + EmotionDisplayType.BALANCED -> { |
| 76 | + buildAnnotatedString { |
| 77 | + withStyle(SpanStyle(color = secondaryColor, fontSize = regularTextStyle.fontSize, fontWeight = regularTextStyle.fontWeight)) { |
| 78 | + append("이 책에서 ") |
| 79 | + } |
| 80 | + withStyle(SpanStyle(color = brandColor, fontSize = emotionTextStyle.fontSize, fontWeight = emotionTextStyle.fontWeight)) { |
| 81 | + append("여러 감정이 고르게 담겼어요") |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +@ComponentPreview |
| 89 | +@Composable |
| 90 | +private fun EmotionTextAllCasesPreview() { |
| 91 | + ReedTheme { |
| 92 | + Column(modifier = Modifier.padding(16.dp)) { |
| 93 | + Text(text = "1개의 감정이 1위인 경우:") |
| 94 | + Text( |
| 95 | + text = EmotionAnalysisResultText( |
| 96 | + emotions = persistentListOf( |
| 97 | + EmotionModel(type = Emotion.WARM, count = 5), |
| 98 | + EmotionModel(type = Emotion.JOY, count = 2), |
| 99 | + ), |
| 100 | + brandColor = ReedTheme.colors.contentBrand, |
| 101 | + secondaryColor = ReedTheme.colors.contentSecondary, |
| 102 | + emotionTextStyle = ReedTheme.typography.label2SemiBold, |
| 103 | + regularTextStyle = ReedTheme.typography.label2Regular, |
| 104 | + ), |
| 105 | + modifier = Modifier.padding(vertical = 8.dp), |
| 106 | + ) |
| 107 | + Spacer(modifier = Modifier.height(16.dp)) |
| 108 | + Text(text = "2개의 감정이 공동 1위인 경우:") |
| 109 | + Text( |
| 110 | + text = EmotionAnalysisResultText( |
| 111 | + emotions = persistentListOf( |
| 112 | + EmotionModel(type = Emotion.WARM, count = 5), |
| 113 | + EmotionModel(type = Emotion.JOY, count = 5), |
| 114 | + EmotionModel(type = Emotion.SADNESS, count = 2), |
| 115 | + ), |
| 116 | + brandColor = ReedTheme.colors.contentBrand, |
| 117 | + secondaryColor = ReedTheme.colors.contentSecondary, |
| 118 | + emotionTextStyle = ReedTheme.typography.label2SemiBold, |
| 119 | + regularTextStyle = ReedTheme.typography.label2Regular, |
| 120 | + ), |
| 121 | + modifier = Modifier.padding(vertical = 8.dp), |
| 122 | + ) |
| 123 | + Spacer(modifier = Modifier.height(16.dp)) |
| 124 | + Text(text = "3~4개의 감정이 공동 1위인 경우:") |
| 125 | + Text( |
| 126 | + text = EmotionAnalysisResultText( |
| 127 | + emotions = persistentListOf( |
| 128 | + EmotionModel(type = Emotion.WARM, count = 3), |
| 129 | + EmotionModel(type = Emotion.JOY, count = 3), |
| 130 | + EmotionModel(type = Emotion.SADNESS, count = 3), |
| 131 | + EmotionModel(type = Emotion.TENSION, count = 3), |
| 132 | + ), |
| 133 | + brandColor = ReedTheme.colors.contentBrand, |
| 134 | + secondaryColor = ReedTheme.colors.contentSecondary, |
| 135 | + emotionTextStyle = ReedTheme.typography.label2SemiBold, |
| 136 | + regularTextStyle = ReedTheme.typography.label2Regular, |
| 137 | + ), |
| 138 | + modifier = Modifier.padding(vertical = 8.dp), |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments