2018-11-06 16:17:41 +01:00
/ * *
* ProjectLaogai
*
2019-01-03 13:46:09 +01:00
* Copyright 2019 < seil0 @mosad . xyz >
2018-11-06 16:17:41 +01: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-27 14:11:47 +02:00
2018-11-06 16:17:41 +01:00
package org.mosad.seil0.projectlaogai.fragments
2018-10-27 14:11:47 +02:00
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2019-06-08 23:14:55 +02:00
import android.widget.ScrollView
2019-02-17 15:05:03 +01:00
import androidx.fragment.app.Fragment
2019-04-03 20:37:17 +02:00
import com.afollestad.materialdialogs.MaterialDialog
2019-06-08 23:14:55 +02:00
import com.google.android.material.floatingactionbutton.FloatingActionButton
2019-03-20 21:33:55 +01:00
import kotlinx.android.synthetic.main.fragment_timetable.*
2018-11-06 16:17:41 +01:00
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
2018-10-30 20:41:22 +01:00
import org.mosad.seil0.projectlaogai.R
2019-04-06 14:13:01 +02:00
import org.mosad.seil0.projectlaogai.controller.CacheController
2019-03-17 18:12:36 +01:00
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
2019-04-06 14:13:01 +02:00
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
2018-11-06 16:17:41 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.DataTypes
2018-11-11 21:12:28 +01:00
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
2019-04-03 20:37:17 +02:00
import org.mosad.seil0.projectlaogai.hsoparser.TimetableWeek
2019-06-26 15:00:43 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import org.mosad.seil0.projectlaogai.uicomponents.LessonLinearLayout
2018-11-06 18:17:49 +01:00
import java.text.SimpleDateFormat
2018-11-06 16:17:41 +01:00
import java.util.*
2018-10-27 14:11:47 +02:00
/ * *
* The timetable controller class
* contains all needed parts to display and the timetable detail screen
* /
class TimeTableFragment : Fragment ( ) {
2019-06-03 10:59:32 +02:00
private val formatter = SimpleDateFormat ( " E dd.MM " , Locale . getDefault ( ) )
2019-06-08 23:14:55 +02:00
private lateinit var scrollViewTimetable : ScrollView
private lateinit var faBtnAddLesson : FloatingActionButton
2019-04-03 20:37:17 +02:00
2018-10-27 14:11:47 +02:00
override fun onCreateView ( inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ) : View ? {
2019-03-20 17:55:30 +01:00
val view : View = inflater . inflate ( R . layout . fragment _timetable , container , false )
2019-06-08 23:14:55 +02:00
scrollViewTimetable = view . findViewById ( R . id . scrollView _Timetable )
faBtnAddLesson = view . findViewById ( R . id . faBtnAddLesson )
2018-10-27 14:11:47 +02:00
2019-04-06 14:13:01 +02:00
// init actions
2019-06-08 23:14:55 +02:00
initActions ( )
2019-04-06 14:13:01 +02:00
2019-06-26 15:00:43 +02:00
if ( timetables [ 0 ] . timetable . days . isNotEmpty ( ) && timetables [ 1 ] . timetable . days . isNotEmpty ( ) ) {
2019-04-03 20:37:17 +02:00
addInitWeeks ( )
2018-11-26 11:17:07 +01:00
} else {
2019-04-03 20:37:17 +02:00
MaterialDialog ( context !! )
. title ( R . string . error )
. message ( R . string . timetable _error )
. show ( )
2018-11-26 11:17:07 +01:00
}
2018-10-27 14:11:47 +02:00
return view
}
2019-06-08 23:14:55 +02:00
/ * *
* initialize the actions
* /
private fun initActions ( ) = doAsync {
uiThread {
refreshLayout _Timetable . setOnRefreshListener {
updateTimetableScreen ( )
}
faBtnAddLesson . setOnClickListener {
MaterialDialog ( context !! )
. title ( text = " Vorlesung hinzufügen " )
. message ( text = " wähle einen Studiengang aus: \n \n Wähle eine Vorlesung aus: \n \n Diese Funktion ist noch nicht verfügbar " )
. show ( )
}
// hide the btnCardValue if the user is scrolling down
2019-06-25 12:07:18 +02:00
scrollViewTimetable . setOnScrollChangeListener { _ , _ , scrollY , _ , oldScrollY ->
2019-06-24 22:41:45 +02:00
if ( scrollY > oldScrollY ) {
faBtnAddLesson . hide ( )
} else {
faBtnAddLesson . show ( )
2019-06-08 23:14:55 +02:00
}
}
2019-06-24 22:41:45 +02:00
2019-06-08 23:14:55 +02:00
}
}
2018-11-06 18:17:49 +01:00
/ * *
2019-04-03 20:37:17 +02:00
* add the current and next weeks lessons
2018-11-06 18:17:49 +01:00
* /
2019-04-03 20:37:17 +02:00
private fun addInitWeeks ( ) = doAsync {
2018-11-11 21:12:28 +01:00
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
2018-11-12 12:32:42 +01:00
val calendar = Calendar . getInstance ( )
2018-11-11 21:12:28 +01:00
2019-04-03 20:37:17 +02:00
// add current week
2019-06-26 15:00:43 +02:00
addWeek ( dayIndex , 5 , timetables [ 0 ] . timetable , calendar ) . get ( )
2019-04-03 20:37:17 +02:00
// add next week
2019-06-26 15:00:43 +02:00
addWeek ( 0 , dayIndex - 1 , timetables [ 1 ] . timetable , calendar )
2019-04-03 20:37:17 +02:00
}
private fun addWeek ( dayStart : Int , dayEnd : Int , timetable : TimetableWeek , calendar : Calendar ) = doAsync {
2019-03-23 21:07:33 +01:00
uiThread {
2018-11-06 16:17:41 +01:00
2019-04-03 20:37:17 +02:00
for ( day in dayStart .. dayEnd ) {
2019-03-23 21:07:33 +01:00
var helpLesson = LessonLinearLayout ( context )
val dayCardView = DayCardView ( context !! )
dayCardView . setDayHeading ( formatter . format ( calendar . time ) )
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
// for each timeslot of the day
2019-04-03 20:37:17 +02:00
for ( ( tsIndex , timeslot ) in timetable . days [ day ] . timeslots . withIndex ( ) ) {
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
for ( lesson in timeslot ) {
2019-03-01 18:59:17 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson . lessonSubject . isNotEmpty ( ) ) {
2018-11-06 16:17:41 +01:00
2019-03-23 21:07:33 +01:00
val lessonLayout = LessonLinearLayout ( context )
lessonLayout . setLesson ( lesson , DataTypes ( ) . getTime ( ) [ tsIndex ] )
dayCardView . getLinLayoutDay ( ) . addView ( lessonLayout )
2019-03-01 18:59:17 +01:00
2019-03-23 21:07:33 +01:00
if ( lesson != timeslot . last ( ) )
lessonLayout . disableDivider ( )
2019-03-03 20:46:51 +01:00
2019-03-23 21:07:33 +01:00
helpLesson = lessonLayout
2019-03-20 21:33:55 +01:00
}
2018-11-06 16:17:41 +01:00
}
}
2019-03-23 21:07:33 +01:00
helpLesson . disableDivider ( )
calendar . add ( Calendar . DATE , 1 )
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
// if there are no lessons don't show the dayCardView
if ( dayCardView . getLinLayoutDay ( ) . childCount > 1 )
linLayout _Timetable . addView ( dayCardView )
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
}
2018-11-12 12:32:42 +01:00
2019-03-23 21:07:33 +01:00
calendar . add ( Calendar . DATE , 1 ) // before this we are at a sunday (no lecture on sundays!)
2018-11-06 16:17:41 +01:00
}
2019-04-06 14:13:01 +02:00
}
private fun updateTimetableScreen ( ) = doAsync {
// update the cache
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context !! ) . get ( ) // blocking since we want the new data
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context !! ) . get ( ) // blocking since we want the new data
CacheController . readTimetable ( cCourse . courseName , 0 , context !! )
CacheController . readTimetable ( cCourse . courseName , 1 , context !! )
uiThread {
// remove all menus from the layout
linLayout _Timetable . removeAllViews ( )
// add the refreshed timetables
val dayIndex = NotRetardedCalendar ( ) . getDayOfWeekIndex ( )
val calendar = Calendar . getInstance ( )
// add current week
2019-06-26 15:00:43 +02:00
addWeek ( dayIndex , 5 , timetables [ 0 ] . timetable , calendar ) . get ( )
2019-04-06 14:13:01 +02:00
// add next week
2019-06-26 15:00:43 +02:00
addWeek ( 0 , dayIndex - 1 , timetables [ 1 ] . timetable , calendar )
2019-04-06 14:13:01 +02:00
refreshLayout _Timetable . isRefreshing = false
}
}
2018-10-27 14:11:47 +02:00
}