Skip to content

Commit

Permalink
refactor EditRecipeViewModel initialization to use launched effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Weaver authored and Cody Weaver committed Dec 30, 2023
1 parent a886a9e commit 4e63f29
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PaInstrumentedTest {

/**
* Tests functionality of NewRecipeFAB
* TODO: TEST CLICK
* TODO: TEST CLICK SETS SAVED STATE HANDLE
*/
@Test
fun createRecipeFABTest() {
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/android/pocketalchemy/PaNavHost.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.android.pocketalchemy

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavType
Expand Down Expand Up @@ -39,8 +40,9 @@ fun PaNavHost() {
val editRecipeViewModel = hiltViewModel<EditRecipeViewModel>()
val recipeIdArg = remember { backStackEntry.arguments?.getString("recipeId") }

// Has no effect on recomposition
editRecipeViewModel.setRecipeId(recipeIdArg)
LaunchedEffect(recipeIdArg) {
editRecipeViewModel.setRecipeId(recipeIdArg)
}

EditRecipeScreen(
navController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class RecipeRepository @Inject constructor(
// Creates new document ref
firestore.collection(RECIPE_COLLECTION).document()
} else {
Log.d(TAG, recipeId)
// Return doc
// Return existing document ref
firestore.collection(RECIPE_COLLECTION).document("$recipeId")
}
}
Expand All @@ -56,7 +55,6 @@ class RecipeRepository @Inject constructor(
* @param recipe Recipe object to insert into collection
*/
fun insertRecipe(recipe: Recipe) {
Log.d(TAG, "$recipe")
recipe.recipeId?.let { id ->
firestore.collection(RECIPE_COLLECTION).document(id)
.set(recipe)
Expand Down
12 changes: 11 additions & 1 deletion app/src/test/java/com/android/pocketalchemy/PaUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.android.pocketalchemy

import com.android.pocketalchemy.model.Recipe
import com.android.pocketalchemy.model.getIconDescRes
import com.android.pocketalchemy.model.getIconRes
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand All @@ -10,6 +13,13 @@ class PaUnitTest {

@Test
fun testRecipeDefaultResources() {
val recipe = Recipe()
var recipe = Recipe()

assertEquals(recipe.getIconRes(), R.drawable.default_recipe_icon)
assertEquals(recipe.getIconDescRes(), R.string.default_recipe_icon_description)

recipe = Recipe(iconRes = 0, iconDescRes = 0)
assertEquals(recipe.getIconRes(), 0)
assertEquals(recipe.getIconDescRes(), 0)
}
}

0 comments on commit 4e63f29

Please sign in to comment.