├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .github
├── renovate.json
└── ISSUE_TEMPLATE
│ └── android-basics-cupcake-app.md
├── app
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── values
│ │ │ │ ├── themes.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── strings.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ ├── cupcake.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── example
│ │ │ │ └── cupcake
│ │ │ │ ├── data
│ │ │ │ ├── DataSource.kt
│ │ │ │ └── OrderUiState.kt
│ │ │ │ ├── MainActivity.kt
│ │ │ │ ├── ui
│ │ │ │ ├── theme
│ │ │ │ │ ├── Type.kt
│ │ │ │ │ ├── Color.kt
│ │ │ │ │ └── Theme.kt
│ │ │ │ ├── components
│ │ │ │ │ └── CommonUi.kt
│ │ │ │ ├── OrderViewModel.kt
│ │ │ │ ├── StartOrderScreen.kt
│ │ │ │ ├── SummaryScreen.kt
│ │ │ │ └── SelectOptionScreen.kt
│ │ │ │ └── CupcakeScreen.kt
│ │ └── AndroidManifest.xml
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── cupcake
│ │ └── test
│ │ ├── ScreenAssertions.kt
│ │ ├── ComposeRuleExtensions.kt
│ │ ├── CupcakeOrderScreenTest.kt
│ │ └── CupcakeScreenNavigationTest.kt
├── proguard-rules.pro
└── build.gradle.kts
├── .gitignore
├── README.md
├── settings.gradle.kts
├── CONTRIBUTING.md
├── gradle.properties
├── gradlew.bat
├── gradlew
└── LICENSE
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.github/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3 | "extends": [
4 | "local>android/.github:renovate-config"
5 | ]
6 | }
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/google-developer-training/basic-android-kotlin-compose-training-cupcake/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Mar 19 17:30:22 PDT 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Mac files
6 | .DS_Store
7 |
8 | # files for the dex VM
9 | *.dex
10 |
11 | # Java class files
12 | *.class
13 |
14 | # generated files
15 | bin/
16 | gen/
17 |
18 | # Ignore gradle files
19 | .gradle/
20 | build/
21 |
22 | # Local configuration file (sdk path, etc)
23 | local.properties
24 |
25 | # Proguard folder generated by Eclipse
26 | proguard/
27 | proguard-project.txt
28 |
29 | # Eclipse files
30 | .project
31 | .classpath
32 | .settings/
33 |
34 | # Android Studio/IDEA
35 | *.iml
36 | .idea
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Cupcake app
2 | =================================
3 |
4 | This app contains an order flow for cupcakes with options for quantity, flavor, and pickup date.
5 | The order details get displayed on an order summary screen and can be shared to another app to
6 | send the order.
7 |
8 |
9 | Pre-requisites
10 | --------------
11 | * Experience with Kotlin syntax.
12 | * How to create and run a project in Android Studio.
13 | * How to create composable functions
14 |
15 |
16 | Getting Started
17 | ---------------
18 | 1. Install Android Studio, if you don't already have it.
19 | 2. Download the sample.
20 | 3. Import the sample into Android Studio.
21 | 4. Build and run the sample.
22 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/android-basics-cupcake-app.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Navigate between screens with Compose issue template
3 | about: Issue template for Navigate between screens with Compose
4 | title: 'Navigate between screens with Compose: Android Basics with Compose'
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **URL of codelab**
11 |
12 |
13 | **In which task and step of the codelab can this issue be found?**
14 |
15 |
16 | **Describe the problem**
17 |
18 |
19 |
20 |
21 | **Steps to reproduce?**
22 | 1. Go to...
23 | 2. Click on...
24 | 3. See error...
25 |
26 | **Versions**
27 | _Android Studio version:_
28 | _API version of the emulator:_
29 |
30 |
31 | **Additional information**
32 | _Include screenshots if they would be useful in clarifying the problem._
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 | 8dp
19 | 16dp
20 | 1dp
21 |
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/cupcake/test/ScreenAssertions.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.test
17 |
18 | import androidx.navigation.NavController
19 | import org.junit.Assert
20 |
21 | fun NavController.assertCurrentRouteName(expectedRouteName: String) {
22 | Assert.assertEquals(expectedRouteName, currentBackStackEntry?.destination?.route)
23 | }
24 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | pluginManagement {
17 | repositories {
18 | google()
19 | mavenCentral()
20 | gradlePluginPortal()
21 | }
22 | }
23 | dependencyResolutionManagement {
24 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
25 | repositories {
26 | google()
27 | mavenCentral()
28 | }
29 | }
30 | rootProject.name = "Cupcake"
31 | include(":app")
32 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to Contribute
2 |
3 | We'd love to accept your patches and contributions to this project. There are
4 | just a few small guidelines you need to follow.
5 |
6 | ## Contributor License Agreement
7 |
8 | Contributions to this project must be accompanied by a Contributor License
9 | Agreement (CLA). You (or your employer) retain the copyright to your
10 | contribution; this simply gives us permission to use and redistribute your
11 | contributions as part of the project. Head over to
12 | to see your current agreements on file or
13 | to sign a new one.
14 |
15 | You generally only need to submit a CLA once, so if you've already submitted one
16 | (even if it was for a different project), you probably don't need to do it
17 | again.
18 |
19 | ## Code reviews
20 |
21 | All submissions, including submissions by project members, require review. We
22 | use GitHub pull requests for this purpose. Consult
23 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
24 | information on using pull requests.
25 |
26 | ## Community Guidelines
27 |
28 | This project follows
29 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/).
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/data/DataSource.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.data
17 |
18 | import com.example.cupcake.R
19 |
20 | object DataSource {
21 | val flavors = listOf(
22 | R.string.vanilla,
23 | R.string.chocolate,
24 | R.string.red_velvet,
25 | R.string.salted_caramel,
26 | R.string.coffee
27 | )
28 |
29 | val quantityOptions = listOf(
30 | Pair(R.string.one_cupcake, 1),
31 | Pair(R.string.six_cupcakes, 6),
32 | Pair(R.string.twelve_cupcakes, 12)
33 | )
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/MainActivity.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake
17 |
18 | import android.os.Bundle
19 | import androidx.activity.ComponentActivity
20 | import androidx.activity.compose.setContent
21 | import androidx.activity.enableEdgeToEdge
22 | import com.example.cupcake.ui.theme.CupcakeTheme
23 |
24 | class MainActivity : ComponentActivity() {
25 | override fun onCreate(savedInstanceState: Bundle?) {
26 | enableEdgeToEdge()
27 | super.onCreate(savedInstanceState)
28 | setContent {
29 | CupcakeTheme {
30 | CupcakeApp()
31 | }
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/theme/Type.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui.theme
17 |
18 | import androidx.compose.material3.Typography
19 | import androidx.compose.ui.text.TextStyle
20 | import androidx.compose.ui.text.font.FontFamily
21 | import androidx.compose.ui.text.font.FontWeight
22 | import androidx.compose.ui.unit.sp
23 |
24 | // Set of Material typography styles to start with
25 | val Typography = Typography(
26 | bodyLarge = TextStyle(
27 | fontFamily = FontFamily.Default,
28 | fontWeight = FontWeight.Normal,
29 | fontSize = 16.sp,
30 | lineHeight = 24.sp,
31 | letterSpacing = 0.5.sp
32 | )
33 | )
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/data/OrderUiState.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.data
17 |
18 | /**
19 | * Data class that represents the current UI state in terms of [quantity], [flavor],
20 | * [dateOptions], selected pickup [date] and [price]
21 | */
22 | data class OrderUiState(
23 | /** Selected cupcake quantity (1, 6, 12) */
24 | val quantity: Int = 0,
25 | /** Flavor of the cupcakes in the order (such as "Chocolate", "Vanilla", etc..) */
26 | val flavor: String = "",
27 | /** Selected date for pickup (such as "Jan 1") */
28 | val date: String = "",
29 | /** Total price for the order */
30 | val price: String = "",
31 | /** Available pickup dates for the order*/
32 | val pickupOptions: List = listOf()
33 | )
34 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/components/CommonUi.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui.components
17 |
18 | import androidx.compose.material3.MaterialTheme
19 | import androidx.compose.material3.Text
20 | import androidx.compose.runtime.Composable
21 | import androidx.compose.ui.Modifier
22 | import androidx.compose.ui.res.stringResource
23 | import com.example.cupcake.R
24 |
25 | /**
26 | * Composable that displays formatted [price] that will be formatted and displayed on screen
27 | */
28 | @Composable
29 | fun FormattedPriceLabel(subtotal: String, modifier: Modifier = Modifier) {
30 | Text(
31 | text = stringResource(R.string.subtotal_price, subtotal),
32 | modifier = modifier,
33 | style = MaterialTheme.typography.headlineSmall
34 | )
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
19 |
20 |
27 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/cupcake/test/ComposeRuleExtensions.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.test
17 |
18 | import androidx.activity.ComponentActivity
19 | import androidx.annotation.StringRes
20 | import androidx.compose.ui.test.SemanticsNodeInteraction
21 | import androidx.compose.ui.test.junit4.AndroidComposeTestRule
22 | import androidx.compose.ui.test.onNodeWithText
23 | import androidx.test.ext.junit.rules.ActivityScenarioRule
24 |
25 | /**
26 | * Finds a semantics node with the given string resource id.
27 | *
28 | * The [onNodeWithText] finder provided by compose ui test API, doesn't support usage of
29 | * string resource id to find the semantics node. This extension function accesses string resource
30 | * using underlying activity property and passes it to [onNodeWithText] function as argument and
31 | * returns the [SemanticsNodeInteraction] object.
32 | */
33 | fun AndroidComposeTestRule, A>.onNodeWithStringId(
34 | @StringRes id: Int
35 | ): SemanticsNodeInteraction = onNodeWithText(activity.getString(id))
36 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 | Cupcake
18 | Order Cupcakes
19 | One Cupcake
20 | Six Cupcakes
21 | Twelve Cupcakes
22 | Choose Flavor
23 | Vanilla
24 | Chocolate
25 | Red Velvet
26 | Salted Caramel
27 | Coffee
28 | Special Flavor
29 | Cancel
30 | Next
31 | Choose Pickup Date
32 | Order Summary
33 | Send Order to Another App
34 | Quantity
35 | Flavor
36 | Pickup date
37 | Subtotal %s
38 | Total %s
39 | New Cupcake Order
40 | Quantity: %1$s \nFlavor: %2$s \nPickup date: %3$s \nTotal: %4$s \n\nThank you!
41 | Back
42 |
43 | - %d cupcake
44 | - %d cupcakes
45 |
46 |
47 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
16 |
22 |
23 |
24 |
30 |
33 |
36 |
37 |
38 |
39 |
45 |
46 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/build.gradle.kts:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | plugins {
17 | id("com.android.application")
18 | id("org.jetbrains.kotlin.android")
19 | }
20 |
21 | android {
22 | namespace = "com.example.cupcake"
23 | compileSdk = 34
24 |
25 | defaultConfig {
26 | applicationId = "com.example.cupcake"
27 | minSdk = 24
28 | targetSdk = 34
29 | versionCode = 1
30 | versionName = "1.0"
31 |
32 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
33 | vectorDrawables {
34 | useSupportLibrary = true
35 | }
36 | }
37 |
38 | buildTypes {
39 | release {
40 | isMinifyEnabled = false
41 | proguardFiles(
42 | getDefaultProguardFile("proguard-android-optimize.txt"),
43 | "proguard-rules.pro"
44 | )
45 | }
46 | }
47 | compileOptions {
48 | sourceCompatibility = JavaVersion.VERSION_1_8
49 | targetCompatibility = JavaVersion.VERSION_1_8
50 | }
51 | kotlinOptions {
52 | jvmTarget = "1.8"
53 | freeCompilerArgs += "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api"
54 | }
55 | buildFeatures {
56 | compose = true
57 | }
58 | composeOptions {
59 | kotlinCompilerExtensionVersion = "1.5.3"
60 | }
61 | packaging {
62 | resources {
63 | excludes += "/META-INF/{AL2.0,LGPL2.1}"
64 | }
65 | }
66 | }
67 |
68 | dependencies {
69 |
70 | implementation(platform("androidx.compose:compose-bom:2023.10.01"))
71 | implementation("androidx.activity:activity-compose:1.8.0")
72 | implementation("androidx.compose.material3:material3")
73 | implementation("androidx.compose.runtime:runtime")
74 | implementation("androidx.compose.runtime:runtime-livedata")
75 | implementation("androidx.compose.ui:ui")
76 | implementation("androidx.compose.ui:ui-graphics")
77 | implementation("androidx.compose.ui:ui-tooling-preview")
78 | implementation("androidx.core:core-ktx:1.12.0")
79 | implementation("androidx.lifecycle:lifecycle-livedata-ktx:${rootProject.extra["lifecycle_version"]}")
80 | implementation("androidx.lifecycle:lifecycle-runtime-ktx:${rootProject.extra["lifecycle_version"]}")
81 | implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:${rootProject.extra["lifecycle_version"]}")
82 | implementation("androidx.lifecycle:lifecycle-viewmodel-savedstate:${rootProject.extra["lifecycle_version"]}")
83 | implementation("androidx.navigation:navigation-compose:2.7.4")
84 |
85 | androidTestImplementation(platform("androidx.compose:compose-bom:2023.10.01"))
86 | androidTestImplementation("androidx.compose.ui:ui-test-junit4")
87 | androidTestImplementation("androidx.navigation:navigation-testing:2.7.4")
88 | androidTestImplementation("androidx.test.espresso:espresso-intents:3.5.1")
89 | androidTestImplementation("androidx.test.ext:junit:1.1.5")
90 |
91 | debugImplementation("androidx.compose.ui:ui-test-manifest")
92 | debugImplementation("androidx.compose.ui:ui-tooling")
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/theme/Color.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui.theme
17 |
18 | import androidx.compose.ui.graphics.Color
19 |
20 | val md_theme_light_primary = Color(0xFF984062)
21 | val md_theme_light_onPrimary = Color(0xFFFFFFFF)
22 | val md_theme_light_primaryContainer = Color(0xFFFFD9E2)
23 | val md_theme_light_onPrimaryContainer = Color(0xFF3E001E)
24 | val md_theme_light_secondary = Color(0xFF74565F)
25 | val md_theme_light_onSecondary = Color(0xFFFFFFFF)
26 | val md_theme_light_secondaryContainer = Color(0xFFFFD9E2)
27 | val md_theme_light_onSecondaryContainer = Color(0xFF2B151C)
28 | val md_theme_light_tertiary = Color(0xFF7C5635)
29 | val md_theme_light_onTertiary = Color(0xFFFFFFFF)
30 | val md_theme_light_tertiaryContainer = Color(0xFFFFDCC2)
31 | val md_theme_light_onTertiaryContainer = Color(0xFF2E1500)
32 | val md_theme_light_error = Color(0xFFBA1A1A)
33 | val md_theme_light_errorContainer = Color(0xFFFFDAD6)
34 | val md_theme_light_onError = Color(0xFFFFFFFF)
35 | val md_theme_light_onErrorContainer = Color(0xFF410002)
36 | val md_theme_light_background = Color(0xFFFFFBFF)
37 | val md_theme_light_onBackground = Color(0xFF201A1B)
38 | val md_theme_light_surface = Color(0xFFFFFBFF)
39 | val md_theme_light_onSurface = Color(0xFF201A1B)
40 | val md_theme_light_surfaceVariant = Color(0xFFF2DDE2)
41 | val md_theme_light_onSurfaceVariant = Color(0xFF514347)
42 | val md_theme_light_outline = Color(0xFF837377)
43 | val md_theme_light_inverseOnSurface = Color(0xFFFAEEEF)
44 | val md_theme_light_inverseSurface = Color(0xFF352F30)
45 | val md_theme_light_inversePrimary = Color(0xFFFFB0C9)
46 | val md_theme_light_surfaceTint = Color(0xFF984062)
47 | val md_theme_light_outlineVariant = Color(0xFFD5C2C6)
48 | val md_theme_light_scrim = Color(0xFF000000)
49 |
50 | val md_theme_dark_primary = Color(0xFFFFB0C9)
51 | val md_theme_dark_onPrimary = Color(0xFF5E1133)
52 | val md_theme_dark_primaryContainer = Color(0xFF7B294A)
53 | val md_theme_dark_onPrimaryContainer = Color(0xFFFFD9E2)
54 | val md_theme_dark_secondary = Color(0xFFE2BDC7)
55 | val md_theme_dark_onSecondary = Color(0xFF422931)
56 | val md_theme_dark_secondaryContainer = Color(0xFF5A3F47)
57 | val md_theme_dark_onSecondaryContainer = Color(0xFFFFD9E2)
58 | val md_theme_dark_tertiary = Color(0xFFEFBD94)
59 | val md_theme_dark_onTertiary = Color(0xFF48290C)
60 | val md_theme_dark_tertiaryContainer = Color(0xFF623F20)
61 | val md_theme_dark_onTertiaryContainer = Color(0xFFFFDCC2)
62 | val md_theme_dark_error = Color(0xFFFFB4AB)
63 | val md_theme_dark_errorContainer = Color(0xFF93000A)
64 | val md_theme_dark_onError = Color(0xFF690005)
65 | val md_theme_dark_onErrorContainer = Color(0xFFFFDAD6)
66 | val md_theme_dark_background = Color(0xFF201A1B)
67 | val md_theme_dark_onBackground = Color(0xFFEBE0E1)
68 | val md_theme_dark_surface = Color(0xFF201A1B)
69 | val md_theme_dark_onSurface = Color(0xFFEBE0E1)
70 | val md_theme_dark_surfaceVariant = Color(0xFF514347)
71 | val md_theme_dark_onSurfaceVariant = Color(0xFFD5C2C6)
72 | val md_theme_dark_outline = Color(0xFF9E8C90)
73 | val md_theme_dark_inverseOnSurface = Color(0xFF201A1B)
74 | val md_theme_dark_inverseSurface = Color(0xFFEBE0E1)
75 | val md_theme_dark_inversePrimary = Color(0xFF984062)
76 | val md_theme_dark_surfaceTint = Color(0xFFFFB0C9)
77 | val md_theme_dark_outlineVariant = Color(0xFF514347)
78 | val md_theme_dark_scrim = Color(0xFF000000)
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/OrderViewModel.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui
17 |
18 | import androidx.lifecycle.ViewModel
19 | import com.example.cupcake.data.OrderUiState
20 | import kotlinx.coroutines.flow.MutableStateFlow
21 | import kotlinx.coroutines.flow.StateFlow
22 | import kotlinx.coroutines.flow.asStateFlow
23 | import kotlinx.coroutines.flow.update
24 | import java.text.NumberFormat
25 | import java.text.SimpleDateFormat
26 | import java.util.Calendar
27 | import java.util.Locale
28 |
29 | /** Price for a single cupcake */
30 | private const val PRICE_PER_CUPCAKE = 2.00
31 |
32 | /** Additional cost for same day pickup of an order */
33 | private const val PRICE_FOR_SAME_DAY_PICKUP = 3.00
34 |
35 | /**
36 | * [OrderViewModel] holds information about a cupcake order in terms of quantity, flavor, and
37 | * pickup date. It also knows how to calculate the total price based on these order details.
38 | */
39 | class OrderViewModel : ViewModel() {
40 |
41 | /**
42 | * Cupcake state for this order
43 | */
44 | private val _uiState = MutableStateFlow(OrderUiState(pickupOptions = pickupOptions()))
45 | val uiState: StateFlow = _uiState.asStateFlow()
46 |
47 | /**
48 | * Set the quantity [numberCupcakes] of cupcakes for this order's state and update the price
49 | */
50 | fun setQuantity(numberCupcakes: Int) {
51 | _uiState.update { currentState ->
52 | currentState.copy(
53 | quantity = numberCupcakes,
54 | price = calculatePrice(quantity = numberCupcakes)
55 | )
56 | }
57 | }
58 |
59 | /**
60 | * Set the [desiredFlavor] of cupcakes for this order's state.
61 | * Only 1 flavor can be selected for the whole order.
62 | */
63 | fun setFlavor(desiredFlavor: String) {
64 | _uiState.update { currentState ->
65 | currentState.copy(flavor = desiredFlavor)
66 | }
67 | }
68 |
69 | /**
70 | * Set the [pickupDate] for this order's state and update the price
71 | */
72 | fun setDate(pickupDate: String) {
73 | _uiState.update { currentState ->
74 | currentState.copy(
75 | date = pickupDate,
76 | price = calculatePrice(pickupDate = pickupDate)
77 | )
78 | }
79 | }
80 |
81 | /**
82 | * Reset the order state
83 | */
84 | fun resetOrder() {
85 | _uiState.value = OrderUiState(pickupOptions = pickupOptions())
86 | }
87 |
88 | /**
89 | * Returns the calculated price based on the order details.
90 | */
91 | private fun calculatePrice(
92 | quantity: Int = _uiState.value.quantity,
93 | pickupDate: String = _uiState.value.date
94 | ): String {
95 | var calculatedPrice = quantity * PRICE_PER_CUPCAKE
96 | // If the user selected the first option (today) for pickup, add the surcharge
97 | if (pickupOptions()[0] == pickupDate) {
98 | calculatedPrice += PRICE_FOR_SAME_DAY_PICKUP
99 | }
100 | val formattedPrice = NumberFormat.getCurrencyInstance().format(calculatedPrice)
101 | return formattedPrice
102 | }
103 |
104 | /**
105 | * Returns a list of date options starting with the current date and the following 3 dates.
106 | */
107 | private fun pickupOptions(): List {
108 | val dateOptions = mutableListOf()
109 | val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())
110 | val calendar = Calendar.getInstance()
111 | // add current date and the following 3 dates.
112 | repeat(4) {
113 | dateOptions.add(formatter.format(calendar.time))
114 | calendar.add(Calendar.DATE, 1)
115 | }
116 | return dateOptions
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/StartOrderScreen.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui
17 |
18 | import androidx.annotation.StringRes
19 | import androidx.compose.foundation.Image
20 | import androidx.compose.foundation.layout.Arrangement
21 | import androidx.compose.foundation.layout.Column
22 | import androidx.compose.foundation.layout.Spacer
23 | import androidx.compose.foundation.layout.fillMaxSize
24 | import androidx.compose.foundation.layout.fillMaxWidth
25 | import androidx.compose.foundation.layout.height
26 | import androidx.compose.foundation.layout.padding
27 | import androidx.compose.foundation.layout.width
28 | import androidx.compose.foundation.layout.widthIn
29 | import androidx.compose.material3.Button
30 | import androidx.compose.material3.MaterialTheme
31 | import androidx.compose.material3.Text
32 | import androidx.compose.runtime.Composable
33 | import androidx.compose.ui.Alignment
34 | import androidx.compose.ui.Modifier
35 | import androidx.compose.ui.res.dimensionResource
36 | import androidx.compose.ui.res.painterResource
37 | import androidx.compose.ui.res.stringResource
38 | import androidx.compose.ui.tooling.preview.Preview
39 | import androidx.compose.ui.unit.dp
40 | import com.example.cupcake.R
41 | import com.example.cupcake.data.DataSource
42 | import com.example.cupcake.ui.theme.CupcakeTheme
43 |
44 | /**
45 | * Composable that allows the user to select the desired cupcake quantity and expects
46 | * [onNextButtonClicked] lambda that expects the selected quantity and triggers the navigation to
47 | * next screen
48 | */
49 | @Composable
50 | fun StartOrderScreen(
51 | quantityOptions: List>,
52 | onNextButtonClicked: (Int) -> Unit,
53 | modifier: Modifier = Modifier
54 | ) {
55 | Column(
56 | modifier = modifier,
57 | verticalArrangement = Arrangement.SpaceBetween
58 | ) {
59 | Column(
60 | modifier = Modifier.fillMaxWidth(),
61 | horizontalAlignment = Alignment.CenterHorizontally,
62 | verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
63 | ) {
64 | Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_medium)))
65 | Image(
66 | painter = painterResource(R.drawable.cupcake),
67 | contentDescription = null,
68 | modifier = Modifier.width(300.dp)
69 | )
70 | Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_medium)))
71 | Text(
72 | text = stringResource(R.string.order_cupcakes),
73 | style = MaterialTheme.typography.headlineSmall
74 | )
75 | Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_small)))
76 | }
77 | Column(
78 | modifier = Modifier.fillMaxWidth(),
79 | horizontalAlignment = Alignment.CenterHorizontally,
80 | verticalArrangement = Arrangement.spacedBy(
81 | dimensionResource(id = R.dimen.padding_medium)
82 | )
83 | ) {
84 | quantityOptions.forEach { item ->
85 | SelectQuantityButton(
86 | labelResourceId = item.first,
87 | onClick = { onNextButtonClicked(item.second) },
88 | modifier = Modifier.fillMaxWidth(),
89 | )
90 | }
91 | }
92 | }
93 | }
94 |
95 | /**
96 | * Customizable button composable that displays the [labelResourceId]
97 | * and triggers [onClick] lambda when this composable is clicked
98 | */
99 | @Composable
100 | fun SelectQuantityButton(
101 | @StringRes labelResourceId: Int,
102 | onClick: () -> Unit,
103 | modifier: Modifier = Modifier
104 | ) {
105 | Button(
106 | onClick = onClick,
107 | modifier = modifier.widthIn(min = 250.dp)
108 | ) {
109 | Text(stringResource(labelResourceId))
110 | }
111 | }
112 |
113 | @Preview
114 | @Composable
115 | fun StartOrderPreview() {
116 | CupcakeTheme {
117 | StartOrderScreen(
118 | quantityOptions = DataSource.quantityOptions,
119 | onNextButtonClicked = {},
120 | modifier = Modifier
121 | .fillMaxSize()
122 | .padding(dimensionResource(R.dimen.padding_medium))
123 | )
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/SummaryScreen.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui
17 |
18 | import androidx.compose.foundation.layout.Arrangement
19 | import androidx.compose.foundation.layout.Column
20 | import androidx.compose.foundation.layout.Row
21 | import androidx.compose.foundation.layout.Spacer
22 | import androidx.compose.foundation.layout.fillMaxHeight
23 | import androidx.compose.foundation.layout.fillMaxWidth
24 | import androidx.compose.foundation.layout.height
25 | import androidx.compose.foundation.layout.padding
26 | import androidx.compose.material3.Button
27 | import androidx.compose.material3.Divider
28 | import androidx.compose.material3.OutlinedButton
29 | import androidx.compose.material3.Text
30 | import androidx.compose.runtime.Composable
31 | import androidx.compose.ui.Alignment
32 | import androidx.compose.ui.Modifier
33 | import androidx.compose.ui.platform.LocalContext
34 | import androidx.compose.ui.res.dimensionResource
35 | import androidx.compose.ui.res.stringResource
36 | import androidx.compose.ui.text.font.FontWeight
37 | import androidx.compose.ui.tooling.preview.Preview
38 | import com.example.cupcake.R
39 | import com.example.cupcake.data.OrderUiState
40 | import com.example.cupcake.ui.components.FormattedPriceLabel
41 | import com.example.cupcake.ui.theme.CupcakeTheme
42 |
43 | /**
44 | * This composable expects [orderUiState] that represents the order state, [onCancelButtonClicked]
45 | * lambda that triggers canceling the order and passes the final order to [onSendButtonClicked]
46 | * lambda
47 | */
48 | @Composable
49 | fun OrderSummaryScreen(
50 | orderUiState: OrderUiState,
51 | onCancelButtonClicked: () -> Unit,
52 | onSendButtonClicked: (String, String) -> Unit,
53 | modifier: Modifier = Modifier
54 | ) {
55 | val resources = LocalContext.current.resources
56 |
57 | val numberOfCupcakes = resources.getQuantityString(
58 | R.plurals.cupcakes,
59 | orderUiState.quantity,
60 | orderUiState.quantity
61 | )
62 | //Load and format a string resource with the parameters.
63 | val orderSummary = stringResource(
64 | R.string.order_details,
65 | numberOfCupcakes,
66 | orderUiState.flavor,
67 | orderUiState.date,
68 | orderUiState.quantity
69 | )
70 | val newOrder = stringResource(R.string.new_cupcake_order)
71 | //Create a list of order summary to display
72 | val items = listOf(
73 | // Summary line 1: display selected quantity
74 | Pair(stringResource(R.string.quantity), numberOfCupcakes),
75 | // Summary line 2: display selected flavor
76 | Pair(stringResource(R.string.flavor), orderUiState.flavor),
77 | // Summary line 3: display selected pickup date
78 | Pair(stringResource(R.string.pickup_date), orderUiState.date)
79 | )
80 |
81 | Column(
82 | modifier = modifier,
83 | verticalArrangement = Arrangement.SpaceBetween
84 | ) {
85 | Column(
86 | modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium)),
87 | verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
88 | ) {
89 | items.forEach { item ->
90 | Text(item.first.uppercase())
91 | Text(text = item.second, fontWeight = FontWeight.Bold)
92 | Divider(thickness = dimensionResource(R.dimen.thickness_divider))
93 | }
94 | Spacer(modifier = Modifier.height(dimensionResource(R.dimen.padding_small)))
95 | FormattedPriceLabel(
96 | subtotal = orderUiState.price,
97 | modifier = Modifier.align(Alignment.End)
98 | )
99 | }
100 | Row(
101 | modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium))
102 | ) {
103 | Column(
104 | verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_small))
105 | ) {
106 | Button(
107 | modifier = Modifier.fillMaxWidth(),
108 | onClick = { onSendButtonClicked(newOrder, orderSummary) }
109 | ) {
110 | Text(stringResource(R.string.send))
111 | }
112 | OutlinedButton(
113 | modifier = Modifier.fillMaxWidth(),
114 | onClick = onCancelButtonClicked
115 | ) {
116 | Text(stringResource(R.string.cancel))
117 | }
118 | }
119 | }
120 | }
121 | }
122 |
123 | @Preview
124 | @Composable
125 | fun OrderSummaryPreview() {
126 | CupcakeTheme {
127 | OrderSummaryScreen(
128 | orderUiState = OrderUiState(0, "Test", "Test", "$300.00"),
129 | onSendButtonClicked = { subject: String, summary: String -> },
130 | onCancelButtonClicked = {},
131 | modifier = Modifier.fillMaxHeight()
132 | )
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/theme/Theme.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui.theme
17 |
18 | import android.app.Activity
19 | import android.os.Build
20 | import androidx.compose.foundation.isSystemInDarkTheme
21 | import androidx.compose.material3.MaterialTheme
22 | import androidx.compose.material3.darkColorScheme
23 | import androidx.compose.material3.dynamicDarkColorScheme
24 | import androidx.compose.material3.dynamicLightColorScheme
25 | import androidx.compose.material3.lightColorScheme
26 | import androidx.compose.runtime.Composable
27 | import androidx.compose.runtime.SideEffect
28 | import androidx.compose.ui.graphics.toArgb
29 | import androidx.compose.ui.platform.LocalContext
30 | import androidx.compose.ui.platform.LocalView
31 | import androidx.core.view.WindowCompat
32 |
33 | private val LightColors = lightColorScheme(
34 | primary = md_theme_light_primary,
35 | onPrimary = md_theme_light_onPrimary,
36 | primaryContainer = md_theme_light_primaryContainer,
37 | onPrimaryContainer = md_theme_light_onPrimaryContainer,
38 | secondary = md_theme_light_secondary,
39 | onSecondary = md_theme_light_onSecondary,
40 | secondaryContainer = md_theme_light_secondaryContainer,
41 | onSecondaryContainer = md_theme_light_onSecondaryContainer,
42 | tertiary = md_theme_light_tertiary,
43 | onTertiary = md_theme_light_onTertiary,
44 | tertiaryContainer = md_theme_light_tertiaryContainer,
45 | onTertiaryContainer = md_theme_light_onTertiaryContainer,
46 | error = md_theme_light_error,
47 | errorContainer = md_theme_light_errorContainer,
48 | onError = md_theme_light_onError,
49 | onErrorContainer = md_theme_light_onErrorContainer,
50 | background = md_theme_light_background,
51 | onBackground = md_theme_light_onBackground,
52 | surface = md_theme_light_surface,
53 | onSurface = md_theme_light_onSurface,
54 | surfaceVariant = md_theme_light_surfaceVariant,
55 | onSurfaceVariant = md_theme_light_onSurfaceVariant,
56 | outline = md_theme_light_outline,
57 | inverseOnSurface = md_theme_light_inverseOnSurface,
58 | inverseSurface = md_theme_light_inverseSurface,
59 | inversePrimary = md_theme_light_inversePrimary,
60 | surfaceTint = md_theme_light_surfaceTint,
61 | outlineVariant = md_theme_light_outlineVariant,
62 | scrim = md_theme_light_scrim,
63 | )
64 |
65 | private val DarkColors = darkColorScheme(
66 | primary = md_theme_dark_primary,
67 | onPrimary = md_theme_dark_onPrimary,
68 | primaryContainer = md_theme_dark_primaryContainer,
69 | onPrimaryContainer = md_theme_dark_onPrimaryContainer,
70 | secondary = md_theme_dark_secondary,
71 | onSecondary = md_theme_dark_onSecondary,
72 | secondaryContainer = md_theme_dark_secondaryContainer,
73 | onSecondaryContainer = md_theme_dark_onSecondaryContainer,
74 | tertiary = md_theme_dark_tertiary,
75 | onTertiary = md_theme_dark_onTertiary,
76 | tertiaryContainer = md_theme_dark_tertiaryContainer,
77 | onTertiaryContainer = md_theme_dark_onTertiaryContainer,
78 | error = md_theme_dark_error,
79 | errorContainer = md_theme_dark_errorContainer,
80 | onError = md_theme_dark_onError,
81 | onErrorContainer = md_theme_dark_onErrorContainer,
82 | background = md_theme_dark_background,
83 | onBackground = md_theme_dark_onBackground,
84 | surface = md_theme_dark_surface,
85 | onSurface = md_theme_dark_onSurface,
86 | surfaceVariant = md_theme_dark_surfaceVariant,
87 | onSurfaceVariant = md_theme_dark_onSurfaceVariant,
88 | outline = md_theme_dark_outline,
89 | inverseOnSurface = md_theme_dark_inverseOnSurface,
90 | inverseSurface = md_theme_dark_inverseSurface,
91 | inversePrimary = md_theme_dark_inversePrimary,
92 | surfaceTint = md_theme_dark_surfaceTint,
93 | outlineVariant = md_theme_dark_outlineVariant,
94 | scrim = md_theme_dark_scrim,
95 | )
96 |
97 | @Composable
98 | fun CupcakeTheme(
99 | darkTheme: Boolean = isSystemInDarkTheme(),
100 | // Dynamic color is available on Android 12+ but turned off for training purposes
101 | dynamicColor: Boolean = false,
102 | content: @Composable () -> Unit
103 | ) {
104 | val colorScheme = when {
105 | dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
106 | val context = LocalContext.current
107 | if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
108 | }
109 |
110 | darkTheme -> DarkColors
111 | else -> LightColors
112 | }
113 | val view = LocalView.current
114 | if (!view.isInEditMode) {
115 | SideEffect {
116 | val window = (view.context as Activity).window
117 | window.statusBarColor = colorScheme.primary.toArgb()
118 | WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
119 | }
120 | }
121 |
122 | MaterialTheme(
123 | colorScheme = colorScheme,
124 | typography = Typography,
125 | content = content
126 | )
127 | }
128 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/ui/SelectOptionScreen.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.ui
17 |
18 | import androidx.compose.foundation.layout.Arrangement
19 | import androidx.compose.foundation.layout.Column
20 | import androidx.compose.foundation.layout.Row
21 | import androidx.compose.foundation.layout.fillMaxHeight
22 | import androidx.compose.foundation.layout.fillMaxWidth
23 | import androidx.compose.foundation.layout.padding
24 | import androidx.compose.foundation.selection.selectable
25 | import androidx.compose.material3.Button
26 | import androidx.compose.material3.Divider
27 | import androidx.compose.material3.OutlinedButton
28 | import androidx.compose.material3.RadioButton
29 | import androidx.compose.material3.Text
30 | import androidx.compose.runtime.Composable
31 | import androidx.compose.runtime.getValue
32 | import androidx.compose.runtime.mutableStateOf
33 | import androidx.compose.runtime.saveable.rememberSaveable
34 | import androidx.compose.runtime.setValue
35 | import androidx.compose.ui.Alignment
36 | import androidx.compose.ui.Modifier
37 | import androidx.compose.ui.res.dimensionResource
38 | import androidx.compose.ui.res.stringResource
39 | import androidx.compose.ui.tooling.preview.Preview
40 | import com.example.cupcake.R
41 | import com.example.cupcake.ui.components.FormattedPriceLabel
42 | import com.example.cupcake.ui.theme.CupcakeTheme
43 |
44 | /**
45 | * Composable that displays the list of items as [RadioButton] options,
46 | * [onSelectionChanged] lambda that notifies the parent composable when a new value is selected,
47 | * [onCancelButtonClicked] lambda that cancels the order when user clicks cancel and
48 | * [onNextButtonClicked] lambda that triggers the navigation to next screen
49 | */
50 | @Composable
51 | fun SelectOptionScreen(
52 | subtotal: String,
53 | options: List,
54 | onSelectionChanged: (String) -> Unit = {},
55 | onCancelButtonClicked: () -> Unit = {},
56 | onNextButtonClicked: () -> Unit = {},
57 | modifier: Modifier = Modifier
58 | ) {
59 | var selectedValue by rememberSaveable { mutableStateOf("") }
60 |
61 | Column(
62 | modifier = modifier,
63 | verticalArrangement = Arrangement.SpaceBetween
64 | ) {
65 | Column(modifier = Modifier.padding(dimensionResource(R.dimen.padding_medium))) {
66 | options.forEach { item ->
67 | Row(
68 | modifier = Modifier.selectable(
69 | selected = selectedValue == item,
70 | onClick = {
71 | selectedValue = item
72 | onSelectionChanged(item)
73 | }
74 | ),
75 | verticalAlignment = Alignment.CenterVertically
76 | ) {
77 | RadioButton(
78 | selected = selectedValue == item,
79 | onClick = {
80 | selectedValue = item
81 | onSelectionChanged(item)
82 | }
83 | )
84 | Text(item)
85 | }
86 | }
87 | Divider(
88 | thickness = dimensionResource(R.dimen.thickness_divider),
89 | modifier = Modifier.padding(bottom = dimensionResource(R.dimen.padding_medium))
90 | )
91 | FormattedPriceLabel(
92 | subtotal = subtotal,
93 | modifier = Modifier
94 | .align(Alignment.End)
95 | .padding(
96 | top = dimensionResource(R.dimen.padding_medium),
97 | bottom = dimensionResource(R.dimen.padding_medium)
98 | )
99 | )
100 | }
101 | Row(
102 | modifier = Modifier
103 | .fillMaxWidth()
104 | .padding(dimensionResource(R.dimen.padding_medium)),
105 | horizontalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.padding_medium)),
106 | verticalAlignment = Alignment.Bottom
107 | ) {
108 | OutlinedButton(
109 | modifier = Modifier.weight(1f),
110 | onClick = onCancelButtonClicked
111 | ) {
112 | Text(stringResource(R.string.cancel))
113 | }
114 | Button(
115 | modifier = Modifier.weight(1f),
116 | // the button is enabled when the user makes a selection
117 | enabled = selectedValue.isNotEmpty(),
118 | onClick = onNextButtonClicked
119 | ) {
120 | Text(stringResource(R.string.next))
121 | }
122 | }
123 | }
124 |
125 | }
126 |
127 | @Preview
128 | @Composable
129 | fun SelectOptionPreview() {
130 | CupcakeTheme {
131 | SelectOptionScreen(
132 | subtotal = "299.99",
133 | options = listOf("Option 1", "Option 2", "Option 3", "Option 4"),
134 | modifier = Modifier.fillMaxHeight()
135 | )
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/cupcake/test/CupcakeOrderScreenTest.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.test
17 |
18 | import androidx.activity.ComponentActivity
19 | import androidx.compose.ui.test.assertIsDisplayed
20 | import androidx.compose.ui.test.assertIsEnabled
21 | import androidx.compose.ui.test.assertIsNotEnabled
22 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
23 | import androidx.compose.ui.test.onNodeWithText
24 | import androidx.compose.ui.test.performClick
25 | import com.example.cupcake.R
26 | import com.example.cupcake.data.DataSource
27 | import com.example.cupcake.data.OrderUiState
28 | import com.example.cupcake.ui.OrderSummaryScreen
29 | import com.example.cupcake.ui.SelectOptionScreen
30 | import com.example.cupcake.ui.StartOrderScreen
31 | import org.junit.Rule
32 | import org.junit.Test
33 |
34 | class CupcakeOrderScreenTest {
35 |
36 | /**
37 | * Note: To access to an empty activity, the code uses ComponentActivity instead of
38 | * MainActivity.
39 | */
40 | @get:Rule
41 | val composeTestRule = createAndroidComposeRule()
42 |
43 | private val fakeOrderUiState = OrderUiState(
44 | quantity = 6,
45 | flavor = "Vanilla",
46 | date = "Wed Jul 21",
47 | price = "$100",
48 | pickupOptions = listOf()
49 | )
50 |
51 | /**
52 | * When quantity options are provided to StartOrderScreen, the options are displayed on the
53 | * screen.
54 | */
55 | @Test
56 | fun startOrderScreen_verifyContent() {
57 |
58 | // When StartOrderScreen is loaded
59 | composeTestRule.setContent {
60 | StartOrderScreen(
61 | quantityOptions = DataSource.quantityOptions,
62 | onNextButtonClicked = {}
63 | )
64 | }
65 |
66 | // Then all the options are displayed on the screen.
67 | DataSource.quantityOptions.forEach {
68 | composeTestRule.onNodeWithStringId(it.first).assertIsDisplayed()
69 | }
70 | }
71 |
72 | /**
73 | * When list of options and subtotal are provided to SelectOptionScreen,the options and subtotal
74 | * are displayed on the screen and the next button is disabled.
75 | */
76 | @Test
77 | fun selectOptionScreen_verifyContent() {
78 | // Given list of options
79 | val flavors = listOf("Vanilla", "Chocolate", "Hazelnut", "Cookie", "Mango")
80 | // And subtotal
81 | val subtotal = "$100"
82 |
83 | // When SelectOptionScreen is loaded
84 | composeTestRule.setContent {
85 | SelectOptionScreen(subtotal = subtotal, options = flavors)
86 | }
87 |
88 | // Then all the options are displayed on the screen.
89 | flavors.forEach { flavor ->
90 | composeTestRule.onNodeWithText(flavor).assertIsDisplayed()
91 | }
92 |
93 | // And then the subtotal is displayed correctly.
94 | composeTestRule.onNodeWithText(
95 | composeTestRule.activity.getString(
96 | R.string.subtotal_price,
97 | subtotal
98 | )
99 | ).assertIsDisplayed()
100 |
101 | // And then the next button is disabled
102 | composeTestRule.onNodeWithStringId(R.string.next).assertIsNotEnabled()
103 | }
104 |
105 | /**
106 | * When list of options and subtotal are provided to SelectOptionScreen, and one of the option
107 | * is selected, then the next button is enabled.
108 | */
109 | @Test
110 | fun selectOptionScreen_optionSelected_NextButtonEnabled() {
111 | // Given list of options
112 | val flavours = listOf("Vanilla", "Chocolate", "Hazelnut", "Cookie", "Mango")
113 | // And sub total
114 | val subTotal = "$100"
115 |
116 | // When SelectOptionScreen is loaded
117 | composeTestRule.setContent {
118 | SelectOptionScreen(subtotal = subTotal, options = flavours)
119 | }
120 |
121 | // And one option is selected
122 | composeTestRule.onNodeWithText("Vanilla").performClick()
123 |
124 | // Then the next button is disabled
125 | composeTestRule.onNodeWithStringId(R.string.next).assertIsEnabled()
126 | }
127 |
128 | /**
129 | * When a OrderUiState is provided to Summary Screen, then the flavor, date and subtotal is
130 | * displayed on the screen.
131 | */
132 | @Test
133 | fun summaryScreen_verifyContentDisplay() {
134 | // When Summary Screen is loaded
135 | composeTestRule.setContent {
136 | OrderSummaryScreen(
137 | orderUiState = fakeOrderUiState,
138 | onCancelButtonClicked = {},
139 | onSendButtonClicked = { _, _ -> },
140 | )
141 | }
142 |
143 | // Then the UI is updated correctly.
144 | composeTestRule.onNodeWithText(fakeOrderUiState.flavor).assertIsDisplayed()
145 | composeTestRule.onNodeWithText(fakeOrderUiState.date).assertIsDisplayed()
146 | composeTestRule.onNodeWithText(
147 | composeTestRule.activity.getString(
148 | R.string.subtotal_price,
149 | fakeOrderUiState.price
150 | )
151 | ).assertIsDisplayed()
152 | }
153 | }
154 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/cupcake/test/CupcakeScreenNavigationTest.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake.test
17 |
18 | import android.icu.util.Calendar
19 | import androidx.activity.ComponentActivity
20 | import androidx.compose.ui.platform.LocalContext
21 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
22 | import androidx.compose.ui.test.onNodeWithContentDescription
23 | import androidx.compose.ui.test.onNodeWithText
24 | import androidx.compose.ui.test.performClick
25 | import androidx.navigation.compose.ComposeNavigator
26 | import androidx.navigation.testing.TestNavHostController
27 | import com.example.cupcake.CupcakeApp
28 | import com.example.cupcake.CupcakeScreen
29 | import com.example.cupcake.R
30 | import org.junit.Before
31 | import org.junit.Rule
32 | import org.junit.Test
33 | import java.text.SimpleDateFormat
34 | import java.util.Locale
35 |
36 | class CupcakeScreenNavigationTest {
37 |
38 | /**
39 | * Note: To access to an empty activity, the code uses ComponentActivity instead of
40 | * MainActivity.
41 | */
42 | @get:Rule
43 | val composeTestRule = createAndroidComposeRule()
44 |
45 | private lateinit var navController: TestNavHostController
46 |
47 | @Before
48 | fun setupCupcakeNavHost() {
49 | composeTestRule.setContent {
50 | navController = TestNavHostController(LocalContext.current).apply {
51 | navigatorProvider.addNavigator(ComposeNavigator())
52 | }
53 | CupcakeApp(navController = navController)
54 | }
55 | }
56 |
57 | @Test
58 | fun cupcakeNavHost_verifyStartDestination() {
59 | navController.assertCurrentRouteName(CupcakeScreen.Start.name)
60 | }
61 |
62 | @Test
63 | fun cupcakeNavHost_verifyBackNavigationNotShownOnStartOrderScreen() {
64 | val backText = composeTestRule.activity.getString(R.string.back_button)
65 | composeTestRule.onNodeWithContentDescription(backText).assertDoesNotExist()
66 | }
67 |
68 | @Test
69 | fun cupcakeNavHost_clickOneCupcake_navigatesToSelectFlavorScreen() {
70 | composeTestRule.onNodeWithStringId(R.string.one_cupcake)
71 | .performClick()
72 | navController.assertCurrentRouteName(CupcakeScreen.Flavor.name)
73 | }
74 |
75 | @Test
76 | fun cupcakeNavHost_clickNextOnFlavorScreen_navigatesToPickupScreen() {
77 | navigateToFlavorScreen()
78 | composeTestRule.onNodeWithStringId(R.string.next)
79 | .performClick()
80 | navController.assertCurrentRouteName(CupcakeScreen.Pickup.name)
81 | }
82 |
83 | @Test
84 | fun cupcakeNavHost_clickBackOnFlavorScreen_navigatesToStartOrderScreen() {
85 | navigateToFlavorScreen()
86 | performNavigateUp()
87 | navController.assertCurrentRouteName(CupcakeScreen.Start.name)
88 | }
89 |
90 | @Test
91 | fun cupcakeNavHost_clickCancelOnFlavorScreen_navigatesToStartOrderScreen() {
92 | navigateToFlavorScreen()
93 | composeTestRule.onNodeWithStringId(R.string.cancel)
94 | .performClick()
95 | navController.assertCurrentRouteName(CupcakeScreen.Start.name)
96 | }
97 |
98 | @Test
99 | fun cupcakeNavHost_clickNextOnPickupScreen_navigatesToSummaryScreen() {
100 | navigateToPickupScreen()
101 | composeTestRule.onNodeWithText(getFormattedDate())
102 | .performClick()
103 | composeTestRule.onNodeWithStringId(R.string.next)
104 | .performClick()
105 | navController.assertCurrentRouteName(CupcakeScreen.Summary.name)
106 | }
107 |
108 | @Test
109 | fun cupcakeNavHost_clickBackOnPickupScreen_navigatesToFlavorScreen() {
110 | navigateToPickupScreen()
111 | performNavigateUp()
112 | navController.assertCurrentRouteName(CupcakeScreen.Flavor.name)
113 | }
114 |
115 | @Test
116 | fun cupcakeNavHost_clickCancelOnPickupScreen_navigatesToStartOrderScreen() {
117 | navigateToPickupScreen()
118 | composeTestRule.onNodeWithStringId(R.string.cancel)
119 | .performClick()
120 | navController.assertCurrentRouteName(CupcakeScreen.Start.name)
121 | }
122 |
123 | @Test
124 | fun cupcakeNavHost_clickCancelOnSummaryScreen_navigatesToStartOrderScreen() {
125 | navigateToSummaryScreen()
126 | composeTestRule.onNodeWithStringId(R.string.cancel)
127 | .performClick()
128 | navController.assertCurrentRouteName(CupcakeScreen.Start.name)
129 | }
130 |
131 | private fun navigateToFlavorScreen() {
132 | composeTestRule.onNodeWithStringId(R.string.one_cupcake)
133 | .performClick()
134 | composeTestRule.onNodeWithStringId(R.string.chocolate)
135 | .performClick()
136 | }
137 |
138 | private fun navigateToPickupScreen() {
139 | navigateToFlavorScreen()
140 | composeTestRule.onNodeWithStringId(R.string.next)
141 | .performClick()
142 | }
143 |
144 | private fun navigateToSummaryScreen() {
145 | navigateToPickupScreen()
146 | composeTestRule.onNodeWithText(getFormattedDate())
147 | .performClick()
148 | composeTestRule.onNodeWithStringId(R.string.next)
149 | .performClick()
150 | }
151 |
152 | private fun performNavigateUp() {
153 | val backText = composeTestRule.activity.getString(R.string.back_button)
154 | composeTestRule.onNodeWithContentDescription(backText).performClick()
155 | }
156 |
157 | private fun getFormattedDate(): String {
158 | val calendar = Calendar.getInstance()
159 | calendar.add(java.util.Calendar.DATE, 1)
160 | val formatter = SimpleDateFormat("E MMM d", Locale.getDefault())
161 | return formatter.format(calendar.time)
162 | }
163 | }
164 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/cupcake.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
22 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
175 |
180 |
185 |
186 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/cupcake/CupcakeScreen.kt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2023 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * https://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.example.cupcake
17 |
18 | import android.content.Context
19 | import android.content.Intent
20 | import androidx.annotation.StringRes
21 | import androidx.compose.foundation.layout.fillMaxHeight
22 | import androidx.compose.foundation.layout.fillMaxSize
23 | import androidx.compose.foundation.layout.padding
24 | import androidx.compose.foundation.rememberScrollState
25 | import androidx.compose.foundation.verticalScroll
26 | import androidx.compose.material.icons.Icons
27 | import androidx.compose.material.icons.filled.ArrowBack
28 | import androidx.compose.material3.Icon
29 | import androidx.compose.material3.IconButton
30 | import androidx.compose.material3.MaterialTheme
31 | import androidx.compose.material3.Scaffold
32 | import androidx.compose.material3.Text
33 | import androidx.compose.material3.TopAppBar
34 | import androidx.compose.material3.TopAppBarDefaults
35 | import androidx.compose.runtime.Composable
36 | import androidx.compose.runtime.collectAsState
37 | import androidx.compose.runtime.getValue
38 | import androidx.compose.ui.Modifier
39 | import androidx.compose.ui.platform.LocalContext
40 | import androidx.compose.ui.res.dimensionResource
41 | import androidx.compose.ui.res.stringResource
42 | import androidx.lifecycle.viewmodel.compose.viewModel
43 | import androidx.navigation.NavHostController
44 | import androidx.navigation.compose.NavHost
45 | import androidx.navigation.compose.composable
46 | import androidx.navigation.compose.currentBackStackEntryAsState
47 | import androidx.navigation.compose.rememberNavController
48 | import com.example.cupcake.data.DataSource
49 | import com.example.cupcake.data.OrderUiState
50 | import com.example.cupcake.ui.OrderSummaryScreen
51 | import com.example.cupcake.ui.OrderViewModel
52 | import com.example.cupcake.ui.SelectOptionScreen
53 | import com.example.cupcake.ui.StartOrderScreen
54 |
55 | /**
56 | * enum values that represent the screens in the app
57 | */
58 | enum class CupcakeScreen(@StringRes val title: Int) {
59 | Start(title = R.string.app_name),
60 | Flavor(title = R.string.choose_flavor),
61 | Pickup(title = R.string.choose_pickup_date),
62 | Summary(title = R.string.order_summary)
63 | }
64 |
65 | /**
66 | * Composable that displays the topBar and displays back button if back navigation is possible.
67 | */
68 | @Composable
69 | fun CupcakeAppBar(
70 | currentScreen: CupcakeScreen,
71 | canNavigateBack: Boolean,
72 | navigateUp: () -> Unit,
73 | modifier: Modifier = Modifier
74 | ) {
75 | TopAppBar(
76 | title = { Text(stringResource(currentScreen.title)) },
77 | colors = TopAppBarDefaults.mediumTopAppBarColors(
78 | containerColor = MaterialTheme.colorScheme.primaryContainer
79 | ),
80 | modifier = modifier,
81 | navigationIcon = {
82 | if (canNavigateBack) {
83 | IconButton(onClick = navigateUp) {
84 | Icon(
85 | imageVector = Icons.Filled.ArrowBack,
86 | contentDescription = stringResource(R.string.back_button)
87 | )
88 | }
89 | }
90 | }
91 | )
92 | }
93 |
94 | @Composable
95 | fun CupcakeApp(
96 | viewModel: OrderViewModel = viewModel(),
97 | navController: NavHostController = rememberNavController()
98 | ) {
99 | // Get current back stack entry
100 | val backStackEntry by navController.currentBackStackEntryAsState()
101 | // Get the name of the current screen
102 | val currentScreen = CupcakeScreen.valueOf(
103 | backStackEntry?.destination?.route ?: CupcakeScreen.Start.name
104 | )
105 |
106 | Scaffold(
107 | topBar = {
108 | CupcakeAppBar(
109 | currentScreen = currentScreen,
110 | canNavigateBack = navController.previousBackStackEntry != null,
111 | navigateUp = { navController.navigateUp() }
112 | )
113 | }
114 | ) { innerPadding ->
115 | val uiState by viewModel.uiState.collectAsState()
116 |
117 | NavHost(
118 | navController = navController,
119 | startDestination = CupcakeScreen.Start.name,
120 | modifier = Modifier
121 | .fillMaxSize()
122 | .verticalScroll(rememberScrollState())
123 | .padding(innerPadding)
124 | ) {
125 | composable(route = CupcakeScreen.Start.name) {
126 | StartOrderScreen(
127 | quantityOptions = DataSource.quantityOptions,
128 | onNextButtonClicked = {
129 | viewModel.setQuantity(it)
130 | navController.navigate(CupcakeScreen.Flavor.name)
131 | },
132 | modifier = Modifier
133 | .fillMaxSize()
134 | .padding(dimensionResource(R.dimen.padding_medium))
135 | )
136 | }
137 | composable(route = CupcakeScreen.Flavor.name) {
138 | val context = LocalContext.current
139 | SelectOptionScreen(
140 | subtotal = uiState.price,
141 | onNextButtonClicked = { navController.navigate(CupcakeScreen.Pickup.name) },
142 | onCancelButtonClicked = {
143 | cancelOrderAndNavigateToStart(viewModel, navController)
144 | },
145 | options = DataSource.flavors.map { id -> context.resources.getString(id) },
146 | onSelectionChanged = { viewModel.setFlavor(it) },
147 | modifier = Modifier.fillMaxHeight()
148 | )
149 | }
150 | composable(route = CupcakeScreen.Pickup.name) {
151 | SelectOptionScreen(
152 | subtotal = uiState.price,
153 | onNextButtonClicked = { navController.navigate(CupcakeScreen.Summary.name) },
154 | onCancelButtonClicked = {
155 | cancelOrderAndNavigateToStart(viewModel, navController)
156 | },
157 | options = uiState.pickupOptions,
158 | onSelectionChanged = { viewModel.setDate(it) },
159 | modifier = Modifier.fillMaxHeight()
160 | )
161 | }
162 | composable(route = CupcakeScreen.Summary.name) {
163 | val context = LocalContext.current
164 | OrderSummaryScreen(
165 | orderUiState = uiState,
166 | onCancelButtonClicked = {
167 | cancelOrderAndNavigateToStart(viewModel, navController)
168 | },
169 | onSendButtonClicked = { subject: String, summary: String ->
170 | shareOrder(context, subject = subject, summary = summary)
171 | },
172 | modifier = Modifier.fillMaxHeight()
173 | )
174 | }
175 | }
176 | }
177 | }
178 |
179 | /**
180 | * Resets the [OrderUiState] and pops up to [CupcakeScreen.Start]
181 | */
182 | private fun cancelOrderAndNavigateToStart(
183 | viewModel: OrderViewModel,
184 | navController: NavHostController
185 | ) {
186 | viewModel.resetOrder()
187 | navController.popBackStack(CupcakeScreen.Start.name, inclusive = false)
188 | }
189 |
190 | /**
191 | * Creates an intent to share order details
192 | */
193 | private fun shareOrder(context: Context, subject: String, summary: String) {
194 | // Create an ACTION_SEND implicit intent with order details in the intent extras
195 | val intent = Intent(Intent.ACTION_SEND).apply {
196 | type = "text/plain"
197 | putExtra(Intent.EXTRA_SUBJECT, subject)
198 | putExtra(Intent.EXTRA_TEXT, summary)
199 | }
200 | context.startActivity(
201 | Intent.createChooser(
202 | intent,
203 | context.getString(R.string.new_cupcake_order)
204 | )
205 | )
206 | }
207 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------