2023-04-24 15:43:42 +02:00
|
|
|
package com.denizk0461.studip.sheet
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2023-04-26 18:36:32 +02:00
|
|
|
import android.util.Log
|
2023-04-24 15:43:42 +02:00
|
|
|
import android.view.View
|
2023-04-25 10:24:15 +02:00
|
|
|
import androidx.fragment.app.FragmentActivity
|
2023-04-24 15:43:42 +02:00
|
|
|
import com.denizk0461.studip.R
|
|
|
|
|
import com.denizk0461.studip.data.parseToMinutes
|
|
|
|
|
import com.denizk0461.studip.data.viewBinding
|
|
|
|
|
import com.denizk0461.studip.databinding.SheetScheduleUpdateBinding
|
|
|
|
|
import com.denizk0461.studip.dialog.TimePickerFragment
|
|
|
|
|
import com.denizk0461.studip.model.StudIPEvent
|
|
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
/**
|
|
|
|
|
* Update sheet to allow the user to edit attributes for a specific schedule event.
|
|
|
|
|
*
|
|
|
|
|
* @param event event to be updated
|
|
|
|
|
* @param onUpdate action to execute to update the event
|
|
|
|
|
* @param onDelete action to execute to delete the event
|
|
|
|
|
*/
|
2023-04-24 15:43:42 +02:00
|
|
|
class ScheduleUpdateSheet(
|
|
|
|
|
private val event: StudIPEvent,
|
|
|
|
|
private val onUpdate: (event: StudIPEvent) -> Unit,
|
|
|
|
|
private val onDelete: (event: StudIPEvent) -> Unit,
|
|
|
|
|
) : AppSheet(R.layout.sheet_schedule_update), TimePickerFragment.OnTimeSetListener {
|
|
|
|
|
|
|
|
|
|
// Editable fields
|
|
|
|
|
private var timeslotStart = event.timeslotStart
|
|
|
|
|
private var timeslotEnd = event.timeslotEnd
|
|
|
|
|
private var colour = event.colour
|
|
|
|
|
|
|
|
|
|
// View binding
|
|
|
|
|
private val binding: SheetScheduleUpdateBinding by viewBinding(SheetScheduleUpdateBinding::bind)
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
// Set editable text fields
|
2023-04-25 10:24:15 +02:00
|
|
|
binding.editTextTitle.setText(event.title)
|
|
|
|
|
binding.editTextLecturers.setText(event.lecturer)
|
|
|
|
|
binding.editTextRoom.setText(event.room)
|
|
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
// Prepare timestamp buttons
|
2023-04-25 10:24:15 +02:00
|
|
|
binding.buttonTimeStart.text = timeslotStart
|
|
|
|
|
binding.buttonTimeStart.setOnClickListener {
|
2023-04-25 12:02:25 +02:00
|
|
|
TimePickerFragment(
|
|
|
|
|
binding.buttonTimeStart.text.toString(),
|
|
|
|
|
this,
|
|
|
|
|
true,
|
|
|
|
|
).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
2023-04-25 10:24:15 +02:00
|
|
|
}
|
|
|
|
|
binding.buttonTimeEnd.text = timeslotEnd
|
|
|
|
|
binding.buttonTimeEnd.setOnClickListener {
|
2023-04-25 12:02:25 +02:00
|
|
|
TimePickerFragment(
|
|
|
|
|
binding.buttonTimeEnd.text.toString(),
|
|
|
|
|
this,
|
|
|
|
|
false,
|
|
|
|
|
).show((context as FragmentActivity).supportFragmentManager, "timePicker")
|
2023-04-24 15:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-25 15:38:30 +02:00
|
|
|
/*
|
|
|
|
|
* 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
|
|
|
|
|
dismiss()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
// Prepare delete button
|
2023-04-24 15:43:42 +02:00
|
|
|
binding.buttonDelete.setOnClickListener {
|
|
|
|
|
onDelete(event)
|
2023-04-25 12:02:25 +02:00
|
|
|
// Dismiss the sheet upon deletion
|
2023-04-24 15:43:42 +02:00
|
|
|
dismiss()
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
// Prepare update/save button
|
2023-04-24 15:43:42 +02:00
|
|
|
binding.buttonSave.setOnClickListener {
|
|
|
|
|
onUpdate(
|
2023-04-25 15:38:30 +02:00
|
|
|
// construct new StudIPEvent from the data the user may have edited
|
2023-04-24 15:43:42 +02:00
|
|
|
StudIPEvent(
|
|
|
|
|
id = event.id,
|
|
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
)
|
2023-04-25 12:02:25 +02:00
|
|
|
// Dismiss the sheet upon update
|
2023-04-24 15:43:42 +02:00
|
|
|
dismiss()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 12:02:25 +02:00
|
|
|
* Checks and stores a new timestamp after the user picks one through [TimePickerFragment].
|
2023-04-24 15:43:42 +02:00
|
|
|
*
|
2023-04-25 12:02:25 +02:00
|
|
|
* @param hours newly set hour
|
|
|
|
|
* @param minutes newly set minute
|
|
|
|
|
* @param isEventStart whether this timestamp is for the start of the course
|
2023-04-24 15:43:42 +02:00
|
|
|
*/
|
|
|
|
|
override fun onTimeSet(hours: Int, minutes: Int, isEventStart: Boolean) {
|
2023-04-26 18:36:32 +02:00
|
|
|
// Parse the separate ints into a timestamp string. Add a leading zero if necessary.
|
|
|
|
|
val newTimestamp = "$hours:${String.format("%02d", minutes)}"
|
2023-04-25 10:24:15 +02:00
|
|
|
|
2023-04-25 12:02:25 +02:00
|
|
|
// If start > end, show an error
|
2023-04-25 10:24:15 +02:00
|
|
|
if ((isEventStart && newTimestamp.parseToMinutes() > timeslotEnd.parseToMinutes()) ||
|
|
|
|
|
(!isEventStart && newTimestamp.parseToMinutes() < timeslotStart.parseToMinutes())) {
|
2023-04-25 15:38:30 +02:00
|
|
|
// TODO tell user that the timestamp must be set earlier/later
|
2023-04-24 15:43:42 +02:00
|
|
|
} else {
|
2023-04-25 12:02:25 +02:00
|
|
|
// If the new timestamp is valid, save it
|
2023-04-25 10:24:15 +02:00
|
|
|
if (isEventStart) {
|
|
|
|
|
timeslotStart = newTimestamp
|
|
|
|
|
binding.buttonTimeStart.text = newTimestamp
|
|
|
|
|
} else {
|
|
|
|
|
timeslotEnd = newTimestamp
|
|
|
|
|
binding.buttonTimeEnd.text = newTimestamp
|
|
|
|
|
}
|
2023-04-24 15:43:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|