migrate dropdown menu

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2025-03-03 15:09:19 +01:00
parent 84ce5df2d7
commit 6ed17603d6
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 54 additions and 49 deletions

View File

@ -8,23 +8,36 @@
package com.nextcloud.talk.diagnose
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.nextcloud.talk.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBar(title: String) {
fun AppBar(title: String, menuItems: List<Pair<String, () -> Unit>>?) {
val backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
var expanded by remember { mutableStateOf(false) }
TopAppBar(
title = { Text(text = title) },
navigationIcon = {
@ -36,6 +49,29 @@ fun AppBar(title: String) {
contentDescription = stringResource(R.string.back_button)
)
}
},
actions = {
Box {
IconButton(onClick = { expanded = true }) {
Icon(Icons.Default.MoreVert, contentDescription = "More Options")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
Modifier.background(colorResource(id = R.color.bg_default))
) {
menuItems?.forEach { (label, action) ->
DropdownMenuItem(
text = { Text(label) },
onClick = {
action()
expanded = false
}
)
}
}
}
}
)
}
@ -43,5 +79,5 @@ fun AppBar(title: String) {
@Preview(showBackground = true)
@Composable
fun AppBarPreview() {
AppBar("title")
AppBar("title", null)
}

View File

@ -16,8 +16,6 @@ import android.os.Build.MANUFACTURER
import android.os.Build.MODEL
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
@ -83,13 +81,27 @@ class DiagnoseActivity : BaseActivity() {
setContent {
val backgroundColor = colorResource(id = R.color.bg_default)
val menuItems = mutableListOf(
stringResource(R.string.nc_common_copy) to { copyToClipboard(diagnoseData.toMarkdownString()) },
stringResource(R.string.share) to { shareToOtherApps(diagnoseData.toMarkdownString()) },
stringResource(R.string.send_email) to { composeEmail(diagnoseData.toMarkdownString()) }
)
if (BrandingUtils.isOriginalNextcloudClient(applicationContext)) {
menuItems.add(
stringResource(R.string.create_issue) to { createGithubIssue(diagnoseData.toMarkdownString()) }
)
}
MaterialTheme(
colorScheme = colorScheme
) {
Scaffold(
topBar = {
AppBar(
title = stringResource(R.string.nc_settings_diagnose_title)
title = stringResource(R.string.nc_settings_diagnose_title),
menuItems
)
},
content = {
@ -122,50 +134,6 @@ class DiagnoseActivity : BaseActivity() {
diagnoseDataState.value = diagnoseData.toList()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_diagnose, menu)
return true
}
override fun onPrepareOptionsMenu(menu: Menu): Boolean {
super.onPrepareOptionsMenu(menu)
menu.findItem(R.id.create_issue).isVisible = BrandingUtils.isOriginalNextcloudClient(applicationContext)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
onBackPressedDispatcher.onBackPressed()
true
}
R.id.copy -> {
copyToClipboard(diagnoseData.toMarkdownString())
true
}
R.id.share -> {
shareToOtherApps(diagnoseData.toMarkdownString())
true
}
R.id.send_mail -> {
composeEmail(diagnoseData.toMarkdownString())
true
}
R.id.create_issue -> {
createGithubIssue(diagnoseData.toMarkdownString())
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
}
private fun shareToOtherApps(message: String) {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
@ -468,6 +436,7 @@ class DiagnoseActivity : BaseActivity() {
markdownText.append("$MARKDOWN_HEADLINE ${it.headline}")
markdownText.append("\n\n")
}
is DiagnoseElement.DiagnoseEntry -> {
markdownText.append("$MARKDOWN_BOLD${it.key}$MARKDOWN_BOLD")
markdownText.append("\n\n")