[APIv2/Timetable] Add searching for the next lesson by team id

This commit is contained in:
Kacper Ziubryniewicz 2019-11-20 22:15:43 +01:00
parent cd379e4175
commit 3ca051983f
2 changed files with 280 additions and 259 deletions

View File

@ -60,6 +60,14 @@ interface TimetableDao {
""") """)
fun getNextWithSubject(profileId: Int, today: Date, subjectId: Long) : LiveData<LessonFull?> fun getNextWithSubject(profileId: Int, today: Date, subjectId: Long) : LiveData<LessonFull?>
@Query("""
$QUERY
WHERE timetable.profileId = :profileId AND ((type != 3 AND date > :today) OR ((type = 3 OR type = 1) AND oldDate > :today)) AND timetable.subjectId = :subjectId AND timetable.teamId = :teamId
ORDER BY id, type
LIMIT 1
""")
fun getNextWithSubjectAndTeam(profileId: Int, today: Date, subjectId: Long, teamId: Long): LiveData<LessonFull?>
@Query(""" @Query("""
$QUERY $QUERY
WHERE (type != 3 AND date >= :dateFrom AND date <= :dateTo) OR ((type = 3 OR type = 1) AND oldDate >= :dateFrom AND oldDate <= :dateTo) WHERE (type != 3 AND date >= :dateFrom AND date <= :dateTo) OR ((type = 3 OR type = 1) AND oldDate >= :dateFrom AND oldDate <= :dateTo)

View File

@ -57,286 +57,300 @@ class EventManualV2Dialog(
private lateinit var event: Event private lateinit var event: Event
private var customColor: Int? = null private var customColor: Int? = null
init { run { init {
if (activity.isFinishing) run {
return@run if (activity.isFinishing)
job = Job() return@run
onShowListener?.invoke(TAG) job = Job()
b = DialogEventManualV2Binding.inflate(activity.layoutInflater) onShowListener?.invoke(TAG)
dialog = MaterialAlertDialogBuilder(activity) b = DialogEventManualV2Binding.inflate(activity.layoutInflater)
.setTitle(R.string.dialog_event_manual_title) dialog = MaterialAlertDialogBuilder(activity)
.setView(b.root) .setTitle(R.string.dialog_event_manual_title)
.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() } .setView(b.root)
.setPositiveButton(R.string.save) { _, _ -> saveEvent() } .setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
.setOnDismissListener { .setPositiveButton(R.string.save) { _, _ -> saveEvent() }
onDismissListener?.invoke(TAG) .setOnDismissListener {
onDismissListener?.invoke(TAG)
}
.show()
event = editingEvent?.clone() ?: Event().also { event ->
event.profileId = profileId
/*defaultDate?.let {
event.eventDate = it
b.date = it
} }
.show() defaultTime?.let {
event.startTime = it
event = editingEvent?.clone() ?: Event().also { event -> b.time = it
event.profileId = profileId }
/*defaultDate?.let { defaultType?.let {
event.eventDate = it event.type = it
b.date = it }*/
} }
defaultTime?.let {
event.startTime = it b.showMore.onClick {
b.time = it // TODO iconics is broken
it.apply {
refreshDrawableState()
if (isChecked)
Anim.expand(b.moreLayout, 200, null)
else
Anim.collapse(b.moreLayout, 200, null)
}
} }
defaultType?.let {
event.type = it loadLists()
}*/
} }
}
b.showMore.onClick { // TODO iconics is broken private fun loadLists() {
it.apply { launch {
refreshDrawableState() val deferred = async(Dispatchers.Default) {
// get the team list
val teams = app.db.teamDao().getAllNow(profileId)
b.teamDropdown.clear()
b.teamDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_team),
""
)
b.teamDropdown += teams.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
if (isChecked) // get the subject list
Anim.expand(b.moreLayout, 200, null) val subjects = app.db.subjectDao().getAllNow(profileId)
else b.subjectDropdown.clear()
Anim.collapse(b.moreLayout, 200, null) b.subjectDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_subject),
""
)
b.subjectDropdown += subjects.map { TextInputDropDown.Item(it.id, it.longName, tag = it) }
// get the teacher list
val teachers = app.db.teacherDao().getAllNow(profileId)
b.teacherDropdown.clear()
b.teacherDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_teacher),
""
)
b.teacherDropdown += teachers.map { TextInputDropDown.Item(it.id, it.fullName, tag = it) }
// get the event type list
val eventTypes = app.db.eventTypeDao().getAllNow(profileId)
b.typeDropdown.clear()
b.typeDropdown += eventTypes.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
} }
} deferred.await()
loadLists() b.teamDropdown.isEnabled = true
}} b.subjectDropdown.isEnabled = true
b.teacherDropdown.isEnabled = true
b.typeDropdown.isEnabled = true
private fun loadLists() { launch { b.typeDropdown.selected?.let { item ->
val deferred = async(Dispatchers.Default) {
// get the team list
val teams = app.db.teamDao().getAllNow(profileId)
b.teamDropdown.clear()
b.teamDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_team),
""
)
b.teamDropdown += teams.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
// get the subject list
val subjects = app.db.subjectDao().getAllNow(profileId)
b.subjectDropdown.clear()
b.subjectDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_subject),
""
)
b.subjectDropdown += subjects.map { TextInputDropDown.Item(it.id, it.longName, tag = it) }
// get the teacher list
val teachers = app.db.teacherDao().getAllNow(profileId)
b.teacherDropdown.clear()
b.teacherDropdown += TextInputDropDown.Item(
-1,
activity.getString(R.string.dialog_event_manual_no_teacher),
""
)
b.teacherDropdown += teachers.map { TextInputDropDown.Item(it.id, it.fullName, tag = it) }
// get the event type list
val eventTypes = app.db.eventTypeDao().getAllNow(profileId)
b.typeDropdown.clear()
b.typeDropdown += eventTypes.map { TextInputDropDown.Item(it.id, it.name, tag = it) }
}
deferred.await()
b.teamDropdown.isEnabled = true
b.subjectDropdown.isEnabled = true
b.teacherDropdown.isEnabled = true
b.typeDropdown.isEnabled = true
b.typeDropdown.selected?.let { item ->
customColor = (item.tag as EventType).color
}
// copy IDs from event being edited
editingEvent?.let {
b.teamDropdown.select(it.teamId)
b.subjectDropdown.select(it.subjectId)
b.teacherDropdown.select(it.teacherId)
b.typeDropdown.select(it.type)?.let { item ->
customColor = (item.tag as EventType).color customColor = (item.tag as EventType).color
} }
if (it.color != -1)
customColor = it.color
}
// copy IDs from the LessonFull // copy IDs from event being edited
defaultLesson?.let { editingEvent?.let {
b.teamDropdown.select(it.displayTeamId) b.teamDropdown.select(it.teamId)
b.subjectDropdown.select(it.displaySubjectId) b.subjectDropdown.select(it.subjectId)
b.teacherDropdown.select(it.displayTeacherId) b.teacherDropdown.select(it.teacherId)
} b.typeDropdown.select(it.type)?.let { item ->
customColor = (item.tag as EventType).color
}
if (it.color != -1)
customColor = it.color
}
b.typeDropdown.setOnChangeListener { // copy IDs from the LessonFull
b.typeDropdown.background.colorFilter = PorterDuffColorFilter((it.tag as EventType).color, PorterDuff.Mode.SRC_ATOP) defaultLesson?.let {
customColor = null b.teamDropdown.select(it.displayTeamId)
return@setOnChangeListener true b.subjectDropdown.select(it.displaySubjectId)
} b.teacherDropdown.select(it.displayTeacherId)
customColor?.let { }
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP)
}
b.typeColor.onClick {
val currentColor = (b.typeDropdown?.selected?.tag as EventType?)?.color ?: Event.COLOR_DEFAULT
val colorPickerDialog = ColorPickerDialog.newBuilder()
.setColor(currentColor)
.create()
colorPickerDialog.setColorPickerDialogListener(
object : ColorPickerDialogListener {
override fun onDialogDismissed(dialogId: Int) {}
override fun onColorSelected(dialogId: Int, color: Int) {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
customColor = color
}
})
colorPickerDialog.show(activity.fragmentManager, "color-picker-dialog")
}
loadDates() b.typeDropdown.setOnChangeListener {
}} b.typeDropdown.background.colorFilter = PorterDuffColorFilter((it.tag as EventType).color, PorterDuff.Mode.SRC_ATOP)
customColor = null
return@setOnChangeListener true
}
customColor?.let {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(it, PorterDuff.Mode.SRC_ATOP)
}
b.typeColor.onClick {
val currentColor = (b.typeDropdown?.selected?.tag as EventType?)?.color
?: Event.COLOR_DEFAULT
val colorPickerDialog = ColorPickerDialog.newBuilder()
.setColor(currentColor)
.create()
colorPickerDialog.setColorPickerDialogListener(
object : ColorPickerDialogListener {
override fun onDialogDismissed(dialogId: Int) {}
override fun onColorSelected(dialogId: Int, color: Int) {
b.typeDropdown.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)
customColor = color
}
})
colorPickerDialog.show(activity.fragmentManager, "color-picker-dialog")
}
private fun loadDates() { launch { loadDates()
val date = Date.getToday() }
val today = date.value }
var weekDay = date.weekDay
val deferred = async(Dispatchers.Default) { private fun loadDates() {
val dates = mutableListOf<TextInputDropDown.Item>() launch {
// item choosing the next lesson of specific subject val date = Date.getToday()
b.subjectDropdown.selected?.let { val today = date.value
if (it.tag is Subject) { var weekDay = date.weekDay
val deferred = async(Dispatchers.Default) {
val dates = mutableListOf<TextInputDropDown.Item>()
// item choosing the next lesson of specific subject
b.subjectDropdown.selected?.let {
if (it.tag is Subject) {
dates += TextInputDropDown.Item(
-it.id,
activity.getString(R.string.dialog_event_manual_date_next_lesson, it.tag.longName)
)
}
}
// TODAY
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_today, date.formattedString),
tag = date.clone()
)
// TOMORROW
if (weekDay < 4) {
date.stepForward(0, 0, 1)
weekDay++
dates += TextInputDropDown.Item( dates += TextInputDropDown.Item(
-it.id, date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_next_lesson, it.tag.longName) activity.getString(R.string.dialog_event_manual_date_tomorrow, date.formattedString),
tag = date.clone()
) )
} }
} // REMAINING SCHOOL DAYS OF THE CURRENT WEEK
while (weekDay < 4) {
// TODAY date.stepForward(0, 0, 1) // step one day forward
dates += TextInputDropDown.Item( weekDay++
date.value.toLong(), dates += TextInputDropDown.Item(
activity.getString(R.string.dialog_event_manual_date_today, date.formattedString), date.value.toLong(),
tag = date.clone() activity.getString(R.string.dialog_event_manual_date_this_week, Week.getFullDayName(weekDay), date.formattedString),
) tag = date.clone()
)
// TOMORROW
if (weekDay < 4) {
date.stepForward(0, 0, 1)
weekDay++
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_tomorrow, date.formattedString),
tag = date.clone()
)
}
// REMAINING SCHOOL DAYS OF THE CURRENT WEEK
while (weekDay < 4) {
date.stepForward(0, 0, 1) // step one day forward
weekDay++
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_this_week, Week.getFullDayName(weekDay), date.formattedString),
tag = date.clone()
)
}
// go to next week Monday
date.stepForward(0, 0, -weekDay + 7)
weekDay = 0
// ALL SCHOOL DAYS OF THE NEXT WEEK
while (weekDay < 4) {
dates += TextInputDropDown.Item(
date.value.toLong(),
activity.getString(R.string.dialog_event_manual_date_next_week, Week.getFullDayName(weekDay), date.formattedString),
tag = date.clone()
)
date.stepForward(0, 0, 1) // step one day forward
weekDay++
}
dates += TextInputDropDown.Item(
-1L,
activity.getString(R.string.dialog_event_manual_date_other)
)
dates
}
val dates = deferred.await()
b.dateDropdown.clear().append(dates)
editingEvent?.eventDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
defaultLesson?.displayDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
if (b.dateDropdown.selected == null) {
b.dateDropdown.select(today.toLong())
}
b.dateDropdown.isEnabled = true
b.dateDropdown.setOnChangeListener { item ->
when {
// next lesson with specified subject
item.id < -1 -> {
// TODO include lesson team in search
app.db.timetableDao().getNextWithSubject(profileId, Date.getToday(), -item.id).observeOnce(activity, Observer {
val lessonDate = it?.displayDate ?: return@Observer
b.dateDropdown.select(TextInputDropDown.Item(
lessonDate.value.toLong(),
lessonDate.formattedString,
tag = lessonDate
))
b.teamDropdown.select(it.displayTeamId)
b.subjectDropdown.select(it.displaySubjectId)
b.teacherDropdown.select(it.displayTeacherId)
defaultLoaded = false
loadHours(it.displayStartTime)
})
return@setOnChangeListener false
} }
// custom date // go to next week Monday
item.id == -1L -> { date.stepForward(0, 0, -weekDay + 7)
MaterialDatePicker.Builder weekDay = 0
.datePicker() // ALL SCHOOL DAYS OF THE NEXT WEEK
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) } ?: Date.getToday()).inMillis) while (weekDay < 4) {
.build() dates += TextInputDropDown.Item(
.apply { date.value.toLong(),
addOnPositiveButtonClickListener { activity.getString(R.string.dialog_event_manual_date_next_week, Week.getFullDayName(weekDay), date.formattedString),
val dateSelected = Date.fromMillis(it) tag = date.clone()
b.dateDropdown.select(TextInputDropDown.Item( )
dateSelected.value.toLong(), date.stepForward(0, 0, 1) // step one day forward
dateSelected.formattedString, weekDay++
tag = dateSelected }
)) dates += TextInputDropDown.Item(
loadHours() -1L,
activity.getString(R.string.dialog_event_manual_date_other)
)
dates
}
val dates = deferred.await()
b.dateDropdown.clear().append(dates)
editingEvent?.eventDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
defaultLesson?.displayDate?.let {
b.dateDropdown.select(TextInputDropDown.Item(
it.value.toLong(),
it.formattedString,
tag = it
))
}
if (b.dateDropdown.selected == null) {
b.dateDropdown.select(today.toLong())
}
b.dateDropdown.isEnabled = true
b.dateDropdown.setOnChangeListener { item ->
when {
// next lesson with specified subject
item.id < -1 -> {
val teamId = defaultLesson?.teamId ?: -1
val selectedLessonDate = defaultLesson?.date ?: Date.getToday()
when (teamId) {
-1L -> app.db.timetableDao().getNextWithSubject(profileId, selectedLessonDate, -item.id)
else -> app.db.timetableDao().getNextWithSubjectAndTeam(profileId, selectedLessonDate, -item.id, teamId)
}.observeOnce(activity, Observer {
val lessonDate = it?.displayDate ?: return@Observer
b.dateDropdown.select(TextInputDropDown.Item(
lessonDate.value.toLong(),
lessonDate.formattedString,
tag = lessonDate
))
b.teamDropdown.select(it.displayTeamId)
b.subjectDropdown.select(it.displaySubjectId)
b.teacherDropdown.select(it.displayTeacherId)
defaultLoaded = false
loadHours(it.displayStartTime)
})
return@setOnChangeListener false
}
// custom date
item.id == -1L -> {
MaterialDatePicker.Builder
.datePicker()
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) }
?: Date.getToday()).inMillis)
.build()
.apply {
addOnPositiveButtonClickListener {
val dateSelected = Date.fromMillis(it)
b.dateDropdown.select(TextInputDropDown.Item(
dateSelected.value.toLong(),
dateSelected.formattedString,
tag = dateSelected
))
loadHours()
}
show(this@EventManualV2Dialog.activity.supportFragmentManager, "MaterialDatePicker")
} }
show(this@EventManualV2Dialog.activity.supportFragmentManager, "MaterialDatePicker")
}
return@setOnChangeListener false return@setOnChangeListener false
} }
// a specific date // a specific date
else -> { else -> {
b.dateDropdown.select(item) b.dateDropdown.select(item)
loadHours() loadHours()
}
} }
return@setOnChangeListener true
} }
return@setOnChangeListener true
}
loadHours() loadHours()
}} }
}
private fun loadHours(defaultHour: Time? = null) { private fun loadHours(defaultHour: Time? = null) {
b.timeDropdown.isEnabled = false b.timeDropdown.isEnabled = false
@ -391,8 +405,7 @@ class EventManualV2Dialog(
// clear subject, teacher selection // clear subject, teacher selection
b.subjectDropdown.deselect() b.subjectDropdown.deselect()
b.teacherDropdown.deselect() b.teacherDropdown.deselect()
} } else {
else {
editingEvent?.let { editingEvent?.let {
b.timeDropdown.select(it.startTime?.value?.toLong()) b.timeDropdown.select(it.startTime?.value?.toLong())
} }
@ -429,7 +442,7 @@ class EventManualV2Dialog(
b.teamDropdown.deselect() b.teamDropdown.deselect()
b.subjectDropdown.deselect() b.subjectDropdown.deselect()
b.teacherDropdown.deselect() b.teacherDropdown.deselect()
item.tag.displayTeamId?.let { item.tag.displayTeamId?.let {
b.teamDropdown.select(it) b.teamDropdown.select(it)
} }
item.tag.displaySubjectId?.let { item.tag.displaySubjectId?.let {
@ -449,4 +462,4 @@ class EventManualV2Dialog(
private fun saveEvent() { private fun saveEvent() {
} }
} }