more "Additional Lessons" work

* add TimetableController, this class will handle all timetable work (main + additional Subjects)
This commit is contained in:
Jannik 2020-06-11 21:41:05 +02:00
parent 6c0624c793
commit 2d753851c0
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
5 changed files with 101 additions and 12 deletions

View File

@ -26,6 +26,8 @@ import android.content.Context
import android.util.Log
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.*
import org.json.JSONObject
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
@ -163,12 +165,21 @@ class TCoRAPIController {
* @param subject the subject to search for
* @param week the week to look up
*/
fun getLessonsAsync(courseName: String, subject: String, week: Int): Deferred<ArrayList<Lesson>> {
fun getLessons(courseName: String, subject: String, week: Int): ArrayList<Lesson> {
val url = URL("$tcorBaseURL/lessons?course=$courseName&subject=$subject&week=$week")
var array: ArrayList<Lesson>
return GlobalScope.async {
Gson().fromJson(url.readText(), ArrayList<Lesson>()::class.java)
runBlocking {
withContext(Dispatchers.Default) {
array = Gson().fromJson(
JsonParser.parseString(url.readText()).asJsonArray,
object : TypeToken<ArrayList<Lesson>>() {}.type
)
}
}
return array
}
}

View File

@ -0,0 +1,66 @@
package org.mosad.seil0.projectlaogai.controller
import org.mosad.seil0.projectlaogai.util.Lesson
/**
* TODO this controller contains
* * a list with all additional lessons
* * the main course and additional info for timetables
* * all concrete objects
*/
class TimetableController {
companion object {
val aLessons = HashMap<String, Lesson>() // the key is courseName_lessonID
val aSubjectMap = HashMap<String, ArrayList<String>>()
/**
* add a subject to the subjects map and all it's lessons
* to the aLessons list
* @param courseName course to which the subject belongs
* @param subject the subjects name
*/
fun addASubject(courseName: String, subject: String) {
// add subject
if (aSubjectMap.containsKey(courseName)) {
aSubjectMap[courseName]?.add(subject)
} else {
aSubjectMap[courseName] = arrayListOf(subject)
}
// add concrete lessons
TCoRAPIController.getLessons(courseName, subject, 0).forEach {lesson ->
//the courseName, subject and lessonID, separator: -
val key = "$courseName-$subject-${lesson.lessonID}"
aLessons[key] = lesson
}
println(aLessons)
}
/**
* remove a subject from the subjects map and all it's lessons
* from the aLessons list
* @param courseName course to which the subject belongs
* @param subject the subjects name
*/
fun removeASubject(courseName: String, subject: String) {
// remove subject
aSubjectMap[courseName]?.remove(subject)
// remove concrete lessons
println(aLessons)
val iter = aLessons.iterator()
while (iter.hasNext()) {
val it = iter.next()
if(it.key.contains("$courseName-$subject")) {
println(it.key)
iter.remove() // use iterator to remove, otherwise ConcurrentModificationException
}
}
}
}
}

View File

@ -55,6 +55,7 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cShowBuffet
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.controller.TimetableController
import org.mosad.seil0.projectlaogai.util.DataTypes
import java.util.*
@ -151,17 +152,27 @@ class SettingsFragment : Fragment() {
}
linLayoutManageLessons.setOnClickListener {
val myItems = listOf("Hello", "World")
val lessons = ArrayList<String>()
TimetableController.aSubjectMap.forEach { pair ->
pair.value.forEach {
lessons.add("${pair.key} - $it")
}
}
MaterialDialog(context!!).show {
title(R.string.manage_lessons)
listItemsMultiChoice(items = myItems)
positiveButton(R.string.delete) {
//TODO call delete action
}
positiveButton(R.string.delete)
negativeButton(R.string.cancel)
getActionButton(WhichButton.POSITIVE).updateTextColor(cColorAccent)
getActionButton(WhichButton.NEGATIVE).updateTextColor(cColorAccent)
listItemsMultiChoice(items = lessons) { dialog, indices, items ->
items.forEach {
val list = it.split(" - ")
TimetableController.removeASubject(list[0], list[1])
// TODO save
}
}
}
}

View File

@ -37,6 +37,7 @@ import kotlinx.coroutines.runBlocking
import org.mosad.seil0.projectlaogai.R
import org.mosad.seil0.projectlaogai.controller.CacheController
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
import org.mosad.seil0.projectlaogai.controller.TimetableController
import org.mosad.seil0.projectlaogai.util.Course
import java.util.stream.Collectors
@ -65,14 +66,13 @@ class AddLessonDialog(_context: Context) {
.customView(R.layout.dialog_add_lesson)
.setPeekHeight(900)
.positiveButton(R.string.add) {
val lessons = runBlocking {
TCoRAPIController.getLessonsAsync(selectedCourse, selectedSubject, 0).await()
}
val lessons = TCoRAPIController.getLessons(selectedCourse, selectedSubject, 0)
println("add lesson \"$selectedCourse: $selectedSubject\"")
println(lessons.toString())
// TODO save lesson
TimetableController.addASubject(selectedCourse, selectedSubject)
// TODO refresh timetable (add a function to show additional lessons)
// TODO save
}
.negativeButton(R.string.cancel)

View File

@ -97,6 +97,7 @@ data class MensaMenu(val meta: MensaMeta = MensaMeta(), val currentWeek: MensaWe
// data classes for the timetable part
data class Lesson(
val lessonID: String,
val lessonSubject: String,
val lessonTeacher: String,
val lessonRoom: String,