2019-03-17 18:12:36 +01:00
/ * *
* ProjectLaogai
*
* Copyright 2019 < 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 .
*
* /
package org.mosad.seil0.projectlaogai.controller
import android.content.Context
import com.google.gson.Gson
import com.google.gson.JsonParser
import org.mosad.seil0.projectlaogai.hsoparser.Course
import org.mosad.seil0.projectlaogai.hsoparser.MensaWeek
import org.mosad.seil0.projectlaogai.hsoparser.TimetableWeek
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import com.google.gson.reflect.TypeToken
class CacheController ( cont : Context ) {
private val context = cont
companion object {
var coursesList = ArrayList < Course > ( )
var mensaCurrentWeek = MensaWeek ( )
var mensaNextWeek = MensaWeek ( )
var timetables = ArrayList < TimetableWeek > ( )
2019-04-03 19:21:51 +02:00
/ * *
* read current and next weeks mensa menus from the cached file
* /
fun readMensa ( context : Context ) {
val file = File ( context . filesDir , " mensa.json " )
// make sure the file exists
if ( ! file . exists ( ) ) {
TCoRAPIController . getMensa ( context ) . get ( )
}
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
val mensaObject = JsonParser ( ) . parse ( bufferedReader . readLine ( ) ) . asJsonObject
val currentWeek = mensaObject . getAsJsonObject ( " currentWeek " )
val nextWeek = mensaObject . getAsJsonObject ( " nextWeek " )
mensaCurrentWeek = Gson ( ) . fromJson ( currentWeek , MensaWeek ( ) . javaClass )
mensaNextWeek = Gson ( ) . fromJson ( nextWeek , MensaWeek ( ) . javaClass )
}
2019-04-06 14:13:01 +02:00
/ * *
* read the weeks timetable from the cached file
* @param courseName the course name ( e . g AI1 )
* @param week the week to read ( 0 for the current and so on )
* /
fun readTimetable ( courseName : String , week : Int , context : Context ) {
val file = File ( context . filesDir , " timetable- $courseName - $week .json " )
// make sure the file exists
if ( ! file . exists ( ) )
TCoRAPIController . getTimetable ( courseName , week , context ) . get ( )
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
val timetableObject = JsonParser ( ) . parse ( bufferedReader . readLine ( ) ) . asJsonObject
// make sure you add the single weeks in the exact order!
if ( timetables . size == week ) {
timetables . add ( Gson ( ) . fromJson ( timetableObject . getAsJsonObject ( " timetable " ) , TimetableWeek ( ) . javaClass ) )
} else if ( timetables . size >= week ) {
timetables [ week ] = Gson ( ) . fromJson ( timetableObject . getAsJsonObject ( " timetable " ) , TimetableWeek ( ) . javaClass )
}
}
2019-03-17 18:12:36 +01:00
}
/ * *
* read coursesList , mensa ( current and next week ) , timetable ( current and next week )
* @param courseName the course name ( e . g AI1 )
* /
fun readStartCache ( courseName : String ) {
readCoursesList ( )
2019-04-03 19:21:51 +02:00
readMensa ( context )
2019-04-06 14:13:01 +02:00
readTimetable ( courseName , 0 , context )
readTimetable ( courseName , 1 , context )
2019-03-17 18:12:36 +01:00
}
/ * *
* read the courses list from the cached file
* add them to the coursesList object
* /
2019-04-03 19:21:51 +02:00
private fun readCoursesList ( ) {
2019-03-17 18:12:36 +01:00
val file = File ( context . filesDir , " courses.json " )
// make sure the file exists
if ( ! file . exists ( ) )
TCoRAPIController ( context ) . getCoursesList ( ) . get ( )
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
val coursesObject = JsonParser ( ) . parse ( bufferedReader . readLine ( ) ) . asJsonObject
coursesList = Gson ( ) . fromJson ( coursesObject . getAsJsonArray ( " courses " ) , object : TypeToken < List < Course > > ( ) { } . type )
}
}