/** * ProjectLaogai * * Copyright 2019 * * 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.GsonBuilder import com.google.gson.JsonParser import java.io.BufferedReader import java.io.File import java.io.FileReader import com.google.gson.reflect.TypeToken import org.mosad.seil0.projectlaogai.hsoparser.* import kotlin.collections.ArrayList class CacheController(cont: Context) { private val context = cont init { // TODO move cache check here } companion object { var coursesList = ArrayList() var timetables = ArrayList() var mensaMenu = MensaMenu(MensaMeta(0, ""), MensaWeek(), MensaWeek()) /** * get the MensaMenu object from the cached json, * if cache is empty create the cache 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 mensaMenu = GsonBuilder().create().fromJson(mensaObject, MensaMenu(MensaMeta(0, ""), MensaWeek(), MensaWeek()).javaClass) } /** * 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) } } } /** * 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() readMensa(context) readTimetable(courseName, 0, context) readTimetable(courseName, 1, context) } /** * read the courses list from the cached file * add them to the coursesList object */ private fun readCoursesList() { 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>() {}.type) } }