[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?>
@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
WHERE (type != 3 AND date >= :dateFrom AND date <= :dateTo) OR ((type = 3 OR type = 1) AND oldDate >= :dateFrom AND oldDate <= :dateTo)

View File

@ -57,7 +57,8 @@ class EventManualV2Dialog(
private lateinit var event: Event
private var customColor: Int? = null
init { run {
init {
run {
if (activity.isFinishing)
return@run
job = Job()
@ -88,7 +89,8 @@ class EventManualV2Dialog(
}*/
}
b.showMore.onClick { // TODO iconics is broken
b.showMore.onClick {
// TODO iconics is broken
it.apply {
refreshDrawableState()
@ -100,9 +102,11 @@ class EventManualV2Dialog(
}
loadLists()
}}
}
}
private fun loadLists() { launch {
private fun loadLists() {
launch {
val deferred = async(Dispatchers.Default) {
// get the team list
val teams = app.db.teamDao().getAllNow(profileId)
@ -178,7 +182,8 @@ class EventManualV2Dialog(
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 currentColor = (b.typeDropdown?.selected?.tag as EventType?)?.color
?: Event.COLOR_DEFAULT
val colorPickerDialog = ColorPickerDialog.newBuilder()
.setColor(currentColor)
.create()
@ -194,9 +199,11 @@ class EventManualV2Dialog(
}
loadDates()
}}
}
}
private fun loadDates() { launch {
private fun loadDates() {
launch {
val date = Date.getToday()
val today = date.value
var weekDay = date.weekDay
@ -289,8 +296,13 @@ class EventManualV2Dialog(
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 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(),
@ -309,7 +321,8 @@ class EventManualV2Dialog(
item.id == -1L -> {
MaterialDatePicker.Builder
.datePicker()
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) } ?: Date.getToday()).inMillis)
.setSelection((b.dateDropdown.selectedId?.let { Date.fromValue(it.toInt()) }
?: Date.getToday()).inMillis)
.build()
.apply {
addOnPositiveButtonClickListener {
@ -336,7 +349,8 @@ class EventManualV2Dialog(
}
loadHours()
}}
}
}
private fun loadHours(defaultHour: Time? = null) {
b.timeDropdown.isEnabled = false
@ -391,8 +405,7 @@ class EventManualV2Dialog(
// clear subject, teacher selection
b.subjectDropdown.deselect()
b.teacherDropdown.deselect()
}
else {
} else {
editingEvent?.let {
b.timeDropdown.select(it.startTime?.value?.toLong())
}