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
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
2022-12-22 17:26:07 +01:00
import androidx.lifecycle.lifecycleScope
2020-02-04 19:58:07 +01:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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-12-06 18:00:28 +01:00
import org.mosad.seil0.projectlaogai.databinding.FragmentMensaBinding
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 ( ) {
2020-12-06 18:00:28 +01:00
private lateinit var binding : FragmentMensaBinding
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View {
binding = FragmentMensaBinding . inflate ( inflater , container , false )
return binding . root
2020-08-17 22:30:12 +02:00
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2018-10-27 00:39:56 +02:00
2020-12-06 18:00:28 +01:00
binding . refreshLayoutMensa . setProgressBackgroundColorSchemeColor ( Preferences . themeSecondary )
2019-06-02 19:09:36 +02:00
2020-08-28 23:21:17 +02:00
initActions ( ) // init actions
2018-10-27 00:39:56 +02:00
2022-12-22 17:26:07 +01:00
lifecycleScope . launch ( Dispatchers . Default ) {
2020-02-04 19:58:07 +01:00
val dayCurrent = if ( NotRetardedCalendar . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar . getDayOfWeekIndex ( )
2020-10-02 14:00:42 +02:00
// add the current and next week
addWeek ( mensaMenu . currentWeek , dayCurrent ) . join ( )
addWeek ( mensaMenu . nextWeek , 0 ) . join ( )
2019-03-10 13:37:13 +01:00
2020-10-02 14:00:42 +02:00
// show a info if there are no more menus
2020-12-06 18:00:28 +01:00
if ( binding . linLayoutMensa . childCount == 0 ) {
2022-12-22 17:26:07 +01:00
val txtViewInfo = TextViewInfo ( requireContext ( ) ) . set {
2020-10-02 14:00:42 +02:00
txt = resources . getString ( R . string . no _more _meals )
}
2020-12-06 18:00:28 +01:00
withContext ( Dispatchers . Main ) { binding . linLayoutMensa . addView ( txtViewInfo ) }
2020-08-17 22:30:12 +02:00
}
}
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
* /
2022-12-22 17:26:07 +01:00
private fun addWeek ( menusWeek : MensaWeek , dayStart : Int ) = lifecycleScope . 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 )
2022-12-22 17:26:07 +01:00
val dayCardView = DayCardView ( requireContext ( ) )
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
2020-09-02 15:36:03 +02:00
if ( meal . heading != " Buffet " || Preferences . showBuffet ) {
2019-03-23 21:07:33 +01:00
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 )
2020-12-06 18:00:28 +01:00
binding . linLayoutMensa . 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
/ * *
2020-08-28 23:21:17 +02:00
* initialize the actions
2019-04-03 19:21:51 +02:00
* /
2020-08-28 23:21:17 +02:00
private fun initActions ( ) {
// set the refresh listener
2020-12-06 18:00:28 +01:00
binding . refreshLayoutMensa . setOnRefreshListener {
2020-08-28 23:21:17 +02:00
updateMensaScreen ( )
2019-04-03 19:21:51 +02:00
}
}
/ * *
* refresh the mensa cache and update the mensa screen
* /
2022-12-22 17:26:07 +01:00
private fun updateMensaScreen ( ) = lifecycleScope . launch ( Dispatchers . Default ) {
CacheController . updateMensaMenu ( requireContext ( ) ) . 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
2020-12-06 18:00:28 +01:00
binding . linLayoutMensa . removeAllViews ( )
2019-04-03 20:37:17 +02:00
// add the refreshed menus
2019-09-22 20:51:36 +02:00
val dayCurrent = if ( NotRetardedCalendar . getDayOfWeekIndex ( ) == 6 ) 0 else NotRetardedCalendar . getDayOfWeekIndex ( )
2019-04-03 19:21:51 +02:00
2020-10-02 14:00:42 +02:00
// add the current and next week
addWeek ( mensaMenu . currentWeek , dayCurrent ) . join ( )
addWeek ( mensaMenu . nextWeek , 0 ) . join ( )
2019-04-03 19:21:51 +02:00
2020-12-06 18:00:28 +01:00
binding . refreshLayoutMensa . isRefreshing = false
2020-08-17 22:30:12 +02:00
// show a info if there are no more menus
2020-12-06 18:00:28 +01:00
if ( binding . linLayoutMensa . childCount == 0 ) {
2022-12-22 17:26:07 +01:00
val txtViewInfo = TextViewInfo ( requireContext ( ) ) . set {
2020-08-17 22:30:12 +02:00
txt = resources . getString ( R . string . no _more _meals )
}
2020-12-06 18:00:28 +01:00
binding . linLayoutMensa . addView ( txtViewInfo )
2020-08-17 22:30:12 +02:00
}
2019-04-03 20:37:17 +02:00
}
2019-04-03 19:21:51 +02:00
}
2018-10-24 18:22:05 +02:00
}