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

120 lines
4.2 KiB
Kotlin
Raw Normal View History

2018-10-24 18:22:05 +02:00
/**
* ProjectLaogai
*
* Copyright 2018 <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.
*
*/
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
2018-10-26 00:50:37 +02:00
import androidx.fragment.app.Fragment
2018-10-24 18:22:05 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2018-10-27 00:39:56 +02:00
import android.widget.LinearLayout
2018-10-29 13:04:20 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import org.mosad.seil0.projectlaogai.MainActivity
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2018-10-29 13:04:20 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.MensaDayCardView
import org.mosad.seil0.projectlaogai.uicomponents.MenuCardView
import java.util.*
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
private lateinit var linLayoutMensaFragment: LinearLayout
private var mainActivity = MainActivity()
2018-10-27 00:39:56 +02:00
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)
2018-10-29 13:04:20 +01:00
addCurrentWeek()
2018-10-27 00:39:56 +02:00
return view
}
private fun addCurrentWeek() {
2018-10-27 00:39:56 +02:00
2018-10-29 13:04:20 +01:00
doAsync {
uiThread {
for(day in Calendar.getInstance().get(Calendar.DAY_OF_WEEK)..7) {
val strDay: String = when(day) {
2018-10-29 13:04:20 +01:00
Calendar.MONDAY -> "Mon"
Calendar.TUESDAY -> "Die"
Calendar.WEDNESDAY -> "Mit"
Calendar.THURSDAY -> "Don"
Calendar.FRIDAY -> "Fre"
Calendar.SATURDAY -> "Sam"
else -> "TODAY" // the app will likely crash here
2018-10-29 13:04:20 +01:00
}
val cardViewMensaDay = MensaDayCardView(context!!, null)
var add = false
for (meal in mainActivity.getWeekMenu()) {
2018-10-29 13:04:20 +01:00
//println("Day: " + meal.day)
if (meal.day.contains(strDay)) {
val menuViewMenu = MenuCardView(context!!, null)
menuViewMenu.setMenuHeading(meal.heading)
for ((i, part) in meal.parts.withIndex()) {
2018-10-29 13:04:20 +01:00
menuViewMenu.getTxtViewMenu().append(part)
if(i < (meal.parts.size - 2))
menuViewMenu.getTxtViewMenu().append("\n")
2018-10-29 13:04:20 +01:00
}
cardViewMensaDay.setDayHeading(meal.day) //TODO move this out of the first for loop, performance!!
cardViewMensaDay.getLinLayoutMensaDay().addView(menuViewMenu)
add = true
}
}
if(add)
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)
}
2018-10-29 13:04:20 +01:00
}
}
}
fun setMainActivity(mainActivity: MainActivity) {
this.mainActivity = mainActivity
}
2018-10-24 18:22:05 +02:00
}