additional Subject are now added to the timetable
This commit is contained in:
parent
69ce9fef5a
commit
f6b00a8d81
|
@ -33,6 +33,7 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
|
|||
import org.mosad.seil0.projectlaogai.util.Course
|
||||
import org.mosad.seil0.projectlaogai.util.MensaMenu
|
||||
import org.mosad.seil0.projectlaogai.util.TimetableCourseWeek
|
||||
import org.mosad.seil0.projectlaogai.util.TimetableWeek
|
||||
import java.io.BufferedReader
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
|
@ -156,10 +157,25 @@ class CacheController(cont: Context) {
|
|||
val timetableObject = JsonParser.parseString(bufferedReader.readLine()).asJsonObject
|
||||
|
||||
// make sure you add the single weeks in the exact order!
|
||||
val timetable = Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass)
|
||||
if (timetables.size > week) {
|
||||
timetables[week] = (Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass))
|
||||
timetables[week] = timetable
|
||||
|
||||
TimetableController.timetable[week] = TimetableWeek(
|
||||
timetable.meta.weekIndex,
|
||||
timetable.meta.weekNumberYear,
|
||||
timetable.timetable.days
|
||||
)
|
||||
} else {
|
||||
timetables.add(Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass))
|
||||
timetables.add(timetable)
|
||||
|
||||
TimetableController.timetable.add(
|
||||
TimetableWeek(
|
||||
timetable.meta.weekIndex,
|
||||
timetable.meta.weekNumberYear,
|
||||
timetable.timetable.days
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,44 @@
|
|||
/**
|
||||
* ProjectLaogai
|
||||
*
|
||||
* Copyright 2019-2020 <seil0@mosad.xyz>
|
||||
*
|
||||
* 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 org.mosad.seil0.projectlaogai.util.Lesson
|
||||
import org.mosad.seil0.projectlaogai.util.TimetableWeek
|
||||
|
||||
/**
|
||||
* TODO this controller contains
|
||||
* * a list with all additional lessons
|
||||
* * the main course and additional info for timetables
|
||||
* * all concrete objects
|
||||
*
|
||||
* TODO add configurable week to addSubject() and removeSubject()
|
||||
*/
|
||||
class TimetableController {
|
||||
|
||||
companion object {
|
||||
val aLessons = HashMap<String, Lesson>() // the key is courseName_lessonID
|
||||
val aSubjectMap = HashMap<String, ArrayList<String>>()
|
||||
val timetable = ArrayList<TimetableWeek>()
|
||||
val lessonMap = HashMap<String, Lesson>() // the key is courseName_lessonID
|
||||
val subjectMap = HashMap<String, ArrayList<String>>()
|
||||
|
||||
/**
|
||||
* add a subject to the subjects map and all it's lessons
|
||||
|
@ -20,21 +46,25 @@ class TimetableController {
|
|||
* @param courseName course to which the subject belongs
|
||||
* @param subject the subjects name
|
||||
*/
|
||||
fun addASubject(courseName: String, subject: String) {
|
||||
fun addSubject(courseName: String, subject: String) {
|
||||
// add subject
|
||||
if (aSubjectMap.containsKey(courseName)) {
|
||||
aSubjectMap[courseName]?.add(subject)
|
||||
if (subjectMap.containsKey(courseName)) {
|
||||
subjectMap[courseName]?.add(subject)
|
||||
} else {
|
||||
aSubjectMap[courseName] = arrayListOf(subject)
|
||||
subjectMap[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
|
||||
lessonMap[key] = lesson
|
||||
|
||||
// add lesson to the timetable
|
||||
val id = lesson.lessonID.split(".")
|
||||
if(id.size == 3)
|
||||
timetable[0].days[id[0].toInt()].timeslots[id[1].toInt()].add(lesson)
|
||||
}
|
||||
println(aLessons)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -43,20 +73,22 @@ class TimetableController {
|
|||
* @param courseName course to which the subject belongs
|
||||
* @param subject the subjects name
|
||||
*/
|
||||
fun removeASubject(courseName: String, subject: String) {
|
||||
fun removeSubject(courseName: String, subject: String) {
|
||||
// remove subject
|
||||
|
||||
aSubjectMap[courseName]?.remove(subject)
|
||||
subjectMap[courseName]?.remove(subject)
|
||||
|
||||
// remove concrete lessons
|
||||
|
||||
println(aLessons)
|
||||
val iter = aLessons.iterator()
|
||||
while (iter.hasNext()) {
|
||||
val it = iter.next()
|
||||
val iterator = lessonMap.iterator()
|
||||
while (iterator.hasNext()) {
|
||||
val it = iterator.next()
|
||||
if(it.key.contains("$courseName-$subject")) {
|
||||
println(it.key)
|
||||
iter.remove() // use iterator to remove, otherwise ConcurrentModificationException
|
||||
// remove the lesson from the lessons list
|
||||
iterator.remove() // use iterator to remove, otherwise ConcurrentModificationException
|
||||
|
||||
// remove the lesson from the timetable
|
||||
val id = it.value.lessonID.split(".")
|
||||
if(id.size == 3)
|
||||
timetable[0].days[id[0].toInt()].timeslots[id[1].toInt()].remove(it.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ import kotlinx.android.synthetic.main.fragment_home.*
|
|||
import kotlinx.coroutines.*
|
||||
import org.mosad.seil0.projectlaogai.R
|
||||
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.mensaMenu
|
||||
import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timetables
|
||||
import org.mosad.seil0.projectlaogai.controller.TimetableController
|
||||
import org.mosad.seil0.projectlaogai.util.Meal
|
||||
import org.mosad.seil0.projectlaogai.util.TimetableDay
|
||||
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
|
||||
|
@ -119,7 +119,7 @@ class HomeFragment : Fragment() {
|
|||
|
||||
withContext(Dispatchers.Main) {
|
||||
|
||||
if (isAdded && timetables.isNotEmpty()) {
|
||||
if (isAdded && TimetableController.timetable.isNotEmpty()) {
|
||||
try {
|
||||
val dayCardView = findNextDay(NotRetardedCalendar.getDayOfWeekIndex())
|
||||
linLayout_Home.addView(dayCardView)
|
||||
|
@ -144,12 +144,13 @@ class HomeFragment : Fragment() {
|
|||
var dayIndexSearch = startDayIndex
|
||||
var weekIndexSearch = 0
|
||||
|
||||
while (dayTimetable == null && weekIndexSearch < timetables.size) {
|
||||
while (dayTimetable == null && weekIndexSearch < TimetableController.timetable.size) {
|
||||
for (dayIndex in dayIndexSearch..5) {
|
||||
dayTimetable = timetables[weekIndexSearch].timetable.days[dayIndex]
|
||||
dayTimetable = TimetableController.timetable[weekIndexSearch].days[dayIndex]
|
||||
|
||||
// some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
|
||||
val daysToAdd = (timetables[weekIndexSearch].meta.weekNumberYear - NotRetardedCalendar.getWeekOfYear()) * 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex())
|
||||
val daysToAdd = ((TimetableController.timetable[weekIndexSearch].weekNumberYear - NotRetardedCalendar.getWeekOfYear())
|
||||
* 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex()))
|
||||
dayCardView.addTimetableDay(dayTimetable, daysToAdd)
|
||||
|
||||
// if there are no lessons don't show the dayCardView
|
||||
|
|
|
@ -153,7 +153,7 @@ class SettingsFragment : Fragment() {
|
|||
|
||||
linLayoutManageLessons.setOnClickListener {
|
||||
val lessons = ArrayList<String>()
|
||||
TimetableController.aSubjectMap.forEach { pair ->
|
||||
TimetableController.subjectMap.forEach { pair ->
|
||||
pair.value.forEach {
|
||||
lessons.add("${pair.key} - $it")
|
||||
}
|
||||
|
@ -169,7 +169,7 @@ class SettingsFragment : Fragment() {
|
|||
listItemsMultiChoice(items = lessons) { dialog, indices, items ->
|
||||
items.forEach {
|
||||
val list = it.split(" - ")
|
||||
TimetableController.removeASubject(list[0], list[1])
|
||||
TimetableController.removeSubject(list[0], list[1])
|
||||
// TODO save
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ import org.mosad.seil0.projectlaogai.controller.CacheController.Companion.timeta
|
|||
import org.mosad.seil0.projectlaogai.controller.PreferencesController
|
||||
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.cCourse
|
||||
import org.mosad.seil0.projectlaogai.controller.TCoRAPIController
|
||||
import org.mosad.seil0.projectlaogai.controller.TimetableController
|
||||
import org.mosad.seil0.projectlaogai.uicomponents.AddSubjectDialog
|
||||
import org.mosad.seil0.projectlaogai.uicomponents.DayCardView
|
||||
import org.mosad.seil0.projectlaogai.util.NotRetardedCalendar
|
||||
|
@ -64,6 +65,8 @@ class TimeTableFragment : Fragment() {
|
|||
|
||||
if (timetables.size > 1 && timetables[0].timetable.days.isNotEmpty() && timetables[1].timetable.days.isNotEmpty()) {
|
||||
initTimetable()
|
||||
|
||||
println(TimetableController.timetable)
|
||||
} else {
|
||||
MaterialDialog(context!!)
|
||||
.title(R.string.error)
|
||||
|
@ -85,7 +88,7 @@ class TimeTableFragment : Fragment() {
|
|||
|
||||
// show the AddLessonDialog if the ftaBtn is clicked
|
||||
faBtnAddSubject.setOnClickListener {
|
||||
AddSubjectDialog(context!!).initialize().show{
|
||||
AddSubjectDialog(context!!).initialize(this@TimeTableFragment).show{
|
||||
getActionButton(WhichButton.POSITIVE).updateTextColor(PreferencesController.cColorAccent)
|
||||
getActionButton(WhichButton.NEGATIVE).updateTextColor(PreferencesController.cColorAccent)
|
||||
}
|
||||
|
@ -110,19 +113,21 @@ class TimeTableFragment : Fragment() {
|
|||
|
||||
addTimetableWeek(currentDayIndex, 5, 0).join() // add current week
|
||||
addTimetableWeek(0, currentDayIndex - 1, 1) // add next week
|
||||
|
||||
// TODO add additional lessons
|
||||
}
|
||||
|
||||
private fun addTimetableWeek(dayBegin: Int, dayEnd: Int, week: Int) = GlobalScope.launch(Dispatchers.Main) {
|
||||
val timetable = timetables[week].timetable
|
||||
val timetableMeta = timetables[week].meta
|
||||
//val timetable = timetables[week].timetable
|
||||
//val timetableMeta = timetables[week].meta
|
||||
|
||||
val timetable = TimetableController.timetable[week]
|
||||
|
||||
for (dayIndex in dayBegin..dayEnd) {
|
||||
val dayCardView = DayCardView(context!!)
|
||||
|
||||
// some wired calendar magic, calculate the correct date to be shown ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
|
||||
val daysToAdd = (timetableMeta.weekNumberYear - NotRetardedCalendar.getWeekOfYear()) * 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex())
|
||||
// some wired calendar magic, calculate the correct date to be shown
|
||||
// ((timetable week - current week * 7) + (dayIndex - dayIndex of current week)
|
||||
val daysToAdd = ((TimetableController.timetable[week].weekNumberYear - NotRetardedCalendar.getWeekOfYear())
|
||||
* 7 + (dayIndex - NotRetardedCalendar.getDayOfWeekIndex()))
|
||||
dayCardView.addTimetableDay(timetable.days[dayIndex], daysToAdd)
|
||||
|
||||
// if there are no lessons don't show the dayCardView
|
||||
|
@ -143,6 +148,11 @@ class TimeTableFragment : Fragment() {
|
|||
CacheController.readTimetable(cCourse.courseName, 0, context!!)
|
||||
CacheController.readTimetable(cCourse.courseName, 1, context!!)
|
||||
|
||||
refreshUI()
|
||||
}
|
||||
|
||||
// TODO rename
|
||||
fun refreshUI() = GlobalScope.launch(Dispatchers.Default) {
|
||||
withContext(Dispatchers.Main) {
|
||||
// remove all menus from the layout
|
||||
linLayout_Timetable.removeAllViews()
|
||||
|
|
|
@ -38,6 +38,7 @@ 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.fragments.TimeTableFragment
|
||||
import org.mosad.seil0.projectlaogai.util.Course
|
||||
import java.util.stream.Collectors
|
||||
|
||||
|
@ -59,7 +60,7 @@ class AddSubjectDialog(_context: Context) {
|
|||
/**
|
||||
* create a new AddLessonDialog (BottomSheet)
|
||||
*/
|
||||
fun initialize(): MaterialDialog {
|
||||
fun initialize(ttf: TimeTableFragment): MaterialDialog {
|
||||
val dialog = MaterialDialog(context, BottomSheet())
|
||||
.title(R.string.add_lesson)
|
||||
.message(R.string.add_lesson_desc)
|
||||
|
@ -70,8 +71,11 @@ class AddSubjectDialog(_context: Context) {
|
|||
println("add lesson \"$selectedCourse: $selectedSubject\"")
|
||||
println(lessons.toString())
|
||||
|
||||
TimetableController.addASubject(selectedCourse, selectedSubject)
|
||||
TimetableController.addSubject(selectedCourse, selectedSubject)
|
||||
// TODO refresh timetable (add a function to show additional lessons)
|
||||
runBlocking {
|
||||
ttf.refreshUI()
|
||||
}
|
||||
// TODO save
|
||||
}
|
||||
.negativeButton(R.string.cancel)
|
||||
|
|
|
@ -106,8 +106,11 @@ data class Lesson(
|
|||
|
||||
data class TimetableDay(val timeslots: Array<ArrayList<Lesson>> = Array(6) { ArrayList<Lesson>() })
|
||||
|
||||
data class TimetableWeek(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
|
||||
data class TimetableWeek(val weekIndex: Int = 0, val weekNumberYear: Int = 0, val days: Array<TimetableDay> = Array(6) { TimetableDay() })
|
||||
|
||||
// TCoR
|
||||
data class TimetableWeekTCoR(val days: Array<TimetableDay> = Array(6) { TimetableDay() })
|
||||
|
||||
data class TimetableCourseMeta(val updateTime: Long = 0, val courseName: String = "", val weekIndex: Int = 0, val weekNumberYear: Int = 0, val link: String = "")
|
||||
|
||||
data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeek = TimetableWeek())
|
||||
data class TimetableCourseWeek(val meta: TimetableCourseMeta = TimetableCourseMeta(), var timetable: TimetableWeekTCoR = TimetableWeekTCoR())
|
||||
|
|
Loading…
Reference in New Issue