/** * 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.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() var mensaCurrentWeek = MensaWeek() var mensaNextWeek = MensaWeek() var timetables = ArrayList() /** * 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) } } /** * 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) readTimetable(courseName, 1) } /** * 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) } /** * 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) { val file = File(context.filesDir, "timetable-$courseName-$week.json") // make sure the file exists if (!file.exists()) TCoRAPIController(context).getTimetable(courseName, week).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) } } }