From 795317f13fce1fba1a3d30398645910090f0f6f3 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 13 May 2020 22:47:05 +0200 Subject: [PATCH] [API/Podlasie] Add getting teachers. --- .../api/edziennik/podlasie/DataPodlasie.kt | 6 +-- .../podlasie/data/api/PodlasieApiMain.kt | 1 + .../podlasie/data/api/PodlasieApiTeachers.kt | 40 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt index 3ec17404..d117c139 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/DataPodlasie.kt @@ -77,7 +77,7 @@ class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(a set(value) { profile?.putStudentData("currentSemester", value) ?: return; mCurrentSemester = value } val schoolShortName: String? - get() = studentLogin?.replace(".podlaskie.pl", "") + get() = studentLogin?.split('@')?.get(1)?.replace(".podlaskie.pl", "") val loginShort: String? get() = studentLogin?.split('@')?.get(0) @@ -93,8 +93,8 @@ class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(a fun getTeacher(firstName: String, lastName: String): Teacher { val name = "$firstName $lastName".fixName() - val id = name.crc32() - return teacherList.singleOrNull { it.id == id } ?: run { + return teacherList.singleOrNull { it.fullName == name } ?: run { + val id = name.crc32() val teacher = Teacher(profileId, id, firstName, lastName) teacherList.put(id, teacher) teacher diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt index 2c0613e6..d73e8d60 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt @@ -25,6 +25,7 @@ class PodlasieApiMain(override val data: DataPodlasie, data.getTeam() // Save the class team when it doesn't exist. json.getInt("LuckyNumber")?.let { PodlasieApiLuckyNumber(data, it) } + json.getJsonArray("Teacher")?.asJsonObjectList()?.let { PodlasieApiTeachers(data, it) } json.getJsonArray("Timetable")?.asJsonObjectList()?.let { PodlasieApiTimetable(data, it) } json.getJsonArray("Marks")?.asJsonObjectList()?.let { PodlasieApiGrades(data, it) } json.getJsonArray("MarkFinal")?.asJsonObjectList()?.let { PodlasieApiFinalGrades(data, it) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt new file mode 100644 index 00000000..ca1fa7b4 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTeachers.kt @@ -0,0 +1,40 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.db.entity.Teacher +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString + +class PodlasieApiTeachers(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { teacher -> + val id = teacher.getLong("ExternalId") ?: return@forEach + val firstName = teacher.getString("FirstName") ?: return@forEach + val lastName = teacher.getString("LastName") ?: return@forEach + val isEducator = teacher.getInt("Educator") == 1 + + val teacherObject = Teacher( + profileId = data.profileId, + id = id, + name = firstName, + surname = lastName, + loginId = null + ) + + data.teacherList.put(id, teacherObject) + + val teamClass = data.teamClass + if (isEducator && teamClass != null) { + data.teamList.put(teamClass.id, teamClass.apply { + teacherId = id + }) + } + } + } +}