In chat activity, fixed adding chat peer to contacts

This commit is contained in:
Juha Heinanen
2025-05-01 09:29:33 +03:00
parent e88c8cc64d
commit 6fbc1a8a0a
@@ -18,7 +18,6 @@ import androidx.annotation.RequiresApi
import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
@@ -100,7 +99,6 @@ class ChatActivity : ComponentActivity() {
private lateinit var aor: String private lateinit var aor: String
private lateinit var account: Account private lateinit var account: Account
private lateinit var peerUri: String private lateinit var peerUri: String
private lateinit var chatPeer: String
private lateinit var ua: UserAgent private lateinit var ua: UserAgent
private var _chatMessages = mutableStateOf<List<Message>>(emptyList()) private var _chatMessages = mutableStateOf<List<Message>>(emptyList())
@@ -108,6 +106,7 @@ class ChatActivity : ComponentActivity() {
private var focus = false private var focus = false
private var lastCall: Long = 0 private var lastCall: Long = 0
private var keyboardController: SoftwareKeyboardController? = null private var keyboardController: SoftwareKeyboardController? = null
private val chatPeer = mutableStateOf("")
private var backInvokedCallback: OnBackInvokedCallback? = null private var backInvokedCallback: OnBackInvokedCallback? = null
private lateinit var onBackPressedCallback: OnBackPressedCallback private lateinit var onBackPressedCallback: OnBackPressedCallback
@@ -160,12 +159,10 @@ class ChatActivity : ComponentActivity() {
ua = userAgent ua = userAgent
} }
chatPeer = Utils.friendlyUri(this, peerUri, userAgent.account, true) chatPeer.value = Utils.friendlyUri(this, peerUri, userAgent.account, true)
imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
val title = String.format(getString(R.string.chat_with), chatPeer)
val messagesObserver = Observer<Long> { val messagesObserver = Observer<Long> {
_chatMessages.value = listOf() _chatMessages.value = listOf()
_chatMessages.value = uaPeerMessages(aor, peerUri) _chatMessages.value = uaPeerMessages(aor, peerUri)
@@ -180,21 +177,21 @@ class ChatActivity : ComponentActivity() {
Surface( Surface(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
) { ) {
ChatScreen(this, title) { goBack() } ChatScreen(this) { goBack() }
} }
} }
} }
} }
@Composable @Composable
fun ChatScreen(ctx: Context, title: String, navigateBack: () -> Unit) { fun ChatScreen(ctx: Context, navigateBack: () -> Unit) {
Scaffold( Scaffold(
modifier = Modifier modifier = Modifier
.fillMaxHeight() .fillMaxHeight()
.imePadding() .imePadding()
.safeDrawingPadding(), .safeDrawingPadding(),
containerColor = LocalCustomColors.current.background, containerColor = LocalCustomColors.current.background,
topBar = { TopAppBar(ctx, title, navigateBack) }, topBar = { TopAppBar(ctx, navigateBack) },
bottomBar = { NewMessage(ctx, peerUri) }, bottomBar = { NewMessage(ctx, peerUri) },
content = { contentPadding -> content = { contentPadding ->
ChatContent(ctx, contentPadding) ChatContent(ctx, contentPadding)
@@ -204,11 +201,11 @@ class ChatActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun TopAppBar(ctx: Context, title: String, navigateBack: () -> Unit) { fun TopAppBar(ctx: Context, navigateBack: () -> Unit) {
TopAppBar( TopAppBar(
title = { title = {
Text( Text(
text = title, text = String.format(getString(R.string.chat_with), chatPeer.value),
color = LocalCustomColors.current.light, color = LocalCustomColors.current.light,
fontSize = 22.sp, fontSize = 22.sp,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
@@ -337,11 +334,11 @@ class ChatActivity : ComponentActivity() {
items(items = chatMessages, key = { message -> message.timeStamp }) { message -> items(items = chatMessages, key = { message -> message.timeStamp }) { message ->
val down = message.direction == MESSAGE_DOWN val down = message.direction == MESSAGE_DOWN
val peer: String = if (down) { val peer: String = if (down) {
if (chatPeer.startsWith("sip:") && if (chatPeer.value.startsWith("sip:") &&
(Utils.uriHostPart(message.peerUri) == Utils.uriHostPart(message.aor))) (Utils.uriHostPart(message.peerUri) == Utils.uriHostPart(message.aor)))
Utils.uriUserPart(message.peerUri) Utils.uriUserPart(message.peerUri)
else else
chatPeer chatPeer.value
} }
else else
stringResource(R.string.you) stringResource(R.string.you)
@@ -366,9 +363,9 @@ class ChatActivity : ComponentActivity() {
) { ) {
Button( Button(
onClick = { onClick = {
if (chatPeer == peerUri) { if (Contact.findContact(peerUri) == null) {
dialogMessage.value = String.format(getString(R.string.long_message_question), dialogMessage.value = String.format(getString(R.string.long_message_question),
peerUri chatPeer.value
) )
positiveButtonText.value = getString(R.string.add_contact) positiveButtonText.value = getString(R.string.add_contact)
positiveAction.value = { positiveAction.value = {
@@ -392,6 +389,7 @@ class ChatActivity : ComponentActivity() {
message.delete() message.delete()
_chatMessages.value = uaPeerMessages(aor, peerUri) _chatMessages.value = uaPeerMessages(aor, peerUri)
} }
neutralButtonText.value = ""
} }
showDialog.value = true showDialog.value = true
}, },
@@ -478,14 +476,12 @@ class ChatActivity : ComponentActivity() {
) )
BaresipService.chatTexts.remove("$aor::$peerUri") BaresipService.chatTexts.remove("$aor::$peerUri")
} }
_chatMessages.value = uaPeerMessages(aor, peerUri)
} }
Lifecycle.Event.ON_PAUSE -> { Lifecycle.Event.ON_PAUSE -> {
if (newMessage.value != TextFieldValue("")) { if (newMessage.value != TextFieldValue("")) {
Log.d(TAG, "Saving newMessage '${newMessage.value.text}' for $aor::$peerUri") Log.d(TAG, "Saving newMessage '${newMessage.value.text}' for $aor::$peerUri")
BaresipService.chatTexts["$aor::$peerUri"] = newMessage.value.text BaresipService.chatTexts["$aor::$peerUri"] = newMessage.value.text
} }
MainActivity.activityAor = aor
} }
else -> {} else -> {}
} }
@@ -520,72 +516,7 @@ class ChatActivity : ComponentActivity() {
.onGloballyPositioned { .onGloballyPositioned {
if (!textFieldLoaded) if (!textFieldLoaded)
textFieldLoaded = true textFieldLoaded = true
} },
.combinedClickable(
onClick = {
val msgText = newMessage.value.text
if (msgText.isNotEmpty()) {
keyboardController?.hide()
val time = System.currentTimeMillis()
val msg = Message(
aor,
peerUri,
msgText,
time,
MESSAGE_UP_WAIT,
0,
"",
true
)
msg.add()
var msgUri = ""
_chatMessages.value += msg
if (Utils.isTelUri(peerUri))
if (ua.account.telProvider == "") {
dialogMessage.value = String.format(
getString(R.string.no_telephony_provider),
Utils.plainAor(aor)
)
showDialog.value = true
} else {
msgUri = Utils.telToSip(peerUri, ua.account)
}
else
msgUri = peerUri
if (msgUri != "")
if (Api.message_send(
ua.uap,
msgUri,
msgText,
time.toString()
) != 0
) {
Toast.makeText(
ctx, "${getString(R.string.message_failed)}!",
Toast.LENGTH_SHORT
).show()
msg.direction = MESSAGE_UP_FAIL
msg.responseReason = getString(R.string.message_failed)
} else {
newMessage.value = TextFieldValue("")
keyboardController?.hide()
BaresipService.chatTexts.remove("$aor::$peerUri")
}
}
},
onLongClick = {
val clipboardManager =
ctx.getSystemService(CLIPBOARD_SERVICE) as
android.content.ClipboardManager
val clipData = clipboardManager.primaryClip
if (clipData != null && clipData.itemCount > 0) {
val pastedText = clipData.getItemAt(0).text.toString()
newMessage.value = TextFieldValue(pastedText)
} else {
Toast.makeText(ctx, "Nothing to paste", Toast.LENGTH_SHORT).show()
}
},
),
singleLine = false, singleLine = false,
trailingIcon = { trailingIcon = {
if (newMessage.value.text.isNotEmpty()) { if (newMessage.value.text.isNotEmpty()) {
@@ -655,6 +586,17 @@ class ChatActivity : ComponentActivity() {
} }
} }
override fun onPause() {
super.onPause()
MainActivity.activityAor = aor
}
override fun onResume() {
super.onResume()
_chatMessages.value = uaPeerMessages(aor, peerUri)
chatPeer.value = Utils.friendlyUri(this, peerUri, ua.account, true)
}
override fun onDestroy() { override fun onDestroy() {
if (Build.VERSION.SDK_INT >= 33) { if (Build.VERSION.SDK_INT >= 33) {
if (backInvokedCallback != null) if (backInvokedCallback != null)