2020-06-12 00:51:50 +02:00
/ * *
* ProjectLaogai
*
* Copyright 2019 - 2020 < 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 .
*
* /
2020-08-10 14:31:17 +02:00
package org.mosad.seil0.projectlaogai.controller.cache
2020-06-11 21:41:05 +02:00
2020-08-10 14:31:17 +02:00
import android.content.Context
import kotlinx.coroutines.Job
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
2020-08-13 21:01:21 +02:00
import org.mosad.seil0.projectlaogai.controller.preferences.Preferences.cCourse
2020-06-11 21:41:05 +02:00
import org.mosad.seil0.projectlaogai.util.Lesson
2020-06-12 00:51:50 +02:00
import org.mosad.seil0.projectlaogai.util.TimetableWeek
2020-06-11 21:41:05 +02:00
/ * *
2020-08-10 14:31:17 +02:00
* The TimetableController contains the timetable , subjectMap
* and lessonMap objects . It also contains the additional subjects logic .
* All functions ro read or update cache files are located in the CacheController .
2020-06-12 00:51:50 +02:00
*
2020-08-10 14:31:17 +02:00
* TODO add configurable week to addSubject ( ) and removeSubject ( ) , updateAdditionalLessons ( )
2020-06-11 21:41:05 +02:00
* /
class TimetableController {
companion object {
2020-06-12 00:51:50 +02:00
val timetable = ArrayList < TimetableWeek > ( )
2020-08-07 12:14:50 +02:00
val lessonMap = HashMap < String , Lesson > ( ) // the key is courseName-subject-lessonID
val subjectMap = HashMap < String , ArrayList < String > > ( ) // the key is courseName
2020-06-11 21:41:05 +02:00
2020-08-10 14:31:17 +02:00
/ * *
* update the main timetable and all additional subjects , async
* /
fun update ( context : Context ) : List < Job > {
return listOf (
CacheController . updateTimetable ( cCourse . courseName , 0 , context ) ,
CacheController . updateTimetable ( cCourse . courseName , 1 , context ) ,
CacheController . updateAdditionalLessons ( context )
)
}
2020-06-11 21:41:05 +02:00
/ * *
2020-08-07 12:14:50 +02:00
* add a subject to the subjectMap and all it ' s lessons
* to the lessonMap
2020-06-11 21:41:05 +02:00
* @param courseName course to which the subject belongs
* @param subject the subjects name
* /
2020-08-10 14:31:17 +02:00
fun addSubject ( courseName : String , subject : String , context : Context ) {
2020-06-11 21:41:05 +02:00
// add subject
2020-06-12 00:51:50 +02:00
if ( subjectMap . containsKey ( courseName ) ) {
subjectMap [ courseName ] ?. add ( subject )
2020-06-11 21:41:05 +02:00
} else {
2020-06-12 00:51:50 +02:00
subjectMap [ courseName ] = arrayListOf ( subject )
2020-06-11 21:41:05 +02:00
}
// add concrete lessons
2020-08-10 14:31:17 +02:00
TCoRAPIController . getLessons ( courseName , subject , 0 ) . forEach { lesson ->
addLesson ( courseName , subject , lesson )
2020-06-11 21:41:05 +02:00
}
2020-08-10 14:31:17 +02:00
CacheController . saveAdditionalSubjects ( context )
2020-06-11 21:41:05 +02:00
}
/ * *
2020-08-07 12:14:50 +02:00
* remove a subject from the subjectMap and all it ' s lessons
* from the lessonMap
2020-06-11 21:41:05 +02:00
* @param courseName course to which the subject belongs
* @param subject the subjects name
* /
2020-08-10 14:31:17 +02:00
fun removeSubject ( courseName : String , subject : String , context : Context ) {
2020-06-11 21:41:05 +02:00
// remove subject
2020-06-12 00:51:50 +02:00
subjectMap [ courseName ] ?. remove ( subject )
2020-06-11 21:41:05 +02:00
// remove concrete lessons
2020-06-12 00:51:50 +02:00
val iterator = lessonMap . iterator ( )
while ( iterator . hasNext ( ) ) {
val it = iterator . next ( )
2020-06-11 21:41:05 +02:00
if ( it . key . contains ( " $courseName - $subject " ) ) {
2020-06-12 00:51:50 +02:00
// remove the lesson from the lessons list
iterator . remove ( ) // use iterator to remove, otherwise ConcurrentModificationException
// remove the lesson from the timetable
val id = it . value . lessonID . split ( " . " )
if ( id . size == 3 )
timetable [ 0 ] . days [ id [ 0 ] . toInt ( ) ] . timeslots [ id [ 1 ] . toInt ( ) ] . remove ( it . value )
2020-06-11 21:41:05 +02:00
}
}
2020-08-10 14:31:17 +02:00
CacheController . saveAdditionalSubjects ( context )
}
/ * *
* add a lesson to the lessonMap , also add it to the timetable
* /
fun addLesson ( courseName : String , subject : String , lesson : Lesson ) {
//the courseName, subject and lessonID, separator: -
val key = " $courseName - $subject - ${lesson.lessonID} "
lessonMap [ key ] = lesson
addLessonToTimetable ( lesson )
}
/ * *
* add a lesson to the timetable
* /
fun addLessonToTimetable ( lesson : Lesson ) {
val id = lesson . lessonID . split ( " . " )
if ( id . size == 3 )
timetable [ 0 ] . days [ id [ 0 ] . toInt ( ) ] . timeslots [ id [ 1 ] . toInt ( ) ] . add ( lesson )
2020-06-11 21:41:05 +02:00
}
}
}