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
2019-06-02 19:09:36 +02:00
import com.google.gson.GsonBuilder
2019-03-17 18:12:36 +01:00
import com.google.gson.JsonParser
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import com.google.gson.reflect.TypeToken
2019-06-02 19:09:36 +02:00
import org.mosad.seil0.projectlaogai.hsoparser.*
import kotlin.collections.ArrayList
2019-03-17 18:12:36 +01:00
class CacheController ( cont : Context ) {
private val context = cont
2019-06-24 22:41:45 +02:00
init {
// TODO move cache check here
}
2019-03-17 18:12:36 +01:00
companion object {
var coursesList = ArrayList < Course > ( )
var timetables = ArrayList < TimetableWeek > ( )
2019-06-02 19:09:36 +02:00
var mensaMenu = MensaMenu ( MensaMeta ( 0 , " " ) , MensaWeek ( ) , MensaWeek ( ) )
2019-04-03 19:21:51 +02:00
/ * *
2019-06-02 19:09:36 +02:00
* get the MensaMenu object from the cached json ,
* if cache is empty create the cache file
2019-04-03 19:21:51 +02:00
* /
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
2019-06-02 19:09:36 +02:00
mensaMenu = GsonBuilder ( ) . create ( ) . fromJson ( mensaObject , MensaMenu ( MensaMeta ( 0 , " " ) , MensaWeek ( ) , MensaWeek ( ) ) . javaClass )
2019-04-03 19:21:51 +02:00
}
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 )
}
}