Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nav refactor #497

Merged
merged 11 commits into from
Jul 31, 2024
1 change: 1 addition & 0 deletions demo-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation(libs.androidx.navigation.fragment.ktx)
implementation(libs.androidx.navigation.ui.ktx)
implementation(libs.opentelemetry.api.incubator)
implementation(libs.androidx.navigation.compose)

coreLibraryDesugaring(libs.desugarJdkLibs)

Expand Down
6 changes: 4 additions & 2 deletions demo-app/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ opentelemetry-alpha = "1.39.0-alpha"
junit = "5.10.3"
spotless = "6.25.0"
kotlin = "2.0.0"
navigation-compose = "2.7.7"

[libraries]
androidx-appcompat = "androidx.appcompat:appcompat:1.7.0"
Expand Down Expand Up @@ -35,8 +36,9 @@ androidx-constraintlayout = { group = "androidx.constraintlayout", name = "const
material = { group = "com.google.android.material", name = "material", version = "1.12.0" }
androidx-lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version = "2.8.4" }
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version = "2.8.4" }
androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version = "2.7.7" }
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version = "2.7.7" }
androidx-navigation-fragment-ktx = { group = "androidx.navigation", name = "navigation-fragment-ktx", version.ref = "navigation-compose" }
androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation-ui-ktx", version.ref = "navigation-compose" }
androidx-navigation-compose = {group = "androidx.navigation", name = "navigation-compose", version.ref = "navigation-compose"}

[bundles]
junit = ["junit-jupiter-api", "junit-jupiter-engine", "junit-vintage-engine"]
Expand Down
2 changes: 1 addition & 1 deletion demo-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name=".AstronomyShopActivity"
android:name=".ui.shop.AstronomyShopActivity"
android:exported="true"
android:label="@string/title_activity_astronomy_shop"
/>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.opentelemetry.android.demo.theme.DemoAppTheme
import io.opentelemetry.android.demo.ui.shop.AstronomyShopActivity

class MainActivity : ComponentActivity() {
private val viewModel by viewModels<DemoViewModel>()
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.android.demo.ui.shop

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import io.opentelemetry.android.demo.MainActivity
import io.opentelemetry.android.demo.clients.ProductCatalogClient
import io.opentelemetry.android.demo.theme.DemoAppTheme
import io.opentelemetry.android.demo.ui.shop.cart.CartScreen
import io.opentelemetry.android.demo.ui.shop.products.ProductDetails
import io.opentelemetry.android.demo.ui.shop.products.ProductList

class AstronomyShopActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AstronomyShopScreen()
}
}
}

@Composable
fun AstronomyShopScreen() {
val productsClient = ProductCatalogClient(LocalContext.current)
val products by remember { mutableStateOf(productsClient.get()) }
val context = LocalContext.current
val astronomyShopNavController = rememberAstronomyShopNavController()

DemoAppTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Scaffold(
bottomBar = {
BottomNavigationBar(
items = listOf(BottomNavItem.Exit, BottomNavItem.List, BottomNavItem.Cart),
currentRoute = astronomyShopNavController.currentRoute,
onItemClicked = { route ->
astronomyShopNavController.navController.navigate(route) {
popUpTo(astronomyShopNavController.navController.graph.startDestinationId)
launchSingleTop = true
breedx-splk marked this conversation as resolved.
Show resolved Hide resolved
}
},
onExitClicked = {
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
(context as? Activity)?.finish()
}
)
}
) { innerPadding ->
NavHost(
navController = astronomyShopNavController.navController,
startDestination = MainDestinations.HOME_ROUTE,
Modifier.padding(innerPadding)
) {
composable(BottomNavItem.List.route) {
ProductList(products = products) { productId ->
astronomyShopNavController.navigateToProductDetail(productId)
}
}
composable(BottomNavItem.Cart.route) {
CartScreen()
}
composable("${MainDestinations.PRODUCT_DETAIL_ROUTE}/{${MainDestinations.PRODUCT_ID_KEY}}") { backStackEntry ->
val productId = backStackEntry.arguments?.getString(MainDestinations.PRODUCT_ID_KEY)
val product = products.find { it.id == productId }
product?.let { ProductDetails(product = it) }
}
}
}
}
}
}
Loading