Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Messages reply functionality #58

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import tech.relaycorp.letro.messages.parser.NewConversationMessageParser
import tech.relaycorp.letro.messages.parser.NewConversationMessageParserImpl
import tech.relaycorp.letro.messages.parser.NewMessageMessageParser
import tech.relaycorp.letro.messages.parser.NewMessageMessageParserImpl
import tech.relaycorp.letro.messages.parser.OutgoingConversationMessageEncoder
import tech.relaycorp.letro.messages.parser.OutgoingConversationMessageEncoderImpl
import tech.relaycorp.letro.messages.parser.OutgoingMessageMessageEncoder
import tech.relaycorp.letro.messages.parser.OutgoingMessageMessageEncoderImpl
import tech.relaycorp.letro.messages.processor.NewConversationProcessor
import tech.relaycorp.letro.messages.processor.NewConversationProcessorImpl
import tech.relaycorp.letro.messages.processor.NewMessageProcessor
Expand Down Expand Up @@ -62,8 +62,8 @@ object ConversationsModule {

@Binds
fun bindOutgoingConversationMessageEncoder(
impl: OutgoingConversationMessageEncoderImpl,
): OutgoingConversationMessageEncoder
impl: OutgoingMessageMessageEncoderImpl,
): OutgoingMessageMessageEncoder

