/** * ProjectLaogai * * Copyright 2019 * * 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 android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import androidx.fragment.app.Fragment import com.afollestad.materialdialogs.MaterialDialog import kotlinx.android.synthetic.main.fragment_home.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.uiThread import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableCurrentWeek import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusCurrentWeek import org.mosad.seil0.projectlaogai.R import org.mosad.seil0.projectlaogai.hsoparser.* import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView import org.mosad.seil0.projectlaogai.uicomponents.LessonTextView import java.util.* /** * The "home" controller class * contains all needed parts to display the apps home screen */ class HomeFragment : Fragment() { private lateinit var linLayoutTimeTable: LinearLayout override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view: View = inflater.inflate(R.layout.fragment_home, container, false) // init UI elements linLayoutTimeTable = view.findViewById(R.id.linLayoutTimeTable) //setText() addCurrentMensaMenu() addCurrentTimeTable() // Inflate the layout for this fragment return view } /** * add the current mensa meal to the home screens */ private fun addCurrentMensaMenu() { doAsync { val dayMenus: ArrayList val cal = Calendar.getInstance() if (cal.get(Calendar.HOUR_OF_DAY) < 15) { dayMenus = cMenusCurrentWeek.day[NotRetardedCalendar().getDayOfWeekIndex()] } else { dayMenus = cMenusCurrentWeek.day[NotRetardedCalendar().getTomorrowWeekIndex()] uiThread { txtView_Menu1Heading.text = resources.getString(R.string.meal_1_tomorrow) txtView_Menu2Heading.text = resources.getString(R.string.meal_2_tomorrow) } } uiThread { if (dayMenus.size >= 2) { // get the index of the first meal, not a "Schneller Teller" loop@ for ((i, meal) in dayMenus.withIndex()) { if (meal.heading.contains("Essen")) { for ((j, part) in dayMenus[i].parts.withIndex()) { txtViewMenu1.append(part) if (j < (dayMenus[i].parts.size - 2)) txtViewMenu1.append("\n") } for ((j, part) in dayMenus[i + 1].parts.withIndex()) { txtViewMenu2.append(part) if (j < (dayMenus[i + 1].parts.size - 2)) txtViewMenu2.append("\n") } break@loop } } } else { if (txtView_Menu1Heading.text == resources.getString(R.string.meal_1_tomorrow)) { txtViewMenu1.text = resources.getString(R.string.no_meal_tomorrow) txtViewMenu2.text = resources.getString(R.string.no_meal_tomorrow) } else { txtViewMenu1.text = resources.getString(R.string.no_meal_today) txtViewMenu2.text = resources.getString(R.string.no_meal_today) } } } } } /** * add the current timetable to the home screen */ private fun addCurrentTimeTable() { val dayIndex = NotRetardedCalendar().getDayOfWeekIndex() if (cTimeTableCurrentWeek.days.isNotEmpty() && dayIndex < 6) { val timeTableDay = cTimeTableCurrentWeek.days[dayIndex] // for all timeslots of the day for ((i, timeslot) in timeTableDay.timeslots.withIndex()) { val lessonCardView = LessonCardView(context!!, null) lessonCardView.getTxtViewTime().text = DataTypes().getTime()[i] for (lesson in timeslot) { val lessonTxtView = LessonTextView(context!!) lessonTxtView.setLesson(lesson) // TODO why does this exist if (lessonTxtView.text.length > 3) lessonCardView.getLinLayoutLesson().addView(lessonTxtView) } if (lessonCardView.getLinLayoutLesson().childCount > 2) linLayoutTimeTable.addView(lessonCardView) } // add a card if there is no lesson today if (linLayoutTimeTable.childCount == 0) { // TODO we could display the next day with a lecture val lessonTxtView = LessonTextView(context!!) lessonTxtView.setText(resources.getString(R.string.no_lesson_today)) val noLessonCardView = LessonCardView(context!!, null) noLessonCardView.getLinLayoutLesson().addView(lessonTxtView) linLayoutTimeTable.addView(noLessonCardView) } } else { if (dayIndex == 6) { // if that's the case it's sunday val lessonTxtView = LessonTextView(context!!) lessonTxtView.setText(resources.getString(R.string.no_lesson_today)) val noLessonCardView = LessonCardView(context!!, null) noLessonCardView.getLinLayoutLesson().addView(lessonTxtView) linLayoutTimeTable.addView(noLessonCardView) } else { MaterialDialog(context!!) .title(R.string.error) .message(R.string.gen_tt_error) .show() // TODO log the error and send feedback } } } }