Moving channels to the local version of RoboHash

This commit is contained in:
Vitor Pamplona
2023-02-10 17:29:10 -05:00
parent e6153e7ef5
commit 8a65f3fbe7
6 changed files with 39 additions and 22 deletions

View File

@@ -34,9 +34,9 @@ class Channel(val id: ByteArray) {
live.refresh() live.refresh()
} }
fun profilePicture(): String { fun profilePicture(): String? {
if (info.picture.isNullOrBlank()) info.picture = null if (info.picture.isNullOrBlank()) info.picture = null
return info.picture ?: "https://robohash.org/${idHex}.png" return info.picture
} }
fun anyNameStartsWith(prefix: String): Boolean { fun anyNameStartsWith(prefix: String): Boolean {

View File

@@ -40,6 +40,7 @@ import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter import coil.compose.rememberAsyncImagePainter
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.NotificationCache import com.vitorpamplona.amethyst.NotificationCache
import com.vitorpamplona.amethyst.RoboHashCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
@@ -85,7 +86,7 @@ fun ChatroomCompose(baseNote: Note, accountViewModel: AccountViewModel, navContr
ChannelName( ChannelName(
channelPicture = channel.profilePicture(), channelPicture = channel.profilePicture(),
channelPicturePlaceholder = null, channelPicturePlaceholder = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
channelTitle = { channelTitle = {
Text( Text(
"${channel.info.name}", "${channel.info.name}",
@@ -138,7 +139,7 @@ fun ChatroomCompose(baseNote: Note, accountViewModel: AccountViewModel, navContr
@Composable @Composable
fun ChannelName( fun ChannelName(
channelPicture: String, channelPicture: String?,
channelPicturePlaceholder: Painter?, channelPicturePlaceholder: Painter?,
channelTitle: @Composable (Modifier) -> Unit, channelTitle: @Composable (Modifier) -> Unit,
channelLastTime: Long?, channelLastTime: Long?,
@@ -151,6 +152,8 @@ fun ChannelName(
AsyncImage( AsyncImage(
model = channelPicture, model = channelPicture,
placeholder = channelPicturePlaceholder, placeholder = channelPicturePlaceholder,
fallback = channelPicturePlaceholder,
error = channelPicturePlaceholder,
contentDescription = "Channel Image", contentDescription = "Channel Image",
modifier = Modifier modifier = Modifier
.width(55.dp) .width(55.dp)

View File

@@ -191,7 +191,9 @@ fun NoteCompose(
.align(Alignment.BottomEnd)) { .align(Alignment.BottomEnd)) {
AsyncImage( AsyncImage(
model = channel.profilePicture(), model = channel.profilePicture(),
placeholder = null, placeholder = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
fallback = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
error = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
contentDescription = "Group Picture", contentDescription = "Group Picture",
modifier = Modifier modifier = Modifier
.width(30.dp) .width(30.dp)

View File

@@ -55,6 +55,7 @@ import androidx.navigation.NavController
import coil.compose.AsyncImage import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter import coil.compose.rememberAsyncImagePainter
import com.vitorpamplona.amethyst.LocalPreferences import com.vitorpamplona.amethyst.LocalPreferences
import com.vitorpamplona.amethyst.RoboHashCache
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Channel import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
@@ -147,15 +148,19 @@ fun ChannelScreen(channelId: String?, accountViewModel: AccountViewModel, accoun
@Composable @Composable
fun ChannelHeader(baseChannel: Channel, account: Account, accountStateViewModel: AccountStateViewModel, navController: NavController) { fun ChannelHeader(baseChannel: Channel, account: Account, accountStateViewModel: AccountStateViewModel, navController: NavController) {
val channelState by baseChannel.live.observeAsState() val channelState by baseChannel.live.observeAsState()
val channel = channelState?.channel val channel = channelState?.channel ?: return
val context = LocalContext.current.applicationContext
Column() { Column() {
Column(modifier = Modifier.padding(12.dp)) { Column(modifier = Modifier.padding(12.dp)) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
AsyncImage( AsyncImage(
model = channel?.profilePicture(), model = channel.profilePicture(),
placeholder = null, placeholder = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
fallback = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
error = rememberAsyncImagePainter(RoboHashCache.get(context, channel.idHex)),
contentDescription = "Profile Image", contentDescription = "Profile Image",
modifier = Modifier modifier = Modifier
.width(35.dp) .width(35.dp)
@@ -168,14 +173,14 @@ fun ChannelHeader(baseChannel: Channel, account: Account, accountStateViewModel:
.weight(1f)) { .weight(1f)) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text( Text(
"${channel?.info?.name}", "${channel.info.name}",
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
) )
} }
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text( Text(
"${channel?.info?.about}", "${channel.info.about}",
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f), color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f),
maxLines = 2, maxLines = 2,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
@@ -185,11 +190,10 @@ fun ChannelHeader(baseChannel: Channel, account: Account, accountStateViewModel:
} }
Row(modifier = Modifier.height(35.dp).padding(bottom = 3.dp)) { Row(modifier = Modifier.height(35.dp).padding(bottom = 3.dp)) {
channel?.let { NoteCopyButton(it) } NoteCopyButton(channel)
channel?.let {
if (channel.creator == account.userProfile()) { if (channel.creator == account.userProfile()) {
EditButton(account, it) EditButton(account, channel)
} }
if (account.followingChannels.contains(channel.idHex)) { if (account.followingChannels.contains(channel.idHex)) {
@@ -197,7 +201,7 @@ fun ChannelHeader(baseChannel: Channel, account: Account, accountStateViewModel:
} else { } else {
JoinButton(account, channel, navController) JoinButton(account, channel, navController)
} }
}
} }
} }
} }

View File

@@ -24,10 +24,12 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
@@ -38,6 +40,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import coil.compose.AsyncImage import coil.compose.AsyncImage
import coil.compose.rememberAsyncImagePainter import coil.compose.rememberAsyncImagePainter
import com.vitorpamplona.amethyst.RoboHashCache
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.NostrChatRoomDataSource import com.vitorpamplona.amethyst.service.NostrChatRoomDataSource
import com.vitorpamplona.amethyst.ui.actions.PostButton import com.vitorpamplona.amethyst.ui.actions.PostButton

View File

@@ -36,6 +36,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardCapitalization
@@ -43,7 +44,9 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController import androidx.navigation.NavController
import coil.compose.rememberAsyncImagePainter
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.RoboHashCache
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Channel import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.LocalCache
@@ -95,6 +98,8 @@ private fun SearchBar(accountViewModel: AccountViewModel, navController: NavCont
val onlineSearch = NostrSearchEventOrUserDataSource val onlineSearch = NostrSearchEventOrUserDataSource
val ctx = LocalContext.current.applicationContext
val isTrailingIconVisible by remember { val isTrailingIconVisible by remember {
derivedStateOf { derivedStateOf {
searchValue.isNotBlank() searchValue.isNotBlank()
@@ -210,7 +215,7 @@ private fun SearchBar(accountViewModel: AccountViewModel, navController: NavCont
itemsIndexed(searchResultsChannels.value, key = { _, item -> "c"+item.idHex }) { index, item -> itemsIndexed(searchResultsChannels.value, key = { _, item -> "c"+item.idHex }) { index, item ->
ChannelName( ChannelName(
channelPicture = item.profilePicture(), channelPicture = item.profilePicture(),
channelPicturePlaceholder = null, channelPicturePlaceholder = rememberAsyncImagePainter(RoboHashCache.get(ctx, item.idHex)),
channelTitle = { channelTitle = {
Text( Text(
"${item.info.name}", "${item.info.name}",