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

141 lines
4.7 KiB
Kotlin
Raw Normal View History

2018-10-24 18:22:05 +02:00
/**
* ProjectLaogai
*
* 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
import androidx.fragment.app.Fragment
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
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaCurrentWeek
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaNextWeek
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.hsoparser.MensaWeek
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
2018-10-24 18:22:05 +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? {
val view: View = inflater.inflate(org.mosad.seil0.projectlaogai.R.layout.fragment_mensa, container, false)
2018-10-27 00:39:56 +02:00
// init actions
refreshAction()
2018-10-27 00:39:56 +02:00
// add the current week (week starts on sunday)
val dayCurrent = if(NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex()
addWeek(mensaCurrentWeek, dayCurrent).get()
// add the next week
addWeek(mensaNextWeek, 0).get()
2018-10-27 00:39:56 +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
}
/**
* add all menus from dayStart to Friday for a given week
* @param menusWeek menu of type MensaWeek you want to add
* @param dayStart the first day of the week to add
*/
private fun addWeek(menusWeek: MensaWeek, dayStart: Int) = doAsync {
2018-10-27 00:39:56 +02:00
uiThread {
2018-10-29 13:04:20 +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!!)
dayCardView.setDayHeading(menusWeek.days[dayIndex].meals[0].day)
for (meal in menusWeek.days[dayIndex].meals) {
val mealLayout = MealLinearLayout(context)
mealLayout.setMeal(meal)
2018-10-29 13:04:20 +01:00
if(meal.heading != "Buffet" || cShowBuffet) {
dayCardView.getLinLayoutDay().addView(mealLayout)
helpMeal = mealLayout
2018-10-29 13:04:20 +01:00
}
}
helpMeal.disableDivider()
if(dayCardView.getLinLayoutDay().childCount > 1)
linLayout_Mensa.addView(dayCardView)
2018-10-29 13:04:20 +01:00
}
2018-10-29 13:04:20 +01:00
}
2018-10-29 13:04:20 +01:00
}
/**
* initialize the refresh action
*/
private fun refreshAction() = doAsync {
uiThread {
// set the refresh listener
refreshLayout_Mensa.setOnRefreshListener {
updateMensaScreen()
refreshLayout_Mensa.isRefreshing = false
}
}
}
/**
* refresh the mensa cache and update the mensa screen
* TODO some nice animations
*/
private fun updateMensaScreen() {
// update the cache
TCoRAPIController.getMensa(context!!)
CacheController.readMensa(context!!)
// remove all menus from the layout
linLayout_Mensa.removeAllViews()
// add the refreshed menus
val dayCurrent = if (NotRetardedCalendar().getDayOfWeekIndex() == 6) 0 else NotRetardedCalendar().getDayOfWeekIndex()
addWeek(mensaCurrentWeek, dayCurrent).get()
// add the next week
addWeek(mensaNextWeek, 0).get()
}
2018-10-24 18:22:05 +02:00
}