From 9e3c08ade86044e249befa3b449384a0118805c4 Mon Sep 17 00:00:00 2001 From: Believethehype Date: Tue, 14 Mar 2023 15:27:58 +0100 Subject: [PATCH] Image upload button in Chats --- .../vitorpamplona/amethyst/model/Account.kt | 2 +- .../ui/screen/loggedIn/ChannelScreen.kt | 28 ++++++++++++++---- .../ui/screen/loggedIn/ChatroomScreen.kt | 29 ++++++++++++++----- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index cc1c66690..b426e658f 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -319,7 +319,7 @@ class Account( LocalCache.consume(signedEvent, null) } - fun sendPrivateMeesage(message: String, toUser: String, replyingTo: Note? = null) { + fun sendPrivateMessage(message: String, toUser: String, replyingTo: Note? = null) { if (!isWriteable()) return val user = LocalCache.users[toUser] ?: return diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt index 43d5a88d9..6aaab262c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChannelScreen.kt @@ -1,5 +1,6 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn +import android.widget.Toast import androidx.compose.animation.animateContentSize import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column @@ -65,7 +66,9 @@ import com.vitorpamplona.amethyst.model.Channel import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.service.NostrChannelDataSource import com.vitorpamplona.amethyst.ui.actions.NewChannelView +import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel import com.vitorpamplona.amethyst.ui.actions.PostButton +import com.vitorpamplona.amethyst.ui.actions.UploadFromGallery import com.vitorpamplona.amethyst.ui.components.ResizeImage import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy import com.vitorpamplona.amethyst.ui.dal.ChannelFeedFilter @@ -82,9 +85,10 @@ fun ChannelScreen( ) { val accountState by accountViewModel.accountLiveData.observeAsState() val account = accountState?.account + val context = LocalContext.current + val channelScreenModel: NewPostViewModel = viewModel() if (account != null && channelId != null) { - val newPost = remember { mutableStateOf(TextFieldValue("")) } val replyTo = remember { mutableStateOf(null) } ChannelFeedFilter.loadMessagesBetween(account, channelId) @@ -98,6 +102,9 @@ fun ChannelScreen( LaunchedEffect(Unit) { feedViewModel.invalidateData() + channelScreenModel.imageUploadingError.collect { error -> + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } } DisposableEffect(channelId) { @@ -171,6 +178,13 @@ fun ChannelScreen( } } + Row(modifier = Modifier.fillMaxWidth()) { + UploadFromGallery( + isUploading = channelScreenModel.isUploadingImage + ) { + channelScreenModel.upload(it, context) + } + } // LAST ROW Row( modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp, top = 5.dp).fillMaxWidth(), @@ -178,8 +192,10 @@ fun ChannelScreen( verticalAlignment = Alignment.CenterVertically ) { TextField( - value = newPost.value, - onValueChange = { newPost.value = it }, + value = channelScreenModel.message, + onValueChange = { + channelScreenModel.updateMessage(it) + }, keyboardOptions = KeyboardOptions.Default.copy( capitalization = KeyboardCapitalization.Sentences ), @@ -195,12 +211,12 @@ fun ChannelScreen( trailingIcon = { PostButton( onPost = { - account.sendChannelMessage(newPost.value.text, channel.idHex, replyTo.value, null) - newPost.value = TextFieldValue("") + account.sendChannelMessage(channelScreenModel.message.text, channel.idHex, replyTo.value, null) + channelScreenModel.message = TextFieldValue("") replyTo.value = null feedViewModel.refresh() // Don't wait a full second before updating }, - newPost.value.text.isNotBlank(), + isActive = channelScreenModel.message.text.isNotBlank() && !channelScreenModel.isUploadingImage, modifier = Modifier.padding(end = 10.dp) ) }, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt index 0465c0124..37a0d0477 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ChatroomScreen.kt @@ -1,5 +1,6 @@ package com.vitorpamplona.amethyst.ui.screen.loggedIn +import android.widget.Toast import androidx.compose.animation.animateContentSize import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement @@ -36,6 +37,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.KeyboardCapitalization @@ -50,7 +52,9 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.service.NostrChatroomDataSource +import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel import com.vitorpamplona.amethyst.ui.actions.PostButton +import com.vitorpamplona.amethyst.ui.actions.UploadFromGallery import com.vitorpamplona.amethyst.ui.components.ObserveDisplayNip05Status import com.vitorpamplona.amethyst.ui.components.ResizeImage import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy @@ -64,9 +68,10 @@ import com.vitorpamplona.amethyst.ui.screen.NostrChatRoomFeedViewModel fun ChatroomScreen(userId: String?, accountViewModel: AccountViewModel, navController: NavController) { val accountState by accountViewModel.accountLiveData.observeAsState() val account = accountState?.account + val context = LocalContext.current + val chatRoomScreenModel: NewPostViewModel = viewModel() if (account != null && userId != null) { - val newPost = remember { mutableStateOf(TextFieldValue("")) } val replyTo = remember { mutableStateOf(null) } ChatroomFeedFilter.loadMessagesBetween(account, userId) @@ -77,6 +82,9 @@ fun ChatroomScreen(userId: String?, accountViewModel: AccountViewModel, navContr LaunchedEffect(userId) { feedViewModel.refresh() + chatRoomScreenModel.imageUploadingError.collect { error -> + Toast.makeText(context, error, Toast.LENGTH_SHORT).show() + } } DisposableEffect(userId) { @@ -148,6 +156,13 @@ fun ChatroomScreen(userId: String?, accountViewModel: AccountViewModel, navContr } } + Row(modifier = Modifier.fillMaxWidth()) { + UploadFromGallery( + isUploading = chatRoomScreenModel.isUploadingImage + ) { + chatRoomScreenModel.upload(it, context) + } + } // LAST ROW Row( modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp, top = 5.dp) @@ -156,8 +171,8 @@ fun ChatroomScreen(userId: String?, accountViewModel: AccountViewModel, navContr verticalAlignment = Alignment.CenterVertically ) { TextField( - value = newPost.value, - onValueChange = { newPost.value = it }, + value = chatRoomScreenModel.message, + onValueChange = { chatRoomScreenModel.updateMessage(it) }, keyboardOptions = KeyboardOptions.Default.copy( capitalization = KeyboardCapitalization.Sentences ), @@ -173,13 +188,13 @@ fun ChatroomScreen(userId: String?, accountViewModel: AccountViewModel, navContr trailingIcon = { PostButton( onPost = { - account.sendPrivateMeesage(newPost.value.text, userId, replyTo.value) - newPost.value = TextFieldValue("") + account.sendPrivateMessage(chatRoomScreenModel.message.text, userId, replyTo.value) + chatRoomScreenModel.message = TextFieldValue("") replyTo.value = null feedViewModel.refresh() // Don't wait a full second before updating }, - newPost.value.text.isNotBlank(), - modifier = Modifier.padding(end = 10.dp) + isActive = chatRoomScreenModel.message.text.isNotBlank() && !chatRoomScreenModel.isUploadingImage, + modifier = Modifier.padding(end = 10.dp), ) }, colors = TextFieldDefaults.textFieldColors(