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 @@ -8,10 +8,10 @@ import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.projectfigma.Adapters.BestSellerAdapter
import com.example.projectfigma.DAO.BestSellerDao
import com.example.projectfigma.DAO.DishesDao
import com.example.projectfigma.DAO.UserDao
import com.example.projectfigma.DataBase.DataBase
import com.example.projectfigma.Entites.BestSeller
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.User
import com.example.projectfigma.Fragments.*
import com.example.projectfigma.R
Expand All @@ -21,7 +21,7 @@ class HomeActivity : AppCompatActivity(),
HeaderButtonsFragment.Listener {

private lateinit var adapter: BestSellerAdapter
private lateinit var dao: BestSellerDao
private lateinit var dao: DishesDao
private lateinit var userDao: UserDao
private lateinit var drawer: DrawerLayout

Expand Down Expand Up @@ -49,7 +49,7 @@ class HomeActivity : AppCompatActivity(),
LinearLayoutManager.HORIZONTAL,
false
)
adapter = BestSellerAdapter(emptyList()) { item: BestSeller ->
adapter = BestSellerAdapter(emptyList()) { item: Dishes ->
Toast.makeText(
this@HomeActivity,
"Clicked: ${item.price}",
Expand All @@ -58,7 +58,7 @@ class HomeActivity : AppCompatActivity(),
}.also { this@HomeActivity.adapter = it }
}

dao.getAllV().observe(this) { list ->
dao.getBestSellers().observe(this) { list ->
adapter.updateList(list)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.projectfigma.Entites.BestSeller
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.R

class BestSellerAdapter(
private var items: List<BestSeller>,
private val onClick: (BestSeller) -> Unit
private var items: List<Dishes>,
private val onClick: (Dishes) -> Unit
) : RecyclerView.Adapter<BestSellerAdapter.VH>() {

inner class VH(view: View) : RecyclerView.ViewHolder(view) {
private val ivFood: ImageView = view.findViewById(R.id.ivFood)
private val tvPrice: TextView = view.findViewById(R.id.tvPrice)

fun bind(item: BestSeller) {
fun bind(item: Dishes) {
tvPrice.text = String.format("$%.2f", item.price)
Glide.with(ivFood.context)
.load(item.imageUri)
Expand All @@ -40,7 +40,7 @@ class BestSellerAdapter(
holder.bind(items[position])
}

fun updateList(newItems: List<BestSeller>) {
fun updateList(newItems: List<Dishes>) {
items = newItems
notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.example.projectfigma.Entites.BestSeller
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.R

class PromoAdapter(private var items: List<BestSeller>) :
class PromoAdapter(private var items: List<Dishes>) :
RecyclerView.Adapter<PromoAdapter.VH>() {

inner class VH(view: View) : RecyclerView.ViewHolder(view) {
private val image: ImageView = view.findViewById(R.id.promo_image)
private val title: TextView = view.findViewById(R.id.title)
private val discount: TextView = view.findViewById(R.id.discount)

fun bind(item: BestSeller) {
fun bind(item: Dishes) {
title.text = "Experience our delicious new dish"
discount.text = "${30}% OFF"
}
Expand All @@ -37,7 +36,7 @@ class PromoAdapter(private var items: List<BestSeller>) :
override fun getItemCount(): Int = items.size

/** Вызываем из Activity/Fragment при получении новых данных */
fun updateList(newItems: List<BestSeller>) {
fun updateList(newItems: List<Dishes>) {
items = newItems
notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.projectfigma.Adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.R

class RecommendAdapter (
private val items: List<Dishes>
) : RecyclerView.Adapter<RecommendAdapter.VH>() {

inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val ivFood: ImageView = itemView.findViewById(R.id.ivFood)
private val tvRating: TextView = itemView.findViewById(R.id.tvRating)
private val tvPrice: TextView = itemView.findViewById(R.id.tvPrice)

fun bind(food: Dishes) {
Glide.with(itemView).load(food.imageUri).into(ivFood)
tvRating.text = String.format("%.1f", food.rating)
tvPrice.text = "$${String.format("%.2f", food.price)}"
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_food, parent, false)
return VH(view)
}

override fun onBindViewHolder(holder: VH, position: Int) {
holder.bind(items[position])
}

override fun getItemCount(): Int = items.size
}
25 changes: 0 additions & 25 deletions app/src/main/java/com/example/projectfigma/DAO/BestSellerDao.kt

This file was deleted.

29 changes: 29 additions & 0 deletions app/src/main/java/com/example/projectfigma/DAO/DishesDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.projectfigma.DAO

import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.projectfigma.Entites.Dishes

@Dao
interface DishesDao {
@Query("SELECT * FROM dishes")
fun getAll(): List<Dishes>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(item: Dishes)

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

@Query("SELECT * FROM dishes")
fun getAllV(): LiveData<List<Dishes>>

@Query("SELECT * FROM dishes b WHERE b.isBestSeller = 1")
fun getBestSellers(): LiveData<List<Dishes>>

@Query("SELECT * FROM dishes b WHERE b.isRecommend = 1")
fun getRecommend(): LiveData<List<Dishes>>
}
38 changes: 25 additions & 13 deletions app/src/main/java/com/example/projectfigma/DataBase/DataBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.projectfigma.DAO.BestSellerDao
import com.example.projectfigma.DAO.DishesDao
import com.example.projectfigma.DAO.SettingsDao
import com.example.projectfigma.DAO.SessionDao
import com.example.projectfigma.DAO.UserDao
import com.example.projectfigma.Entites.AppSettings
import com.example.projectfigma.Entites.BestSeller
import com.example.projectfigma.Entites.Dishes
import com.example.projectfigma.Entites.Session
import com.example.projectfigma.Entites.User
import com.example.projectfigma.R
Expand All @@ -22,14 +22,14 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch

@Database(
entities = [User::class, BestSeller::class, Session::class, AppSettings::class],
version = 3
entities = [User::class, Dishes::class, Session::class, AppSettings::class],
version = 4
)
@TypeConverters(Converters::class)
abstract class DataBase : RoomDatabase() {

abstract fun getUserDao(): UserDao
abstract fun getBestSellerDao(): BestSellerDao
abstract fun getBestSellerDao(): DishesDao
abstract fun getSessionDao(): SessionDao
abstract fun getSettingsDao(): SettingsDao

Expand All @@ -40,21 +40,33 @@ abstract class DataBase : RoomDatabase() {
private val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

private fun prepopulateBestSellers(packageName: String) = listOf(
BestSeller(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_1}",
price = 103.0
price = 103.0,
isBestSeller = true,
isRecommend = false,
rating = 3.0
),
BestSeller(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_2}",
price = 50.0
price = 50.0,
isBestSeller = true,
isRecommend = false,
rating = 4.0
),
BestSeller(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_3}",
price = 12.99
price = 12.99,
isBestSeller = true,
isRecommend = false,
rating = 4.0
),
BestSeller(
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.best_seller_card_4}",
price = 8.20
price = 8.20,
isBestSeller = true,
isRecommend = false,
rating = 4.0
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.example.projectfigma.Entites
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "best_seller")
data class BestSeller(
@Entity(tableName = "dishes")
data class Dishes(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
val imageUri: String,
val price: Double
val price: Double,
val rating: Double,
val isRecommend: Boolean,
val isBestSeller: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BannerFood : Fragment() {

// 5) А обновление списка (LiveData) можно тоже повесить здесь:
val dao = DataBase.getDb(requireContext()).getBestSellerDao()
dao.getAllV().observe(viewLifecycleOwner) { list ->
dao.getBestSellers().observe(viewLifecycleOwner) { list ->
promoAdapter.updateList(list)
}
}
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/java/com/example/projectfigma/Fragments/Recommend.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.projectfigma.Fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.projectfigma.Adapters.RecommendAdapter
import com.example.projectfigma.DataBase.DataBase
import com.example.projectfigma.R
import kotlinx.coroutines.launch

//class Recommend : Fragment(R.layout.fragment_recommend) {
//
// private lateinit var adapter: RecommendAdapter
// private val db by lazy { DataBase.getInstance(requireContext()) }
// // допустим, в UserDao есть suspend fun getCurrent(): User
// // в FoodDao — suspend fun getRecommendedForUser(userId: Int): List<Food>
//
// override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// super.onViewCreated(view, savedInstanceState)
// val rv = view.findViewById<RecyclerView>(R.id.rvRecommend)
// rv.layoutManager = GridLayoutManager(requireContext(), 2)
//
// // загрузка из БД через корутины
// viewLifecycleOwner.lifecycleScope.launch {
// val user = db.
// val recommendList = db.foodDao().getRecommendedForUser(user.id)
// adapter = RecommendAdapter(recommendList)
// rv.adapter = adapter
// }
// }
//}
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/bg_rating.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#80000000"/>
<corners android:radius="8dp"/>
</shape>
Binary file added app/src/main/res/drawable/ic_star_solid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions app/src/main/res/layout/fragment_recommend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- res/layout/fragment_recommend.xml -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:id="@+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recommend"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="#3E2723"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvRecommend"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:layout_weight="1"/>
</LinearLayout>
Loading