Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ class FavoritesActivity : AppCompatActivity() {
setContentView(binding.root)
StatusBar.hideStatusBar(window)

val dateBase = DataBase.getDb(this)

val rv = findViewById<RecyclerView>(R.id.rvFoods).apply {
layoutManager = GridLayoutManager(context, 2)
}

supportFragmentManager.beginTransaction()
.replace(R.id.buttonPanel, BottomPanelFragment())
.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class RegActivity() : AppCompatActivity(){
binding.passwordEditText.text.toString(),
email,
binding.mobileNumberEdit.text.toString(),
selectedDate ?: Date()
selectedDate ?: Date(),
favoriteDishesId = emptyList()
)

Thread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.projectfigma.DataBase.DataBase
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.User
import com.example.projectfigma.R
import kotlin.coroutines.coroutineContext

class FavoriteFoodAdapter(
private val items: MutableList<Dishes>
private val items: MutableList<Dishes>,
private val onFavoriteClick: (dish: Dishes) -> Unit
) : RecyclerView.Adapter<FavoriteFoodAdapter.VH>() {

inner class VH(view: View) : RecyclerView.ViewHolder(view) {
Expand All @@ -34,16 +38,14 @@ class FavoriteFoodAdapter(
holder.tvTitle.text = dish.name
holder.tvDesc.text = dish.description

// загрузка изображения блюда
Glide.with(holder.itemView)
.load(dish.imageUri)
.into(holder.imgFood)

// иконка категории
holder.imgTag.setImageResource(dish.category.iconRes)

// иконка «избранное»— всегда заполнена
holder.imgFavorite.setImageResource(R.drawable.ic_heart_border)
holder.imgFavorite.setOnClickListener { onFavoriteClick(dish) }
}

override fun getItemCount(): Int = items.size
Expand All @@ -54,4 +56,5 @@ class FavoriteFoodAdapter(
items.addAll(newItems)
notifyDataSetChanged()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.projectfigma.Converters

import androidx.room.TypeConverter

class ConvertersList {
@TypeConverter
fun fromIntList(value: List<Int>?): String = value?.joinToString(",") ?: ""
@TypeConverter
fun toIntList(value: String): List<Int> =
if (value.isEmpty()) emptyList() else value.split(",").map { it.toInt() }
}
7 changes: 7 additions & 0 deletions app/src/main/java/com/example/projectfigma/DAO/DishesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Enums.DishCategory

@Dao
interface DishesDao {
Expand All @@ -31,6 +32,12 @@ interface DishesDao {
@Query("SELECT * FROM dishes b WHERE b.isRecommend = 1")
fun getRecommend(): LiveData<List<Dishes>>

@Query("SELECT * FROM dishes d WHERE d.id = :id")
fun getDishById(id : Int) : Dishes

@Query("SELECT * FROM dishes WHERE id IN (:ids)")
fun getDishesByIds(ids: List<Int>): List<Dishes>

@Update
suspend fun update(item: Dishes)
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/example/projectfigma/DAO/UserDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package com.example.projectfigma.DAO

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.User

@Dao
interface UserDao {

@Insert
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertUser(item: User)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(items: List<User>)

@Update
fun updateUser(user: User)

Expand Down
44 changes: 39 additions & 5 deletions app/src/main/java/com/example/projectfigma/DataBase/DataBase.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.example.projectfigma.DataBase

import android.content.Context
import android.text.style.TtsSpan
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.projectfigma.Converters.ConvertToDishesCategory
import com.example.projectfigma.Converters.ConvertersList
import com.example.projectfigma.DAO.DishesDao
import com.example.projectfigma.DAO.SettingsDao
import com.example.projectfigma.DAO.SessionDao
Expand All @@ -22,12 +24,17 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import java.util.Date

@Database(
entities = [User::class, Dishes::class, Session::class, AppSettings::class],
version = 5
version = 6
)
@TypeConverters(
ConvertersToDateTime::class,
ConvertToDishesCategory::class,
ConvertersList::class
)
@TypeConverters(ConvertersToDateTime::class, ConvertToDishesCategory::class)
abstract class DataBase : RoomDatabase() {

abstract fun getUserDao(): UserDao
Expand All @@ -41,6 +48,17 @@ abstract class DataBase : RoomDatabase() {

private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

private fun prepopulateUsers(): List<User> = listOf(
User(
name = "TestUser",
gmail = "test@yandex.ru",
password = "1111",
mobileNumber = "818412481",
dateOfBirth = Date(),
favoriteDishesId = listOf(1,2,3)
)
)

private fun prepopulateBestSellers(packageName: String) = listOf(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_1}",
Expand Down Expand Up @@ -92,6 +110,16 @@ abstract class DataBase : RoomDatabase() {
description = "Бургер",
category = DishCategory.MEAL
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.roll}",
price = 25.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0,
name = "Роллы",
description = "Роллы",
category = DishCategory.VEGAN
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.roll}",
price = 25.0,
Expand Down Expand Up @@ -121,10 +149,16 @@ abstract class DataBase : RoomDatabase() {
applicationScope.launch {
val database = getDb(appContext)

val bestSellerDao = database.getDishesDao()
if (bestSellerDao.getAll().isEmpty()) {
bestSellerDao.insertAll(prepopulateBestSellers(pkg))
val dishesDao = database.getDishesDao()
if (dishesDao.getAll().isEmpty()) {
dishesDao.insertAll(prepopulateBestSellers(pkg))
}

val userDao = database.getUserDao()
if (userDao.getAllUsers().isEmpty()) {
userDao.insertAll(prepopulateUsers())
}

val settingsDao = database.getSettingsDao()
settingsDao.upsert(AppSettings(id = 0, isFirstRun = true))

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/example/projectfigma/Entites/Dishes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class Dishes(
val rating: Double,
val isRecommend: Boolean,
val isBestSeller: Boolean,
val name : String,
val description : String,
val name: String,
val description: String,
val category: DishCategory
)
3 changes: 2 additions & 1 deletion app/src/main/java/com/example/projectfigma/Entites/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ data class User(
@ColumnInfo(name = "mobileNumber")
var mobileNumber : String,
@ColumnInfo(name = "dateOfBirth")
var dateOfBirth : Date
var dateOfBirth : Date,
var favoriteDishesId: List<Int>
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BottomPanelFragment : Fragment() {
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.activity_bottom_panel, container, false)
): View? = inflater.inflate(R.layout.fragment_bottom_panel, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.example.projectfigma.Fragments

import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.projectfigma.Adapters.FavoriteFoodAdapter
import com.example.projectfigma.DataBase.DataBase
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.User
import com.example.projectfigma.R
import com.example.projectfigma.databinding.FragmentBestSellerBinding
import com.example.projectfigma.databinding.FragmentFavoriteListBinding
Expand All @@ -24,6 +28,8 @@
private val binding get() = _binding!!

private lateinit var adapter: FavoriteFoodAdapter
private var currentUser: User? = null
private lateinit var dataBase: DataBase

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -34,23 +40,57 @@
return binding.root
}

override fun onAttach(context: Context) {
super.onAttach(context)
dataBase = DataBase.getDb(context)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

adapter = FavoriteFoodAdapter(mutableListOf())
// 1) Загрузить текущего пользователя из БД
lifecycleScope.launch(Dispatchers.IO) {
currentUser = dataBase
.getUserDao().getUserByEmail(
dataBase.getSessionDao().getSession()?.userEmail.toString()
)
withContext(Dispatchers.Main) {
setupRecycler()
loadFavorites()
}
}
}
private fun setupRecycler() {
adapter = FavoriteFoodAdapter(mutableListOf()) { dish ->
currentUser?.let { user ->
// 2) Удаляем ID блюда из списка
val newList = user.favoriteDishesId.toMutableList()
newList.remove(dish.id.toInt())
user.favoriteDishesId = newList
// 3) Сохраняем пользователя в БД и обновляем UI
lifecycleScope.launch(Dispatchers.IO) {
DataBase.getDb(requireContext()).getUserDao().updateUser(user)
val all = DataBase.getDb(requireContext()).getDishesDao().getAll()
val fav = all.filter { it.id.toInt() in newList }
withContext(Dispatchers.Main) { adapter.updateList(fav) }
}
}
}
binding.rvFoods.layoutManager = GridLayoutManager(requireContext(), 2)
binding.rvFoods.adapter = adapter
}

lifecycleScope.launch {
val dishesList = withContext(Dispatchers.IO) {
DataBase.getDb(requireContext())
.getDishesDao()
.getAll()
private fun loadFavorites() {
currentUser?.let { user ->
lifecycleScope.launch(Dispatchers.IO) {
val all = dataBase.getDishesDao().getDishesByIds(user.favoriteDishesId)
println(" FFFFFFFFFFFFFFFF " + all.toString())
println(" FFFFFFFFFFFFFFFF " + user)
withContext(Dispatchers.Main) { adapter.updateList(all) }
}
adapter.updateList(dishesList)
}
}


override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_favorites.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
</androidx.constraintlayout.widget.ConstraintLayout>

<include
layout="@layout/activity_bottom_panel"
layout="@layout/fragment_bottom_panel"
android:layout_width="wrap_content"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_forget_password.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
</LinearLayout>

<include
layout="@layout/activity_bottom_panel"
layout="@layout/fragment_bottom_panel"
android:layout_width="wrap_content"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@

<!-- Нижняя панель -->
<include
layout="@layout/activity_bottom_panel"
layout="@layout/fragment_bottom_panel"
android:layout_width="wrap_content"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_log.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
</LinearLayout>

<include
layout="@layout/activity_bottom_panel"
layout="@layout/fragment_bottom_panel"
android:layout_width="wrap_content"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_reg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
</LinearLayout>

<include
layout="@layout/activity_bottom_panel"
layout="@layout/fragment_bottom_panel"
android:layout_width="wrap_content"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down