2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < 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
2019-03-22 21:59:32 +01:00
import android.graphics.Typeface
2018-10-24 18:22:05 +02:00
import android.os.Bundle
2019-08-23 19:19:35 +02:00
import android.util.Log
2018-10-24 18:22:05 +02:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-03-22 21:59:32 +01:00
import android.widget.TextView
2018-11-01 02:12:43 +01:00
import androidx.fragment.app.Fragment
2018-10-29 18:03:20 +01:00
import kotlinx.android.synthetic.main.fragment_home.*
2020-02-18 15:18:36 +01:00
import kotlinx.coroutines.*
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2020-08-10 14:31:17 +02:00
import org.mosad.seil0.projectlaogai.controller.cache.CacheController.Companion.mensaMenu
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.Meal
import org.mosad.seil0.projectlaogai.util.TimetableDay
2019-03-20 21:33:55 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
2019-03-22 21:59:32 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.MealLinearLayout
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.NotRetardedCalendar
2019-03-22 21:59:32 +01:00
import java.text.SimpleDateFormat
2018-10-29 13:04:20 +01:00
import java.util.*
2018-10-24 18:22:05 +02:00
/ * *
* The " home " controller class
* contains all needed parts to display the apps home screen
* /
class HomeFragment : Fragment ( ) {
2019-09-22 20:51:36 +02:00
private val className = " HomeFragment "
2019-03-23 21:07:33 +01:00
private val formatter = SimpleDateFormat ( " E dd.MM " , Locale . getDefault ( ) )
2018-10-24 18:22:05 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
2020-08-17 22:30:12 +02:00
return inflater . inflate ( R . layout . fragment _home , container , false )
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2018-10-24 18:22:05 +02:00
2020-02-04 19:58:07 +01:00
addMensaMenu ( )
addTimeTable ( )
2018-10-24 18:22:05 +02:00
}
2018-11-05 20:59:41 +01:00
/ * *
* add the current mensa meal to the home screens
* /
2020-02-04 19:58:07 +01:00
private fun addMensaMenu ( ) = GlobalScope . launch ( Dispatchers . Default ) {
2019-03-22 21:59:32 +01:00
2019-03-23 21:07:33 +01:00
var dayMeals : ArrayList < Meal >
val cal = Calendar . getInstance ( )
val mensaCardView = DayCardView ( context !! )
2018-11-13 16:17:05 +01:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2019-03-10 13:37:13 +01:00
2019-06-24 22:41:45 +02:00
if ( isAdded ) {
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
2019-09-22 20:51:36 +02:00
dayMeals = mensaMenu . currentWeek . days [ NotRetardedCalendar . getDayOfWeekIndex ( ) ] . meals
2019-06-24 22:41:45 +02:00
mensaCardView . setDayHeading ( activity !! . resources . getString ( R . string . today _date , formatter . format ( cal . time ) ) )
} else {
2019-09-22 20:51:36 +02:00
dayMeals = mensaMenu . currentWeek . days [ NotRetardedCalendar . getTomorrowWeekIndex ( ) ] . meals
2019-06-24 22:41:45 +02:00
cal . add ( Calendar . DATE , 1 )
mensaCardView . setDayHeading ( activity !! . resources . getString ( R . string . tomorrow _date , formatter . format ( cal . time ) ) )
}
2018-10-29 13:04:20 +01:00
2019-06-24 22:41:45 +02:00
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 " ) ) {
2019-03-17 20:52:34 +01:00
2019-06-24 22:41:45 +02:00
val meal1Layout = MealLinearLayout ( context )
meal1Layout . setMeal ( dayMeals [ i ] )
mensaCardView . getLinLayoutDay ( ) . addView ( meal1Layout )
2018-11-16 14:19:24 +01:00
2019-06-24 22:41:45 +02:00
val meal2Layout = MealLinearLayout ( context )
meal2Layout . setMeal ( dayMeals [ i + 1 ] )
meal2Layout . disableDivider ( )
mensaCardView . getLinLayoutDay ( ) . addView ( meal2Layout )
2018-11-16 14:19:24 +01:00
2019-06-24 22:41:45 +02:00
break @loop
}
2018-11-01 01:06:53 +01:00
}
2019-06-24 22:41:45 +02:00
} else {
mensaCardView . getLinLayoutDay ( ) . addView ( getNoCard ( resources . getString ( R . string . mensa _closed ) ) )
2018-10-29 13:04:20 +01:00
}
2019-03-22 21:59:32 +01:00
2019-06-24 22:41:45 +02:00
linLayout _Home . addView ( mensaCardView )
2018-10-29 13:04:20 +01:00
}
2019-03-23 21:07:33 +01:00
2018-10-29 13:04:20 +01:00
}
2018-10-24 18:22:05 +02:00
}
2018-11-05 20:59:41 +01:00
/ * *
* add the current timetable to the home screen
* /
2020-02-04 19:58:07 +01:00
private fun addTimeTable ( ) = GlobalScope . launch ( Dispatchers . Default ) {
2019-03-23 21:07:33 +01:00
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2019-03-23 21:07:33 +01:00
2020-06-12 00:51:50 +02:00
if ( isAdded && TimetableController . timetable . isNotEmpty ( ) ) {
2019-09-22 20:51:36 +02:00
try {
val dayCardView = findNextDay ( NotRetardedCalendar . getDayOfWeekIndex ( ) )
2019-06-24 22:41:45 +02:00
linLayout _Home . addView ( dayCardView )
2019-09-22 20:51:36 +02:00
} catch ( ex : Exception ) {
Log . e ( className , " could not load timetable " , ex ) // TODO send feedback
2019-03-22 21:59:32 +01:00
}
}
2018-11-01 01:06:53 +01:00
}
2019-03-22 21:59:32 +01:00
2019-03-23 21:07:33 +01:00
}
2019-03-24 17:23:10 +01:00
/ * *
* find the next day with a lesson
2019-09-22 20:51:36 +02:00
* start at week 0 , startDayIndex and search every cached week until we find a ) a day with a timetable
* or b ) we find no timetable and add a no lesson card
2019-03-24 17:23:10 +01:00
* @param startDayIndex the day index you want to start searching
* @return a DayCardView with all lessons added
* /
2020-02-04 19:58:07 +01:00
private fun findNextDay ( startDayIndex : Int ) : DayCardView {
2019-09-22 20:51:36 +02:00
val dayCardView = DayCardView ( context !! )
2019-03-24 17:23:10 +01:00
var dayTimetable : TimetableDay ? = null
var dayIndexSearch = startDayIndex
2019-09-22 20:51:36 +02:00
var weekIndexSearch = 0
2019-07-18 00:32:31 +02:00
2020-06-12 00:51:50 +02:00
while ( dayTimetable == null && weekIndexSearch < TimetableController . timetable . size ) {
2019-09-22 20:51:36 +02:00
for ( dayIndex in dayIndexSearch .. 5 ) {
2020-06-12 00:51:50 +02:00
dayTimetable = TimetableController . timetable [ weekIndexSearch ] . days [ dayIndex ]
2019-03-24 17:23:10 +01:00
2019-09-25 22:46:58 +02:00
// some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
2020-06-12 00:51:50 +02:00
val daysToAdd = ( ( TimetableController . timetable [ weekIndexSearch ] . weekNumberYear - NotRetardedCalendar . getWeekOfYear ( ) )
* 7 + ( dayIndex - NotRetardedCalendar . getDayOfWeekIndex ( ) ) )
2019-09-22 20:51:36 +02:00
dayCardView . addTimetableDay ( dayTimetable , daysToAdd )
2019-03-24 17:23:10 +01:00
2019-09-22 20:51:36 +02:00
// if there are no lessons don't show the dayCardView
2019-03-24 17:23:10 +01:00
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 )
return dayCardView
}
2019-09-22 20:51:36 +02:00
2019-03-24 17:23:10 +01:00
weekIndexSearch ++
2019-09-22 20:51:36 +02:00
dayIndexSearch = 0
2019-03-24 17:23:10 +01:00
}
2019-09-22 20:51:36 +02:00
// there was no day found in the cached weeks, add no lesson card
2019-03-24 17:23:10 +01:00
dayCardView . setDayHeading ( formatter . format ( Calendar . getInstance ( ) . time ) )
dayCardView . getLinLayoutDay ( ) . addView ( getNoCard ( resources . getString ( R . string . no _lesson _today ) ) ) // if there is no lecture at all show the no lesson card
return dayCardView
}
/ * *
* @param text the text to show on the card
* @return a TextView with the text and all needed parameters
* /
2019-03-23 21:07:33 +01:00
private fun getNoCard ( text : String ) : TextView {
val noLesson = TextView ( context )
noLesson . text = text
noLesson . textSize = 18.0F
noLesson . setTypeface ( null , Typeface . BOLD )
noLesson . textAlignment = View . TEXT _ALIGNMENT _CENTER
2020-02-04 19:58:07 +01:00
noLesson . setPadding ( 7 , 7 , 7 , 7 )
2019-03-23 21:07:33 +01:00
return noLesson
2018-10-29 18:03:20 +01:00
}
2018-10-24 18:22:05 +02:00
}