Skip to content

Commit

Permalink
fix: can't use new lines to write sms (closes #328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 27, 2023
1 parent 19c8bd9 commit 9c4c5eb
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.bnyro.contacts.ui.components.base

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.TextFieldColors
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.contentColorFor
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex

private val SearchBarIconOffsetX: Dp = 4.dp
private val TonalElevation = 10.dp

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ElevatedTextInputField(
query: String,
onQueryChange: (String) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
colors: TextFieldColors = SearchBarDefaults.inputFieldColors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
) {
val focusRequester = remember { FocusRequester() }

val elevationColor = MaterialTheme.colorScheme.surfaceColorAtElevation(TonalElevation)
Surface(
shape = RoundedCornerShape(36.dp),
color = elevationColor,
contentColor = contentColorFor(elevationColor),
tonalElevation = TonalElevation,
modifier = modifier
.zIndex(1f)
) {
BasicTextField(
value = query,
onValueChange = onQueryChange,
modifier = modifier
.height(SearchBarDefaults.InputFieldHeight)
.fillMaxWidth()
.focusRequester(focusRequester)
.semantics {
onClick {
focusRequester.requestFocus()
true
}
},
enabled = enabled,
textStyle = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.onBackground),
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Default),
cursorBrush = SolidColor(MaterialTheme.colorScheme.onBackground),
interactionSource = interactionSource,
decorationBox = @Composable { innerTextField ->
TextFieldDefaults.DecorationBox(
value = query,
innerTextField = innerTextField,
enabled = enabled,
singleLine = true,
visualTransformation = VisualTransformation.None,
interactionSource = interactionSource,
placeholder = placeholder,
leadingIcon = leadingIcon?.let { leading ->
{
Box(Modifier.offset(x = SearchBarIconOffsetX)) { leading() }
}
},
trailingIcon = trailingIcon?.let { trailing ->
{
Box(Modifier.offset(x = -SearchBarIconOffsetX)) { trailing() }
}
},
shape = SearchBarDefaults.inputFieldShape,
colors = colors,
contentPadding = TextFieldDefaults.contentPaddingWithoutLabel(),
container = {},
)
}
)
}
}
16 changes: 3 additions & 13 deletions app/src/main/java/com/bnyro/contacts/ui/screens/SmsThreadScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
Expand All @@ -61,6 +59,7 @@ import androidx.compose.ui.unit.sp
import com.bnyro.contacts.R
import com.bnyro.contacts.obj.ContactData
import com.bnyro.contacts.ui.components.base.ClickableIcon
import com.bnyro.contacts.ui.components.base.ElevatedTextInputField
import com.bnyro.contacts.ui.components.base.FullScreenDialog
import com.bnyro.contacts.ui.components.dialogs.ConfirmationDialog
import com.bnyro.contacts.ui.models.SmsModel
Expand Down Expand Up @@ -271,23 +270,15 @@ fun SmsThreadScreen(
var text by remember {
mutableStateOf(initialText)
}
val focusRequester = remember {
FocusRequester()
}

SearchBar(
ElevatedTextInputField(
modifier = Modifier
.weight(1f)
.focusRequester(focusRequester),
.weight(1f),
query = text,
onQueryChange = { text = it },
placeholder = {
Text(stringResource(R.string.send))
},
onSearch = {},
active = false,
onActiveChange = {},
content = {}
)

Spacer(modifier = Modifier.width(8.dp))
Expand All @@ -309,7 +300,6 @@ fun SmsThreadScreen(
smsModel.sendSms(context, address, text)

text = ""
focusRequester.freeFocus()
}
) {
Icon(
Expand Down

0 comments on commit 9c4c5eb

Please sign in to comment.