@ -40,6 +40,7 @@ import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timeta
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.hsoparser.TimetableDay
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.LessonLinearLayout
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
@ -52,13 +53,13 @@ import java.util.*
* /
class HomeFragment : Fragment ( ) {
val formatter = SimpleDateFormat ( " E dd.MM " , Locale . GERMANY )
private val formatter = SimpleDateFormat ( " E dd.MM " , Locale . getDefault ( ) )
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _home , container , false )
addMensaMenu ( )
addMensaMenu ( ) . get ( )
addTimeTable ( )
// Inflate the layout for this fragment
@ -68,56 +69,46 @@ class HomeFragment : Fragment() {
/ * *
* add the current mensa meal to the home screens
* /
private fun addMensaMenu ( ) {
doAsync {
private fun addMensaMenu ( ) = doAsync {
var dayMeals : ArrayList < Meal >
val cal = Calendar . getInstance ( )
val mensaCardView = DayCardView ( context !! )
var dayMeals : ArrayList < Meal >
val cal = Calendar . getInstance ( )
val mensaCardView = DayCardView ( context !! )
uiThread {
uiThread {
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) ] . meals
mensaCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( cal . time ) ) )
} else {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getTomorrowWeekIndex ( ) ] . meals
cal . add ( Calendar . DATE , 1 )
mensaCardView . setDayHeading ( resources . getString ( R . string . tomorrow _date , formatter . format ( cal . time ) ) )
}
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) ] . meals
mensaCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( cal . time ) ) )
} else {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getTomorrowWeekIndex ( ) ] . meals
cal . add ( Calendar . DATE , 1 )
mensaCardView . setDayHeading ( resources . getString ( R . string . tomorrow _date , formatter . format ( cal . time ) ) )
}
if ( dayMeals . size >= 2 ) {
// get the index of the first meal, not a "Schneller Teller"
loop @ for ( ( i , meal ) in dayMeals . withIndex ( ) ) {
if ( meal . heading . contains ( " Essen " ) ) {
if ( dayMeals . size >= 2 ) {
// get the index of the first meal, not a "Schneller Teller"
loop @ for ( ( i , meal ) in dayMeals . withIndex ( ) ) {
if ( meal . heading . contains ( " Essen " ) ) {
val meal1Layout = MealLinearLayout ( context )
meal1Layout . setMeal ( dayMeals [ i ] )
mensaCardView . getLinLayoutDay ( ) . addView ( meal1Layout )
val meal1Layout = MealLinearLayout ( context )
meal1Layout . setMeal ( dayMeals [ i ] )
mensaCardView . getLinLayoutDay ( ) . addView ( meal1Layout )
val meal2Layout = MealLinearLayout ( context )
meal2Layout . setMeal ( dayMeals [ i + 1 ] )
meal2Layout . disableDivider ( )
mensaCardView . getLinLayoutDay ( ) . addView ( meal2Layout )
val meal2Layout = MealLinearLayout ( context )
meal2Layout . setMeal ( dayMeals [ i + 1 ] )
meal2Layout . disableDivider ( )
mensaCardView . getLinLayoutDay ( ) . addView ( meal2Layout )
break @loop
}
break @loop
}
} else {
val noFood = TextView ( context )
noFood . text = resources . getString ( R . string . mensa _closed )
noFood . setTextColor ( ContextCompat . getColor ( context !! , R . color . textPrimary ) )
noFood . textSize = 18.0F
noFood . setTypeface ( null , Typeface . BOLD )
noFood . textAlignment = View . TEXT _ALIGNMENT _CENTER
noFood . setPadding ( 7 , 7 , 7 , 7 )
mensaCardView . getLinLayoutDay ( ) . addView ( noFood )
}
linLayout _Home . addView ( mensaCardView )
} else {
mensaCardView . getLinLayoutDay ( ) . addView ( getNoCard ( resources . getString ( R . string . mensa _closed ) ) )
}
linLayout _Home . addView ( mensaCardView )
}
}
@ -125,67 +116,104 @@ class HomeFragment : Fragment() {
/ * *
* add the current timetable to the home screen
* /
private fun addTimeTable ( ) {
private fun addTimeTable ( ) = doAsync {
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
var helpLesson = LessonLinearLayout ( context !! )
val dayCardView = DayCardView ( context !! )
dayCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( Calendar . getInstance ( ) . time ) ) )
val cal = Calendar . getInstance ( )
var dayCardView = DayCardView ( context !! )
uiThread {
if ( timetables . isNotEmpty ( ) && dayIndex < 6 ) {
// first check the current day
dayCardView = addDayTimetable ( timetables [ 0 ] . days [ dayIndex ] )
dayCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( cal . time ) ) )
// if there are no lessons don't show the dayCardView
if ( dayCardView . getLinLayoutDay ( ) . childCount <= 1 ) {
// get next day with at least one lecture
var dayTimetable : TimetableDay ? = null
var weekIndex = 0
var dayIndexSearch = dayIndex + 1
loop @ while ( dayTimetable == null && weekIndex <= timetables . size ) {
for ( i in ( dayIndexSearch ) .. 5 ) {
dayTimetable = timetables [ weekIndex ] . days [ i ]
cal . add ( Calendar . DATE , 1 )
// add the timetable to the card, if it contains at least one lesson break!
dayCardView = addDayTimetable ( dayTimetable )
dayCardView . setDayHeading ( formatter . format ( cal . time ) )
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 )
break @loop
}
dayIndexSearch = 0
weekIndex ++
cal . add ( Calendar . DATE , 1 )
}
}
doAsync {
uiThread {
if ( dayCardView . getLinLayoutDay ( ) . childCount <= 1 )
dayCardView . getLinLayoutDay ( ) . addView ( getNoCard ( resources . getString ( R . string . no _lesson _today ) ) ) // if there is no lecture at all show the no lesson card
if ( timetables [ 0 ] . days . isNotEmpty ( ) && dayIndex < 6 ) {
linLayout _Home . addView ( dayCardView )
} else {
if ( dayIndex == 6 ) {
// TODO iss this necessary?
// if that's the case it's sunday
dayCardView . getLinLayoutDay ( ) . addView ( getNoCard ( resources . getString ( R . string . no _lesson _today ) ) )
linLayout _Home . addView ( dayCardView )
} else {
MaterialDialog ( context !! )
. title ( R . string . error )
. message ( R . string . gen _tt _error )
. show ( )
// TODO log the error and send feedback
}
// for all timeslots of the day
for ( ( tsIndex , timeslot ) in timetables [ 0 ] . days [ dayIndex ] . 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 ( )
}
private fun addDayTimetable ( dayTimetable : TimetableDay ) : DayCardView {
var helpLesson = LessonLinearLayout ( context )
val dayCardView = DayCardView ( context !! )
helpLesson = lessonLayout
}
}
}
for ( ( tsIndex , timeslot ) in dayTimetable . timeslots . withIndex ( ) ) {
helpLesson . disableDivider ( )
// if there are no lessons don't show the dayCardView
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 ) {
linLayout _Home . addView ( dayCardView )
} else {
// TODO we could display the next day with a lecture
val noLesson = DayCardView ( context !! )
noLesson . setDayHeading ( resources . getString ( R . string . no _lesson _today ) )
for ( lesson in timeslot ) {
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
linLayout _Home . addView ( noLesson )
}
} else {
if ( dayIndex == 6 ) {
// if that's the case it's sunday
val noLesson = DayCardView ( context !! )
noLesson . setDayHeading ( resources . getString ( R . string . no _lesson _today ) )
linLayout _Home . addView ( noLesson )
} else {
MaterialDialog ( context !! )
. title ( R . string . error )
. message ( R . string . gen _tt _error )
. show ( )
// TODO log the error and send feedback
val lessonLayout = LessonLinearLayout ( context )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
if ( lesson != timeslot . last ( ) ) {
lessonLayout . disableDivider ( )
}
helpLesson = lessonLayout
}
}
}
helpLesson . disableDivider ( )
return dayCardView
}
private fun getNoCard ( text : String ) : TextView {
val noLesson = TextView ( context )
noLesson . text = text
noLesson . setTextColor ( ContextCompat . getColor ( context !! , R . color . textPrimary ) )
noLesson . textSize = 18.0F
noLesson . setTypeface ( null , Typeface . BOLD )
noLesson . textAlignment = View . TEXT _ALIGNMENT _CENTER
noLesson . setPadding ( 7 , 7 , 7 , 7 )
return noLesson
}
}