186 lines
7.0 KiB
Kotlin
186 lines
7.0 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 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.R
|
|
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaCurrentWeek
|
|
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
|
|
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
|
|
import org.mosad.seil0.projectlaogai.hsoparser.Meal
|
|
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
|
|
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
|
|
import org.mosad.seil0.projectlaogai.uicomponents.LessonTextView
|
|
import java.util.*
|
|
import kotlin.collections.ArrayList
|
|
|
|
/**
|
|
* 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 dayMeals: ArrayList<Meal>
|
|
val cal = Calendar.getInstance()
|
|
|
|
|
|
if (cal.get(Calendar.HOUR_OF_DAY) < 15) {
|
|
dayMeals = mensaCurrentWeek.days[NotRetardedCalendar().getDayOfWeekIndex()].meals
|
|
} else {
|
|
dayMeals = mensaCurrentWeek.days[NotRetardedCalendar().getTomorrowWeekIndex()].meals
|
|
uiThread {
|
|
txtView_Menu1Heading.text = resources.getString(R.string.meal_1_tomorrow)
|
|
txtView_Menu2Heading.text = resources.getString(R.string.meal_2_tomorrow)
|
|
}
|
|
}
|
|
|
|
uiThread {
|
|
|
|
if (dayMeals.size >= 2) {
|
|
// get the index of the first meal, not a "Schneller Teller"
|
|
loop@ for ((i, meal) in dayMeals.withIndex()) {
|
|
if (meal.heading.contains("Essen")) {
|
|
|
|
dayMeals[i].parts.forEachIndexed { index, part ->
|
|
txtViewMenu1.append(part)
|
|
if (index < (dayMeals[i].parts.size - 1))
|
|
txtViewMenu1.append("\n")
|
|
}
|
|
|
|
dayMeals[i + 1].parts.forEachIndexed { index, part ->
|
|
txtViewMenu2.append(part)
|
|
if (index < (dayMeals[i + 1].parts.size - 1))
|
|
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 (timetables[0].days.isNotEmpty() && dayIndex < 6) {
|
|
|
|
val timeTableDay = timetables[0].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)
|
|
|
|
// make sure no empty lesson is added
|
|
if (lessonTxtView.text.length > 3)
|
|
lessonCardView.getLinLayoutLesson().addView(lessonTxtView)
|
|
}
|
|
|
|
if (lessonCardView.getLinLayoutLesson().childCount > 1)
|
|
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
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|