Skip to content

Commit

Permalink
add ErrOnLoginScreen and empty recipe list placeholder surface
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Weaver authored and Cody Weaver committed Jan 5, 2024
1 parent bfdb2c2 commit 2ba9ea1
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ android {
isMinifyEnabled = false
isDebuggable = true
isDefault = true
buildConfigField("Boolean", "DEBUG", "false")
buildConfigField("Boolean", "DEBUG", "true")
}

release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.lifecycle.Lifecycle
import com.android.pocketalchemy.firebase.AuthRepository
import com.android.pocketalchemy.repository.AuthRepository
import com.android.pocketalchemy.ui.theme.PocketAlchemyTheme
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.android.pocketalchemy
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import com.android.pocketalchemy.firebase.AuthRepository
import com.android.pocketalchemy.repository.AuthRepository
import com.android.pocketalchemy.ui.theme.PocketAlchemyTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
Expand Down
22 changes: 19 additions & 3 deletions app/src/main/java/com/android/pocketalchemy/PaNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.android.pocketalchemy.editrecipe.EditRecipeScreen
import com.android.pocketalchemy.editrecipe.EditRecipeViewModel
import com.android.pocketalchemy.firebase.AuthRepository
import com.android.pocketalchemy.login.ErrOnLoginScreen
import com.android.pocketalchemy.login.LoginScreen
import com.android.pocketalchemy.recipelist.RecipeListScreen
import com.android.pocketalchemy.repository.AuthRepository

