Implemented functionality for adding a new event manually; added floating action buttons for adding an event and refreshing the canteen offers

This commit is contained in:
denizk0461
2023-05-03 14:16:32 +02:00
parent ae8db7ce48
commit df1d127a17
18 changed files with 416 additions and 155 deletions
@@ -3,6 +3,7 @@ package com.denizk0461.studip.sheet
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.viewModels
import androidx.transition.TransitionManager
@@ -27,8 +28,8 @@ import com.denizk0461.studip.viewmodel.ScheduleUpdateViewModel
class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update), TimePickerFragment.OnTimeSetListener {
// Editable fields
private var timeslotStart = ""
private var timeslotEnd = ""
private var timeslotStart = "12:00"
private var timeslotEnd = "13:00"
private var colour = -1
// View binding
@@ -40,144 +41,245 @@ class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update), TimePicker
// Whether the user has once clicked the delete button; used to prevent an accidental click
private var hasClickedDelete: Boolean = false
// Determines whether the user is editing an existing event or creating a new one
private var isEditing: Boolean = false
// Day the event is scheduled for, default is Monday
private var selectedDay = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
try {
// Retrieve event from arguments bundle
val event: StudIPEvent = arguments.getParcelableCompat("event")
if (arguments?.getBoolean("isEditing") == true) {
// Set values of the text field to that of the event
timeslotStart = event.timeslotStart
timeslotEnd = event.timeslotEnd
colour = event.colour
try {
/*
* Retrieve event from arguments bundle; determines whether this sheet is for editing or
* creating an event.
*/
val event: StudIPEvent = arguments.getParcelableCompat("event")
// Set editable text fields
binding.editTextTitle.setText(event.title)
binding.editTextLecturers.setText(event.lecturer)
binding.editTextRoom.setText(event.room)
// - Set-up for editing an existing event - //
// Set the title to reflect that an event is being edited
binding.sheetTitle.text = getString(R.string.sheet_schedule_update_header_edit)
/*
* Show academic quarter suggestion if it is applicable. For this to be the case, both the
* start timestamp and the end timestamp must match up in a pattern that would make it
* likely for the course to use the academic quarter. Example:
* 14:00 - 16:00 -> likely 14:15 - 15:45
*
* 13:00 - 15:30 -> likely no academic quarter intended
*/
binding.buttonAcademicQuarter.setOnClickListener {
try {
// Retrieve new timestamps with the academic quarter applied, or throw a tantrum
val newStart = getTimestampAcademicQuarterStart(timeslotStart)
val newEnd = getTimestampAcademicQuarterEnd(timeslotEnd)
// User is editing an existing event
isEditing = true
// Set the buttons to the new timesatmps
timeslotStart = newStart
binding.buttonTimeStart.text = newStart
timeslotEnd = newEnd
binding.buttonTimeEnd.text = newEnd
// Set day
selectedDay = event.day
// Set values of the text field to that of the event
timeslotStart = event.timeslotStart
timeslotEnd = event.timeslotEnd
colour = event.colour
// Set editable text fields
binding.editTextTitle.setText(event.title)
binding.editTextLecturers.setText(event.lecturer)
binding.editTextRoom.setText(event.room)
// Prepare delete button
binding.buttonDelete.setOnClickListener {
/*
* Attempt to fix the issue of the bottom sheet jumping up when hiding the
* button.
* Check if the user has already clicked the button before; this is done to prevent
* accidentally deleting an event.
*/
TransitionManager.beginDelayedTransition(binding.sheet.parent as ViewGroup)
binding.buttonAcademicQuarter.visibility = View.GONE
// Catch if a timeslot couldn't be found in the list, and tell the user
} catch (e: AcademicQuarterNotApplicableException) {
showToast(
context,
getString(R.string.sheet_schedule_update_hint_quarter_error)
)
if (hasClickedDelete) {
viewModel.delete(event)
// Dismiss the sheet upon deletion
dismiss()
} else {
// Update the text to show the user that they clicked the button once
TransitionManager.beginDelayedTransition(binding.buttonContainer as ViewGroup)
binding.buttonDelete.text =
getString(R.string.sheet_schedule_update_delete_confirm)
hasClickedDelete = true
}
}
}
binding.buttonAcademicQuarter.visibility = getVisibilityForAcademicQuarter(
timeslotsForAcademicQuarter.contains(timeslotStart),
timeslotsForAcademicQuarter.contains(timeslotEnd),
)
// Prepare update/save button
binding.buttonSave.setOnClickListener {
// Check if any of the text fields are empty
checkIfTextFieldsEmpty {
// Update the event if all fields are filled
viewModel.update(
// construct new StudIPEvent from the data the user may have edited
StudIPEvent(
eventId = event.eventId,
title = binding.editTextTitle.text.toString().trim(),
lecturer = binding.editTextLecturers.text.toString().trim(),
room = binding.editTextRoom.text.toString().trim(),
day = selectedDay,
timeslotStart = timeslotStart,
timeslotEnd = timeslotEnd,
timeslotId = timeslotStart.parseToMinutes(),
colour = colour,
)
)
// Dismiss the sheet upon update
dismiss()
}
}
// Prepare timestamp buttons
binding.buttonTimeStart.text = timeslotStart
binding.buttonTimeStart.setOnClickListener {
// Launch a time picker dialogue to pick a new start timestamp
TimePickerFragment(
this,
true,
).also { sheet ->
val bundle = Bundle()
bundle.putString("timestamp", binding.buttonTimeStart.text.toString())
sheet.arguments = bundle
}.show((context as FragmentActivity).supportFragmentManager, "timePicker")
}
binding.buttonTimeEnd.text = timeslotEnd
binding.buttonTimeEnd.setOnClickListener {
// Launch a time picker dialogue to pick a new end timestamp
TimePickerFragment(
this,
false,
).also { sheet ->
val bundle = Bundle()
bundle.putString("timestamp", binding.buttonTimeEnd.text.toString())
sheet.arguments = bundle
}.show((context as FragmentActivity).supportFragmentManager, "timePicker")
}
} catch (e: ParcelNotFoundException) {
// Tell the user that something went wrong when retrieving Parcelable
showToast(context, getString(R.string.event_update_sheet_error))
/*
* TODO implement option to change the colour for the course - single-selection coloured
* filter buttons with checkmarks when they're selected maybe?
*/
// Prepare cancel button
binding.buttonCancel.setOnClickListener {
// Do nothing and dismiss the sheet
// Close the sheet
dismiss()
}
// Prepare delete button
binding.buttonDelete.setOnClickListener {
/*
* Check if the user has already clicked the button before; this is done to prevent
* accidentally deleting an event.
*/
if (hasClickedDelete) {
viewModel.delete(event)
// Dismiss the sheet upon deletion
dismiss()
} else {
// Update the text to show the user that they clicked the button once
TransitionManager.beginDelayedTransition(binding.buttonContainer as ViewGroup)
binding.buttonDelete.text = getString(R.string.sheet_schedule_update_delete_confirm)
hasClickedDelete = true
}
}
} else {
// - Set-up for creating a new event - //
// Set the title to reflect that an event is being added
binding.sheetTitle.text = getString(R.string.sheet_schedule_update_header_add)
// Hide delete button, since there is nothing to delete
binding.buttonDelete.visibility = View.GONE
// Prepare update/save button
binding.buttonSave.setOnClickListener {
viewModel.update(
// construct new StudIPEvent from the data the user may have edited
StudIPEvent(
eventId = event.eventId,
title = binding.editTextTitle.text.toString(),
lecturer = binding.editTextLecturers.text.toString(),
room = binding.editTextRoom.text.toString(),
day = event.day,
timeslotStart = timeslotStart,
timeslotEnd = timeslotEnd,
timeslotId = timeslotStart.parseToMinutes(),
colour = colour,
// Check if any of the text fields are empty
checkIfTextFieldsEmpty {
// Insert new event into the database
viewModel.insert(
// construct new StudIPEvent from the data the user may have edited
StudIPEvent(
title = binding.editTextTitle.text.toString().trim(),
lecturer = binding.editTextLecturers.text.toString().trim(),
room = binding.editTextRoom.text.toString().trim(),
day = selectedDay,
timeslotStart = timeslotStart,
timeslotEnd = timeslotEnd,
timeslotId = timeslotStart.parseToMinutes(),
colour = colour,
)
)
)
// Dismiss the sheet upon update
dismiss()
// Dismiss the sheet upon update
dismiss()
}
}
} catch (e: ParcelNotFoundException) {
// Tell the user that something went wrong when retrieving Parcelable
showToast(context, getString(R.string.event_update_sheet_error))
}
// Close the sheet
// - Set-up that is required for both editing and creating a new event - //
/*
* TODO implement option to change the colour for the course - single-selection coloured
* filter buttons with checkmarks when they're selected maybe?
*/
// Set visibility for academic quarter button based on whether the change applies
binding.buttonAcademicQuarter.visibility = getVisibilityForAcademicQuarter(
timeslotsForAcademicQuarter.contains(timeslotStart),
timeslotsForAcademicQuarter.contains(timeslotEnd),
)
// Retrieve new values for the day of the current event
binding.buttonDay.text = getString(when (selectedDay) {
1 -> R.string.tuesday
2 -> R.string.wednesday
3 -> R.string.thursday
4 -> R.string.friday
5 -> R.string.saturday
6 -> R.string.sunday
else -> R.string.monday // assume Monday
})
// Open the menu for the day picker button
binding.buttonDay.setOnClickListener {
PopupMenu(binding.root.context, binding.buttonDay).apply {
setOnMenuItemClickListener { item ->
// Retrieve new values for the day selected by the user
val (newDay, newDayId) = when (item?.itemId) {
R.id.tuesday -> Pair(1, R.string.tuesday)
R.id.wednesday -> Pair(2, R.string.wednesday)
R.id.thursday -> Pair(3, R.string.thursday)
R.id.friday -> Pair(4, R.string.friday)
R.id.saturday -> Pair(5, R.string.saturday)
R.id.sunday -> Pair(6, R.string.sunday)
else -> Pair(0, R.string.monday) // assume Monday
}
selectedDay = newDay
// Set newly selected day to the button
binding.buttonDay.text = getString(newDayId)
true
}
inflate(R.menu.menu_days)
show()
}
}
/*
* Show academic quarter suggestion if it is applicable. For this to be the case, both the
* start timestamp and the end timestamp must match up in a pattern that would make it
* likely for the course to use the academic quarter. Example:
* 14:00 - 16:00 -> likely 14:15 - 15:45
*
* 13:00 - 15:30 -> likely no academic quarter intended
*/
binding.buttonAcademicQuarter.setOnClickListener {
try {
// Retrieve new timestamps with the academic quarter applied, or throw a tantrum
val newStart = getTimestampAcademicQuarterStart(timeslotStart)
val newEnd = getTimestampAcademicQuarterEnd(timeslotEnd)
// Set the buttons to the new time stamps
timeslotStart = newStart
binding.buttonTimeStart.text = newStart
timeslotEnd = newEnd
binding.buttonTimeEnd.text = newEnd
/*
* Attempt to fix the issue of the bottom sheet jumping up when hiding the
* button.
*/
TransitionManager.beginDelayedTransition(binding.sheet.parent as ViewGroup)
binding.buttonAcademicQuarter.visibility = View.GONE
// Catch if a timeslot couldn't be found in the list, and tell the user
} catch (e: AcademicQuarterNotApplicableException) {
showToast(
context,
getString(R.string.sheet_schedule_update_hint_quarter_error)
)
}
}
// Prepare timestamp buttons
binding.buttonTimeStart.text = timeslotStart
binding.buttonTimeStart.setOnClickListener {
// Launch a time picker dialogue to pick a new start timestamp
TimePickerFragment(
this,
true,
).also { sheet ->
val bundle = Bundle()
bundle.putString("timestamp", binding.buttonTimeStart.text.toString())
sheet.arguments = bundle
}.show((context as FragmentActivity).supportFragmentManager, "timePicker")
}
binding.buttonTimeEnd.text = timeslotEnd
binding.buttonTimeEnd.setOnClickListener {
// Launch a time picker dialogue to pick a new end timestamp
TimePickerFragment(
this,
false,
).also { sheet ->
val bundle = Bundle()
bundle.putString("timestamp", binding.buttonTimeEnd.text.toString())
sheet.arguments = bundle
}.show((context as FragmentActivity).supportFragmentManager, "timePicker")
}
// Prepare cancel button
binding.buttonCancel.setOnClickListener {
// Do nothing and dismiss the sheet
dismiss()
}
}
@@ -218,6 +320,53 @@ class ScheduleUpdateSheet : AppSheet(R.layout.sheet_schedule_update), TimePicker
}
}
/**
* Checks whether the text fields in the sheet are blank (meaning, with whitespace trimmed from
* the start and the end of the string). Executes an action if the fields are all filled.
*
* @param actionIfFilled action to execute if all fields are filled
*/
private fun checkIfTextFieldsEmpty(actionIfFilled: () -> Unit) {
// Assume at the start that no fields are empty; this may be changed during the checks
var hasEmptyFields = false
// Check if the title field is blank
if (binding.editTextTitle.text.toString().isBlank()) {
// Set an error on the title field
binding.editTextTitle.error =
getString(R.string.sheet_schedule_update_text_field_empty_error)
// Set that the action must not be executed
hasEmptyFields = true
}
// Check if the lecturer field is blank
if (binding.editTextLecturers.text.toString().isBlank()) {
// Set an error on the lecturer field
binding.editTextLecturers.error =
getString(R.string.sheet_schedule_update_text_field_empty_error)
// Set that the action must not be executed
hasEmptyFields = true
}
// Check if the room field is blank
if (binding.editTextRoom.text.toString().isBlank()) {
// Set an error on the room field
binding.editTextRoom.error =
getString(R.string.sheet_schedule_update_text_field_empty_error)
// Set that the action must not be executed
hasEmptyFields = true
}
// If any fields are empty, don't execute the action
if (!hasEmptyFields) {
actionIfFilled()
}
}
/**
* Determines which visibility the academic quarter button should have given the timestamps.
*