@ -22,11 +22,13 @@
package org.mosad.seil0.projectlaogai.fragments
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import kotlinx.android.synthetic.main.fragment_home.*
@ -40,8 +42,9 @@ import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.LessonLinearLayout
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
/ * *
* The " home " controller class
@ -49,18 +52,14 @@ import kotlin.collections.ArrayList
* /
class HomeFragment : Fragment ( ) {
private lateinit var linLayoutTimeTable : LinearLayout
val formatter = SimpleDateFormat ( " E dd.MM " , Locale . GERMANY )
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _home , container , false )
// init UI elements
linLayoutTimeTable = view . findViewById ( R . id . linLayoutTimeTable )
//setText()
addCurrentMensaMenu ( )
addCurrentTimeTable ( )
addMensaMenu ( )
addTimeTable ( )
// Inflate the layout for this fragment
return view
@ -69,54 +68,55 @@ class HomeFragment : Fragment() {
/ * *
* add the current mensa meal to the home screens
* /
private fun addCurrent MensaMenu ( ) {
private fun addMensaMenu ( ) {
doAsync {
val dayMeals : ArrayList < Meal >
var dayMeals : ArrayList < Meal >
val cal = Calendar . getInstance ( )
val mensaCardView = DayCardView ( context !! )
uiThread {
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) ] . meals
} else {
dayMeals = mensaCurrentWeek . days [ NotRetardedCalendar ( ) . getTomorrowWeekIndex ( ) ] . meals
uiThread {
txtView _Menu1Heading . text = resources . getString ( R . string . meal _1 _tomorrow )
txtView _Menu2Heading . text = resources . getString ( R . string . meal _2 _tomorrow )
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 ) ) )
}
}
uiThread {
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 " ) ) {
dayMeals [ i ] . parts . forEachIndexed { index , part ->
txtViewMenu1 . append ( part )
if ( index < ( dayMeals [ i ] . parts . size - 1 ) )
txtViewMenu1 . append ( " \n " )
}
val meal1Layout = MealLinearLayout ( context )
meal1Layout . setMeal ( dayMeals [ i ] )
mensaCardView . getLinLayoutDay ( ) . addView ( meal1Layout )
dayMeals [ i + 1 ] . parts . forEachIndexed { index , part ->
txtViewMenu2 . append ( part )
if ( index < ( dayMeals [ i + 1 ] . parts . size - 1 ) )
txtViewMenu2 . append ( " \n " )
}
val meal2Layout = MealLinearLayout ( context )
meal2Layout . setMeal ( dayMeals [ i + 1 ] )
meal2Layout . disableDivider ( )
mensaCardView . getLinLayoutDay ( ) . addView ( meal2Layout )
break @loop
}
}
} else {
if ( txtView _Menu1Heading . text == resources . getString ( R . string . meal _1 _tomorrow ) ) {
txtViewMenu1 . text = resources . getString ( R . string . no _meal _tomorrow )
txtViewMenu2 . text = resources . getString ( R . string . no _meal _tomorrow )
} else {
txtViewMenu1 . text = resources . getString ( R . string . no _meal _today )
txtViewMenu2 . text = resources . getString ( R . string . no _meal _today )
}
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 )
}
}
@ -125,60 +125,67 @@ class HomeFragment : Fragment() {
/ * *
* add the current timetable to the home screen
* /
private fun addCurrent TimeTable ( ) {
private fun addTimeTable ( ) {
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
var helpLesson = LessonLinearLayout ( context !! )
val dayCardView = DayCardView ( context !! )
dayCardView . setDayHeading ( " Heute " )
dayCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( Calendar . getInstance ( ) . time ) ) )
doAsync {
uiThread {
if ( timetables [ 0 ] . days . isNotEmpty ( ) && dayIndex < 6 ) {
if ( timetables [ 0 ] . days . isNotEmpty ( ) && dayIndex < 6 ) {
// for all timeslots of the day
for ( ( tsIndex , timeslot ) in timetables [ 0 ] . days [ dayIndex ] . timeslots . withIndex ( ) ) {
// for all timeslots of the day
for ( ( tsIndex , timeslot ) in timetables [ 0 ] . days [ dayIndex ] . timeslots . withIndex ( ) ) {
for ( lesson in timeslot ) {
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
for ( lesson in timeslot ) {
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
val lessonLayout = LessonLinearLayout ( context !! )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
val lessonLayout = LessonLinearLayout ( context !! )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
if ( lesson != timeslot . last ( ) ) {
lessonLayout . disableDivider ( )
if ( lesson != timeslot . last ( ) ) {
lessonLayout . disableDivider ( )
}
helpLesson = lessonLayout
}
}
}
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 ) )
helpLesson = lessonLayout
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 ) )
helpLesson . disableDivider ( )
// if there are no lessons don't show the dayCardView
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 ) {
linLayoutTimeTable . 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 ) )
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
}
linLayoutTimeTable . 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 ) )
linLayoutTimeTable . addView ( noLesson )
} else {
MaterialDialog ( context !! )
. title ( R . string . error )
. message ( R . string . gen _tt _error )
. show ( )
// TODO log the error and send feedback
}
}
}
}
}
}