Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

added unit test and updated View Model with out of stock condition #31

Open
wants to merge 1 commit into
base: viewmodel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ android {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
namespace 'com.example.dessertclicker'
}

dependencies {
Expand All @@ -56,4 +57,5 @@ dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
testImplementation("junit:junit:4.13.2")
}
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.dessertclicker">
xmlns:tools="http://schemas.android.com/tools">

<application
android:icon="@mipmap/ic_dessert_clicker"
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/example/dessertclicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ private fun DessertClickerApp(
@Composable
private fun DessertClickerApp(
uiState: DessertUiState,
onDessertClicked: () -> Unit,
modifier: Modifier = Modifier
onDessertClicked: () -> Unit
) {
Scaffold(
topBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object Datasource {
Dessert(R.drawable.lollipop, 3000, 4000),
Dessert(R.drawable.marshmallow, 4000, 8000),
Dessert(R.drawable.nougat, 5000, 16000),
Dessert(R.drawable.oreo, 6000, 20000)
Dessert(R.drawable.oreo, 6000, 20000),
Dessert(R.drawable.out_of_stock, 1, 20001)
)
}
49 changes: 30 additions & 19 deletions app/src/main/java/com/example/dessertclicker/ui/DessertViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,44 @@ class DessertViewModel : ViewModel() {
private val _dessertUiState = MutableStateFlow(DessertUiState())
val dessertUiState: StateFlow<DessertUiState> = _dessertUiState.asStateFlow()

fun onDessertClicked() {
_dessertUiState.update { cupcakeUiState ->
val dessertsSold = cupcakeUiState.dessertsSold + 1
val nextDessertIndex = determineDessertIndex(dessertsSold)
cupcakeUiState.copy(
currentDessertIndex = nextDessertIndex,
revenue = cupcakeUiState.revenue + cupcakeUiState.currentDessertPrice,
dessertsSold = dessertsSold,
currentDessertImageId = dessertList[nextDessertIndex].imageId,
currentDessertPrice = dessertList[nextDessertIndex].price
)
}
fun onDessertClicked(){
_dessertUiState.update { cupCakeUiState ->
val dessertsSold: Int = if (cupCakeUiState.currentDessertIndex >= 13){
cupCakeUiState.dessertsSold
}else{
cupCakeUiState.dessertsSold + 1
}
val nextDessertIndex = determineDessertIndex(dessertsSold)
if(cupCakeUiState.currentDessertIndex >= 13){
cupCakeUiState.copy(
currentDessertIndex = nextDessertIndex,
dessertsSold = dessertsSold,
revenue = cupCakeUiState.revenue,
currentDessertPrice = dessertList[nextDessertIndex].price,
currentDessertImageId = dessertList[nextDessertIndex].imageId
)
}else{
cupCakeUiState.copy(
currentDessertIndex = nextDessertIndex,
dessertsSold = dessertsSold,
revenue = cupCakeUiState.revenue + cupCakeUiState.currentDessertPrice,
currentDessertPrice = dessertList[nextDessertIndex].price,
currentDessertImageId = dessertList[nextDessertIndex].imageId
)
}

}

}

private fun determineDessertIndex(dessertsSold: Int): Int {
var dessertIndex = 0
for (index in dessertList.indices) {
if (dessertsSold >= dessertList[index].startProductionAmount) {
dessertIndex = index
} else {
// The list of desserts is sorted by startProductionAmount. As you sell more
// desserts, you'll start producing more expensive desserts as determined by
// startProductionAmount. We know to break as soon as we see a dessert who's
// "startProductionAmount" is greater than the amount sold.
break
}
}
return dessertIndex
return if (dessertIndex <= 12) dessertIndex
else 13
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.dessertclicker

import com.example.dessertclicker.data.Datasource.dessertList
import com.example.dessertclicker.data.DessertUiState
import com.example.dessertclicker.ui.DessertViewModel
import junit.framework.Assert.assertEquals
import org.junit.Test

class DessertViewModelTest {
private var viewModel = DessertViewModel()
private var testUiState = DessertUiState()
@Test
fun onDessertClicked_updates_UI_state_correctly() {
// Set the initial state with some values
val initialUiState = DessertUiState(
currentDessertIndex = 0,
dessertsSold = 0,
revenue = 0,
currentDessertPrice = dessertList[0].price,
currentDessertImageId = dessertList[0].imageId
)

// Simulate clicking the dessert
viewModel.onDessertClicked()
val nextDessertIndex = testDetermineDessertIndex(initialUiState.dessertsSold)
// Expected UI state after the click
val expectedUiState = initialUiState.copy(

currentDessertIndex = nextDessertIndex,
dessertsSold = 1,
revenue = dessertList[initialUiState.currentDessertIndex].price,
currentDessertPrice = dessertList[initialUiState.currentDessertIndex].price,
currentDessertImageId = dessertList[initialUiState.currentDessertIndex].imageId
)

// Assert that the UI state has been updated correctly
assertEquals(expectedUiState, viewModel.dessertUiState.value)
}

private fun testDetermineDessertIndex(dessertsSold: Int): Int {
var dessertIndex = 0
for (index in dessertList.indices) {
if (dessertsSold >= dessertList[index].startProductionAmount) {
dessertIndex = index
}
}
return if (dessertIndex <= 12) dessertIndex
else 13
}
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'com.android.application' version '8.5.0' apply false
id 'com.android.library' version '8.5.0' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}

Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jun 14 16:14:31 EDT 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME