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
2018-11-06 16:17:41 +01:00
import android.graphics.Color
2018-10-27 14:11:47 +02:00
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-11-06 16:17:41 +01:00
import android.widget.LinearLayout
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.MainActivity
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
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
2018-11-06 16:17:41 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
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 ( ) {
2018-11-06 16:17:41 +01:00
private lateinit var linLayoutTTFragment : LinearLayout
2018-10-27 14:11:47 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _time _table , container , false )
2018-11-06 16:17:41 +01:00
linLayoutTTFragment = view . findViewById ( R . id . linLayout _TTFragment )
2019-02-14 16:25:13 +01:00
if ( MainActivity . cTimeTableCurrentWeek . isNotEmpty ( ) ) {
2018-11-26 11:17:07 +01:00
addCurrentWeek ( )
} 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
/ * *
* add the remaining days of the current week to the timetable screen
* /
2018-11-06 16:17:41 +01:00
private fun addCurrentWeek ( ) {
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
2018-11-06 16:17:41 +01:00
doAsync {
uiThread {
2018-11-12 12:32:42 +01:00
// add current weeks days
2018-11-11 21:12:28 +01:00
for ( day in dayIndex .. 5 ) {
2018-11-06 16:17:41 +01:00
val cardViewTimeTableDay = MensaDayCardView ( context !! , null )
2018-11-06 18:17:49 +01:00
cardViewTimeTableDay . setDayHeading ( formatter . format ( calendar . time ) )
2018-11-06 16:17:41 +01:00
// for each lessen of the day
2019-02-14 16:25:13 +01:00
for ( ( i , lesson ) in MainActivity . cTimeTableCurrentWeek [ day ] . withIndex ( ) ) {
2018-11-06 16:17:41 +01:00
val lessonCardView = LessonCardView ( context !! , null )
lessonCardView . setBackgroundColor ( Color . TRANSPARENT )
lessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . string _new _line , lesson . lessonSubject )
lessonCardView . getTxtViewLesson ( ) . append ( lesson . lessonTeacher + " \n " )
lessonCardView . getTxtViewLesson ( ) . append ( lesson . lessonRoom )
lessonCardView . getTxtViewTime ( ) . text = DataTypes ( ) . getTime ( ) [ i ]
2019-01-03 01:45:28 +01:00
// only add the lesson if it contains data
2018-11-06 16:17:41 +01:00
if ( lessonCardView . getTxtViewLesson ( ) . text . length > 2 )
cardViewTimeTableDay . getLinLayoutMensaDay ( ) . addView ( lessonCardView )
}
2018-11-12 12:32:42 +01:00
calendar . add ( Calendar . DATE , 1 )
2019-01-03 01:45:28 +01:00
// if the day contains no lessons add a text "No lesson today"
if ( cardViewTimeTableDay . getLinLayoutMensaDay ( ) . childCount <= 1 ) {
val lessonCardView = LessonCardView ( context !! , null )
lessonCardView . setBackgroundColor ( Color . TRANSPARENT )
lessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . no _lesson _today )
cardViewTimeTableDay . getLinLayoutMensaDay ( ) . addView ( lessonCardView )
}
2018-11-06 16:17:41 +01:00
linLayoutTTFragment . addView ( cardViewTimeTableDay )
}
2019-01-03 13:46:09 +01:00
// TODO if there is no lesson at one day , show a no lesson card
2018-11-12 12:32:42 +01:00
// add next weeks days, max number = dayIndex, if timetable was loaded
2019-02-14 16:25:13 +01:00
if ( MainActivity . cTimeTableNextWeek . isNotEmpty ( ) ) {
2018-11-12 12:32:42 +01:00
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 lessen of the day
2019-02-14 16:25:13 +01:00
for ( ( i , lesson ) in MainActivity . cTimeTableNextWeek [ day ] . withIndex ( ) ) {
2018-11-12 12:32:42 +01:00
val lessonCardView = LessonCardView ( context !! , null )
lessonCardView . setBackgroundColor ( Color . TRANSPARENT )
lessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . string _new _line , lesson . lessonSubject )
lessonCardView . getTxtViewLesson ( ) . append ( lesson . lessonTeacher + " \n " )
lessonCardView . getTxtViewLesson ( ) . append ( lesson . lessonRoom )
lessonCardView . getTxtViewTime ( ) . text = DataTypes ( ) . getTime ( ) [ i ]
if ( lessonCardView . getTxtViewLesson ( ) . text . length > 2 )
cardViewTimeTableDay . getLinLayoutMensaDay ( ) . addView ( lessonCardView )
}
calendar . add ( Calendar . DATE , 1 )
linLayoutTTFragment . addView ( cardViewTimeTableDay )
}
}
2018-11-06 16:17:41 +01:00
}
}
}
2018-10-27 14:11:47 +02:00
}