2023-04-08 11:41:02 +02:00
|
|
|
package com.denizk0461.studip.fragment
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
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 com.denizk0461.studip.R
|
2023-04-16 21:41:46 +02:00
|
|
|
import com.denizk0461.studip.adapter.StudIPEventItemAdapter
|
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
|
2023-04-12 22:07:18 +02:00
|
|
|
import com.denizk0461.studip.model.StudIPEvent
|
2023-04-24 15:43:42 +02:00
|
|
|
import com.denizk0461.studip.sheet.ScheduleUpdateSheet
|
2023-04-08 17:30:05 +02:00
|
|
|
import com.denizk0461.studip.viewmodel.EventViewModel
|
2023-04-12 16:46:38 +02:00
|
|
|
import com.google.android.material.tabs.TabLayoutMediator
|
2023-04-10 21:12:29 +02:00
|
|
|
import java.util.*
|
2023-04-08 11:41:02 +02:00
|
|
|
|
|
|
|
|
/**
|
2023-04-23 11:57:03 +02:00
|
|
|
* User-facing fragment view that displays the user's Stud.IP schedule.
|
2023-04-08 11:41:02 +02:00
|
|
|
*/
|
2023-04-23 17:13:48 +02:00
|
|
|
class EventFragment : AppFragment() {
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Nullable view binding reference
|
2023-04-08 17:30:05 +02:00
|
|
|
private var _binding: FragmentEventBinding? = null
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
/*
|
|
|
|
|
* Non-null reference to the view binding. This property is only valid between onCreateView and
|
|
|
|
|
* onDestroyView.
|
|
|
|
|
*/
|
2023-04-08 11:41:02 +02:00
|
|
|
private val binding get() = _binding!!
|
2023-04-12 16:46:38 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// 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
|
2023-04-10 21:12:29 +02:00
|
|
|
private var dayOfWeek: Int = 0
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Titles for the view pager's tabs
|
2023-04-12 16:46:38 +02:00
|
|
|
private val dayStrings = listOf(
|
|
|
|
|
R.string.monday,
|
|
|
|
|
R.string.tuesday,
|
|
|
|
|
R.string.wednesday,
|
|
|
|
|
R.string.thursday,
|
|
|
|
|
R.string.friday,
|
|
|
|
|
)
|
2023-04-08 11:41:02 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Instantiate the view binding
|
2023-04-08 17:30:05 +02:00
|
|
|
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-23 11:57:03 +02:00
|
|
|
/*
|
|
|
|
|
* Determine the current day.
|
|
|
|
|
* TODO expand to weekend
|
|
|
|
|
*/
|
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-23 11:57:03 +02:00
|
|
|
// Set up the view pager's adapter
|
2023-04-16 21:41:46 +02:00
|
|
|
viewPagerAdapter = StudIPEventPageAdapter(listOf(), object : StudIPEventItemAdapter.OnClickListener {
|
2023-04-12 22:07:18 +02:00
|
|
|
override fun onClick(event: StudIPEvent) {
|
2023-04-23 11:57:03 +02:00
|
|
|
// TODO implement functionality or delete
|
2023-04-12 22:07:18 +02:00
|
|
|
}
|
2023-04-23 11:57:03 +02:00
|
|
|
override fun onLongClick(event: StudIPEvent): Boolean {
|
2023-04-24 15:43:42 +02:00
|
|
|
// Open a bottom sheet to edit the event
|
|
|
|
|
openBottomSheet(ScheduleUpdateSheet(event, onUpdate = { eventToUpdate ->
|
|
|
|
|
viewModel.update(eventToUpdate)
|
|
|
|
|
}, onDelete = { eventToDelete ->
|
|
|
|
|
viewModel.delete(eventToDelete)
|
|
|
|
|
}))
|
|
|
|
|
return true
|
2023-04-12 22:07:18 +02:00
|
|
|
}
|
2023-04-23 11:57:03 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Assign the adapter to the view pager
|
2023-04-12 16:46:38 +02:00
|
|
|
binding.viewPager.adapter = viewPagerAdapter
|
2023-04-12 10:49:30 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Create and attach the object mediating the tabs for the view pager
|
2023-04-12 16:46:38 +02:00
|
|
|
TabLayoutMediator(binding.dayTabLayout, binding.viewPager) { tab, position ->
|
|
|
|
|
tab.text = getString(dayStrings[position])
|
|
|
|
|
}.attach()
|
|
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Set up LiveData observer to refresh the view on update
|
2023-04-12 16:46:38 +02:00
|
|
|
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
|
2023-04-23 11:57:03 +02:00
|
|
|
// Update the item list in the view pager's adapter
|
2023-04-12 16:46:38 +02:00
|
|
|
viewPagerAdapter.setNewItems(events)
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Scroll to the current day
|
2023-04-12 10:49:30 +02:00
|
|
|
switchToCurrentDayView()
|
2023-04-08 17:30:05 +02:00
|
|
|
}
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-13 12:25:43 +02:00
|
|
|
override fun onResume() {
|
|
|
|
|
super.onResume()
|
2023-04-23 11:57:03 +02:00
|
|
|
|
|
|
|
|
// Scroll to the current day
|
2023-04-13 12:25:43 +02:00
|
|
|
switchToCurrentDayView()
|
|
|
|
|
}
|
2023-04-12 10:49:30 +02:00
|
|
|
|
2023-04-23 11:57:03 +02:00
|
|
|
// Invalidate the view binding
|
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-23 11:57:03 +02:00
|
|
|
/**
|
|
|
|
|
* Scroll to the current day. Current day is determined after the fragment's view has been
|
|
|
|
|
* created.
|
|
|
|
|
*/
|
2023-04-12 10:49:30 +02:00
|
|
|
private fun switchToCurrentDayView() {
|
2023-04-13 12:25:43 +02:00
|
|
|
binding.viewPager.currentItem = dayOfWeek
|
2023-04-12 10:49:30 +02:00
|
|
|
}
|
2023-04-08 11:41:02 +02:00
|
|
|
}
|