Skip to content

Commit

Permalink
Merge pull request #2380 from DataDog/yl/compose/add-benchmark-screen
Browse files Browse the repository at this point in the history
RUM-7085: Add compose screens in benchmark application
  • Loading branch information
ambushwork authored Nov 7, 2024
2 parents 3835e65 + 6d5bc34 commit 0bba965
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sample/benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ dependencies {
implementation(libs.timber)
implementation(platform(libs.androidXComposeBom))
implementation(libs.bundles.androidXCompose)
implementation(libs.coilCompose)
implementation(project(":features:dd-sdk-android-logs"))
implementation(project(":features:dd-sdk-android-rum"))
implementation(project(":features:dd-sdk-android-trace"))
Expand All @@ -92,6 +93,7 @@ dependencies {
implementation(project(":features:dd-sdk-android-webview"))
implementation(project(":features:dd-sdk-android-session-replay"))
implementation(project(":features:dd-sdk-android-session-replay-material"))
implementation(project(":features:dd-sdk-android-session-replay-compose"))
implementation(project(":tools:benchmark"))

testImplementation(libs.bundles.jUnit5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MainActivity : AppCompatActivity() {
DatadogBenchmark.Config.resolveSyntheticsBundle(intent.extras)
)
if (datadogBenchmark.isComposeEnabled) {
supportActionBar?.hide()
setContent {
MainView()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.sample.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import com.datadog.sample.benchmark.R

@Composable
internal fun ImageScreen() {
LazyColumn(modifier = Modifier.fillMaxSize()) {
item {
LocalImageNoBackground()
}

item {
LocalIconDoubleBackground()
}

item {
IconButtonSingleBackground()
}
item {
CoilImage()
}
}
}

@Composable
private fun LocalImageNoBackground() {
Row(verticalAlignment = Alignment.CenterVertically) {
Image(
modifier = Modifier.imageModifier(),
painter = painterResource(R.drawable.ic_dd_icon_rgb),
contentDescription = "purple dog"
)
DescriptionText("Image")
}
}

@Composable
private fun LocalIconDoubleBackground() {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(
modifier = Modifier
.padding(16.dp)
.clip(CircleShape)
.background(Color.Green)
.padding(32.dp)
.size(128.dp)
.clip(RoundedCornerShape(8.dp))
.background(Color.DarkGray)
.padding(4.dp),
painter = painterResource(R.drawable.ic_dd_icon_red),
tint = Color.Red,
contentDescription = "red dog"
)
DescriptionText("Icon")
}
}

@Composable
private fun IconButtonSingleBackground() {
Row(verticalAlignment = Alignment.CenterVertically) {
IconButton(
modifier = Modifier
.padding(16.dp)
.clip(RoundedCornerShape(16.dp))
.background(Color.Black),
onClick = {}
) {
Icon(
modifier = Modifier
.padding(16.dp)
.size(160.dp),
painter = painterResource(R.drawable.ic_dd_icon_white),
tint = Color.White,
contentDescription = "white dog"
)
}
DescriptionText("Icon Button")
}
}

@Composable
private fun CoilImage() {
Row(verticalAlignment = Alignment.CenterVertically) {
AsyncImage(
modifier = Modifier.imageModifier(),
model = SMALL_IMAGE_URL,
contentScale = ContentScale.Fit,
contentDescription = "Network Image"
)
DescriptionText("Network Image")
}
}

@Composable
private fun DescriptionText(description: String) {
Text(
description,
fontSize = 20.sp
)
}

private fun Modifier.imageModifier(): Modifier {
return this
.padding(32.dp)
.size(160.dp)
}

@Composable
@Preview(showBackground = true)
internal fun PreviewImageSample() {
ImageScreen()
}

private const val SMALL_IMAGE_URL = "https://picsum.photos/100/100"
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,96 @@

package com.datadog.benchmark.sample.compose

import androidx.compose.foundation.layout.padding
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Info
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hierarchy
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 com.datadog.android.BuildConfig

@Composable
internal fun MainView() {
// TODO RUM-7085: Add compose view
AppScaffold()
}

@Composable
internal fun AppScaffold() {
val navController = rememberNavController()
Scaffold(
topBar = {
TopAppBar {
Text(
modifier = Modifier.padding(start = 16.dp),
text = "Benchmark application (${BuildConfig.BUILD_TYPE})",
style = MaterialTheme.typography.h6
)
}
},
bottomBar = {
BottomNavigationBar(navHostController = navController)
}
) { padding ->
NavHost(
navController = navController,
startDestination = Screen.Text.route,
Modifier.padding(padding)
) {
composable(Screen.Text.route) { TextScreen() }
composable(Screen.Image.route) { ImageScreen() }
composable(Screen.Other.route) { OtherScreen() }
}
}
}

@Composable
internal fun BottomNavigationBar(
modifier: Modifier = Modifier,
navHostController: NavHostController
) {
val currentBackStackEntry by navHostController.currentBackStackEntryAsState()
val currentDestination = currentBackStackEntry?.destination
val screens = listOf(Screen.Text, Screen.Image, Screen.Other)
BottomNavigation(modifier) {
screens.forEach { screen ->
BottomNavigationItem(
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
icon = { Icon(screen.icon, contentDescription = screen.title) },
label = { Text(screen.title) },
onClick = {
navHostController.navigate(screen.route) {
// Avoid multiple copies of the same destination when reselecting
popUpTo(navHostController.graph.startDestinationId) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}

internal sealed class Screen(val route: String, val title: String, val icon: ImageVector) {
object Text : Screen("text", "Text", Icons.Default.Edit)
object Image : Screen("image", "Image", Icons.Default.Info)
object Other : Screen("other", "Other", Icons.Default.Done)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

package com.datadog.benchmark.sample.compose

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Divider
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Slider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

@Composable
internal fun OtherScreen() {
Column(modifier = Modifier.padding(16.dp)) {
SampleSlide()
SampleDropDownMenu()
}
}

@Composable
private fun SampleSlide() {
var value by remember { mutableFloatStateOf(0f) }
Slider(
value = value,
onValueChange = {
value = it
}
)
}

@Composable
private fun SampleDropDownMenu() {
var expanded by remember { mutableStateOf(false) }
Button(
onClick = { expanded = !expanded }
) {
Text("Drop Down Menu")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = !expanded }
) {
DropdownMenuItem(onClick = {}) { Text("Item 1") }
Divider()
DropdownMenuItem(onClick = {}) { Text("Item 2") }
Divider()
DropdownMenuItem(onClick = {}) { Text("Item 3") }
}
}

@Composable
@Preview(showBackground = true)
internal fun PreviewOtherScreen() {
OtherScreen()
}
Loading

0 comments on commit 0bba965

Please sign in to comment.