Skip to content

Commit

Permalink
Merge pull request #143 from Shreyansh1563/master
Browse files Browse the repository at this point in the history
Fixed Error: Poduct to Product #134
  • Loading branch information
yuvrajsinghgmx authored Oct 24, 2024
2 parents bc1609b + 1b043cb commit c4a4cf4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import android.content.Context
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import com.google.gson.Gson
import com.yuvrajsinghgmx.shopsmart.screens.Product
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import java.io.Serializable

data class Poduct(val name: String, val amount: Int, val imageUrl: String? = null, val dateAdded: Serializable = System.currentTimeMillis())
data class Product(val name: String, val amount: Int, val imageUrl: String? = null, val dateAdded: Long = System.currentTimeMillis())
object ShoppingList{
val ITEMS_KEY = stringPreferencesKey("items")
}

suspend fun saveItems(context: Context, items: List<Poduct>) {
suspend fun saveItems(context: Context, items: List<com.yuvrajsinghgmx.shopsmart.datastore.Product>) {
context.dataStore.edit { preferences ->
val jsonString = Gson().toJson(items)
preferences[ShoppingList.ITEMS_KEY] = jsonString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
Expand Down Expand Up @@ -85,17 +84,15 @@ import androidx.navigation.NavController
import coil.compose.AsyncImage
import com.google.gson.Gson
import com.yuvrajsinghgmx.shopsmart.R
import com.yuvrajsinghgmx.shopsmart.datastore.Poduct
import com.yuvrajsinghgmx.shopsmart.datastore.Product
import com.yuvrajsinghgmx.shopsmart.datastore.saveItems
import com.yuvrajsinghgmx.shopsmart.utils.SharedPrefsHelper
import com.yuvrajsinghgmx.shopsmart.viewmodel.ShoppingListViewModel
import kotlinx.coroutines.launch

data class Product(val name: String, val amount: Int, val imageUrl: String? = null, val dateAdded: Long = System.currentTimeMillis())

private fun saveOrdersToSharedPreferences(context: Context, items: List<Product>) {
try {
val orders = items.map { Poduct(it.name, it.amount, it.imageUrl) }
val orders = items.map { Product(it.name, it.amount, it.imageUrl) }
if (orders.isNotEmpty()) {
SharedPrefsHelper.saveOrders(context, orders)
Log.d("HomeScreen", "Orders saved: ${orders.size}")
Expand Down Expand Up @@ -343,8 +340,8 @@ fun ListScreen(viewModel: ShoppingListViewModel = hiltViewModel(), navController
modifier = Modifier.padding(8.dp, 3.dp, 0.dp, 3.dp)
)
}
items(products) { product ->
val isChecked = product in selectedItems
items(products.size) { index ->
val isChecked = products[index] in selectedItems
Card(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -359,10 +356,10 @@ fun ListScreen(viewModel: ShoppingListViewModel = hiltViewModel(), navController
.fillMaxSize()
.padding(10.dp)
) {
if (product.imageUrl != null) {
if (products[index].imageUrl != null) {
AsyncImage(
model = product.imageUrl,
contentDescription = product.name,
model = products[index].imageUrl,
contentDescription = products[index].name,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(70.dp, 70.dp)
Expand All @@ -378,15 +375,15 @@ fun ListScreen(viewModel: ShoppingListViewModel = hiltViewModel(), navController

Column(modifier = Modifier.weight(1f)) {
Text(
text = product.name,
text = products[index].name,
style = TextStyle(
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
)
)
Spacer(modifier = Modifier.height(4.dp))
Text(
text = "${product.amount}",
text = "${products[index].amount}",
style = TextStyle(fontSize = 16.sp, color = Color.Gray)
)
}
Expand All @@ -395,9 +392,9 @@ fun ListScreen(viewModel: ShoppingListViewModel = hiltViewModel(), navController
checked = isChecked,
onCheckedChange = { checked ->
if (checked) {
selectedItems.add(product)
selectedItems.add(products[index])
} else {
selectedItems.remove(product)
selectedItems.remove(products[index])
}
showDeleteButton = selectedItems.isNotEmpty()
selectAll = selectedItems.size == items.value.size
Expand Down Expand Up @@ -532,7 +529,7 @@ fun ListScreen(viewModel: ShoppingListViewModel = hiltViewModel(), navController
)
val updatedItems = items.value.toMutableList().also { it.add(newProduct) }
viewModel.updateItems(updatedItems)
saveItems(context, updatedItems.map { Poduct(it.name, it.amount, it.imageUrl, it.dateAdded) })
saveItems(context, updatedItems.map { Product(it.name, it.amount, it.imageUrl, it.dateAdded) })
newItem = ""
newAmount = ""
isLoading = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
Expand All @@ -21,6 +20,7 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yuvrajsinghgmx.shopsmart.datastore.Product
import com.yuvrajsinghgmx.shopsmart.utils.SharedPrefsHelper
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -129,8 +129,8 @@ fun MyOrders(navController: NavController, selectedItemsJson: String) {
.padding(horizontal = 16.dp)
.padding(bottom = 100.dp)
) {
items(orders) { order ->
OrderItem(order)
items(orders.size) { index ->
OrderItem(orders[index])
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package com.yuvrajsinghgmx.shopsmart.utils

import android.content.Context
import android.net.Uri
import androidx.webkit.internal.ApiFeature.T
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yuvrajsinghgmx.shopsmart.datastore.Poduct
import com.yuvrajsinghgmx.shopsmart.screens.Product
import com.yuvrajsinghgmx.shopsmart.datastore.Product

object SharedPrefsHelper {
private const val PREFS_NAME = "ShopSmartPrefs"
Expand Down Expand Up @@ -42,8 +42,7 @@ object SharedPrefsHelper {
return if (uriString != null) Uri.parse(uriString) else null
}

// New methods for handling orders
fun saveOrders(context: Context, orders: List<Poduct>) {
fun saveOrders(context: Context, orders: List<Product>) {
val gson = Gson()
val json = gson.toJson(orders)
getPrefs(context).edit().putString(KEY_ORDERS, json).apply()
Expand All @@ -53,7 +52,7 @@ object SharedPrefsHelper {
val gson = Gson()
val json = getPrefs(context).getString(KEY_ORDERS, null)
return if (json != null) {
val type = object : TypeToken<List<Product>>() {}.type
val type = object : TypeToken<Product>() {}.type
gson.fromJson(json, type)
} else {
emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.yuvrajsinghgmx.shopsmart.viewmodel
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yuvrajsinghgmx.shopsmart.datastore.Product
import com.yuvrajsinghgmx.shopsmart.datastore.getItems
import com.yuvrajsinghgmx.shopsmart.repository.ImageRepo
import com.yuvrajsinghgmx.shopsmart.screens.Product
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down

0 comments on commit c4a4cf4

Please sign in to comment.