2018-11-06 16:17:41 +01:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < 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-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.*
2020-02-04 19:58:07 +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.TimetableController
import org.mosad.seil0.projectlaogai.controller.cache.TimetableController.Companion.timetable
2020-08-17 22:30:12 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences
2020-08-11 17:09:46 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.dialogs.AddSubjectDialog
2019-06-26 15:00:43 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
2020-08-17 22:30:12 +02:00
import org.mosad.seil0.projectlaogai.uicomponents.TextViewInfo
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.NotRetardedCalendar
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-08 23:14:55 +02:00
private lateinit var scrollViewTimetable : ScrollView
2020-06-11 21:51:46 +02:00
private lateinit var faBtnAddSubject : 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 ? {
2020-08-17 22:30:12 +02:00
return inflater . inflate ( R . layout . fragment _timetable , container , false )
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2018-10-27 14:11:47 +02:00
2019-06-08 23:14:55 +02:00
scrollViewTimetable = view . findViewById ( R . id . scrollView _Timetable )
2020-06-11 21:51:46 +02:00
faBtnAddSubject = view . findViewById ( R . id . faBtnAddSubject )
2020-08-17 22:30:12 +02:00
refreshLayout _Timetable . setProgressBackgroundColorSchemeColor ( Preferences . themeSecondary )
2018-10-27 14:11:47 +02:00
2020-08-17 22:30:12 +02:00
initActions ( ) // init actions
2019-04-06 14:13:01 +02:00
2020-08-07 12:14:50 +02:00
if ( timetable . size > 1 && timetable [ 0 ] . days . isNotEmpty ( ) && timetable [ 1 ] . days . isNotEmpty ( ) ) {
2020-06-05 20:56:37 +02:00
initTimetable ( )
2018-11-26 11:17:07 +01:00
} else {
2020-08-17 22:30:12 +02:00
val txtViewInfo = TextViewInfo ( context !! ) . set {
txt = resources . getString ( R . string . timetable _generic _error )
} . showImage ( )
linLayout _Timetable . addView ( txtViewInfo )
2018-11-26 11:17:07 +01:00
}
2018-10-27 14:11:47 +02:00
}
2019-06-08 23:14:55 +02:00
/ * *
* initialize the actions
* /
2020-06-05 20:56:37 +02:00
private fun initActions ( ) = GlobalScope . launch ( Dispatchers . Main ) {
2019-06-08 23:14:55 +02:00
2020-06-05 20:56:37 +02:00
refreshLayout _Timetable . setOnRefreshListener {
2020-08-10 14:31:17 +02:00
runBlocking { TimetableController . update ( context !! ) . joinAll ( ) }
reloadTimetableUI ( )
2020-06-05 20:56:37 +02:00
}
2019-06-08 23:14:55 +02:00
2020-06-05 20:56:37 +02:00
// show the AddLessonDialog if the ftaBtn is clicked
2020-06-11 21:51:46 +02:00
faBtnAddSubject . setOnClickListener {
2020-08-11 17:09:46 +02:00
AddSubjectDialog ( context !! )
. positiveButton {
TimetableController . addSubject ( selectedCourse , selectedSubject , context )
runBlocking { reloadTimetableUI ( ) }
} . show ( )
2020-06-05 20:56:37 +02:00
}
2019-06-08 23:14:55 +02:00
2020-06-05 20:56:37 +02:00
// hide the btnCardValue if the user is scrolling down
scrollViewTimetable . setOnScrollChangeListener { _ , _ , scrollY , _ , oldScrollY ->
if ( scrollY > oldScrollY ) {
2020-06-11 21:51:46 +02:00
faBtnAddSubject . hide ( )
2020-06-05 20:56:37 +02:00
} else {
2020-06-11 21:51:46 +02:00
faBtnAddSubject . show ( )
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
* /
2020-06-05 20:56:37 +02:00
private fun initTimetable ( ) = GlobalScope . launch ( Dispatchers . Default ) {
2019-09-22 20:51:36 +02:00
val currentDayIndex = NotRetardedCalendar . getDayOfWeekIndex ( )
2018-11-11 21:12:28 +01:00
2020-06-05 20:56:37 +02:00
addTimetableWeek ( currentDayIndex , 5 , 0 ) . join ( ) // add current week
addTimetableWeek ( 0 , currentDayIndex - 1 , 1 ) // add next week
}
2019-09-22 20:51:36 +02:00
2020-06-05 20:56:37 +02:00
private fun addTimetableWeek ( dayBegin : Int , dayEnd : Int , week : Int ) = GlobalScope . launch ( Dispatchers . Main ) {
for ( dayIndex in dayBegin .. dayEnd ) {
val dayCardView = DayCardView ( context !! )
2018-11-06 16:17:41 +01:00
2020-06-12 00:51:50 +02:00
// some wired calendar magic, calculate the correct date to be shown
// ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
2020-08-10 14:31:17 +02:00
val daysToAdd = ( ( timetable [ week ] . weekNumberYear - NotRetardedCalendar . getWeekOfYear ( ) )
2020-06-12 00:51:50 +02:00
* 7 + ( dayIndex - NotRetardedCalendar . getDayOfWeekIndex ( ) ) )
2020-08-10 14:31:17 +02:00
dayCardView . addTimetableDay ( timetable [ week ] . days [ dayIndex ] , daysToAdd )
2018-11-12 12:32:42 +01:00
2020-06-05 20:56:37 +02: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
2018-11-06 16:17:41 +01:00
}
2019-04-06 14:13:01 +02:00
}
2020-08-10 14:31:17 +02:00
/ * *
* clear linLayout _Timetable , add the updated timetable
* /
2020-08-11 17:09:46 +02:00
private fun reloadTimetableUI ( ) = GlobalScope . launch ( Dispatchers . Default ) {
2020-02-04 19:58:07 +01:00
withContext ( Dispatchers . Main ) {
2020-08-10 14:31:17 +02:00
// remove all lessons from the layout
2019-04-06 14:13:01 +02:00
linLayout _Timetable . removeAllViews ( )
// add the refreshed timetables
2019-09-22 20:51:36 +02:00
val dayIndex = NotRetardedCalendar . getDayOfWeekIndex ( )
2019-04-06 14:13:01 +02:00
2020-06-05 20:56:37 +02:00
addTimetableWeek ( dayIndex , 5 , 0 ) . join ( ) // add current week
addTimetableWeek ( 0 , dayIndex - 1 , 1 ) // add next week
2019-04-06 14:13:01 +02:00
refreshLayout _Timetable . isRefreshing = false
}
}
2018-10-27 14:11:47 +02:00
}