Fully documented all Kotlin files and added TODOs to functions that need to be updated in some way

This commit is contained in:
denizk0461
2023-04-23 11:57:03 +02:00
parent 2534849329
commit fbad84aa09
33 changed files with 1220 additions and 571 deletions
@@ -16,17 +16,29 @@ import com.google.android.material.tabs.TabLayoutMediator
import java.util.*
/**
* A simple [Fragment] subclass as the default destination in the navigation.
* User-facing fragment view that displays the user's Stud.IP schedule.
*/
class EventFragment : Fragment() {
// Nullable view binding reference
private var _binding: FragmentEventBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
/*
* Non-null reference to the view binding. This property is only valid between onCreateView and
* onDestroyView.
*/
private val binding get() = _binding!!
// Adapter for the view pager displaying all events
private lateinit var viewPagerAdapter: StudIPEventPageAdapter
// View model reference for providing access to the database
private val viewModel: EventViewModel by viewModels()
// Used to determine the current day
private var dayOfWeek: Int = 0
// Titles for the view pager's tabs
private val dayStrings = listOf(
R.string.monday,
R.string.tuesday,
@@ -35,9 +47,7 @@ class EventFragment : Fragment() {
R.string.friday,
)
private lateinit var viewPagerAdapter: StudIPEventPageAdapter
private val viewModel: EventViewModel by viewModels()
// Instantiate the view binding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentEventBinding.inflate(inflater, container, false)
return binding.root
@@ -46,6 +56,10 @@ class EventFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/*
* Determine the current day.
* TODO expand to weekend
*/
dayOfWeek = when (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) {
Calendar.TUESDAY -> 1
Calendar.WEDNESDAY -> 2
@@ -54,42 +68,53 @@ class EventFragment : Fragment() {
else -> 0
}
// Set up the view pager's adapter
viewPagerAdapter = StudIPEventPageAdapter(listOf(), object : StudIPEventItemAdapter.OnClickListener {
override fun onClick(event: StudIPEvent) {
// TODO implement functionality or delete
}
override fun onLongClick(event: StudIPEvent) {
override fun onLongClick(event: StudIPEvent): Boolean {
// TODO implement functionality or delete
return false
}
})//DummyData.events.toList())
})
// Assign the adapter to the view pager
binding.viewPager.adapter = viewPagerAdapter
// Create and attach the object mediating the tabs for the view pager
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
tab.text = getString(dayStrings[position])
}.attach()
// Set up LiveData observer to refresh the view on update
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
// Update the item list in the view pager's adapter
viewPagerAdapter.setNewItems(events)
// Scroll to the current day
switchToCurrentDayView()
}
// binding.buttonFirst.setOnClickListener {
// findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
// }
}
override fun onResume() {
super.onResume()
// Scroll to the current day
switchToCurrentDayView()
}
// Invalidate the view binding
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
/**
* Scroll to the current day. Current day is determined after the fragment's view has been
* created.
*/
private fun switchToCurrentDayView() {
binding.viewPager.currentItem = dayOfWeek
// binding.dayTabLayout.getTabAt(dayOfWeek)?.select()
}
}