/**
* Launches navigation host.
Expand All @@ -29,14 +31,19 @@ fun PaNavHost(
LoginScreen {
authRepository.signInAnonymousUser(
onSuccess = {
navController.navigate("recipeListScreen")
navController.navigateAndPopAll("recipeListScreen")
},
onFailure = {
// TODO: Navigate to error on login screen
navController.navigateAndPopAll("errOnLoginScreen")
}
)
}
}
composable("errOnLoginScreen") {
ErrOnLoginScreen {
navController.navigateAndPopAll("loginScreen")
}
}
composable("recipeListScreen") {
RecipeListScreen(
navController,
Expand Down Expand Up @@ -66,4 +73,13 @@ fun PaNavHost(
)
}
}
}

/**
* Extension function to navigate to dest and clear back stack
*/
fun NavController.navigateAndPopAll(route: String) {
this.navigate(route) {
popUpTo(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import com.android.pocketalchemy.firebase.AuthRepository
import androidx.lifecycle.viewModelScope
import com.android.pocketalchemy.repository.AuthRepository
import com.android.pocketalchemy.model.Recipe
import com.android.pocketalchemy.model.RecipeRepository
import com.android.pocketalchemy.repository.RecipeRepository
import com.google.firebase.firestore.toObject
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
Expand Down Expand Up @@ -97,9 +99,10 @@ class EditRecipeViewModel @Inject constructor(
*/
fun saveRecipe() {
// TODO: Check no required fields are empty!!!
recipeRepository.insertRecipe(this.recipeState.value)

this.clearRecipeId()
viewModelScope.launch {
recipeRepository.insertRecipe(recipeState.value)
clearRecipeId()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.android.pocketalchemy.login

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.android.pocketalchemy.R

@Composable
fun ErrOnLoginScreen(
onNavigateToLoginScreen: () -> Unit
) {
Surface(
color = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
modifier = Modifier.fillMaxSize(1f)
) {

Box (
modifier = Modifier.padding(16.dp)
) {
Column(
modifier = Modifier.align(Alignment.Center)
.padding(16.dp)
) {
Text(
text = stringResource(id = R.string.app_name),
modifier = Modifier.align(Alignment.CenterHorizontally),
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.ExtraBold,
textAlign = TextAlign.Center,
)
Text(
text = stringResource(R.string.err_on_login_msg),
modifier = Modifier.align(Alignment.CenterHorizontally)
.fillMaxWidth(.75f),
textAlign = TextAlign.Center
)
TextButton(
onClick = onNavigateToLoginScreen,
modifier = Modifier.align(Alignment.CenterHorizontally)
) {
Text(
text = stringResource(R.string.back_to_login_label)
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.android.pocketalchemy.R
import com.android.pocketalchemy.model.Recipe

/**
Expand All @@ -26,16 +34,14 @@ fun RecipeList(
) {
val recipes: State<List<Recipe>>
= recipeListViewModel.recipes.collectAsState(initial = emptyList())

LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
contentPadding = PaddingValues(8.dp)
) {
if (recipes.value.isEmpty()) {
// TODO: Show help card when list is empty
} else {

if (recipes.value.isNotEmpty()) {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(8.dp),
contentPadding = PaddingValues(8.dp)
) {
items(recipes.value) {
Box(
modifier = Modifier.padding(vertical = 8.dp)
Expand All @@ -44,5 +50,24 @@ fun RecipeList(
}
}
}
} else {
Surface(
color = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
onClick = { onNavigateToEditRecipe(null) }, // navigates to new recipe
modifier = Modifier.fillMaxSize(1f)
) {
Box {
Text(
text = stringResource(id = R.string.empty_recipe_list_prompt),
modifier = Modifier.align(Alignment.Center),
style = MaterialTheme.typography.headlineLarge,
fontWeight = FontWeight.Bold,
textAlign = TextAlign.Center,
)
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun RecipeListScreen(
PaTopAppBar(titleId = R.string.app_name)
},
floatingActionButton = {
NewRecipeFAB() { onNavigateToEditRecipe(null) }
NewRecipeFAB { onNavigateToEditRecipe(null) }
},
bottomBar = {
PaNavBar(navController, isRecipeListSelected = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.android.pocketalchemy.recipelist

import androidx.lifecycle.ViewModel
import com.android.pocketalchemy.model.Recipe
import com.android.pocketalchemy.model.RecipeRepository
import com.android.pocketalchemy.repository.RecipeRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
Expand All @@ -21,6 +21,7 @@ class RecipeListViewModel @Inject constructor(
*/
val recipes: Flow<List<Recipe>>
get() = recipeRepository.getUserRecipeList()

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.android.pocketalchemy.firebase
package com.android.pocketalchemy.repository

import android.util.Log
import com.google.firebase.auth.FirebaseAuth
Expand Down Expand Up @@ -49,10 +49,6 @@ class AuthRepository @Inject constructor(
onFailure()
}
}
.addOnFailureListener {
// DEBUG USE
throw it
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.android.pocketalchemy.model
package com.android.pocketalchemy.repository

import android.util.Log
import com.android.pocketalchemy.firebase.AuthRepository
import com.android.pocketalchemy.firebase.FirestoreCollections.RECIPE_COLLECTION
import com.android.pocketalchemy.model.Recipe
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.dataObjects
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

private const val TAG = "RecipeRepository"

/**
* Repository for accessing recipe collection.
* @param firestore firestore instance
Expand All @@ -29,8 +27,8 @@ class RecipeRepository @Inject constructor(
fun getUserRecipeList(): Flow<List<Recipe>> {
val user = authRepository.getUser()
return firestore.collection(RECIPE_COLLECTION)
.whereEqualTo(Recipe.USER_ID_FIELD, user.uid)
.dataObjects<Recipe>()
.whereEqualTo(Recipe.USER_ID_FIELD, user.uid)
.dataObjects<Recipe>()

}

Expand Down Expand Up @@ -66,4 +64,8 @@ class RecipeRepository @Inject constructor(
}
}
}

companion object {
private const val TAG = "RecipeRepository"
}
}
7 changes: 6 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
<string name="edit_recipe_title">Edit Recipe</string>

// Login screen labels.
<string name="guest_login_label">continue as guest</string>
<string name="guest_login_label">continue as guest</string>\
<string name="err_on_login_msg">Sorry, we could not authenticate your profile right now …</string>
<string name="back_to_login_label">go back to login screen</string>

// Recipe list screen labels.
<string name="empty_recipe_list_prompt">Create a new recipe to get started.</string>

// Edit recipe screen labels.
<string name="recipe_title_label">Title</string>
Expand Down

0 comments on commit 2ba9ea1

Please sign in to comment.