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,15 +74,14 @@ 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 { faBtnAddLesson.setOnClickListener {
//initAddLessonDialog().show()
AddLessonDialog(context!!).initAddLessonDialog().show() AddLessonDialog(context!!).initAddLessonDialog().show()
} }
@ -97,25 +96,23 @@ 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) {
for (dayIndex in startDayIndex..dayEndIndex) {
val dayCardView = DayCardView(context!!) val dayCardView = DayCardView(context!!)
// some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week) // some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
@ -128,7 +125,6 @@ class TimeTableFragment : Fragment() {
} }
} }
}
private fun updateTimetableScreen() = GlobalScope.launch(Dispatchers.Default) { private fun updateTimetableScreen() = GlobalScope.launch(Dispatchers.Default) {
// update the cache // update the cache
@ -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) { }