2018-11-06 16:17:41 +01:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < seil0 @mosad . xyz >
2018-11-06 16:17:41 +01:00
*
* 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 .
*
* /
2018-10-27 14:11:47 +02:00
2018-11-06 16:17:41 +01:00
package org.mosad.seil0.projectlaogai.fragments
2018-10-27 14:11:47 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-02-17 15:05:03 +01:00
import androidx.fragment.app.Fragment
2019-03-20 21:33:55 +01:00
import kotlinx.android.synthetic.main.fragment_timetable.*
2018-11-06 16:17:41 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
2018-11-06 16:17:41 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
2018-11-11 21:12:28 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
2019-03-20 21:33:55 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.*
2018-11-06 18:17:49 +01:00
import java.text.SimpleDateFormat
2018-11-06 16:17:41 +01:00
import java.util.*
2018-10-27 14:11:47 +02:00
/ * *
* 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 ? {
2019-03-20 17:55:30 +01:00
val view : View = inflater . inflate ( R . layout . fragment _timetable , container , false )
2018-10-27 14:11:47 +02:00
2019-03-20 21:33:55 +01:00
if ( timetables [ 0 ] . days . isNotEmpty ( ) && timetables [ 1 ] . days . isNotEmpty ( ) ) {
addWeeks ( )
2018-11-26 11:17:07 +01:00
} else {
// TODO show card with error msg
}
2018-10-27 14:11:47 +02:00
return view
}
2018-11-06 18:17:49 +01:00
/ * *
2019-03-20 21:33:55 +01:00
* add the all days with at least one lesson to the timetable screen
2018-11-06 18:17:49 +01:00
* /
2019-03-23 21:07:33 +01:00
private fun addWeeks ( ) = doAsync {
2018-11-11 21:12:28 +01:00
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
2018-11-12 12:32:42 +01:00
val formatter = SimpleDateFormat ( " E dd.MM " , Locale . GERMANY ) // TODO change to android call when min api is 24
val calendar = Calendar . getInstance ( )
2018-11-11 21:12:28 +01:00
2019-03-23 21:07:33 +01:00
uiThread {
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
// add the current week
for ( day in dayIndex .. 5 ) {
var helpLesson = LessonLinearLayout ( context )
val dayCardView = DayCardView ( context !! )
dayCardView . setDayHeading ( formatter . format ( calendar . time ) )
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
// for each timeslot of the day
for ( ( tsIndex , timeslot ) in timetables [ 0 ] . days [ day ] . timeslots . withIndex ( ) ) {
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
for ( lesson in timeslot ) {
2019-03-01 18:59:17 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
val lessonLayout = LessonLinearLayout ( context )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
2019-03-01 18:59:17 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson != timeslot . last ( ) )
lessonLayout . disableDivider ( )
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
helpLesson = lessonLayout
2019-03-20 21:33:55 +01:00
}
2018-11-06 16:17:41 +01:00
}
}
2019-03-23 21:07:33 +01:00
helpLesson . disableDivider ( )
calendar . add ( Calendar . DATE , 1 )
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
// if there are no lessons don't show the dayCardView
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 )
linLayout _Timetable . addView ( dayCardView )
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
}
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
calendar . add ( Calendar . DATE , 1 ) // before this we are at a sunday (no lecture on sundays!)
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
// add the next week
for ( day in 0. . ( dayIndex - 1 ) ) {
var helpLesson = LessonLinearLayout ( context !! )
val dayCardView = DayCardView ( context !! )
dayCardView . setDayHeading ( formatter . format ( calendar . time ) )
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
// for each timeslot of the day
for ( ( tsIndex , timeslot ) in timetables [ 1 ] . days [ day ] . timeslots . withIndex ( ) ) {
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
for ( lesson in timeslot ) {
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
val lessonLayout = LessonLinearLayout ( context !! )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson != timeslot . last ( ) )
lessonLayout . disableDivider ( )
2019-03-20 21:33:55 +01:00
2019-03-23 21:07:33 +01:00
helpLesson = lessonLayout
}
2018-11-12 12:32:42 +01:00
}
}
2019-03-23 21:07:33 +01:00
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 )
2018-11-06 16:17:41 +01:00
}
}
2019-03-23 21:07:33 +01:00
2018-11-06 16:17:41 +01:00
}
2019-03-20 21:33:55 +01:00
2018-10-27 14:11:47 +02:00
}