2023-04-27 21:58:10 +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-28 11:18:05 +02:00
|
|
|
import androidx.fragment.app.viewModels
|
2023-04-27 21:58:10 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import com.denizk0461.studip.adapter.StudIPEventPageAdapter
|
|
|
|
|
import com.denizk0461.studip.adapter.StudIPEventItemAdapter
|
|
|
|
|
import com.denizk0461.studip.databinding.RecyclerViewBinding
|
|
|
|
|
import com.denizk0461.studip.model.StudIPEvent
|
2023-04-28 11:18:05 +02:00
|
|
|
import com.denizk0461.studip.sheet.ScheduleUpdateSheet
|
|
|
|
|
import com.denizk0461.studip.viewmodel.EventPageViewModel
|
2023-04-27 21:58:10 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fragment that is instantiated by [StudIPEventPageAdapter] to display individual days' pages and
|
|
|
|
|
* their events.
|
|
|
|
|
*/
|
2023-04-30 22:47:59 +02:00
|
|
|
class EventPageFragment : AppFragment(), StudIPEventItemAdapter.OnClickListener {
|
2023-04-27 21:58:10 +02:00
|
|
|
|
|
|
|
|
// Nullable view binding reference
|
|
|
|
|
private var _binding: RecyclerViewBinding? = null
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Non-null reference to the view binding. This property is only valid between onCreateView and
|
|
|
|
|
* onDestroyView.
|
|
|
|
|
*/
|
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
|
2023-04-28 11:18:05 +02:00
|
|
|
// View model reference for providing access to the database
|
|
|
|
|
private val viewModel: EventPageViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adapter that manages the page's items
|
|
|
|
|
*/
|
2023-04-30 22:47:59 +02:00
|
|
|
private lateinit var eventAdapter: StudIPEventItemAdapter
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Current day that is used to show only events of a given day (0 = Monday, 4 = Friday)
|
|
|
|
|
*/
|
|
|
|
|
private var currentDay = -1
|
2023-04-28 11:18:05 +02:00
|
|
|
|
2023-04-27 21:58:10 +02:00
|
|
|
// Instantiate the view binding
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
2023-04-30 22:47:59 +02:00
|
|
|
|
|
|
|
|
// Retrieve current day to fetch items specifically for that day
|
|
|
|
|
currentDay = arguments?.getInt("currentDay") ?: -1
|
|
|
|
|
|
|
|
|
|
// Instantiate event adapter with the current day
|
2023-05-02 20:52:56 +02:00
|
|
|
eventAdapter = StudIPEventItemAdapter(
|
|
|
|
|
currentDay,
|
|
|
|
|
viewModel.preferenceCourseHighlighting,
|
|
|
|
|
this,
|
|
|
|
|
)
|
2023-04-30 22:47:59 +02:00
|
|
|
|
|
|
|
|
// Inflate view binding
|
2023-04-27 21:58:10 +02:00
|
|
|
_binding = RecyclerViewBinding.inflate(inflater, container, false)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
|
|
binding.recyclerView.apply {
|
|
|
|
|
// Set page to scroll vertically
|
|
|
|
|
layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Create new adapter for every page. Attribute position denotes day that will be set up
|
2023-04-28 11:18:05 +02:00
|
|
|
* by the newly created adapter (0 = Monday, 4 = Friday, 6 = Sunday)
|
2023-04-27 21:58:10 +02:00
|
|
|
*/
|
2023-04-28 11:18:05 +02:00
|
|
|
adapter = eventAdapter
|
2023-04-27 21:58:10 +02:00
|
|
|
|
|
|
|
|
// Animate creation of new page
|
|
|
|
|
scheduleLayoutAnimation()
|
|
|
|
|
}
|
2023-04-28 11:18:05 +02:00
|
|
|
|
|
|
|
|
// Set up LiveData observer to refresh the view on update
|
|
|
|
|
viewModel.getEventsForDay(currentDay).observe(viewLifecycleOwner) { events ->
|
|
|
|
|
eventAdapter.setNewData(events)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onClick(event: StudIPEvent) {
|
|
|
|
|
// TODO implement functionality or delete
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onLongClick(event: StudIPEvent): Boolean {
|
|
|
|
|
// Open a bottom sheet to edit the event
|
2023-05-02 15:02:47 +02:00
|
|
|
openBottomSheet(
|
|
|
|
|
ScheduleUpdateSheet().also { sheet ->
|
|
|
|
|
val bundle = Bundle()
|
|
|
|
|
bundle.putParcelable("event", event)
|
|
|
|
|
sheet.arguments = bundle
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-04-28 11:18:05 +02:00
|
|
|
return true
|
2023-04-27 21:58:10 +02:00
|
|
|
}
|
|
|
|
|
}
|