2023-04-08 11:41:02 +02:00
|
|
|
package com.denizk0461.studip.fragment
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2023-04-11 16:50:45 +02:00
|
|
|
import android.util.Log
|
2023-04-08 11:41:02 +02:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2023-04-08 17:30:05 +02:00
|
|
|
import androidx.fragment.app.viewModels
|
2023-04-10 14:50:57 +02:00
|
|
|
import androidx.navigation.fragment.NavHostFragment.Companion.findNavController
|
2023-04-08 13:43:26 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import androidx.recyclerview.widget.PagerSnapHelper
|
2023-04-11 16:50:45 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
|
2023-04-10 14:50:57 +02:00
|
|
|
import com.denizk0461.studip.R
|
2023-04-08 15:13:11 +02:00
|
|
|
import com.denizk0461.studip.adapter.StudIPEventPageAdapter
|
2023-04-08 17:30:05 +02:00
|
|
|
import com.denizk0461.studip.databinding.FragmentEventBinding
|
|
|
|
|
import com.denizk0461.studip.viewmodel.EventViewModel
|
2023-04-11 16:50:45 +02:00
|
|
|
import com.google.android.material.tabs.TabLayout
|
|
|
|
|
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
|
2023-04-10 21:12:29 +02:00
|
|
|
import java.util.*
|
2023-04-08 11:41:02 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A simple [Fragment] subclass as the default destination in the navigation.
|
|
|
|
|
*/
|
2023-04-08 17:30:05 +02:00
|
|
|
class EventFragment : Fragment() {
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-08 17:30:05 +02:00
|
|
|
private var _binding: FragmentEventBinding? = null
|
2023-04-08 11:41:02 +02:00
|
|
|
|
|
|
|
|
// This property is only valid between onCreateView and
|
|
|
|
|
// onDestroyView.
|
|
|
|
|
private val binding get() = _binding!!
|
2023-04-08 15:13:11 +02:00
|
|
|
private lateinit var recyclerViewAdapter: StudIPEventPageAdapter
|
2023-04-11 16:50:45 +02:00
|
|
|
private lateinit var layoutManager: LinearLayoutManager
|
2023-04-10 21:12:29 +02:00
|
|
|
private var dayOfWeek: Int = 0
|
2023-04-11 16:50:45 +02:00
|
|
|
private val pagerSnapHelper = PagerSnapHelper()
|
|
|
|
|
private var isUserScrolling = false
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-08 17:30:05 +02:00
|
|
|
private val viewModel: EventViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
|
_binding = FragmentEventBinding.inflate(inflater, container, false)
|
2023-04-08 11:41:02 +02:00
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-04-10 21:12:29 +02:00
|
|
|
dayOfWeek = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
|
|
|
|
|
Calendar.TUESDAY -> 1
|
|
|
|
|
Calendar.WEDNESDAY -> 2
|
|
|
|
|
Calendar.THURSDAY -> 3
|
|
|
|
|
Calendar.FRIDAY -> 4
|
|
|
|
|
else -> 0
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 17:30:05 +02:00
|
|
|
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
|
2023-04-10 20:54:15 +02:00
|
|
|
recyclerViewAdapter = StudIPEventPageAdapter(events)//DummyData.events.toList())
|
2023-04-08 17:30:05 +02:00
|
|
|
binding.recyclerView.adapter = recyclerViewAdapter
|
2023-04-11 16:50:45 +02:00
|
|
|
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
|
|
|
|
binding.recyclerView.layoutManager = layoutManager
|
2023-04-10 20:54:15 +02:00
|
|
|
binding.recyclerView.onFlingListener = null
|
2023-04-11 16:50:45 +02:00
|
|
|
pagerSnapHelper.attachToRecyclerView(binding.recyclerView)
|
2023-04-10 21:12:29 +02:00
|
|
|
// binding.recyclerView.scheduleLayoutAnimation()
|
2023-04-12 10:49:30 +02:00
|
|
|
|
|
|
|
|
switchToCurrentDayView()
|
2023-04-08 17:30:05 +02:00
|
|
|
}
|
2023-04-08 13:43:26 +02:00
|
|
|
|
2023-04-10 14:50:57 +02:00
|
|
|
binding.fab.setOnClickListener { view ->
|
|
|
|
|
launchWebview()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-11 16:50:45 +02:00
|
|
|
binding.recyclerView.addOnScrollListener(object : OnScrollListener() {
|
2023-04-12 09:46:27 +02:00
|
|
|
|
|
|
|
|
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
|
|
|
|
super.onScrollStateChanged(recyclerView, newState)
|
|
|
|
|
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
|
|
|
|
|
binding.dayTabs.getTabAt(layoutManager.findFirstVisibleItemPosition())?.select()
|
2023-04-11 16:50:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
binding.dayTabs.addOnTabSelectedListener(object : OnTabSelectedListener {
|
|
|
|
|
override fun onTabSelected(tab: TabLayout.Tab) {
|
2023-04-12 09:46:27 +02:00
|
|
|
isUserScrolling = false
|
|
|
|
|
binding.recyclerView.smoothScrollToPosition(tab.position)
|
|
|
|
|
Log.d("HELLO4", binding.recyclerView.scrollY.toString())
|
|
|
|
|
// TODO where to set isUserScrolling back to true?
|
2023-04-11 16:50:45 +02:00
|
|
|
}
|
|
|
|
|
override fun onTabUnselected(tab: TabLayout.Tab?) {}
|
|
|
|
|
override fun onTabReselected(tab: TabLayout.Tab?) {}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// binding.daytabmonday.setOnClickListener { binding.recyclerView.scrollToPosition(0) }
|
|
|
|
|
// binding.daytabtuesday.setOnClickListener { binding.recyclerView.scrollToPosition(1) }
|
|
|
|
|
// binding.daytabwednesday.setOnClickListener { binding.recyclerView.scrollToPosition(2) }
|
|
|
|
|
// binding.daytabthursday.setOnClickListener { binding.recyclerView.scrollToPosition(3) }
|
|
|
|
|
// binding.daytabfriday.setOnClickListener { binding.recyclerView.scrollToPosition(4) }
|
|
|
|
|
|
2023-04-08 11:41:02 +02:00
|
|
|
// binding.buttonFirst.setOnClickListener {
|
|
|
|
|
// findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 10:49:30 +02:00
|
|
|
override fun onResume() {
|
|
|
|
|
super.onResume()
|
|
|
|
|
switchToCurrentDayView()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 11:41:02 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
|
|
|
|
_binding = null
|
|
|
|
|
}
|
2023-04-10 14:50:57 +02:00
|
|
|
|
2023-04-12 10:49:30 +02:00
|
|
|
private fun switchToCurrentDayView() {
|
|
|
|
|
binding.recyclerView.scrollToPosition(dayOfWeek)
|
|
|
|
|
binding.dayTabs.getTabAt(dayOfWeek)?.select()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 14:50:57 +02:00
|
|
|
private fun launchWebview() {
|
|
|
|
|
findNavController(this).navigate(R.id.action_FirstFragment_to_SecondFragment)
|
|
|
|
|
}
|
2023-04-12 10:49:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Highlights the current or next course.
|
|
|
|
|
*/
|
|
|
|
|
private fun highlightCurrentCourse() {
|
|
|
|
|
|
|
|
|
|
}
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|