add courses to addLesson Dialog, read lessonSubjects from tcor

This commit is contained in:
Jannik 2020-05-29 16:56:15 +02:00
parent 1ba3f1fa87
commit 34a68ff75d
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
4 changed files with 58 additions and 13 deletions

View File

@ -56,7 +56,7 @@ class CacheController(cont: Context) {
// if a) it's monday and the last cache update was on sunday or b) the cache is older than 24hr, update blocking
if ((currentDay == Calendar.MONDAY && cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || (currentTime - mensaMenu.meta.updateTime) > 86400) {
Log.i(className, "update mensa blocking")
GlobalScope.launch(Dispatchers.Default) { TCoRAPIController.getMensa(context).join() }
GlobalScope.launch(Dispatchers.Default) { TCoRAPIController.getMensaMenu(context).join() }
}
// check if we need to update the timetables before displaying them
@ -84,7 +84,7 @@ class CacheController(cont: Context) {
TCoRAPIController.getCoursesList(context)
if (currentTime - PreferencesController.mensaCacheTime > 10800)
TCoRAPIController.getMensa(context)
TCoRAPIController.getMensaMenu(context)
if (currentTime - PreferencesController.timetableCacheTime > 10800) {
TCoRAPIController.getTimetable(cCourse.courseName, 0, context)
@ -129,7 +129,7 @@ class CacheController(cont: Context) {
// make sure the file exists
if (!file.exists())
runBlocking { TCoRAPIController.getMensa(context).join() }
runBlocking { TCoRAPIController.getMensaMenu(context).join() }
val fileReader = FileReader(file)
val bufferedReader = BufferedReader(fileReader)

View File

@ -24,11 +24,9 @@ package org.mosad.seil0.projectlaogai.controller
import android.content.Context
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import org.json.JSONObject
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
@ -48,7 +46,8 @@ class TCoRAPIController {
private const val tcorBaseURL = "https://tcor.mosad.xyz"
/**
* get the json object from tcor api and write it as file (cache)
* 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 {
val url = URL("$tcorBaseURL/courseList")
@ -81,9 +80,10 @@ class TCoRAPIController {
}
/**
* get the json object from tcor api and write it as file (cache)
* 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 getMensa(context: Context) = GlobalScope.launch {
fun getMensaMenu(context: Context) = GlobalScope.launch {
val url = URL("$tcorBaseURL/mensamenu")
val file = File(context.filesDir, "mensa.json")
@ -115,7 +115,8 @@ class TCoRAPIController {
}
/**
* get the json object from tcor api and write it as file (cache)
* 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 {
val url = URL("$tcorBaseURL/timetable?courseName=$courseName&week=$week")
@ -147,6 +148,19 @@ class TCoRAPIController {
}
}
/**
* TODO
*/
fun getLessonSubjectList(courseName: String, week: Int): Deferred<Array<String>> {
val url = URL("$tcorBaseURL/lessonSubjectList?courseName=$courseName&week=$week")
return GlobalScope.async {
println(courseName)
println(url.readText())
Gson().fromJson(url.readText(), Array<String>::class.java)
}
}
}
}

View File

@ -124,7 +124,7 @@ class MensaFragment : Fragment() {
*/
private fun updateMensaScreen() = GlobalScope.launch(Dispatchers.Default) {
// update the cache
TCoRAPIController.getMensa(context!!).join() // blocking since we want the new data
TCoRAPIController.getMensaMenu(context!!).join() // blocking since we want the new data
CacheController.readMensa(context!!)
withContext(Dispatchers.Main) {

View File

@ -22,12 +22,14 @@
package org.mosad.seil0.projectlaogai.fragments
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.ScrollView
import android.widget.Spinner
import androidx.fragment.app.Fragment
@ -41,11 +43,15 @@ import kotlinx.android.synthetic.main.fragment_timetable.*
import kotlinx.coroutines.*
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.coursesList
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController.Companion.getLessonSubjectList
import org.mosad.seil0.projectlaogai.hsoparser.Course
import org.mosad.seil0.projectlaogai.hsoparser.NotRetardedCalendar
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
import java.util.stream.Collectors
/**
* The timetable controller class
@ -178,9 +184,34 @@ class TimeTableFragment : Fragment() {
val spinnerCourses: Spinner = dialog.getCustomView().findViewById(R.id.spinner_Courses)
val spinnerLessons: Spinner = dialog.getCustomView().findViewById(R.id.spinner_Lessons)
val field1List: List<String>
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
field1List = coursesList.stream().map(Course::courseName).collect(Collectors.toList())
} else {
field1List = ArrayList()
coursesList.forEach { course ->
field1List.add(course.courseName)
}
}
ArrayAdapter(
context!!,
android.R.layout.simple_spinner_item,
field1List
).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinnerCourses.adapter = adapter
}
// FIXME onItemSelectedListener is called on dialog.show
spinnerCourses.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
println(parent.getItemAtPosition(pos))
println("onItemSelected: ${parent.getItemAtPosition(pos)}")
// TODO get Lessons
val lessonSubjects = getLessonSubjectList(parent.getItemAtPosition(pos).toString(), 0)
println(lessonSubjects)
}
override fun onNothingSelected(parent: AdapterView<*>) {