From 929287a553dfd0e17a25ea4e830bfe60fae2c6ab Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 20 Oct 2019 14:46:00 +0200 Subject: [PATCH] [APIv2/Vulcan] Add getting basic dictionaries (teachers and subjects) --- .../api/v2/vulcan/data/VulcanData.kt | 6 ++ .../vulcan/data/api/VulcanApiDictionaries.kt | 68 +++++++++++++++++++ .../v2/vulcan/data/api/VulcanApiTemplate.kt | 2 - app/src/main/res/values/strings.xml | 1 + 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiDictionaries.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt index fb2dc15d..ff860a7f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt @@ -6,7 +6,9 @@ package pl.szczodrzynski.edziennik.api.v2.vulcan.data import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_DICTIONARIES import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_GRADES +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.api.VulcanApiDictionaries import pl.szczodrzynski.edziennik.api.v2.vulcan.data.api.VulcanApiGrades import pl.szczodrzynski.edziennik.utils.Utils @@ -38,6 +40,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) { Utils.d(TAG, "Using endpoint $endpointId") when (endpointId) { + ENDPOINT_VULCAN_API_DICTIONARIES -> { + data.startProgress(R.string.edziennik_progress_endpoint_dictionaries) + VulcanApiDictionaries(data) { onSuccess() } + } ENDPOINT_VULCAN_API_GRADES -> { data.startProgress(R.string.edziennik_progress_endpoint_grades) VulcanApiGrades(data) { onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiDictionaries.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiDictionaries.kt new file mode 100644 index 00000000..3fb89406 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiDictionaries.kt @@ -0,0 +1,68 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-20 + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_DICTIONARIES +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_DICTIONARIES +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject +import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher +import pl.szczodrzynski.edziennik.getJsonArray +import pl.szczodrzynski.edziennik.getJsonObject +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString + +class VulcanApiDictionaries(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) { + companion object { + const val TAG = "VulcanApiDictionaries" + } + + init { + apiGet(TAG, VULCAN_API_ENDPOINT_DICTIONARIES) { json, _ -> + val elements = json.getJsonObject("Data") + + elements?.getJsonArray("Pracownicy")?.forEach { saveTeacher(it.asJsonObject) } + elements?.getJsonArray("Przedmioty")?.forEach { saveSubject(it.asJsonObject) } + + data.setSyncNext(ENDPOINT_VULCAN_API_DICTIONARIES, SYNC_ALWAYS) + onSuccess() + } + } + + private fun saveTeacher(teacher: JsonObject) { + val id = teacher.getLong("Id") ?: return + val name = teacher.getString("Imie") ?: "" + val surname = teacher.getString("Nazwisko") ?: "" + val loginId = teacher.getString("LoginId") ?: "-1" + + val teacherObject = Teacher( + profileId, + id, + name, + surname, + loginId + ) + + data.teacherList.put(id, teacherObject) + } + + private fun saveSubject(subject: JsonObject) { + val id = subject.getLong("Id") ?: return + val longName = subject.getString("Nazwa") ?: "" + val shortName = subject.getString("Kod") ?: "" + + val subjectObject = Subject( + profileId, + id, + longName, + shortName + ) + + data.subjectList.put(id, subjectObject) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt index db380c59..2c2bb824 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiTemplate.kt @@ -4,9 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api -import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_GRADES import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan -import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_GRADES import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.getJsonArray diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e8158af5..af102476 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -950,4 +950,5 @@ Pobieranie informacji o klasie... Pobieranie nieobecności nauczycieli... Pobieranie rodzajów nieobecności nauczycieli... + Pobieranie słowników...