ProjectLaogai/app/src/main/java/org/mosad/seil0/projectlaogai/fragments/MensaFragment.kt

114 lines
4.2 KiB
Kotlin

/**
* ProjectLaogai
*
* Copyright 2019 <seil0@mosad.xyz>
*
* 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.
*
*/
package org.mosad.seil0.projectlaogai.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusCurrentWeek
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusNextWeek
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.hsoparser.MealWeek
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MenuCardView
/**
* The mensa controller class
* contains all needed parts to display and the mensa detail screen
*/
class MensaFragment : Fragment() {
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)
// 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)
return view
}
/**
* add all menus from dayStart to Friday for a given week
*/
private fun addWeek(menusWeek: MealWeek, dayStart: Int) {
doAsync {
uiThread {
// only add the days dayStart to Fri since the mensa is closed on Sat/Sun
for (dayIndex in dayStart..4) {
val cardViewMensaDay = MensaDayCardView(context!!, null)
for (meal in menusWeek.day[dayIndex]) {
val menuViewMenu = MenuCardView(context!!, null)
menuViewMenu.setMenuHeading(meal.heading)
meal.parts.forEachIndexed { partIndex, part ->
menuViewMenu.getTxtViewMenu().append(part)
if(partIndex < (meal.parts.size - 2))
menuViewMenu.getTxtViewMenu().append("\n")
}
cardViewMensaDay.setDayHeading(meal.day)
if(meal.heading != "Buffet" || cShowBuffet)
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu)
}
println("$dayIndex:" + cardViewMensaDay.getLinLayoutMensaDay().childCount) // remove
if(cardViewMensaDay.getLinLayoutMensaDay().childCount > 1)
linLayoutMensaFragment.addView(cardViewMensaDay)
}
// 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)
}
}
}
}
}