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
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
2018-11-01 02:12:43 +01:00
import org.mosad.seil0.projectlaogai.MainActivity
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
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
2018-11-01 02:12:43 +01:00
private var mainActivity = MainActivity ( )
2018-10-24 18:22:05 +02:00
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 ( )
if ( cal . get ( Calendar . HOUR _OF _DAY ) < 15 ) {
dayMenus = MensaParser ( ) . getMensaMenuDay ( mainActivity . getWeekMenu ( ) , cal . get ( Calendar . DAY _OF _WEEK ) )
} else {
dayMenus = MensaParser ( ) . getMensaMenuDay ( mainActivity . getWeekMenu ( ) , cal . get ( Calendar . DAY _OF _WEEK ) + 1 )
uiThread {
2018-11-16 14:19:24 +01:00
// TODO Mensa closed today is showing
2018-11-13 16:17:05 +01:00
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 ( ) ) {
if ( meal . heading . contains ( " Essen " ) ) {
for ( part in dayMenus [ i ] . parts ) {
txtViewMenu1 . append ( part )
}
for ( part in dayMenus [ i + 1 ] . parts ) {
txtViewMenu2 . append ( part )
}
break @loop
}
2018-11-01 01:06:53 +01:00
}
} 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
2018-11-12 12:32:42 +01:00
if ( mainActivity . getTimeTableCurrentWeek ( ) . isNotEmpty ( ) && dayIndex < 6 ) {
2018-11-11 21:12:28 +01:00
2018-11-12 12:32:42 +01:00
val timeTableDay = mainActivity . getTimeTableCurrentWeek ( ) [ dayIndex ]
2018-10-24 18:22:05 +02:00
2018-11-06 10:47:51 +01:00
for ( i in 0. . 5 ) {
val lessonCardView = LessonCardView ( context !! , null )
2018-10-24 18:22:05 +02:00
2018-11-06 10:47:51 +01:00
lessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . string _new _line , timeTableDay [ i ] . lessonSubject )
lessonCardView . getTxtViewLesson ( ) . append ( timeTableDay [ i ] . lessonTeacher + " \n " )
lessonCardView . getTxtViewLesson ( ) . append ( timeTableDay [ i ] . lessonRoom )
lessonCardView . getTxtViewTime ( ) . text = DataTypes ( ) . getTime ( ) [ i ]
if ( lessonCardView . getTxtViewLesson ( ) . text . length > 2 )
linLayoutTimeTable . addView ( lessonCardView )
}
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
val noLessonCardView = LessonCardView ( context !! , null )
noLessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . no _lesson _today )
linLayoutTimeTable . addView ( noLessonCardView )
}
} else {
2018-11-11 21:12:28 +01:00
if ( dayIndex == 6 ) {
// if that's the case it's sunday
val noLessonCardView = LessonCardView ( context !! , null )
noLessonCardView . getTxtViewLesson ( ) . text = resources . getString ( R . string . no _lesson _today )
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
2018-11-01 02:12:43 +01:00
fun setMainActivity ( mainActivity : MainActivity ) {
this . mainActivity = mainActivity
2018-10-24 18:22:05 +02:00
}
}