/** * ProjectLaogai * * Copyright 2019 * * 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 android.widget.LinearLayout import androidx.fragment.app.Fragment 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.LessonCardView import org.mosad.seil0.projectlaogai.uicomponents.LessonTextView import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView 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() { private lateinit var linLayoutTTFragment: LinearLayout override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view: View = inflater.inflate(R.layout.fragment_time_table, container, false) linLayoutTTFragment = view.findViewById(R.id.linLayout_TTFragment) if (timetables[0].days.isNotEmpty()) { addCurrentWeek() } else { // TODO show card with error msg } return view } /** * add the remaining days of the current week to the timetable screen */ private fun addCurrentWeek() { val dayIndex = NotRetardedCalendar().getDayOfWeekIndex() val formatter = SimpleDateFormat("E dd.MM", Locale.GERMANY) // TODO change to android call when min api is 24 val calendar = Calendar.getInstance() doAsync { uiThread { // add current weeks days for (day in dayIndex..5) { val cardViewTimeTableDay = MensaDayCardView(context!!, null) cardViewTimeTableDay.setDayHeading(formatter.format(calendar.time)) // for each timeslot of the day for ((i, timeslot) in timetables[0].days[day].timeslots.withIndex()) { val lessonCardView = LessonCardView(context!!, null) lessonCardView.getTxtViewTime().text = DataTypes().getTime()[i] //println(timeslot) for (lesson in timeslot) { val lessonTxtView = LessonTextView(context!!) lessonTxtView.setLesson(lesson) // TODO why does this exist if (lessonTxtView.text.length > 3) lessonCardView.getLinLayoutLesson().addView(lessonTxtView) } // only add the lesson if it contains data if (lessonCardView.getLinLayoutLesson().childCount > 1) cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView) } calendar.add(Calendar.DATE, 1) // if the day contains no lessons add a text "No lesson today" if (cardViewTimeTableDay.getLinLayoutMensaDay().childCount <= 1) { val lessonTxtView = LessonTextView(context!!) lessonTxtView.setText(resources.getString(R.string.no_lesson_today)) val lessonCardView = LessonCardView(context!!, null) lessonCardView.getLinLayoutLesson().addView(lessonTxtView) cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView) } linLayoutTTFragment.addView(cardViewTimeTableDay) } // add next weeks days, max number = dayIndex, if timetable was loaded if (timetables[1].days.isNotEmpty()) { calendar.add(Calendar.DATE, 1) // before this we are at a sunday (no lecture on sundays!) for (day in 0..(dayIndex - 1)) { val cardViewTimeTableDay = MensaDayCardView(context!!, null) cardViewTimeTableDay.setDayHeading(formatter.format(calendar.time)) // for each timeslot of the day for ((i, timeslot) in timetables[1].days[day].timeslots.withIndex()) { val lessonCardView = LessonCardView(context!!, null) lessonCardView.getTxtViewTime().text = DataTypes().getTime()[i] for (lesson in timeslot) { val lessonTxtView = LessonTextView(context!!) lessonTxtView.setLesson(lesson) if (lessonTxtView.text.length > 3) lessonCardView.getLinLayoutLesson().addView(lessonTxtView) } // only add the lesson if it contains data if (lessonCardView.getLinLayoutLesson().childCount > 1) cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView) } calendar.add(Calendar.DATE, 1) // if the day contains no lessons add a text "No lesson today" if (cardViewTimeTableDay.getLinLayoutMensaDay().childCount <= 1) { val lessonTxtView = LessonTextView(context!!) lessonTxtView.setText(resources.getString(R.string.no_lesson_today)) val lessonCardView = LessonCardView(context!!, null) lessonCardView.getLinLayoutLesson().addView(lessonTxtView) cardViewTimeTableDay.getLinLayoutMensaDay().addView(lessonCardView) } linLayoutTTFragment.addView(cardViewTimeTableDay) } } } } } }