Browse Source

fix warings in TCoRAPIController

pull/46/head
Jannik 2 years ago
parent
commit
72e9efb9e7
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 97
      app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt
  2. 24
      app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt

97
app/src/main/java/org/mosad/seil0/projectlaogai/controller/TCoRAPIController.kt

@ -32,6 +32,7 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
import org.mosad.seil0.projectlaogai.hsoparser.CoursesList
import org.mosad.seil0.projectlaogai.hsoparser.Lesson
import org.mosad.seil0.projectlaogai.hsoparser.MensaMenu
import org.mosad.seil0.projectlaogai.hsoparser.TimetableCourseWeek
import java.io.BufferedWriter
@ -49,29 +50,27 @@ class TCoRAPIController {
* Get a array of all currently available courses at the tcor API.
* Read the json object from tcor api and write it as file (cache).
*/
fun getCoursesList(context: Context) = GlobalScope.launch {
fun getCoursesList(context: Context): Job {
val url = URL("$tcorBaseURL/courseList")
val file = File(context.filesDir, "courses.json")
try {
// read data from the API
val coursesObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
return GlobalScope.launch(Dispatchers.IO) {
try {
// read data from the API
val coursesObject = JSONObject(url.readText())
// write the json object to a file
withContext(Dispatchers.IO) {
// write the json object to a file
val writer = BufferedWriter(FileWriter(file))
writer.write(coursesObject.toString())
writer.close()
}
// update cache time
coursesCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /courseList", ex)
// update cache time
coursesCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /courseList", ex)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
// write a empty file, since it is loaded later
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(CoursesList()))
writer.close()
@ -83,29 +82,27 @@ class TCoRAPIController {
* Get this and next weeks mensa menus from the tcor API.
* Read the json object from tcor api and write it as file (cache).
*/
fun getMensaMenu(context: Context) = GlobalScope.launch {
fun getMensaMenu(context: Context): Job {
val url = URL("$tcorBaseURL/mensamenu")
val file = File(context.filesDir, "mensa.json")
try {
// read data from the API
val mensaObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
return GlobalScope.launch(Dispatchers.IO) {
try {
// read data from the API
val mensaObject = JSONObject(url.readText())
// write the json object to a file
withContext(Dispatchers.IO) {
// write the json object to a file
val writer = BufferedWriter(FileWriter(file))
writer.write(mensaObject.toString())
writer.close()
}
// update cache time
mensaCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /mensamenu", ex)
// update cache time
mensaCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /mensamenu", ex)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
// write a empty file, since it is loaded later
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(MensaMenu()))
writer.close()
@ -118,29 +115,27 @@ class TCoRAPIController {
* Get the timetable for @param courseName at week @param week
* Read 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): Job {
val url = URL("$tcorBaseURL/timetable?courseName=$courseName&week=$week")
val file = File(context.filesDir, "timetable-$courseName-$week.json")
try {
// read data from the API
val timetableObject = JSONObject(url.readText()) //JSONObject(inReader.readLine())
return GlobalScope.launch(Dispatchers.IO) {
try {
// read data from the API
val timetableObject = JSONObject(url.readText())
// write the json object to a file
withContext(Dispatchers.IO) {
// write the json object to a file
val writer = BufferedWriter(FileWriter(file))
writer.write(timetableObject.toString())
writer.close()
}
// update cache time
timetableCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /timetable", ex)
// update cache time
timetableCacheTime = System.currentTimeMillis() / 1000
PreferencesController.save(context)
} catch (ex: Exception) {
Log.e(className, "failed to get /timetable", ex)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
// write a empty file, since it is loaded later
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(TimetableCourseWeek()))
writer.close()
@ -150,7 +145,9 @@ class TCoRAPIController {
}
/**
* TODO
* Get all lessons for a course at one week (async)
* @param courseName the course name
* @param week the week to look up
*/
fun getLessonSubjectListAsync(courseName: String, week: Int): Deferred<ArrayList<String>> {
val url = URL("$tcorBaseURL/lessonSubjectList?courseName=$courseName&week=$week")
@ -159,6 +156,20 @@ class TCoRAPIController {
Gson().fromJson(url.readText(), ArrayList<String>()::class.java)
}
}
/**
* Get all occurrences of a lesson for a course at one week
* @param courseName the course name
* @param lessonSubject the subject to search for
* @param week the week to look up
*/
fun getLessonsAsync(courseName: String, lessonSubject: String, week: Int): Deferred<ArrayList<Lesson>> {
val url = URL("$tcorBaseURL/lessons?courseName=$courseName&lessonSubject=$lessonSubject&week=$week")
return GlobalScope.async {
Gson().fromJson(url.readText(), ArrayList<Lesson>()::class.java)
}
}
}
}

24
app/src/main/java/org/mosad/seil0/projectlaogai/uicomponents/AddLessonDialog.kt

@ -47,12 +47,13 @@ class AddLessonDialog(_context: Context) {
private val context = _context
private lateinit var spinnerCourses: Spinner
private lateinit var spinnerLessons: Spinner
private lateinit var spinnerSubjects: Spinner
private val lessonSubjectsList = ArrayList<String>()
private val subjectsList = ArrayList<String>()
private val courseNamesList = getCourseNames()
var selectedCourse = ""
var selectedSubject = ""
/**
* create a new AddLessonDialog (BottomSheet)
@ -64,22 +65,29 @@ class AddLessonDialog(_context: Context) {
.customView(R.layout.dialog_add_lesson)
.setPeekHeight(900)
.positiveButton(R.string.add) {
println("add course \"$selectedCourse\"")
println("add lesson \"$selectedCourse: $selectedSubject\"")
// TODO call add function
val test = runBlocking {
TCoRAPIController.getLessonsAsync(selectedCourse, selectedSubject, 0).await()
}
println(test.toString())
}
.negativeButton(R.string.cancel) { }
// initialize the spinners
spinnerCourses = dialog.getCustomView().findViewById(R.id.spinner_Courses)
spinnerLessons = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
spinnerSubjects = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
setArrayAdapter(spinnerCourses, courseNamesList)
val lessonsAdapter = setArrayAdapter(spinnerLessons, lessonSubjectsList)
val lessonsAdapter = setArrayAdapter(spinnerSubjects, subjectsList)
spinnerCourses.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerCourses.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
selectedCourse = parent.getItemAtPosition(pos).toString()
// TODO show loading dialog while loading
val lessonSubjects = runBlocking {
TCoRAPIController.getLessonSubjectListAsync(parent.getItemAtPosition(pos).toString(), 0).await()
}
@ -94,10 +102,10 @@ class AddLessonDialog(_context: Context) {
}
}
spinnerLessons.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerLessons.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
spinnerSubjects.setSelection(0,false) // don't call onItemSelected() on spinnerCourses.onItemSelectedListener
spinnerSubjects.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
selectedCourse = parent.getItemAtPosition(pos).toString()
selectedSubject = parent.getItemAtPosition(pos).toString()
}
override fun onNothingSelected(parent: AdapterView<*>) {

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close