From 5b54e7bcbf45a59a2bbc58c6046a2c97f5505435 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 13 Apr 2025 19:00:22 +0300 Subject: [PATCH] Composed chats and chats activities Removed unused code and menu resource files --- .../kotlin/com/tutpro/baresip/ChatActivity.kt | 137 ++++++++---------- .../com/tutpro/baresip/ChatsActivity.kt | 16 +- .../com/tutpro/baresip/CodecsActivity.kt | 10 -- app/src/main/res/menu/accounts_menu.xml | 6 - app/src/main/res/menu/call_icon.xml | 12 -- app/src/main/res/menu/check_icon.xml | 12 -- app/src/main/res/menu/main_menu.xml | 28 ---- app/src/main/res/menu/mic_icon.xml | 12 -- app/src/main/res/menu/rec_icon.xml | 11 -- 9 files changed, 72 insertions(+), 172 deletions(-) delete mode 100644 app/src/main/res/menu/accounts_menu.xml delete mode 100644 app/src/main/res/menu/call_icon.xml delete mode 100644 app/src/main/res/menu/check_icon.xml delete mode 100644 app/src/main/res/menu/main_menu.xml delete mode 100644 app/src/main/res/menu/mic_icon.xml delete mode 100644 app/src/main/res/menu/rec_icon.xml diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt index f6719799..465bf9b4 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt @@ -1,7 +1,6 @@ package com.tutpro.baresip import android.content.Context -import android.content.DialogInterface import android.content.Intent import android.os.Bundle import android.os.SystemClock @@ -74,7 +73,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.Observer -import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.tutpro.baresip.CustomElements.AlertDialog import com.tutpro.baresip.CustomElements.Text import com.tutpro.baresip.CustomElements.verticalScrollbar import kotlinx.coroutines.launch @@ -257,6 +256,25 @@ class ChatActivity : ComponentActivity() { @Composable fun Messages(ctx: Context) { + + val showDialog = remember { mutableStateOf(false) } + val dialogMessage = remember { mutableStateOf("") } + val positiveButtonText = remember { mutableStateOf("") } + val positiveAction = remember { mutableStateOf({}) } + val neutralButtonText = remember { mutableStateOf("") } + val neutralAction = remember { mutableStateOf({}) } + + AlertDialog( + showDialog = showDialog, + title = getString(R.string.confirmation), + message = dialogMessage.value, + positiveButtonText = positiveButtonText.value, + onPositiveClicked = positiveAction.value, + neutralButtonText = neutralButtonText.value, + onNeutralClicked = neutralAction.value, + negativeButtonText = getString(R.string.cancel) + ) + val lazyListState = rememberLazyListState() val coroutineScope = rememberCoroutineScope() @@ -319,65 +337,34 @@ class ChatActivity : ComponentActivity() { ) { Button( onClick = { - val dialogClickListener = - DialogInterface.OnClickListener { _, which -> - when (which) { - DialogInterface.BUTTON_POSITIVE -> { - val i = Intent(ctx, BaresipContactActivity::class.java) - val b = Bundle() - b.putBoolean("new", true) - b.putString("uri", peerUri) - i.putExtras(b) - ctx.startActivity(i) - } - - DialogInterface.BUTTON_NEGATIVE -> { - message.delete() - _chatMessages.value = uaPeerMessages(aor, peerUri) - } - - DialogInterface.BUTTON_NEUTRAL -> { - } - } + if (chatPeer == peerUri) { + dialogMessage.value = String.format(ctx.getString(R.string.long_message_question), + peerUri + ) + positiveButtonText.value = ctx.getString(R.string.add_contact) + positiveAction.value = { + val i = Intent(ctx, BaresipContactActivity::class.java) + val b = Bundle() + b.putBoolean("new", true) + b.putString("uri", peerUri) + i.putExtras(b) + ctx.startActivity(i) } - val builder = MaterialAlertDialogBuilder(ctx, R.style.AlertDialogTheme) - if (chatPeer == peerUri) - with(builder) { - setTitle(R.string.confirmation) - setMessage( - String.format( - ctx.getString(R.string.long_message_question), - peerUri - ) - ) - setNeutralButton( - ctx.getString(R.string.cancel), - dialogClickListener - ) - setNegativeButton( - ctx.getString(R.string.delete), - dialogClickListener - ) - setPositiveButton( - ctx.getString(R.string.add_contact), - dialogClickListener - ) - show() + neutralButtonText.value = ctx.getString(R.string.delete) + neutralAction.value = { + message.delete() + _chatMessages.value = uaPeerMessages(aor, peerUri) } - else - with(builder) { - setTitle(R.string.confirmation) - setMessage(ctx.getText(R.string.short_message_question)) - setNeutralButton( - ctx.getString(R.string.cancel), - dialogClickListener - ) - setNegativeButton( - ctx.getString(R.string.delete), - dialogClickListener - ) - show() + } + else { + dialogMessage.value = ctx.getString(R.string.short_message_question) + positiveButtonText.value = ctx.getString(R.string.delete) + positiveAction.value = { + message.delete() + _chatMessages.value = uaPeerMessages(aor, peerUri) } + } + showDialog.value = true }, shape = if (message.direction == MESSAGE_DOWN) RoundedCornerShape(50.dp, 20.dp, 20.dp, 10.dp) @@ -441,8 +428,18 @@ class ChatActivity : ComponentActivity() { @OptIn(ExperimentalFoundationApi::class) @Composable fun NewMessage(ctx: Context, peerUri: String) { + + val showDialog = remember { mutableStateOf(false) } + val dialogMessage = remember { mutableStateOf("") } + + AlertDialog( + showDialog = showDialog, + title = getString(R.string.notice), + message = dialogMessage.value, + positiveButtonText = getString(R.string.ok), + ) + var newMessage by remember { mutableStateOf("") } - // var textFieldState = rememberTextFieldState() Row(modifier = Modifier .fillMaxWidth() .navigationBarsPadding() @@ -479,13 +476,10 @@ class ChatActivity : ComponentActivity() { _chatMessages.value += msg if (Utils.isTelUri(peerUri)) if (ua.account.telProvider == "") { - Utils.alertView( - ctx, getString(R.string.notice), - String.format( - getString(R.string.no_telephony_provider), - Utils.plainAor(aor) - ) - ) + dialogMessage.value = String.format( + getString(R.string.no_telephony_provider), + Utils.plainAor(aor)) + showDialog.value = true } else { msgUri = Utils.telToSip(peerUri, ua.account) } @@ -569,13 +563,10 @@ class ChatActivity : ComponentActivity() { _chatMessages.value += msg if (Utils.isTelUri(peerUri)) if (ua.account.telProvider == "") { - Utils.alertView( - ctx, getString(R.string.notice), - String.format( - getString(R.string.no_telephony_provider), - Utils.plainAor(aor) - ) - ) + dialogMessage.value = String.format( + getString(R.string.no_telephony_provider), + Utils.plainAor(aor)) + showDialog.value = true } else { msgUri = Utils.telToSip(peerUri, ua.account) } diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt index 2f12fb6a..4133ac7f 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt @@ -5,7 +5,6 @@ import android.content.Intent import android.graphics.BitmapFactory import android.os.Bundle import android.text.format.DateUtils.isToday -import android.view.Menu import androidx.activity.ComponentActivity import androidx.activity.OnBackPressedCallback import androidx.activity.compose.setContent @@ -527,7 +526,6 @@ class ChatsActivity: ComponentActivity() { } Spacer(modifier = Modifier.height(8.dp)) } - OutlinedTextField( value = newPeer, placeholder = { @@ -584,18 +582,20 @@ class ChatsActivity: ComponentActivity() { modifier = Modifier.padding(end = 4.dp), onClick = { showSuggestions = false - var peerUri = newPeer.trim() - if (peerUri.isNotEmpty()) { - val uris = Contact.contactUris(peerUri) - if (uris.size == 1) { + var peerText = newPeer.trim() + if (peerText.isNotEmpty()) { + val uris = Contact.contactUris(peerText) + if (uris.isEmpty()) + makeChat(peerText) + else if (uris.size == 1) makeChat(uris[0]) - } else if (uris.size > 1) { + else { items.value = uris itemAction.value = { index -> makeChat(uris[index]) } + showDialog.value = true } - showDialog.value = true } newPeer = "" focusManager.clearFocus() diff --git a/app/src/main/kotlin/com/tutpro/baresip/CodecsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/CodecsActivity.kt index 817dc9e6..3372608d 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CodecsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CodecsActivity.kt @@ -329,16 +329,6 @@ class CodecsActivity : ComponentActivity() { finish() } - - override fun onCreateOptionsMenu(menu: Menu): Boolean { - - super.onCreateOptionsMenu(menu) - val inflater = menuInflater - inflater.inflate(R.menu.check_icon, menu) - return true - - } - private fun goBack() { BaresipService.activities.remove("codecs,$aor,$media") finish() diff --git a/app/src/main/res/menu/accounts_menu.xml b/app/src/main/res/menu/accounts_menu.xml deleted file mode 100644 index 9d5fd68b..00000000 --- a/app/src/main/res/menu/accounts_menu.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - diff --git a/app/src/main/res/menu/call_icon.xml b/app/src/main/res/menu/call_icon.xml deleted file mode 100644 index 45f31c9a..00000000 --- a/app/src/main/res/menu/call_icon.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - diff --git a/app/src/main/res/menu/check_icon.xml b/app/src/main/res/menu/check_icon.xml deleted file mode 100644 index 30eeeb48..00000000 --- a/app/src/main/res/menu/check_icon.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - diff --git a/app/src/main/res/menu/main_menu.xml b/app/src/main/res/menu/main_menu.xml deleted file mode 100644 index 1b618132..00000000 --- a/app/src/main/res/menu/main_menu.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/menu/mic_icon.xml b/app/src/main/res/menu/mic_icon.xml deleted file mode 100644 index 09eebb23..00000000 --- a/app/src/main/res/menu/mic_icon.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - diff --git a/app/src/main/res/menu/rec_icon.xml b/app/src/main/res/menu/rec_icon.xml deleted file mode 100644 index 1ab37ee4..00000000 --- a/app/src/main/res/menu/rec_icon.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - \ No newline at end of file