ProjectLaogai/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/TimeTableFragment.kt

145 lines
5.1 KiB
Kotlin

/**
* ProjectLaogai
*
* Copyright 2019 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.seil0.projectlaogai.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_timetable.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.*
import java.text.SimpleDateFormat
import java.util.*
/**
* The timetable controller class
* contains all needed parts to display and the timetable detail screen
*/
class TimeTableFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_timetable, container, false)
if (timetables[0].days.isNotEmpty() && timetables[1].days.isNotEmpty()) {
addWeeks()
} else {
// TODO show card with error msg
}
return view
}
/**
* add the all days with at least one lesson to the timetable screen
*/
private fun addWeeks() = doAsync {
val dayIndex = NotRetardedCalendar().getDayOfWeekIndex()
val formatter = SimpleDateFormat("E dd.MM", Locale.getDefault()) // TODO change to android call when min api is 24
val calendar = Calendar.getInstance()
uiThread {
// add the current week
for (day in dayIndex..5) {
var helpLesson = LessonLinearLayout(context)
val dayCardView = DayCardView(context!!)
dayCardView.setDayHeading(formatter.format(calendar.time))
// for each timeslot of the day
for ((tsIndex, timeslot) in timetables[0].days[day].timeslots.withIndex()) {
for(lesson in timeslot) {
if(lesson.lessonSubject.isNotEmpty()) {
val lessonLayout = LessonLinearLayout(context)
lessonLayout.setLesson(lesson, DataTypes().getTime()[tsIndex])
dayCardView.getLinLayoutDay().addView(lessonLayout)
if (lesson != timeslot.last())
lessonLayout.disableDivider()
helpLesson = lessonLayout
}
}
}
helpLesson.disableDivider()
calendar.add(Calendar.DATE, 1)
// if there are no lessons don't show the dayCardView
if (dayCardView.getLinLayoutDay().childCount > 1)
linLayout_Timetable.addView(dayCardView)
}
calendar.add(Calendar.DATE, 1) // before this we are at a sunday (no lecture on sundays!)
// add the next week
for (day in 0..(dayIndex - 1)) {
var helpLesson = LessonLinearLayout(context!!)
val dayCardView = DayCardView(context!!)
dayCardView.setDayHeading(formatter.format(calendar.time))
// for each timeslot of the day
for ((tsIndex, timeslot) in timetables[1].days[day].timeslots.withIndex()) {
for(lesson in timeslot) {
if(lesson.lessonSubject.isNotEmpty()) {
val lessonLayout = LessonLinearLayout(context!!)
lessonLayout.setLesson(lesson, DataTypes().getTime()[tsIndex])
dayCardView.getLinLayoutDay().addView(lessonLayout)
if (lesson != timeslot.last())
lessonLayout.disableDivider()
helpLesson = lessonLayout
}
}
}
helpLesson.disableDivider()
calendar.add(Calendar.DATE, 1)
// if there are no lessons don't show the dayCardView
if (dayCardView.getLinLayoutDay().childCount > 1)
linLayout_Timetable.addView(dayCardView)
}
}
}
}