From 948f330ebeb30dc69857af9e048d69b2298dd97a Mon Sep 17 00:00:00 2001 From: Seil0 Date: Fri, 5 Jun 2020 20:56:37 +0200 Subject: [PATCH] TimeTableFragment clean up --- .../fragments/TimeTableFragment.kt | 77 +++++++++---------- .../uicomponents/AddLessonDialog.kt | 9 ++- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt b/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt index 39a059d..9ddc3af 100644 --- a/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt +++ b/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt @@ -60,7 +60,7 @@ class TimeTableFragment : Fragment() { initActions() if (timetables[0].timetable.days.isNotEmpty() && timetables[1].timetable.days.isNotEmpty()) { - addInitWeeks() + initTimetable() } else { MaterialDialog(context!!) .title(R.string.error) @@ -74,27 +74,24 @@ class TimeTableFragment : Fragment() { /** * initialize the actions */ - private fun initActions() = GlobalScope.launch(Dispatchers.Default) { + private fun initActions() = GlobalScope.launch(Dispatchers.Main) { - withContext(Dispatchers.Main) { - refreshLayout_Timetable.setOnRefreshListener { - updateTimetableScreen() + refreshLayout_Timetable.setOnRefreshListener { + 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 */ - private fun addInitWeeks() = GlobalScope.launch(Dispatchers.Default) { + private fun initTimetable() = GlobalScope.launch(Dispatchers.Default) { val currentDayIndex = NotRetardedCalendar.getDayOfWeekIndex() - addWeek(currentDayIndex, 5, 0).join() // add current week - addWeek(0, currentDayIndex - 1, 1) // add next week + addTimetableWeek(currentDayIndex, 5, 0).join() // add current 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) { - val timetable = timetables[weekIndex].timetable - val timetableMeta = timetables[weekIndex].meta + private fun addTimetableWeek(dayBegin: Int, dayEnd: Int, week: Int) = GlobalScope.launch(Dispatchers.Main) { + val timetable = timetables[week].timetable + val timetableMeta = timetables[week].meta - withContext(Dispatchers.Main) { + for (dayIndex in dayBegin..dayEnd) { + val dayCardView = DayCardView(context!!) - for (dayIndex in startDayIndex..dayEndIndex) { - 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) + 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) - val daysToAdd =(timetableMeta.weekNumberYear - NotRetardedCalendar.getWeekOfYear()) * 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex()) - dayCardView.addTimetableDay(timetable.days[dayIndex], daysToAdd) + // if there are no lessons don't show the dayCardView + if (dayCardView.getLinLayoutDay().childCount > 1) + 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 val dayIndex = NotRetardedCalendar.getDayOfWeekIndex() - // add current week - addWeek(dayIndex, 5, 0).join() - - // add next week - addWeek(0, dayIndex - 1, 1) + addTimetableWeek(dayIndex, 5, 0).join() // add current week + addTimetableWeek(0, dayIndex - 1, 1) // add next week refreshLayout_Timetable.isRefreshing = false } diff --git a/app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt b/app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt index 958713f..bf4d3c3 100644 --- a/app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt +++ b/app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt @@ -65,13 +65,14 @@ class AddLessonDialog(_context: Context) { .customView(R.layout.dialog_add_lesson) .setPeekHeight(900) .positiveButton(R.string.add) { - println("add lesson \"$selectedCourse: $selectedSubject\"") - // TODO call add function - val test = runBlocking { + val lessons = runBlocking { 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) { }