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
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-03-17 18:12:36 +01:00
import androidx.fragment.app.Fragment
2019-03-20 21:33:55 +01:00
import kotlinx.android.synthetic.main.fragment_mensa.*
2018-10-29 13:04:20 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2019-06-02 19:09:36 +02:00
import org.mosad.seil0.projectlaogai.R
2019-04-03 19:21:51 +02:00
import org.mosad.seil0.projectlaogai.controller.CacheController
2019-06-02 19:09:36 +02:00
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaMenu
2019-03-20 21:33:55 +01:00
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
2019-04-03 19:21:51 +02:00
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.MensaWeek
2019-03-10 13:37:13 +01:00
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.MealLinearLayout
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
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
2019-06-02 19:09:36 +02:00
val view : View = inflater . inflate ( R . layout . fragment _mensa , container , false )
2019-04-03 19:21:51 +02:00
// init actions
2019-06-08 23:14:55 +02:00
refreshAction ( )
2018-10-27 00:39:56 +02:00
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 ( )
2019-06-02 19:09:36 +02:00
addWeek ( mensaMenu . currentWeek , dayCurrent ) . get ( )
2019-03-10 13:37:13 +01:00
// add the next week
2019-06-02 19:09:36 +02:00
addWeek ( mensaMenu . nextWeek , 0 )
2018-10-27 00:39:56 +02:00
2019-04-03 19:21:51 +02:00
// TODO should we show a info if there is no more food this & next week?
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
2019-04-03 19:21:51 +02:00
* @param menusWeek menu of type MensaWeek you want to add
* @param dayStart the first day of the week to add
2019-03-10 13:37:13 +01:00
* /
2019-03-23 21:07:33 +01:00
private fun addWeek ( menusWeek : MensaWeek , dayStart : Int ) = doAsync {
2018-10-27 00:39:56 +02:00
2019-03-23 21:07:33 +01:00
uiThread {
2018-10-29 13:04:20 +01:00
2019-03-23 21:07:33 +01:00
// only add the days dayStart to Fri since the mensa is closed on Sat/Sun
for ( dayIndex in dayStart .. 4 ) {
var helpMeal = MealLinearLayout ( context )
val dayCardView = DayCardView ( context !! )
2019-04-08 14:04:26 +02:00
if ( menusWeek . days [ dayIndex ] . meals . isNotEmpty ( ) )
dayCardView . setDayHeading ( menusWeek . days [ dayIndex ] . meals [ 0 ] . day )
2019-03-20 21:33:55 +01:00
2019-03-23 21:07:33 +01:00
for ( meal in menusWeek . days [ dayIndex ] . meals ) {
val mealLayout = MealLinearLayout ( context )
mealLayout . setMeal ( meal )
2018-10-29 13:04:20 +01:00
2019-03-23 21:07:33 +01:00
if ( meal . heading != " Buffet " || cShowBuffet ) {
dayCardView . getLinLayoutDay ( ) . addView ( mealLayout )
helpMeal = mealLayout
2018-10-29 13:04:20 +01:00
}
}
2018-11-01 01:06:53 +01:00
2019-03-23 21:07:33 +01:00
helpMeal . disableDivider ( )
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 )
linLayout _Mensa . addView ( dayCardView )
2018-10-29 13:04:20 +01:00
}
2019-03-23 21:07:33 +01:00
2018-10-29 13:04:20 +01:00
}
2019-03-10 13:37:13 +01:00
2018-10-29 13:04:20 +01:00
}
2019-04-03 19:21:51 +02:00
/ * *
* initialize the refresh action
* /
private fun refreshAction ( ) = doAsync {
uiThread {
// set the refresh listener
refreshLayout _Mensa . setOnRefreshListener {
updateMensaScreen ( )
}
}
}
/ * *
* refresh the mensa cache and update the mensa screen
* /
2019-04-03 20:37:17 +02:00
private fun updateMensaScreen ( ) = doAsync {
2019-04-03 19:21:51 +02:00
// update the cache
2019-04-03 20:37:17 +02:00
TCoRAPIController . getMensa ( context !! ) . get ( ) // blocking since we want the new data
2019-04-03 19:21:51 +02:00
CacheController . readMensa ( context !! )
2019-04-03 20:37:17 +02:00
uiThread {
// remove all menus from the layout
linLayout _Mensa . removeAllViews ( )
// add the refreshed menus
val dayCurrent = if ( NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
2019-06-02 19:09:36 +02:00
addWeek ( mensaMenu . currentWeek , dayCurrent ) . get ( )
2019-04-03 19:21:51 +02:00
2019-04-03 20:37:17 +02:00
// add the next week
2019-06-02 19:09:36 +02:00
addWeek ( mensaMenu . nextWeek , 0 )
2019-04-03 19:21:51 +02:00
2019-04-03 20:37:17 +02:00
refreshLayout _Mensa . isRefreshing = false
}
2019-04-03 19:21:51 +02:00
}
2018-10-24 18:22:05 +02:00
}