Skip to content
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
1 change: 1 addition & 0 deletions DittoToolsAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(platform(libs.androidx.compose.composeBom))
implementation(libs.androidx.compose.runtime.runtimeLivedata)
implementation(libs.androidx.compose.material.icons.extended)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a linear ticket to bundle the material icons with tools, as opposed to including the library?

Out of scope for this PR.

But including the material icons as a dependency increases apk size, since its including all icons (and variants). We should be mindful to not increase a customer's apk size if not needed.

I am not sure if gradle would be smart enough to not include these resources if not needed at compile time, that might be something, as part of the ticket you create, to see if the apk size is significantly reduced by removing this library/dependency and just including the icons we want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, and I can test it out to see how much of a difference it makes

implementation(libs.joda.time.joda.time)
implementation(libs.androidx.datastore.preferences)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,32 @@ package live.ditto.tools.toolsviewer

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import live.ditto.Ditto
import live.ditto.tools.R
Expand All @@ -36,6 +40,8 @@ import live.ditto.tools.peerslist.PeersListViewer
import live.ditto.tools.presencedegradationreporter.PresenceDegradationReporterScreen
import live.ditto.tools.presenceviewer.DittoPresenceViewer
import live.ditto.tools.toolsviewer.navigation.Screens
import live.ditto.tools.toolsviewer.theme.ToolsBackgroundDark
import live.ditto.tools.toolsviewer.theme.ToolsBackgroundLight
import live.ditto.tools.toolsviewer.viewmodel.ToolsViewerViewModel

/**
Expand All @@ -61,6 +67,7 @@ fun DittoToolsViewer(
)
}

@OptIn(ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.O)
@Composable
private fun DittoToolsViewerScaffold(
Expand All @@ -69,28 +76,92 @@ private fun DittoToolsViewerScaffold(
onExitTools: () -> Unit,
viewModel: ToolsViewerViewModel = ToolsViewerViewModel()
) {

val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
val isMainScreen = currentRoute == Screens.MainScreen.route

// Read parent app's status bar color
val view = LocalView.current
val statusBarColor = remember {
if (!view.isInEditMode) {
val colorInt = (view.context as? android.app.Activity)?.window?.statusBarColor
colorInt?.let { Color(it) } ?: Color(0xFF6200EE) // Fallback to material purple
} else {
Color(0xFF6200EE)
}
}

// Determine if we should use light or dark content based on background luminance
val useLightContent = statusBarColor.luminance() < 0.5f
val contentColor = if (useLightContent) Color.White else Color.Black

// Background color that adapts to dark mode
val isSystemInDarkTheme = androidx.compose.foundation.isSystemInDarkTheme()
val backgroundColor = if (isSystemInDarkTheme) ToolsBackgroundDark else ToolsBackgroundLight

val handleBackNavigation: () -> Unit = {
if (isMainScreen) {
onExitTools()
} else {
navController.popBackStack()
}
}

// Handle system back button
BackHandler(enabled = true, onBack = handleBackNavigation)

Scaffold(
modifier = modifier,
bottomBar = {
BottomAppBar(
actions = {
Button(onClick = { onExitTools() }) {
Text(text = "Exit Tools")
containerColor = backgroundColor,
topBar = {
TopAppBar(
title = {
Text(
text = when (currentRoute) {
Screens.MainScreen.route -> stringResource(R.string.tools_menu_title)
Screens.PresenceViewerScreen.route -> stringResource(R.string.presence_viewer_tool_label)
Screens.PeersListViewerScreen.route -> stringResource(R.string.peers_list_tool_label)
Screens.DataBrowserScreen.route -> stringResource(R.string.data_browser_tool_label)
Screens.ExportLogsScreen.route -> stringResource(R.string.export_logs_tool_label)
Screens.ExportLogsToPortalScreen.route -> stringResource(R.string.export_logs_to_portal_tool_label)
Screens.DiskUsageScreen.route -> stringResource(R.string.disk_usage_tool_label)
Screens.HealthScreen.route -> stringResource(R.string.health_viewer_tool_label)
Screens.HeartbeatScreen.route -> stringResource(R.string.heartbeat_tool_label)
Screens.PresenceDegradationReporterScreen.route -> stringResource(R.string.presence_degradation_reporter_tool_label)
else -> stringResource(R.string.tools_menu_title)
}
)
},
navigationIcon = {
IconButton(onClick = handleBackNavigation) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = if (isMainScreen) {
stringResource(R.string.exit_tools_content_description)
} else {
stringResource(R.string.back_button_content_description)
}
)
}
},
floatingActionButton = {
MenuFloatingActionButton {
if (navController.currentDestination?.route != Screens.MainScreen.route) {
navController.popBackStack()
}
actions = {
IconButton(onClick = onExitTools) {
Icon(
imageVector = Icons.Default.Close,
contentDescription = stringResource(R.string.exit_tools_content_description)
)
}
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = statusBarColor,
titleContentColor = contentColor,
navigationIconContentColor = contentColor,
actionIconContentColor = contentColor
)
)
}
) { contentPadding ->
) { contentPadding ->
ToolsViewerContent(
navController = navController,
viewModel = viewModel,
Expand All @@ -112,7 +183,7 @@ private fun ToolsViewerContent(
navController = navController,
contentPadding = contentPadding,
ditto = ditto,
toolMenuItems = viewModel.toolsMenuItems()
toolMenuSections = viewModel.toolsMenuSections()
)
}

Expand All @@ -122,17 +193,19 @@ private fun ToolsViewerNavHost(
navController: NavHostController,
contentPadding: PaddingValues,
ditto: Ditto,
toolMenuItems: List<ToolMenuItem>
toolMenuSections: List<ToolMenuSection>
) {
NavHost(
modifier = Modifier.padding(contentPadding),
modifier = Modifier
.fillMaxSize()
.padding(top = contentPadding.calculateTopPadding()),
navController = navController,
startDestination = Screens.MainScreen.route,
) {
composable(Screens.MainScreen.route) {
ToolsMenu(
navController = navController,
menuItems = toolMenuItems,
menuSections = toolMenuSections,
)
}
composable(Screens.PresenceViewerScreen.route) {
Expand Down Expand Up @@ -171,33 +244,3 @@ private fun ToolsViewerNavHost(
}
}

@Composable
private fun MenuFloatingActionButton(onClick: () -> Unit) {
ExtendedFloatingActionButton(
onClick = { onClick() },
icon = { Icon(Icons.Filled.Build, stringResource(R.string.tools_menu_content_description)) },
text = { Text(text = stringResource(R.string.tools_menu)) },
modifier = Modifier.onKeyEvent { keyEvent ->
when (keyEvent.key) {
Key.Spacebar -> {
when (keyEvent.type) {
KeyEventType.KeyUp -> {
onClick()
true
}
else -> false
}
}
else -> false
}
}
)
}

@Preview
@Composable
private fun MenuFloatingActionButtonPreview() {
MenuFloatingActionButton(
onClick = { }
)
}
Loading