fix crash on first start, if tcor is not reachable

This commit is contained in:
Jannik 2020-02-27 12:09:24 +01:00
parent ccc0f0f2bc
commit faa07966da
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
2 changed files with 42 additions and 12 deletions

View File

@ -24,16 +24,22 @@ package org.mosad.seil0.projectlaogai.controller
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import kotlinx.coroutines.* import com.google.gson.GsonBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject import org.json.JSONObject
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
import org.mosad.seil0.projectlaogai.hsoparser.CoursesList
import org.mosad.seil0.projectlaogai.hsoparser.MensaMenu
import org.mosad.seil0.projectlaogai.hsoparser.TimetableCourseWeek
import java.io.BufferedWriter import java.io.BufferedWriter
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.net.URL import java.net.URL
import kotlin.Exception
class TCoRAPIController { class TCoRAPIController {
@ -45,10 +51,10 @@ class TCoRAPIController {
* get the json object from tcor api and write it as file (cache) * get the json object from tcor api and write it as file (cache)
*/ */
fun getCoursesList(context: Context) = GlobalScope.launch { fun getCoursesList(context: Context) = GlobalScope.launch {
try { val url = URL("$tcorBaseURL/courseList")
val url = URL("$tcorBaseURL/courseList") val file = File(context.filesDir, "courses.json")
val file = File(context.filesDir, "courses.json")
try {
// read data from the API // read data from the API
val coursesObject = JSONObject(url.readText()) //JSONObject(inReader.readLine()) val coursesObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
@ -64,18 +70,24 @@ class TCoRAPIController {
PreferencesController.save(context) PreferencesController.save(context)
} catch (ex: Exception) { } catch (ex: Exception) {
Log.e(className, "failed to get /courseList", ex) Log.e(className, "failed to get /courseList", ex)
}
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(CoursesList()))
writer.close()
}
}
} }
/** /**
* get the json object from tcor api and write it as file (cache) * get the json object from tcor api and write it as file (cache)
*/ */
fun getMensa(context: Context) = GlobalScope.launch { fun getMensa(context: Context) = GlobalScope.launch {
try { val url = URL("$tcorBaseURL/mensamenu")
val url = URL("$tcorBaseURL/mensamenu") val file = File(context.filesDir, "mensa.json")
val file = File(context.filesDir, "mensa.json")
try {
// read data from the API // read data from the API
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine()) val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
@ -91,6 +103,13 @@ class TCoRAPIController {
PreferencesController.save(context) PreferencesController.save(context)
} catch (ex: Exception) { } catch (ex: Exception) {
Log.e(className, "failed to get /mensamenu", ex) Log.e(className, "failed to get /mensamenu", ex)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(MensaMenu()))
writer.close()
}
} }
} }
@ -99,10 +118,10 @@ class TCoRAPIController {
* get the json object from tcor api and write it as file (cache) * get the json object from tcor api and write it as file (cache)
*/ */
fun getTimetable(courseName: String, week: Int, context: Context) = GlobalScope.launch { fun getTimetable(courseName: String, week: Int, context: Context) = GlobalScope.launch {
try { val url = URL("$tcorBaseURL/timetable?courseName=$courseName&week=$week")
val url = URL("$tcorBaseURL/timetable?courseName=$courseName&week=$week") val file = File(context.filesDir, "timetable-$courseName-$week.json")
val file = File(context.filesDir, "timetable-$courseName-$week.json")
try {
// read data from the API // read data from the API
val timetableObject = JSONObject(url.readText()) //JSONObject(inReader.readLine()) val timetableObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
@ -118,6 +137,13 @@ class TCoRAPIController {
PreferencesController.save(context) PreferencesController.save(context)
} catch (ex: Exception) { } catch (ex: Exception) {
Log.e(className, "failed to get /timetable", ex) Log.e(className, "failed to get /timetable", ex)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(TimetableCourseWeek()))
writer.close()
}
} }
} }

View File

@ -124,6 +124,10 @@ class NotRetardedCalendar {
// data classes for the course part // data classes for the course part
data class Course(val courseLink: String, val courseName: String) data class Course(val courseLink: String, val courseName: String)
data class CoursesMeta(val updateTime: Long = 0, val totalCourses: Int = 0)
data class CoursesList(val meta: CoursesMeta = CoursesMeta(), val courses: ArrayList<Course> = ArrayList())
// data classes for the Mensa part // data classes for the Mensa part
data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String) data class Meal(val day: String, val heading: String, val parts: ArrayList<String>, val additives: String)