diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt index 50c2e886..a59f4d95 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/DataVulcan.kt @@ -10,7 +10,10 @@ import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_VULCAN_API import pl.szczodrzynski.edziennik.data.api.models.Data import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Profile +import pl.szczodrzynski.edziennik.data.db.entity.Team import pl.szczodrzynski.edziennik.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.values class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { @@ -26,6 +29,25 @@ class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app } } + init { + // during the first sync `profile.studentClassName` is already set + if (teamList.values().none { it.type == Team.TYPE_CLASS }) { + profile?.studentClassName?.also { name -> + val id = Utils.crc16(name.toByteArray()).toLong() + + val teamObject = Team( + profileId, + id, + name, + Team.TYPE_CLASS, + "$schoolName:$name", + -1 + ) + teamList.put(id, teamObject) + } + } + } + override fun generateUserCode() = "$schoolName:$studentId" /** diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanApi.kt index 3f755b01..bf19db15 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanApi.kt @@ -13,8 +13,6 @@ import io.github.wulkanowy.signer.android.signContent import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan import pl.szczodrzynski.edziennik.data.api.models.ApiError -import pl.szczodrzynski.edziennik.data.db.entity.Team -import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection import java.util.* @@ -42,22 +40,6 @@ open class VulcanApi(open val data: DataVulcan, open val lastSync: Long?) { d(tag, "Request: Vulcan/Api - $url") - if (data.teamList.size() == 0) { - data.profile?.studentClassName?.also { name -> - val id = Utils.crc16(name.toByteArray()).toLong() - - val teamObject = Team( - profileId, - id, - name, - Team.TYPE_CLASS, - "${data.schoolName}:$name", - -1 - ) - data.teamList.put(id, teamObject) - } - } - val finalPayload = JsonObject() parameters.map { (name, value) -> when (value) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiTimetable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiTimetable.kt index ac621ec7..d8899dbf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiTimetable.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiTimetable.kt @@ -86,31 +86,36 @@ class VulcanApiTimetable(override val data: DataVulcan, data.teamList[id] = team } team.id - } ?: data.studentClassId.toLong() + } ?: data.teamClass?.id ?: -1 - val subjectId = lesson.getLong("IdPrzedmiot")?.let { - when (it) { - 0L -> { - val subjectName = lesson.getString("PrzedmiotNazwa") ?: "" + val subjectId = lesson.getLong("IdPrzedmiot").let { id -> + // get the specified subject name + val subjectName = lesson.getString("PrzedmiotNazwa") ?: "" - data.subjectList.singleOrNull { subject -> subject.longName == subjectName }?.id - ?: { - /** - * CREATE A NEW SUBJECT IF IT DOESN'T EXIST - */ - - val subjectObject = Subject( - profileId, - -1 * crc16(subjectName.toByteArray()).toLong(), - subjectName, - subjectName - ) - data.subjectList.put(subjectObject.id, subjectObject) - subjectObject.id - }.invoke() - } - else -> it + val condition = when (id) { + // "special" subject - e.g. one time classes, a trip, etc. + 0L -> { subject: Subject -> subject.longName == subjectName } + // normal subject, check if it exists + else -> { subject: Subject -> subject.id == id } } + + data.subjectList.singleOrNull(condition)?.id ?: { + /** + * CREATE A NEW SUBJECT IF IT DOESN'T EXIST + */ + + val subjectObject = Subject( + profileId, + if (id == null || id == 0L) + -1 * crc16(subjectName.toByteArray()).toLong() + else + id, + subjectName, + subjectName + ) + data.subjectList.put(subjectObject.id, subjectObject) + subjectObject.id + }() } val lessonObject = Lesson(profileId, -1).apply {