2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < seil0 @mosad . xyz >
2018-10-24 18:22:05 +02: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-30 20:41:22 +01:00
package org.mosad.seil0.projectlaogai.fragments
2018-10-24 18:22:05 +02:00
2019-03-22 21:59:32 +01:00
import android.graphics.Typeface
2018-10-24 18:22:05 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-03-22 21:59:32 +01:00
import android.widget.TextView
import androidx.core.content.ContextCompat
2018-11-01 02:12:43 +01:00
import androidx.fragment.app.Fragment
2018-11-06 10:47:51 +01:00
import com.afollestad.materialdialogs.MaterialDialog
2018-10-29 18:03:20 +01:00
import kotlinx.android.synthetic.main.fragment_home.*
2018-10-29 13:04:20 +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.mensaCurrentWeek
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
import org.mosad.seil0.projectlaogai.hsoparser.Meal
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
2019-03-20 21:33:55 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.LessonLinearLayout
2019-03-22 21:59:32 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
import java.text.SimpleDateFormat
2018-10-29 13:04:20 +01:00
import java.util.*
2018-10-24 18:22:05 +02:00
/ * *
* The " home " controller class
* contains all needed parts to display the apps home screen
* /
class HomeFragment : Fragment ( ) {
2019-03-22 21:59:32 +01:00
val formatter = SimpleDateFormat ( " E dd.MM " , Locale . GERMANY )
2018-10-24 18:22:05 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _home , container , false )
2019-03-22 21:59:32 +01:00
addMensaMenu ( )
addTimeTable ( )
2018-10-24 18:22:05 +02:00
// Inflate the layout for this fragment
return view
}
2018-11-05 20:59:41 +01:00
/ * *
* add the current mensa meal to the home screens
* /
2019-03-22 21:59:32 +01:00
private fun addMensaMenu ( ) {
2018-11-05 20:59:41 +01:00
doAsync {
2019-03-22 21:59:32 +01:00
var dayMeals : ArrayList < Meal >
2018-11-13 16:17:05 +01:00
val cal = Calendar . getInstance ( )
2019-03-22 21:59:32 +01:00
val mensaCardView = DayCardView ( context !! )
2018-11-13 16:17:05 +01:00
2019-03-22 21:59:32 +01:00
uiThread {
2019-03-10 13:37:13 +01:00
2019-03-22 21:59:32 +01:00
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 ) ) )
2018-11-13 16:17:05 +01:00
}
2018-10-29 13:04:20 +01:00
2019-03-17 18:12:36 +01:00
if ( dayMeals . size >= 2 ) {
2018-11-16 14:19:24 +01:00
// get the index of the first meal, not a "Schneller Teller"
2019-03-17 18:12:36 +01:00
loop @ for ( ( i , meal ) in dayMeals . withIndex ( ) ) {
2018-12-02 00:05:23 +01:00
if ( meal . heading . contains ( " Essen " ) ) {
2019-03-17 20:52:34 +01:00
2019-03-22 21:59:32 +01:00
val meal1Layout = MealLinearLayout ( context )
meal1Layout . setMeal ( dayMeals [ i ] )
mensaCardView . getLinLayoutDay ( ) . addView ( meal1Layout )
2018-11-16 14:19:24 +01:00
2019-03-22 21:59:32 +01:00
val meal2Layout = MealLinearLayout ( context )
meal2Layout . setMeal ( dayMeals [ i + 1 ] )
meal2Layout . disableDivider ( )
mensaCardView . getLinLayoutDay ( ) . addView ( meal2Layout )
2018-11-16 14:19:24 +01:00
break @loop
}
2018-11-01 01:06:53 +01:00
}
} else {
2019-03-22 21:59:32 +01:00
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 )
2018-10-29 13:04:20 +01:00
}
2019-03-22 21:59:32 +01:00
linLayout _Home . addView ( mensaCardView )
2018-10-29 13:04:20 +01:00
}
}
2018-10-24 18:22:05 +02:00
}
2018-11-05 20:59:41 +01:00
/ * *
* add the current timetable to the home screen
* /
2019-03-22 21:59:32 +01:00
private fun addTimeTable ( ) {
2018-11-11 21:12:28 +01:00
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
2019-03-20 21:33:55 +01:00
var helpLesson = LessonLinearLayout ( context !! )
val dayCardView = DayCardView ( context !! )
2019-03-22 21:59:32 +01:00
dayCardView . setDayHeading ( resources . getString ( R . string . today _date , formatter . format ( Calendar . getInstance ( ) . time ) ) )
doAsync {
uiThread {
2018-10-24 18:22:05 +02:00
2019-03-22 21:59:32 +01:00
if ( timetables [ 0 ] . days . isNotEmpty ( ) && dayIndex < 6 ) {
2018-11-11 21:12:28 +01:00
2019-03-22 21:59:32 +01:00
// for all timeslots of the day
for ( ( tsIndex , timeslot ) in timetables [ 0 ] . days [ dayIndex ] . timeslots . withIndex ( ) ) {
2018-10-24 18:22:05 +02:00
2019-03-22 21:59:32 +01:00
for ( lesson in timeslot ) {
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
2019-03-01 18:59:17 +01:00
2019-03-22 21:59:32 +01:00
val lessonLayout = LessonLinearLayout ( context !! )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
2018-11-06 10:47:51 +01:00
2019-03-22 21:59:32 +01:00
if ( lesson != timeslot . last ( ) ) {
lessonLayout . disableDivider ( )
}
helpLesson = lessonLayout
}
2019-03-20 21:33:55 +01:00
}
2019-03-22 21:59:32 +01:00
}
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 ) )
2019-03-20 21:33:55 +01:00
2019-03-22 21:59:32 +01:00
linLayout _Home . addView ( noLesson )
2019-03-20 21:33:55 +01:00
}
2019-03-22 21:59:32 +01:00
} 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 ) )
2018-11-01 01:06:53 +01:00
2019-03-22 21:59:32 +01:00
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
}
2019-03-03 20:46:51 +01:00
2019-03-22 21:59:32 +01:00
}
2018-11-11 21:12:28 +01:00
2019-03-22 21:59:32 +01:00
}
2018-11-01 01:06:53 +01:00
}
2019-03-22 21:59:32 +01:00
2018-10-29 18:03:20 +01:00
}
2018-10-24 18:22:05 +02:00
}