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 @@ -35,7 +35,7 @@ class HomeActivity : AppCompatActivity(),
drawer = findViewById(R.id.drawer_layout)

val db = DataBase.getDb(this)
dao = db.getBestSellerDao()
dao = db.getDishesDao()
userDao = db.getUserDao()

val email = intent.getStringExtra("user_email")
Expand All @@ -58,7 +58,7 @@ class HomeActivity : AppCompatActivity(),
}.also { this@HomeActivity.adapter = it }
}

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

Expand Down
3 changes: 3 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 @@ -24,6 +24,9 @@ interface DishesDao {
@Query("SELECT * FROM dishes b WHERE b.isBestSeller = 1")
fun getBestSellers(): LiveData<List<Dishes>>

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

@Query("SELECT * FROM dishes b WHERE b.isRecommend = 1")
fun getRecommend(): LiveData<List<Dishes>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.example.projectfigma.Entites.Session
@Dao
interface SessionDao {
@Query("SELECT * FROM session WHERE id = 0")
suspend fun getSession(): Session?
fun getSession(): Session?

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun upsert(session: Session)
Expand Down
18 changes: 16 additions & 2 deletions app/src/main/java/com/example/projectfigma/DataBase/DataBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlinx.coroutines.launch
abstract class DataBase : RoomDatabase() {

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

Expand Down Expand Up @@ -67,6 +67,20 @@ abstract class DataBase : RoomDatabase() {
isBestSeller = true,
isRecommend = false,
rating = 4.0
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.burger}",
price = 10.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0
),
Dishes(
imageUri = "android.resource://$packageName/${R.drawable.roll}",
price = 25.0,
isBestSeller = false,
isRecommend = true,
rating = 5.0
)
)

Expand All @@ -87,7 +101,7 @@ abstract class DataBase : RoomDatabase() {
applicationScope.launch {
val database = getDb(appContext)

val bestSellerDao = database.getBestSellerDao()
val bestSellerDao = database.getDishesDao()
if (bestSellerDao.getAll().isEmpty()) {
bestSellerDao.insertAll(prepopulateBestSellers(pkg))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BannerFood : Fragment() {
}, 4000)

// 5) А обновление списка (LiveData) можно тоже повесить здесь:
val dao = DataBase.getDb(requireContext()).getBestSellerDao()
val dao = DataBase.getDb(requireContext()).getDishesDao()
dao.getBestSellers().observe(viewLifecycleOwner) { list ->
promoAdapter.updateList(list)
}
Expand Down
51 changes: 30 additions & 21 deletions app/src/main/java/com/example/projectfigma/Fragments/Recommend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,35 @@ 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.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

//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
// }
// }
//}
class Recommend : Fragment(R.layout.fragment_recommend) {
private val db by lazy { DataBase.getDb(requireContext()) }

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 {
// 1. Получаем пользователя в IO
val sessionEmail = db.getSessionDao().getSession()?.userEmail.orEmpty()
val user = withContext(Dispatchers.IO) {
db.getUserDao().getUserByEmail(sessionEmail)
}

// 2. Теперь наблюдаем LiveData рекомендаций
// и каждый раз создаём новый адаптер с реальным списком
withContext(Dispatchers.Main) {
db.getDishesDao()
.getRecommend()
.observe(viewLifecycleOwner) { list ->
val adapter = RecommendAdapter(list)
rv.adapter = adapter
}
}
}
}
}
Binary file added app/src/main/res/drawable/burger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/roll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion app/src/main/res/layout/activity_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
android:id="@+id/food_category_frame"
android:layout_width="wrap_content"
android:layout_height="77dp"
android:paddingTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
Expand Down Expand Up @@ -153,6 +152,7 @@
</FrameLayout>

<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -168,6 +168,22 @@
tools:layout="@layout/fragment_banner_food" />

</FrameLayout>

<FrameLayout
android:layout_width="313dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frameLayout2">

<fragment
android:id="@+id/recommend"
android:name="com.example.projectfigma.Fragments.Recommend"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_recommend" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

<!-- Нижняя панель -->
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/fragment_recommend.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
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">
android:layout_height="match_parent">

<TextView
android:id="@+id/tvHeader"
Expand All @@ -19,6 +18,5 @@
android:id="@+id/rvRecommend"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="12dp"
android:layout_weight="1"/>
</LinearLayout>
6 changes: 2 additions & 4 deletions app/src/main/res/layout/item_food.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="16dp"
app:cardUseCompatPadding="true">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="200dp">
android:layout_height="120dp">

<!-- Фоновое изображение блюда -->
<ImageView
Expand Down Expand Up @@ -38,8 +37,7 @@
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/ic_star_solid"
app:tint="@android:color/white" />
android:src="@drawable/ic_star_solid" />
<TextView
android:id="@+id/tvRating"
android:layout_width="wrap_content"
Expand Down