2019-03-17 18:12:36 +01:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2019-03-17 18:12:36 +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 .
*
* /
package org.mosad.seil0.projectlaogai.controller
import android.content.Context
2020-02-05 14:10:45 +01:00
import android.util.Log
2019-03-17 18:12:36 +01:00
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
2019-06-26 15:00:43 +02:00
import com.google.gson.reflect.TypeToken
2020-02-18 15:18:36 +01:00
import kotlinx.coroutines.*
2019-06-26 15:00:43 +02:00
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
2019-10-17 19:19:47 +02:00
import org.mosad.seil0.projectlaogai.hsoparser.Course
import org.mosad.seil0.projectlaogai.hsoparser.MensaMenu
import org.mosad.seil0.projectlaogai.hsoparser.TimetableCourseWeek
2019-03-17 18:12:36 +01:00
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
2019-06-26 15:00:43 +02:00
import java.util.*
2019-06-02 19:09:36 +02:00
import kotlin.collections.ArrayList
2019-03-17 18:12:36 +01:00
class CacheController ( cont : Context ) {
2020-02-05 14:10:45 +01:00
private val className = " CacheController "
2019-03-17 18:12:36 +01:00
private val context = cont
2019-06-24 22:41:45 +02:00
init {
2019-06-26 15:00:43 +02:00
val cal = Calendar . getInstance ( )
val currentDay = Calendar . getInstance ( ) . get ( Calendar . DAY _OF _WEEK )
val currentTime = System . currentTimeMillis ( ) / 1000
// check if we need to update the mensa data before displaying it
readMensa ( context )
cal . time = Date ( mensaMenu . meta . updateTime * 1000 )
// if a) it's monday and the last cache update was on sunday or b) the cache is older than 24hr, update blocking
2019-07-01 20:31:06 +02:00
if ( ( currentDay == Calendar . MONDAY && cal . get ( Calendar . DAY _OF _WEEK ) == Calendar . SUNDAY ) || ( currentTime - mensaMenu . meta . updateTime ) > 86400 ) {
2020-02-05 14:10:45 +01:00
Log . i ( className , " update mensa blocking " )
2020-05-29 16:56:15 +02:00
GlobalScope . launch ( Dispatchers . Default ) { TCoRAPIController . getMensaMenu ( context ) . join ( ) }
2019-06-26 15:00:43 +02:00
}
// check if we need to update the timetables before displaying them
readTimetable ( cCourse . courseName , 0 , context )
cal . time = Date ( timetables [ 0 ] . meta . updateTime * 1000 )
2019-07-01 20:31:06 +02:00
// if a) it`s monday and the last cache update was not on a sunday or b) the cache is older than 24hr, update blocking
2020-02-04 19:58:07 +01:00
if ( ( currentDay == Calendar . MONDAY && cal . get ( Calendar . DAY _OF _WEEK ) == Calendar . SUNDAY ) || ( currentTime - timetables [ 0 ] . meta . updateTime ) > 86400 ) {
2020-02-05 14:10:45 +01:00
Log . i ( className , " updating timetable after sunday! " )
2019-06-26 15:00:43 +02:00
2020-02-04 19:58:07 +01:00
GlobalScope . launch ( Dispatchers . Default ) {
val threads = listOf (
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context ) ,
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context )
)
threads . joinAll ( )
}
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context )
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context )
2019-06-26 15:00:43 +02:00
}
// check if an update is necessary, not blocking
2020-02-04 19:58:07 +01:00
if ( currentTime - PreferencesController . coursesCacheTime > 86400 )
2019-06-26 15:00:43 +02:00
TCoRAPIController . getCoursesList ( context )
2020-02-04 19:58:07 +01:00
if ( currentTime - PreferencesController . mensaCacheTime > 10800 )
2020-05-29 16:56:15 +02:00
TCoRAPIController . getMensaMenu ( context )
2019-06-26 15:00:43 +02:00
2020-02-04 19:58:07 +01:00
if ( currentTime - PreferencesController . timetableCacheTime > 10800 ) {
2019-06-26 15:00:43 +02:00
TCoRAPIController . getTimetable ( cCourse . courseName , 0 , context )
TCoRAPIController . getTimetable ( cCourse . courseName , 1 , context )
}
readStartCache ( cCourse . courseName )
2019-06-24 22:41:45 +02:00
}
2019-03-17 18:12:36 +01:00
companion object {
var coursesList = ArrayList < Course > ( )
2019-06-26 15:00:43 +02:00
var timetables = ArrayList < TimetableCourseWeek > ( )
var mensaMenu = MensaMenu ( )
/ * *
* read the courses list from the cached file
* add them to the coursesList object
* /
private fun readCoursesList ( context : Context ) {
val file = File ( context . filesDir , " courses.json " )
// make sure the file exists
if ( ! file . exists ( ) )
2020-02-18 15:18:36 +01:00
runBlocking { TCoRAPIController . getCoursesList ( context ) . join ( ) }
2019-06-26 15:00:43 +02:00
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
2020-01-15 15:08:05 +01:00
val coursesObject = JsonParser . parseString ( bufferedReader . readLine ( ) ) . asJsonObject
2019-06-26 15:00:43 +02:00
2020-02-04 19:58:07 +01:00
coursesList = Gson ( ) . fromJson (
coursesObject . getAsJsonArray ( " courses " ) ,
object : TypeToken < List < Course > > ( ) { } . type
)
2019-06-26 15:00:43 +02:00
}
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
2020-02-04 19:58:07 +01:00
if ( ! file . exists ( ) )
2020-05-29 16:56:15 +02:00
runBlocking { TCoRAPIController . getMensaMenu ( context ) . join ( ) }
2019-04-03 19:21:51 +02:00
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
2020-01-15 15:08:05 +01:00
val mensaObject = JsonParser . parseString ( bufferedReader . readLine ( ) ) . asJsonObject
2019-04-03 19:21:51 +02:00
2019-06-26 15:00:43 +02:00
mensaMenu = GsonBuilder ( ) . create ( ) . fromJson ( mensaObject , MensaMenu ( ) . 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
2020-02-18 15:18:36 +01:00
if ( ! file . exists ( ) )
runBlocking { TCoRAPIController . getTimetable ( courseName , week , context ) . join ( ) }
2019-04-06 14:13:01 +02:00
val fileReader = FileReader ( file )
val bufferedReader = BufferedReader ( fileReader )
2020-01-15 15:08:05 +01:00
val timetableObject = JsonParser . parseString ( bufferedReader . readLine ( ) ) . asJsonObject
2019-04-06 14:13:01 +02:00
// make sure you add the single weeks in the exact order!
2019-06-26 15:00:43 +02:00
if ( timetables . size > week ) {
timetables [ week ] = ( Gson ( ) . fromJson ( timetableObject , TimetableCourseWeek ( ) . javaClass ) )
} else {
timetables . add ( Gson ( ) . fromJson ( timetableObject , TimetableCourseWeek ( ) . javaClass ) )
2019-04-06 14:13:01 +02:00
}
}
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 )
* /
2020-02-05 14:10:45 +01:00
private fun readStartCache ( courseName : String ) {
2019-06-26 15:00:43 +02:00
readCoursesList ( context )
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
}
}