|
1 | 1 | package com.example.projectfigma.Fragments |
2 | 2 |
|
| 3 | +import android.content.Intent |
3 | 4 | import android.os.Bundle |
4 | 5 | import android.os.Handler |
5 | 6 | import android.os.Looper |
6 | 7 | import androidx.fragment.app.Fragment |
7 | 8 | import android.view.LayoutInflater |
8 | 9 | import android.view.View |
9 | 10 | import android.view.ViewGroup |
| 11 | +import androidx.lifecycle.lifecycleScope |
10 | 12 | import androidx.viewpager2.widget.ViewPager2 |
| 13 | +import com.example.projectfigma.Activity.AdverstsingPageActivity |
| 14 | +import com.example.projectfigma.Activity.LogActivity |
11 | 15 | import com.example.projectfigma.Adapters.PromoAdapter |
12 | 16 | import com.example.projectfigma.DataBase.DataBase |
13 | 17 | import com.example.projectfigma.R |
14 | 18 | import com.example.projectfigma.databinding.FragmentBannerFoodBinding |
15 | 19 | import com.example.projectfigma.databinding.FragmentBestSellerBinding |
16 | 20 | import com.google.android.material.tabs.TabLayout |
17 | 21 | import com.google.android.material.tabs.TabLayoutMediator |
| 22 | +import kotlinx.coroutines.Dispatchers |
| 23 | +import kotlinx.coroutines.delay |
| 24 | +import kotlinx.coroutines.isActive |
| 25 | +import kotlinx.coroutines.launch |
| 26 | +import kotlinx.coroutines.withContext |
18 | 27 |
|
19 | 28 | class BannerFood : Fragment() { |
20 | 29 | private var _binding: FragmentBannerFoodBinding? = null |
21 | 30 | private val binding get() = _binding!! |
22 | 31 | private lateinit var promoAdapter: PromoAdapter |
23 | 32 |
|
24 | | - |
25 | | - override fun onCreate(savedInstanceState: Bundle?) { |
26 | | - |
27 | | - super.onCreate(savedInstanceState) |
| 33 | + override fun onCreateView( |
| 34 | + inflater: LayoutInflater, |
| 35 | + container: ViewGroup?, |
| 36 | + savedInstanceState: Bundle? |
| 37 | + ): View { |
| 38 | + _binding = FragmentBannerFoodBinding.inflate(inflater, container, false) |
| 39 | + return binding.root |
28 | 40 | } |
29 | 41 |
|
30 | 42 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
31 | 43 | super.onViewCreated(view, savedInstanceState) |
32 | 44 |
|
33 | 45 | // 1) Инициализируем адаптер |
34 | | - promoAdapter = PromoAdapter(emptyList()) |
| 46 | + promoAdapter = PromoAdapter(emptyList()) { |
| 47 | + val intent = Intent(requireContext(), AdverstsingPageActivity::class.java) |
| 48 | + startActivity(intent) |
| 49 | + requireActivity().finish() |
| 50 | + } |
35 | 51 | binding.viewPager.adapter = promoAdapter |
36 | 52 |
|
37 | 53 | // 2) Настраиваем TabLayoutMediator, сразу задаём иконки |
38 | 54 | TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, _ -> |
39 | | - tab.setIcon(R.drawable.tab_indicator_unselected) |
| 55 | + tab.setIcon(com.example.projectfigma.R.drawable.tab_indicator_unselected) |
40 | 56 | }.attach() |
41 | 57 |
|
42 | | - // 3) Подписываемся на смену вкладки, чтобы менять иконки |
| 58 | + // 3) Слушаем смену вкладки для изменения иконки |
43 | 59 | binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { |
44 | 60 | override fun onTabSelected(tab: TabLayout.Tab) { |
45 | | - tab.setIcon(R.drawable.tab_indicator_selected) |
| 61 | + tab.setIcon(com.example.projectfigma.R.drawable.tab_indicator_selected) |
46 | 62 | } |
47 | 63 |
|
48 | 64 | override fun onTabUnselected(tab: TabLayout.Tab) { |
49 | | - tab.setIcon(R.drawable.tab_indicator_unselected) |
| 65 | + tab.setIcon(com.example.projectfigma.R.drawable.tab_indicator_unselected) |
50 | 66 | } |
51 | 67 |
|
52 | 68 | override fun onTabReselected(tab: TabLayout.Tab) {} |
53 | 69 | }) |
54 | 70 |
|
55 | | - // 4) Тут же можете запустить автопрокрутку, если она нужна в фрагменте |
56 | | - val handler = Handler(Looper.getMainLooper()) |
57 | | - handler.postDelayed(object : Runnable { |
58 | | - override fun run() { |
| 71 | + // 4) Автопрокрутка с помощью корутины |
| 72 | + viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) { |
| 73 | + while (isActive) { |
| 74 | + delay(4000) |
59 | 75 | if (promoAdapter.itemCount > 1) { |
60 | 76 | val next = (binding.viewPager.currentItem + 1) % promoAdapter.itemCount |
61 | 77 | binding.viewPager.setCurrentItem(next, true) |
62 | 78 | } |
63 | | - handler.postDelayed(this, 4000) |
64 | 79 | } |
65 | | - }, 4000) |
| 80 | + } |
66 | 81 |
|
67 | | - // 5) А обновление списка (LiveData) можно тоже повесить здесь: |
| 82 | + // 5) Обновление списка из БД |
68 | 83 | val dao = DataBase.getDb(requireContext()).getDishesDao() |
69 | 84 | dao.getBestSellers().observe(viewLifecycleOwner) { list -> |
70 | 85 | promoAdapter.updateList(list) |
71 | 86 | } |
72 | 87 | } |
73 | 88 |
|
74 | | - override fun onCreateView( |
75 | | - inflater: LayoutInflater, |
76 | | - container: ViewGroup?, |
77 | | - savedInstanceState: Bundle? |
78 | | - ): View { |
79 | | - _binding = FragmentBannerFoodBinding.inflate(inflater, container, false) |
80 | | - return binding.root |
81 | | - } |
82 | | - |
83 | 89 | override fun onDestroyView() { |
84 | 90 | super.onDestroyView() |
85 | 91 | _binding = null |
|
0 commit comments