amethyst/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt

155 wiersze
4.9 KiB
Kotlin
Czysty Zwykły widok Historia

2023-01-11 18:31:20 +00:00
package com.vitorpamplona.amethyst.ui.note
2023-06-16 19:22:44 +00:00
import android.content.Context
import android.util.Log
import androidx.compose.foundation.layout.Spacer
2023-06-16 19:22:44 +00:00
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
2023-01-11 18:31:20 +00:00
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
2023-06-16 19:22:44 +00:00
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.PlayCircle
2023-01-11 18:31:20 +00:00
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
2023-05-16 01:26:59 +00:00
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
2023-06-16 19:22:44 +00:00
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
2023-01-11 18:31:20 +00:00
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
2023-06-16 19:22:44 +00:00
import androidx.lifecycle.LifecycleOwner
import com.vitorpamplona.amethyst.model.Note
2023-01-11 18:31:20 +00:00
import com.vitorpamplona.amethyst.model.User
2023-06-16 19:22:44 +00:00
import com.vitorpamplona.amethyst.service.tts.TextToSpeechHelper
import com.vitorpamplona.amethyst.ui.actions.ImmutableListOfLists
import com.vitorpamplona.amethyst.ui.actions.toImmutableListOfLists
2023-05-16 01:26:59 +00:00
import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
import com.vitorpamplona.amethyst.ui.theme.StdButtonSizeModifier
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.placeholderText
2023-01-11 18:31:20 +00:00
@Composable
fun NoteUsernameDisplay(baseNote: Note, weight: Modifier = Modifier) {
2023-03-07 18:46:44 +00:00
val noteState by baseNote.live().metadata.observeAsState()
val author = remember(noteState) {
noteState?.note?.author
} ?: return
2023-05-16 01:26:59 +00:00
UsernameDisplay(author, weight)
}
@Composable
fun UsernameDisplay(baseUser: User, weight: Modifier = Modifier) {
2023-03-07 18:46:44 +00:00
val userState by baseUser.live().metadata.observeAsState()
2023-05-16 01:26:59 +00:00
val bestUserName = remember(userState) { userState?.user?.bestUsername() }
val bestDisplayName = remember(userState) { userState?.user?.bestDisplayName() }
val npubDisplay = remember { baseUser.pubkeyDisplayHex() }
val tags = remember(userState) { userState?.user?.info?.latestMetadata?.tags?.toImmutableListOfLists() }
2023-05-16 01:26:59 +00:00
UserNameDisplay(bestUserName, bestDisplayName, npubDisplay, tags, weight)
}
@Composable
private fun UserNameDisplay(
bestUserName: String?,
bestDisplayName: String?,
npubDisplay: String,
tags: ImmutableListOfLists<String>?,
modifier: Modifier
) {
if (bestUserName != null && bestDisplayName != null) {
2023-05-16 01:26:59 +00:00
CreateTextWithEmoji(
text = bestDisplayName,
tags = tags,
fontWeight = FontWeight.Bold,
maxLines = 1
2023-03-07 18:46:44 +00:00
)
if (bestDisplayName != bestUserName) {
CreateTextWithEmoji(
text = remember { "@$bestUserName" },
tags = tags,
color = MaterialTheme.colors.placeholderText,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = modifier
)
}
Spacer(StdHorzSpacer)
DrawPlayName(bestDisplayName)
} else if (bestDisplayName != null) {
2023-05-16 01:26:59 +00:00
CreateTextWithEmoji(
text = bestDisplayName,
tags = tags,
2023-03-07 18:46:44 +00:00
fontWeight = FontWeight.Bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = modifier
2023-03-07 18:46:44 +00:00
)
Spacer(StdHorzSpacer)
DrawPlayName(bestDisplayName)
} else if (bestUserName != null) {
2023-05-16 01:26:59 +00:00
CreateTextWithEmoji(
text = bestUserName,
2023-05-16 01:26:59 +00:00
tags = tags,
2023-03-07 18:46:44 +00:00
fontWeight = FontWeight.Bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = modifier
2023-03-07 18:46:44 +00:00
)
Spacer(StdHorzSpacer)
DrawPlayName(bestUserName)
2023-03-07 18:46:44 +00:00
} else {
Text(
text = npubDisplay,
2023-03-07 18:46:44 +00:00
fontWeight = FontWeight.Bold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = modifier
2023-03-07 18:46:44 +00:00
)
}
}
2023-06-16 19:22:44 +00:00
@Composable
fun DrawPlayName(name: String) {
val context = LocalContext.current
val lifecycleOwner = LocalLifecycleOwner.current
DrawPlayNameIcon {
speak(name, context, lifecycleOwner)
}
}
@Composable
fun DrawPlayNameIcon(onClick: () -> Unit) {
IconButton(
onClick = onClick,
modifier = StdButtonSizeModifier
) {
Icon(
imageVector = Icons.Outlined.PlayCircle,
contentDescription = null,
modifier = StdButtonSizeModifier,
tint = MaterialTheme.colors.placeholderText
)
}
}
2023-06-16 19:22:44 +00:00
private fun speak(
message: String,
context: Context,
owner: LifecycleOwner
) {
TextToSpeechHelper
.getInstance(context)
.registerLifecycle(owner)
.speak(message)
.highlight()
.onDone {
Log.d("TextToSpeak", "speak: done")
}
.onError {
Log.d("TextToSpeak", "speak error: $it")
}
}