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
import android.os.Bundle
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.Fragment
2018-10-24 18:22:05 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-10-27 00:39:56 +02:00
import android.widget.LinearLayout
2018-10-29 13:04:20 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2019-03-09 22:15:53 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusCurrentWeek
2019-03-10 13:37:13 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusNextWeek
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cShowBuffet
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2019-03-10 13:37:13 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.MealWeek
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
2018-10-29 13:04:20 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MenuCardView
2018-10-24 18:22:05 +02:00
/ * *
2018-10-27 14:11:47 +02:00
* The mensa controller class
* contains all needed parts to display and the mensa detail screen
2018-10-24 18:22:05 +02:00
* /
class MensaFragment : Fragment ( ) {
2018-10-27 00:39:56 +02:00
private lateinit var linLayoutMensaFragment : LinearLayout
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
val view : View = inflater . inflate ( R . layout . fragment _mensa , container , false )
linLayoutMensaFragment = view . findViewById ( R . id . linLayout _MensaFragment )
2019-03-10 13:37:13 +01:00
// add the current week (week starts on sunday)
val dayCurrent = if ( NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
addWeek ( cMenusCurrentWeek , dayCurrent )
// add the next week
addWeek ( cMenusNextWeek , 0 )
2018-10-27 00:39:56 +02:00
return view
}
2019-03-10 13:37:13 +01:00
/ * *
* add all menus from dayStart to Friday for a given week
* /
private fun addWeek ( menusWeek : MealWeek , dayStart : Int ) {
2018-10-27 00:39:56 +02:00
2018-10-29 13:04:20 +01:00
doAsync {
uiThread {
2019-03-10 13:37:13 +01:00
// only add the days dayStart to Fri since the mensa is closed on Sat/Sun
for ( dayIndex in dayStart .. 4 ) {
2018-10-29 13:04:20 +01:00
val cardViewMensaDay = MensaDayCardView ( context !! , null )
2019-03-10 13:37:13 +01:00
for ( meal in menusWeek . day [ dayIndex ] ) {
val menuViewMenu = MenuCardView ( context !! , null )
menuViewMenu . setMenuHeading ( meal . heading )
2018-10-29 13:04:20 +01:00
2019-03-10 13:37:13 +01:00
meal . parts . forEachIndexed { partIndex , part ->
menuViewMenu . getTxtViewMenu ( ) . append ( part )
if ( partIndex < ( meal . parts . size - 2 ) )
menuViewMenu . getTxtViewMenu ( ) . append ( " \n " )
}
2018-10-29 13:04:20 +01:00
2019-03-10 13:37:13 +01:00
cardViewMensaDay . setDayHeading ( meal . day )
2018-10-29 13:04:20 +01:00
2019-03-10 13:37:13 +01:00
if ( meal . heading != " Buffet " || cShowBuffet )
2018-10-29 13:04:20 +01:00
cardViewMensaDay . getLinLayoutMensaDay ( ) . addView ( menuViewMenu )
}
2019-03-10 13:37:13 +01:00
println ( " $dayIndex : " + cardViewMensaDay . getLinLayoutMensaDay ( ) . childCount ) // remove
2018-10-29 13:04:20 +01:00
2019-03-10 13:37:13 +01:00
if ( cardViewMensaDay . getLinLayoutMensaDay ( ) . childCount > 1 )
2018-10-29 13:04:20 +01:00
linLayoutMensaFragment . addView ( cardViewMensaDay )
2019-03-10 13:37:13 +01:00
2018-10-29 13:04:20 +01:00
}
2018-11-01 01:06:53 +01:00
// add a card if there are no more meals in this week
if ( linLayoutMensaFragment . childCount == 0 ) {
val cardViewNoMoreFood = MensaDayCardView ( context !! , null )
cardViewNoMoreFood . setDayHeading ( resources . getString ( R . string . no _more _food ) )
linLayoutMensaFragment . addView ( cardViewNoMoreFood )
}
2018-10-29 13:04:20 +01:00
}
}
2019-03-10 13:37:13 +01:00
2018-10-29 13:04:20 +01:00
}
2018-10-24 18:22:05 +02:00
}