TimeTableFragment clean up

This commit is contained in:
Jannik 2020-06-05 20:56:37 +02:00
parent 72e9efb9e7
commit 948f330ebe
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 40 additions and 46 deletions

View File

@ -60,7 +60,7 @@ class TimeTableFragment : Fragment() {
initActions() initActions()
if (timetables[0].timetable.days.isNotEmpty() && timetables[1].timetable.days.isNotEmpty()) { if (timetables[0].timetable.days.isNotEmpty() && timetables[1].timetable.days.isNotEmpty()) {
addInitWeeks() initTimetable()
} else { } else {
MaterialDialog(context!!) MaterialDialog(context!!)
.title(R.string.error) .title(R.string.error)
@ -74,27 +74,24 @@ class TimeTableFragment : Fragment() {
/** /**
* initialize the actions * initialize the actions
*/ */
private fun initActions() = GlobalScope.launch(Dispatchers.Default) { private fun initActions() = GlobalScope.launch(Dispatchers.Main) {
withContext(Dispatchers.Main) { refreshLayout_Timetable.setOnRefreshListener {
refreshLayout_Timetable.setOnRefreshListener { updateTimetableScreen()
updateTimetableScreen() }
// show the AddLessonDialog if the ftaBtn is clicked
faBtnAddLesson.setOnClickListener {
AddLessonDialog(context!!).initAddLessonDialog().show()
}
// hide the btnCardValue if the user is scrolling down
scrollViewTimetable.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
if (scrollY > oldScrollY) {
faBtnAddLesson.hide()
} else {
faBtnAddLesson.show()
} }
faBtnAddLesson.setOnClickListener {
//initAddLessonDialog().show()
AddLessonDialog(context!!).initAddLessonDialog().show()
}
// hide the btnCardValue if the user is scrolling down
scrollViewTimetable.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
if (scrollY > oldScrollY) {
faBtnAddLesson.hide()
} else {
faBtnAddLesson.show()
}
}
} }
} }
@ -102,31 +99,30 @@ class TimeTableFragment : Fragment() {
/** /**
* add the current and next weeks lessons * add the current and next weeks lessons
*/ */
private fun addInitWeeks() = GlobalScope.launch(Dispatchers.Default) { private fun initTimetable() = GlobalScope.launch(Dispatchers.Default) {
val currentDayIndex = NotRetardedCalendar.getDayOfWeekIndex() val currentDayIndex = NotRetardedCalendar.getDayOfWeekIndex()
addWeek(currentDayIndex, 5, 0).join() // add current week addTimetableWeek(currentDayIndex, 5, 0).join() // add current week
addWeek(0, currentDayIndex - 1, 1) // add next week addTimetableWeek(0, currentDayIndex - 1, 1) // add next week
// TODO add additional lessons
} }
private fun addWeek(startDayIndex: Int, dayEndIndex: Int, weekIndex: Int) = GlobalScope.launch(Dispatchers.Default) { private fun addTimetableWeek(dayBegin: Int, dayEnd: Int, week: Int) = GlobalScope.launch(Dispatchers.Main) {
val timetable = timetables[weekIndex].timetable val timetable = timetables[week].timetable
val timetableMeta = timetables[weekIndex].meta val timetableMeta = timetables[week].meta
withContext(Dispatchers.Main) { for (dayIndex in dayBegin..dayEnd) {
val dayCardView = DayCardView(context!!)
for (dayIndex in startDayIndex..dayEndIndex) { // some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
val dayCardView = DayCardView(context!!) val daysToAdd = (timetableMeta.weekNumberYear - NotRetardedCalendar.getWeekOfYear()) * 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex())
dayCardView.addTimetableDay(timetable.days[dayIndex], daysToAdd)
// some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week) // if there are no lessons don't show the dayCardView
val daysToAdd =(timetableMeta.weekNumberYear - NotRetardedCalendar.getWeekOfYear()) * 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex()) if (dayCardView.getLinLayoutDay().childCount > 1)
dayCardView.addTimetableDay(timetable.days[dayIndex], daysToAdd) linLayout_Timetable.addView(dayCardView)
// if there are no lessons don't show the dayCardView
if (dayCardView.getLinLayoutDay().childCount > 1)
linLayout_Timetable.addView(dayCardView)
}
} }
} }
@ -148,11 +144,8 @@ class TimeTableFragment : Fragment() {
// add the refreshed timetables // add the refreshed timetables
val dayIndex = NotRetardedCalendar.getDayOfWeekIndex() val dayIndex = NotRetardedCalendar.getDayOfWeekIndex()
// add current week addTimetableWeek(dayIndex, 5, 0).join() // add current week
addWeek(dayIndex, 5, 0).join() addTimetableWeek(0, dayIndex - 1, 1) // add next week
// add next week
addWeek(0, dayIndex - 1, 1)
refreshLayout_Timetable.isRefreshing = false refreshLayout_Timetable.isRefreshing = false
} }

View File

@ -65,13 +65,14 @@ class AddLessonDialog(_context: Context) {
.customView(R.layout.dialog_add_lesson) .customView(R.layout.dialog_add_lesson)
.setPeekHeight(900) .setPeekHeight(900)
.positiveButton(R.string.add) { .positiveButton(R.string.add) {
println("add lesson \"$selectedCourse: $selectedSubject\"") val lessons = runBlocking {
// TODO call add function
val test = runBlocking {
TCoRAPIController.getLessonsAsync(selectedCourse, selectedSubject, 0).await() TCoRAPIController.getLessonsAsync(selectedCourse, selectedSubject, 0).await()
} }
println("add lesson \"$selectedCourse: $selectedSubject\"")
println(lessons.toString())
println(test.toString()) // TODO save lesson
// TODO refresh timetable (add a function to show additional lessons)
} }
.negativeButton(R.string.cancel) { } .negativeButton(R.string.cancel) { }