Skip to content

Commit

Permalink
Final Push of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohanraj123 committed Apr 28, 2024
1 parent 31a5e7c commit 4c88a75
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 93 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ dependencies {
implementation(libs.sharedpreferences)
implementation(libs.lifecycle.compose)
implementation(libs.runtime.livedata)
implementation(libs.swipe.refresh)

// Dagger-hilt
implementation("com.google.dagger:hilt-android:2.48")
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/com/example/cookbook/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.cookbook.presentation.view.navigation.Navigation
import com.example.cookbook.presentation.viewmodel.HomeScreenViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class RandomRecipeRepositoryImpl(
override suspend fun getRandomRecipe(apiKey: String, number: Int):
Result<RandomRecipeResponse> {
return try {
val response = retrofitApi.getRandomRecipe(apiKey, number)
val response = retrofitApi
.getRandomRecipe(apiKey, number)
.awaitResponse()

if (response.isSuccessful) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.cookbook.presentation.view

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
Expand Down Expand Up @@ -39,7 +38,15 @@ fun LogInScreen(
var password by remember { mutableStateOf("") }
var passwordLengthError by remember { mutableStateOf("") }
val preferenceManager = remember { PreferenceManager(context) }
var loggedIn by remember { mutableStateOf( preferenceManager.getBoolean("loggedIn", false))}
var loggedIn by remember {
mutableStateOf(
preferenceManager
.getBoolean(
"loggedIn",
false
)
)
}

DisposableEffect(Unit) {
val observer = Observer<LogInResult> { logInResult ->
Expand Down Expand Up @@ -94,7 +101,7 @@ fun LogInScreen(
value = password,
onValueChange = {
password = it
passwordLengthError = if (password.length < 6) "Password should atleast be of 6 characters" else ""
passwordLengthError = if (password.length < 6) "Password should at least be of 6 characters" else ""
},
placeHolder = "Password",
keyboardType = KeyboardType.Password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.cookbook.ui.theme.ButtonColor


@Composable
fun CustomButton(
onClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package com.example.cookbook.presentation.view.components

import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Divider
import androidx.compose.material3.DrawerState
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.cookbook.presentation.view.recipedetailsscreen.ScrollContent
import com.example.cookbook.utils.PreferenceManager
import kotlinx.coroutines.launch

@Composable
fun SideNavDrawer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomTextField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
Expand All @@ -18,11 +17,8 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.cookbook.R

@Composable
fun CategoriesList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ package com.example.cookbook.presentation.view.homescreen
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Card
import androidx.compose.material3.CardElevation
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -32,8 +26,6 @@ import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.example.cookbook.R

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
Expand All @@ -36,6 +38,9 @@ import com.example.cookbook.presentation.view.recipedetailsscreen.ScrollContent
import com.example.cookbook.presentation.viewmodel.HomeScreenViewModel
import com.example.cookbook.presentation.viewmodel.RecipeDetailScreenViewModel
import com.example.cookbook.ui.theme.ButtonColor
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -45,12 +50,16 @@ fun HomeScreen(
recipeDetailScreenViewModel: RecipeDetailScreenViewModel,
backStackEntry: NavBackStackEntry
) {
val apiKey = stringResource(id = R.string.api_key)

val isLoading by homeScreenViewModel.isLoading.collectAsState(initial = false)
val popularItems by homeScreenViewModel.popularItems.collectAsState(emptyList())

var isDrawerOpen by remember { mutableStateOf(false)}
val pullRefreshState = rememberSwipeRefreshState(isRefreshing = false)
var isInitialFetchedCompleted by rememberSaveable { mutableStateOf(false) }
val scope = rememberCoroutineScope()
val name = backStackEntry.arguments?.getString("name")

var isDrawerOpen by remember { mutableStateOf(false) }
Scaffold(
topBar = {
TopAppBar(
Expand All @@ -74,12 +83,27 @@ fun HomeScreen(
)
},
) { innerPadding ->
SwipeRefresh(
state = pullRefreshState,
onRefresh = {
scope.launch {
homeScreenViewModel
.getRandomRecipe(
apiKey = apiKey,
number = 20
)
}
}
) {

ScrollContent(padding = innerPadding)
Box(modifier = Modifier.fillMaxSize()) {
if (isLoading) {
CircularProgressIndicator(modifier = Modifier
.padding(16.dp)
.align(Alignment.Center))
CircularProgressIndicator(
modifier = Modifier
.padding(16.dp)
.align(Alignment.Center)
)
} else {
Column(
modifier = Modifier
Expand Down Expand Up @@ -113,10 +137,13 @@ fun HomeScreen(
}
}
}
}
}

val apiKey = stringResource(id = R.string.api_key)
LaunchedEffect(key1 = Unit) {
homeScreenViewModel.getRandomRecipe(apiKey = apiKey, number = 20)
if (!isInitialFetchedCompleted) {
LaunchedEffect(Unit) {
homeScreenViewModel.getRandomRecipe(apiKey = apiKey, number = 20)
isInitialFetchedCompleted = true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.cookbook.presentation.view.homescreen

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
Expand Down Expand Up @@ -33,7 +32,6 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import coil.compose.rememberImagePainter
import com.example.cookbook.data.models.randomrecipemodel.Recipe
Expand Down Expand Up @@ -85,38 +83,36 @@ fun DishCard(
Column(
modifier = Modifier.fillMaxSize()
) {
// Background image of dish

Box(
modifier = Modifier
.fillMaxWidth()
.height(200.dp)
.clip(RoundedCornerShape(16.dp))
) {
Image(
painter = rememberImagePainter(recipe.image), // Use Coil or other image loading library
painter = rememberImagePainter(recipe.image),
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop
)
}

recipe.title?.let {
Text(
text = it,
modifier = Modifier
.padding(vertical = 8.dp)
.fillMaxWidth(),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyMedium
)
}
Text(
text = recipe.title,
modifier = Modifier
.padding(vertical = 8.dp)
.fillMaxWidth(),
textAlign = TextAlign.Center,
style = MaterialTheme.typography.bodyMedium
)

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp)
) {
// Display vegetarian icon if recipe is vegetarian
if (recipe.vegetarian == true) {

if (recipe.vegetarian) {
Icon(
imageVector = Icons.Default.CheckCircle,
contentDescription = null,
Expand All @@ -134,7 +130,6 @@ fun DishCard(
Spacer(modifier = Modifier.width(4.dp))
}

// Display time to make
Icon(
imageVector = Icons.Default.DateRange,
contentDescription = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.cookbook.presentation.view.recipedetailsscreen

import android.graphics.drawable.Icon
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
Expand All @@ -17,7 +16,6 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarColors
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
Expand All @@ -35,21 +33,26 @@ fun Header(
recipeDetailsScreenViewModel: RecipeDetailScreenViewModel,
onBackClick: () -> Unit
) {
val selectedRecipe = recipeDetailsScreenViewModel.selectedRecipe.collectAsState().value
val selectedRecipe =
recipeDetailsScreenViewModel
.selectedRecipe
.collectAsState()
.value

Scaffold(
topBar = {
TopAppBar(
navigationIcon = {
IconButton(onClick = { onBackClick() }) {
androidx.compose.material3.Icon(
Icons.Default.ArrowBack,
contentDescription = null
)
}
IconButton(onClick = { onBackClick() }) {
Icon(
Icons.Default.ArrowBack,
contentDescription = null
)
}
},
title = { Text(text = "Details") },
colors = TopAppBarDefaults.topAppBarColors(
colors = TopAppBarDefaults
.topAppBarColors(
containerColor = ButtonColor,
titleContentColor = Color.White
)
Expand All @@ -67,11 +70,9 @@ fun Header(
Name(recipeDetailsScreenViewModel)
ImageViewer(recipeDetailsScreenViewModel)
Informations(recipeDetailsScreenViewModel)
Ingredients(selectedRecipe = selectedRecipe)
Instructions(recipeDetailsScreenViewModel)
}
} else {
// Show loading indicator while fetching data
Text("Loading recipe details...")
}
}
}
Expand Down
Loading

0 comments on commit 4c88a75

Please sign in to comment.