Skip to content

Commit

Permalink
Merge pull request #15 from google-developer-training/main_new_e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
android-dev-lxl authored Jan 29, 2024
2 parents d29bc13 + 0181f5e commit 28a2a63
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ android {

dependencies {
implementation(platform("androidx.compose:compose-bom:2023.08.00"))
implementation("androidx.activity:activity-compose:1.7.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material3:material3-window-size-class")
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:${rootProject.extra["lifecycle_version"]}")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:${rootProject.extra["lifecycle_version"]}")

Expand Down
22 changes: 20 additions & 2 deletions app/src/main/java/com/example/sports/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ package com.example.sports
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.material3.Surface
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLayoutDirection
import com.example.sports.ui.SportsApp
import com.example.sports.ui.theme.SportsTheme

Expand All @@ -31,11 +40,20 @@ import com.example.sports.ui.theme.SportsTheme
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)

setContent {
SportsTheme {
Surface {
val layoutDirection = LocalLayoutDirection.current
Surface(
modifier = Modifier
.padding(
start = WindowInsets.safeDrawing.asPaddingValues()
.calculateStartPadding(layoutDirection),
end = WindowInsets.safeDrawing.asPaddingValues()
.calculateEndPadding(layoutDirection)
)
) {
val windowSize = calculateWindowSizeClass(this)
SportsApp(
windowSize = windowSize.widthSizeClass,
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/example/sports/ui/SportsScreens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,19 @@ private fun SportsListAndDetail(
SportsList(
sports = sports,
onClick = onClick,
contentPadding = contentPadding,
contentPadding = PaddingValues(
top = contentPadding.calculateTopPadding(),
),
modifier = Modifier
.weight(2f)
.padding(horizontal = dimensionResource(R.dimen.padding_medium))
)
SportsDetail(
selectedSport = selectedSport,
modifier = Modifier.weight(3f),
contentPadding = contentPadding,
contentPadding = PaddingValues(
top = contentPadding.calculateTopPadding(),
),
onBackPressed = onBackPressed,
)
}
Expand Down
27 changes: 4 additions & 23 deletions app/src/main/java/com/example/sports/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.example.sports.ui.theme

import android.app.Activity
import android.os.Build
import android.view.View
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
Expand All @@ -27,7 +26,6 @@ import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
Expand Down Expand Up @@ -113,10 +111,13 @@ fun SportsTheme(
darkTheme -> DarkColorScheme
else -> LightColorScheme
}

val view = LocalView.current
if (!view.isInEditMode) {
SideEffect {
setUpEdgeToEdge(view, darkTheme)
val window = (view.context as Activity).window
window.statusBarColor = colorScheme.primary.toArgb()
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
}
}

Expand All @@ -126,23 +127,3 @@ fun SportsTheme(
content = content
)
}

/**
* Sets up edge-to-edge for the window of this [view]. The system icon colors are set to either
* light or dark depending on whether the [darkTheme] is enabled or not.
*/
private fun setUpEdgeToEdge(view: View, darkTheme: Boolean) {
val window = (view.context as Activity).window
WindowCompat.setDecorFitsSystemWindows(window, false)
window.statusBarColor = Color.Transparent.toArgb()
val navigationBarColor = when {
Build.VERSION.SDK_INT >= 29 -> Color.Transparent.toArgb()
Build.VERSION.SDK_INT >= 26 -> Color(0xFF, 0xFF, 0xFF, 0x63).toArgb()
// Min sdk version for this app is 24, this block is for SDK versions 24 and 25
else -> Color(0x00, 0x00, 0x00, 0x50).toArgb()
}
window.navigationBarColor = navigationBarColor
val controller = WindowCompat.getInsetsController(window, view)
controller.isAppearanceLightStatusBars = darkTheme
controller.isAppearanceLightNavigationBars = !darkTheme
}
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
buildscript {
extra.apply {
set("lifecycle_version", "2.6.1")
set("lifecycle_version", "2.6.2")
}
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.1.0" apply false
id("com.android.library") version "8.1.0" apply false
id("com.android.application") version "8.1.3" apply false
id("com.android.library") version "8.1.3" apply false
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
}

0 comments on commit 28a2a63

Please sign in to comment.