2018-10-24 18:22:05 +02:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* 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 android.widget.LinearLayout
2018-11-01 02:12:43 +01:00
import androidx.fragment.app.Fragment
2018-11-06 10:47:51 +01:00
import com.afollestad.materialdialogs.MaterialDialog
2018-10-29 18:03:20 +01:00
import kotlinx.android.synthetic.main.fragment_home.*
2018-10-29 13:04:20 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2019-02-17 15:05:03 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cTimeTableCurrentWeek
2019-03-09 22:15:53 +01:00
import org.mosad.seil0.projectlaogai.PreferencesController.Companion.cMenusCurrentWeek
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2018-11-11 21:12:28 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.*
2018-10-29 13:04:20 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.LessonCardView
2019-03-03 20:46:51 +01:00
import org.mosad.seil0.projectlaogai.uicomponents.LessonTextView
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 ( ) {
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
2018-10-29 18:03:20 +01:00
linLayoutTimeTable = view . findViewById ( R . id . linLayoutTimeTable )
2018-10-24 18:22:05 +02:00
2018-10-29 13:04:20 +01:00
//setText()
2018-10-29 18:03:20 +01:00
addCurrentMensaMenu ( )
addCurrentTimeTable ( )
2018-10-24 18:22:05 +02:00
// Inflate the layout for this fragment
return view
}
2018-11-05 20:59:41 +01:00
/ * *
* add the current mensa meal to the home screens
* /
2018-10-29 18:03:20 +01:00
private fun addCurrentMensaMenu ( ) {
2018-11-05 20:59:41 +01:00
doAsync {
2018-11-13 16:17:05 +01:00
val dayMenus : ArrayList < Meal >
val cal = Calendar . getInstance ( )
2019-03-10 13:37:13 +01:00
2018-11-13 16:17:05 +01:00
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
2019-03-10 13:37:13 +01:00
dayMenus = cMenusCurrentWeek . day [ NotRetardedCalendar ( ) . getDayOfWeekIndex ( ) ]
2018-11-13 16:17:05 +01:00
} else {
2019-03-10 13:37:13 +01:00
dayMenus = cMenusCurrentWeek . day [ NotRetardedCalendar ( ) . getTomorrowWeekIndex ( ) ]
2018-11-13 16:17:05 +01:00
uiThread {
txtView _Menu1Heading . text = resources . getString ( R . string . meal _1 _tomorrow )
txtView _Menu2Heading . text = resources . getString ( R . string . meal _2 _tomorrow )
}
}
2018-10-29 13:04:20 +01:00
uiThread {
2018-11-01 01:06:53 +01:00
if ( dayMenus . size >= 2 ) {
2018-11-16 14:19:24 +01:00
// get the index of the first meal, not a "Schneller Teller"
loop @ for ( ( i , meal ) in dayMenus . withIndex ( ) ) {
2018-12-02 00:05:23 +01:00
if ( meal . heading . contains ( " Essen " ) ) {
2018-11-26 11:17:07 +01:00
for ( ( j , part ) in dayMenus [ i ] . parts . withIndex ( ) ) {
2018-11-16 14:19:24 +01:00
txtViewMenu1 . append ( part )
2018-12-02 00:05:23 +01:00
if ( j < ( dayMenus [ i ] . parts . size - 2 ) )
2018-11-26 11:17:07 +01:00
txtViewMenu1 . append ( " \n " )
2018-11-16 14:19:24 +01:00
}
2018-11-26 11:17:07 +01:00
for ( ( j , part ) in dayMenus [ i + 1 ] . parts . withIndex ( ) ) {
2018-11-16 14:19:24 +01:00
txtViewMenu2 . append ( part )
2018-12-02 00:05:23 +01:00
if ( j < ( dayMenus [ i + 1 ] . parts . size - 2 ) )
2018-11-26 11:17:07 +01:00
txtViewMenu2 . append ( " \n " )
2018-11-16 14:19:24 +01:00
}
break @loop
}
2018-11-01 01:06:53 +01:00
}
} else {
2018-12-02 00:05:23 +01:00
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 )
}
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
* /
2018-10-29 18:03:20 +01:00
private fun addCurrentTimeTable ( ) {
2018-11-11 21:12:28 +01:00
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
2018-10-24 18:22:05 +02:00
2019-03-01 18:59:17 +01:00
if ( cTimeTableCurrentWeek . days . isNotEmpty ( ) && dayIndex < 6 ) {
2018-11-11 21:12:28 +01:00
2019-03-01 18:59:17 +01:00
val timeTableDay = cTimeTableCurrentWeek . days [ dayIndex ]
2018-10-24 18:22:05 +02:00
2019-03-01 18:59:17 +01:00
// for all timeslots of the day
for ( ( i , timeslot ) in timeTableDay . timeslots . withIndex ( ) ) {
2019-03-03 20:46:51 +01:00
val lessonCardView = LessonCardView ( context !! , null )
lessonCardView . getTxtViewTime ( ) . text = DataTypes ( ) . getTime ( ) [ i ]
2018-10-24 18:22:05 +02:00
2019-03-01 18:59:17 +01:00
for ( lesson in timeslot ) {
2019-03-03 20:46:51 +01:00
val lessonTxtView = LessonTextView ( context !! )
lessonTxtView . setLesson ( lesson )
2019-03-01 18:59:17 +01:00
2019-03-03 20:46:51 +01:00
// TODO why does this exist
if ( lessonTxtView . text . length > 3 )
lessonCardView . getLinLayoutLesson ( ) . addView ( lessonTxtView )
2019-03-01 18:59:17 +01:00
}
2018-11-06 10:47:51 +01:00
2019-03-03 20:46:51 +01:00
if ( lessonCardView . getLinLayoutLesson ( ) . childCount > 2 )
linLayoutTimeTable . addView ( lessonCardView )
2018-11-06 10:47:51 +01:00
}
2018-11-01 01:06:53 +01:00
2018-11-06 10:47:51 +01:00
// add a card if there is no lesson today
if ( linLayoutTimeTable . childCount == 0 ) {
// TODO we could display the next day with a lecture
2019-03-03 20:46:51 +01:00
val lessonTxtView = LessonTextView ( context !! )
lessonTxtView . setText ( resources . getString ( R . string . no _lesson _today ) )
2018-11-06 10:47:51 +01:00
val noLessonCardView = LessonCardView ( context !! , null )
2019-03-03 20:46:51 +01:00
noLessonCardView . getLinLayoutLesson ( ) . addView ( lessonTxtView )
2018-11-06 10:47:51 +01:00
linLayoutTimeTable . addView ( noLessonCardView )
}
} else {
2018-11-11 21:12:28 +01:00
if ( dayIndex == 6 ) {
// if that's the case it's sunday
2019-03-03 20:46:51 +01:00
val lessonTxtView = LessonTextView ( context !! )
lessonTxtView . setText ( resources . getString ( R . string . no _lesson _today ) )
2018-11-11 21:12:28 +01:00
val noLessonCardView = LessonCardView ( context !! , null )
2019-03-03 20:46:51 +01:00
noLessonCardView . getLinLayoutLesson ( ) . addView ( lessonTxtView )
2018-11-11 21:12:28 +01:00
linLayoutTimeTable . addView ( noLessonCardView )
} else {
MaterialDialog ( context !! )
. title ( R . string . error )
. message ( R . string . gen _tt _error )
. show ( )
// TODO log the error and send feedback
}
2018-11-01 01:06:53 +01:00
}
2018-10-29 18:03:20 +01:00
}
2018-10-24 18:22:05 +02:00
}