2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < 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
2020-08-17 22:30:12 +02:00
import android.util.TypedValue
2018-10-24 18:22:05 +02:00
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.*
2020-02-04 19:58:07 +01:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
2020-08-17 22:30:12 +02:00
import org.mosad.seil0.projectlaogai.MainActivity
2019-06-02 19:09:36 +02:00
import org.mosad.seil0.projectlaogai.R
2020-08-10 14:31:17 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.CacheController
import org.mosad.seil0.projectlaogai.controller.cache.CacheController.Companion.mensaMenu
2020-08-17 22:30:12 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cShowBuffet
2019-03-20 21:33:55 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
2020-08-17 22:30:12 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.TextViewInfo
import org.mosad.seil0.projectlaogai.util.MensaWeek
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.NotRetardedCalendar
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 ? {
2020-08-17 22:30:12 +02:00
return inflater . inflate ( R . layout . fragment _mensa , container , false )
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2018-10-27 00:39:56 +02:00
2020-08-17 22:30:12 +02:00
refreshLayout _Mensa . setProgressBackgroundColorSchemeColor ( Preferences . themeSecondary )
2019-06-02 19:09:36 +02:00
2020-08-17 22:30:12 +02:00
refreshAction ( ) // init actions
2018-10-27 00:39:56 +02:00
2020-02-04 19:58:07 +01:00
GlobalScope . launch ( Dispatchers . Default ) {
val dayCurrent = if ( NotRetardedCalendar . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar . getDayOfWeekIndex ( )
addWeek ( mensaMenu . currentWeek , dayCurrent ) . join ( )
// add the next week
addWeek ( mensaMenu . nextWeek , 0 )
}
2019-03-10 13:37:13 +01:00
2020-08-17 22:30:12 +02:00
// show a info if there are no more menus
if ( linLayout _Mensa . childCount == 0 ) {
val txtViewInfo = TextViewInfo ( context !! ) . set {
txt = resources . getString ( R . string . no _more _meals )
}
linLayout _Mensa . addView ( txtViewInfo )
}
2018-10-27 00:39:56 +02:00
}
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
* /
2020-02-04 19:58:07 +01:00
private fun addWeek ( menusWeek : MensaWeek , dayStart : Int ) = GlobalScope . launch ( Dispatchers . Default ) {
2018-10-27 00:39:56 +02:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
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 ( )
2020-03-03 20:13:47 +01:00
if ( dayCardView . getLinLayoutDay ( ) . childCount > 2 )
2019-03-23 21:07:33 +01:00
linLayout _Mensa . addView ( dayCardView )
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
* /
2020-02-04 19:58:07 +01:00
private fun refreshAction ( ) = GlobalScope . launch ( Dispatchers . Default ) {
withContext ( Dispatchers . Main ) {
2019-04-03 19:21:51 +02:00
// set the refresh listener
refreshLayout _Mensa . setOnRefreshListener {
updateMensaScreen ( )
}
}
}
/ * *
* refresh the mensa cache and update the mensa screen
* /
2020-02-04 19:58:07 +01:00
private fun updateMensaScreen ( ) = GlobalScope . launch ( Dispatchers . Default ) {
2020-08-07 12:14:50 +02:00
CacheController . updateMensaMenu ( context !! ) . join ( ) // blocking since we want the new data
2019-04-03 19:21:51 +02:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2019-04-03 20:37:17 +02:00
// remove all menus from the layout
linLayout _Mensa . removeAllViews ( )
// add the refreshed menus
2019-09-22 20:51:36 +02:00
val dayCurrent = if ( NotRetardedCalendar . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar . getDayOfWeekIndex ( )
2020-02-04 19:58:07 +01:00
addWeek ( mensaMenu . currentWeek , dayCurrent ) . join ( )
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
2020-08-17 22:30:12 +02:00
// show a info if there are no more menus
if ( linLayout _Mensa . childCount == 0 ) {
val txtViewInfo = TextViewInfo ( context !! ) . set {
txt = resources . getString ( R . string . no _more _meals )
}
linLayout _Mensa . addView ( txtViewInfo )
}
2019-04-03 20:37:17 +02:00
}
2019-04-03 19:21:51 +02:00
}
2018-10-24 18:22:05 +02:00
}