@Binds
fun bindNewConversationProcessor(
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/tech/relaycorp/letro/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class HomeViewModel @Inject constructor(
val uiState: StateFlow<HomeUiState>
get() = _uiState

private val _createNewMessageSignal: MutableSharedFlow<Unit> = MutableSharedFlow()
val createNewMessageSignal: SharedFlow<Unit>
get() = _createNewMessageSignal
private val _createNewConversationSignal: MutableSharedFlow<Unit> = MutableSharedFlow()
val createNewConversationSignal: SharedFlow<Unit>
get() = _createNewConversationSignal

init {
viewModelScope.launch {
Expand Down Expand Up @@ -59,7 +59,7 @@ class HomeViewModel @Inject constructor(
viewModelScope.launch {
when (uiState.value.currentTab) {
TAB_CHATS -> {
_createNewMessageSignal.emit(Unit)
_createNewConversationSignal.emit(Unit)
}
TAB_CONTACTS -> {
_uiState.update {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ import tech.relaycorp.letro.ui.common.LetroButton
import tech.relaycorp.letro.ui.common.LetroTextField
import tech.relaycorp.letro.ui.theme.HorizontalScreenPadding
import tech.relaycorp.letro.ui.theme.LetroColor
import tech.relaycorp.letro.ui.utils.ConversationsStringsProvider
import tech.relaycorp.letro.utils.ext.applyIf

@Composable
fun CreateNewMessageScreen(
conversationsStringsProvider: ConversationsStringsProvider,
onBackClicked: () -> Unit,
onMessageSent: () -> Unit,
viewModel: CreateNewMessageViewModel = hiltViewModel(),
Expand Down Expand Up @@ -138,12 +141,16 @@ fun CreateNewMessageScreen(
Text(
text = stringResource(id = R.string.new_message_from),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
color = MaterialTheme.colorScheme.onSurfaceVariant
.applyIf(uiState.isOnlyTextEditale) {
copy(alpha = 0.38f)
},
)
Spacer(modifier = Modifier.width(16.dp))
Text(
text = uiState.sender,
color = MaterialTheme.colorScheme.onSurface,
color = MaterialTheme.colorScheme.onSurface
.copy(alpha = 0.38F),
style = MaterialTheme.typography.bodyLarge,
)
}
Expand All @@ -158,7 +165,10 @@ fun CreateNewMessageScreen(
Text(
text = stringResource(id = R.string.new_message_to),
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurfaceVariant,
color = MaterialTheme.colorScheme.onSurfaceVariant
.applyIf(uiState.isOnlyTextEditale) {
copy(alpha = 0.38f)
},
)
if (uiState.showRecipientAsChip) {
Spacer(modifier = Modifier.width(16.dp))
Expand All @@ -171,6 +181,7 @@ fun CreateNewMessageScreen(
viewModel.onRecipientRemoveClick()
recipientTextFieldValueState = TextFieldValue()
},
isEditable = !uiState.isOnlyTextEditale,
)
Spacer(modifier = Modifier.height(TextFieldDefaults.MinHeight))
}
Expand Down Expand Up @@ -214,20 +225,38 @@ fun CreateNewMessageScreen(
}
else -> {
Column {
LetroTextField(
value = subjectTextFieldValueState,
onValueChange = {
subjectTextFieldValueState = it
viewModel.onSubjectTextChanged(it.text)
},
placeHolderText = stringResource(id = R.string.new_message_subject_hint),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
),
placeholderColor = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.onFocusChanged { viewModel.onSubjectTextFieldFocused(it.isFocused) },
)
if (!uiState.isOnlyTextEditale) {
LetroTextField(
value = subjectTextFieldValueState,
onValueChange = {
subjectTextFieldValueState = it
viewModel.onSubjectTextChanged(it.text)
},
placeHolderText = stringResource(id = R.string.new_message_subject_hint),
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Next,
),
placeholderColor = MaterialTheme.colorScheme.onSurfaceVariant,
modifier = Modifier
.onFocusChanged { viewModel.onSubjectTextFieldFocused(it.isFocused) },
)
} else {
Box(
modifier = Modifier
.height(TextFieldDefaults.MinHeight)
.padding(horizontal = 16.dp),
) {
Text(
text = if (uiState.showNoSubjectText) conversationsStringsProvider.noSubject else uiState.subject,
style = MaterialTheme.typography.bodyLarge,
color = MaterialTheme.colorScheme.onSurface
.copy(alpha = 0.38F),
maxLines = 1,
modifier = Modifier
.align(Alignment.CenterStart),
)
}
}
Divider()
LetroTextField(
value = messageTextFieldValueState,
Expand Down Expand Up @@ -277,13 +306,14 @@ private fun SuggestContactsList(
private fun RecipientChipView(
text: String,
onRemoveClick: () -> Unit,
isEditable: Boolean,
modifier: Modifier = Modifier,
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = modifier
.background(
color = MaterialTheme.colorScheme.surfaceVariant,
color = if (isEditable) MaterialTheme.colorScheme.surfaceVariant else MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.06F),
shape = RoundedCornerShape(80.dp),
)
.padding(
Expand All @@ -294,15 +324,20 @@ private fun RecipientChipView(
Text(
text = text,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurface,
)
Spacer(modifier = Modifier.width(8.dp))
Icon(
painter = painterResource(id = R.drawable.ic_chip_delete),
contentDescription = stringResource(id = R.string.content_description_recipient_clear),
tint = LetroColor.OnSurfaceContainer,
modifier = Modifier
.clickable { onRemoveClick() },
color = MaterialTheme.colorScheme.onSurface
.applyIf(!isEditable) {
copy(alpha = 0.3F)
},
)
if (isEditable) {
Spacer(modifier = Modifier.width(8.dp))
Icon(
painter = painterResource(id = R.drawable.ic_chip_delete),
contentDescription = stringResource(id = R.string.content_description_recipient_clear),
tint = LetroColor.OnSurfaceContainer,
modifier = Modifier
.clickable { onRemoveClick() },
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tech.relaycorp.letro.messages.compose

import androidx.annotation.IntDef
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -13,7 +15,11 @@ import tech.relaycorp.letro.account.storage.AccountRepository
import tech.relaycorp.letro.contacts.model.Contact
import tech.relaycorp.letro.contacts.model.ContactPairingStatus
import tech.relaycorp.letro.contacts.storage.ContactsRepository
import tech.relaycorp.letro.messages.compose.CreateNewMessageViewModel.ScreenType.Companion.NEW_CONVERSATION
import tech.relaycorp.letro.messages.compose.CreateNewMessageViewModel.ScreenType.Companion.REPLY_TO_EXISTING_CONVERSATION
import tech.relaycorp.letro.messages.model.ExtendedConversation
import tech.relaycorp.letro.messages.repository.ConversationsRepository
import tech.relaycorp.letro.ui.navigation.Route
import tech.relaycorp.letro.utils.ext.emitOn
import tech.relaycorp.letro.utils.ext.isEmptyOrBlank
import tech.relaycorp.letro.utils.ext.isNotEmptyOrBlank
Expand All @@ -24,9 +30,25 @@ class CreateNewMessageViewModel @Inject constructor(
private val accountRepository: AccountRepository,
private val contactsRepository: ContactsRepository,
private val conversationsRepository: ConversationsRepository,
private val savedStateHandle: SavedStateHandle,
) : ViewModel() {

private val _uiState = MutableStateFlow(NewMessageUiState())
@ScreenType
private val screenType: Int = savedStateHandle[Route.CreateNewMessage.KEY_SCREEN_TYPE]!!
private val conversation: ExtendedConversation? =
(savedStateHandle.get(Route.CreateNewMessage.KEY_CONVERSATION_ID) as? String)
?.let { conversationsRepository.getConversation(it) }

private val _uiState = MutableStateFlow(
NewMessageUiState(
sender = conversation?.ownerVeraId ?: "",
recipient = conversation?.contactDisplayName ?: "",
subject = conversation?.subject ?: "",
showNoSubjectText = conversation != null && conversation.subject.isNullOrEmpty(),
showRecipientAsChip = conversation != null,
isOnlyTextEditale = conversation != null,
),
)
val uiState: StateFlow<NewMessageUiState>
get() = _uiState

Expand Down Expand Up @@ -133,12 +155,26 @@ class CreateNewMessageViewModel @Inject constructor(

fun onSendMessageClick() {
val contact = contacts.find { it.contactVeraId == uiState.value.recipient } ?: return
conversationsRepository.createNewConversation(
ownerVeraId = uiState.value.sender,
recipient = contact,
messageText = uiState.value.messageText,
subject = uiState.value.subject,
)
when (screenType) {
NEW_CONVERSATION -> {
conversationsRepository.createNewConversation(
ownerVeraId = uiState.value.sender,
recipient = contact,
messageText = uiState.value.messageText,
subject = uiState.value.subject,
)
}
REPLY_TO_EXISTING_CONVERSATION -> {
val conversation = conversation ?: return
conversationsRepository.reply(
conversationId = conversation.conversationId,
messageText = uiState.value.messageText,
)
}
else -> {
throw IllegalStateException("Unknown screen type $screenType!")
}
}
_messageSentSignal.emitOn(Unit, viewModelScope)
}

Expand Down Expand Up @@ -171,6 +207,14 @@ class CreateNewMessageViewModel @Inject constructor(
): Boolean {
return contacts.any { recipient == it.contactVeraId } && messageText.isNotEmptyOrBlank()
}

@IntDef(NEW_CONVERSATION, REPLY_TO_EXISTING_CONVERSATION)
annotation class ScreenType {
companion object {
const val NEW_CONVERSATION = 0
const val REPLY_TO_EXISTING_CONVERSATION = 1
}
}
}

data class NewMessageUiState(
Expand All @@ -181,5 +225,7 @@ data class NewMessageUiState(
val showRecipientIsNotYourContactError: Boolean = false,
val isSendButtonEnabled: Boolean = false,
val showRecipientAsChip: Boolean = false,
val isOnlyTextEditale: Boolean = false,
val showNoSubjectText: Boolean = false,
val suggestedContacts: List<Contact>? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class NewConversationMessageParserImpl @Inject constructor() : NewConversationMe

override fun parse(content: ByteArray): NewConversationIncomingMessage {
val conversation = mockConversation() // TODO: parse conversation
val message = mockMessage(conversation.conversationId)
val message = mockMessage(conversation.conversationId) // TODO: create message object
return NewConversationIncomingMessage(
content = NewConversationIncomingMessageContent(
conversation = conversation,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.relaycorp.letro.messages.parser

import tech.relaycorp.letro.messages.storage.entity.Conversation
import tech.relaycorp.letro.messages.storage.entity.Message
import javax.inject.Inject

interface OutgoingMessageMessageEncoder {
fun encodeNewConversationContent(conversation: Conversation): ByteArray
fun encodeNewMessageContent(message: Message): ByteArray
}

class OutgoingMessageMessageEncoderImpl @Inject constructor() : OutgoingMessageMessageEncoder {

override fun encodeNewConversationContent(conversation: Conversation): ByteArray {
return ByteArray(0) // TODO: encode conversation to ByteArray
}

override fun encodeNewMessageContent(message: Message): ByteArray {
return ByteArray(0) // TODO: encode message to ByteArray
}
}
Loading
Loading