2023-04-16 18:19:06 +02:00
|
|
|
package com.denizk0461.studip.fragment
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.fragment.app.viewModels
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.denizk0461.studip.adapter.CanteenOfferPageAdapter
|
2023-04-16 18:19:06 +02:00
|
|
|
import com.denizk0461.studip.databinding.FragmentCanteenBinding
|
|
|
|
|
import com.denizk0461.studip.viewmodel.CanteenViewModel
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.google.android.material.tabs.TabLayoutMediator
|
2023-04-16 18:19:06 +02:00
|
|
|
|
|
|
|
|
class CanteenFragment : Fragment() {
|
|
|
|
|
|
|
|
|
|
private var _binding: FragmentCanteenBinding? = null
|
|
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
|
// onDestroyView.
|
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
private var dates = listOf<String>()
|
|
|
|
|
|
|
|
|
|
private lateinit var viewPagerAdapter: CanteenOfferPageAdapter
|
2023-04-16 18:19:06 +02:00
|
|
|
private val viewModel: CanteenViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
|
_binding = FragmentCanteenBinding.inflate(inflater, container, false)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
viewPagerAdapter = CanteenOfferPageAdapter(listOf(), 0)
|
|
|
|
|
binding.viewPager.adapter = viewPagerAdapter
|
|
|
|
|
|
|
|
|
|
createTabLayoutMediator()
|
|
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
viewModel.allOffers.observe(viewLifecycleOwner) { offers ->
|
2023-04-16 21:41:46 +02:00
|
|
|
|
|
|
|
|
dates = offers.map { it.date }.distinct()
|
|
|
|
|
createTabLayoutMediator()
|
|
|
|
|
|
|
|
|
|
viewPagerAdapter.setNewItems(offers, dates.size)
|
|
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
// TODO this must be changed. if an exception is raised, then this will not fire
|
|
|
|
|
binding.swipeRefreshLayout.isRefreshing = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
binding.swipeRefreshLayout.setOnRefreshListener {
|
|
|
|
|
viewModel.fetchOffers {
|
|
|
|
|
// binding.swipeRefreshLayout.isRefreshing = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 21:41:46 +02:00
|
|
|
private fun createTabLayoutMediator() {
|
|
|
|
|
if (dates.isNotEmpty()) {
|
|
|
|
|
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
|
|
|
|
|
tab.text = dates[position]
|
|
|
|
|
}.attach()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-16 18:19:06 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
_binding = null
|
|
|
|
|
}
|
|
|
|
|
}
|