From fcffa2afeb18cbbb55c164140c5fb5b2b6c00987 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 13 May 2020 16:56:42 +0200 Subject: [PATCH] [API/Podlasie] Fix saving class team, add main endpoint and getting the timetable. --- .../api/edziennik/podlasie/DataPodlasie.kt | 43 +++++++-- .../edziennik/podlasie/PodlasieFeatures.kt | 11 ++- .../edziennik/podlasie/data/PodlasieApi.kt | 3 +- .../edziennik/podlasie/data/PodlasieData.kt | 7 ++ .../podlasie/data/api/PodlasieApiMain.kt | 32 +++++++ .../podlasie/data/api/PodlasieApiTimetable.kt | 89 +++++++++++++++++++ .../podlasie/firstlogin/PodlasieFirstLogin.kt | 1 + 7 files changed, 179 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.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 45edda13..3ec17404 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 @@ -4,13 +4,11 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.crc32 +import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_PODLASIE_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.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.data.db.entity.* +import kotlin.text.replace class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { @@ -83,4 +81,39 @@ class DataPodlasie(app: App, profile: Profile?, loginStore: LoginStore) : Data(a val loginShort: String? get() = studentLogin?.split('@')?.get(0) + + fun getSubject(name: String): Subject { + val id = name.crc32() + return subjectList.singleOrNull { it.id == id } ?: run { + val subject = Subject(profileId, id, name, name) + subjectList.put(id, subject) + subject + } + } + + fun getTeacher(firstName: String, lastName: String): Teacher { + val name = "$firstName $lastName".fixName() + val id = name.crc32() + return teacherList.singleOrNull { it.id == id } ?: run { + val teacher = Teacher(profileId, id, firstName, lastName) + teacherList.put(id, teacher) + teacher + } + } + + fun getTeam(name: String? = null): Team { + if (name == "cała klasa" || name == null) return teamClass ?: run { + val id = className!!.crc32() + val teamCode = "$schoolShortName:$className" + val team = Team(profileId, id, className, Team.TYPE_CLASS, teamCode, -1) + teamList.put(id, team) + return team + } else { + val id = name.crc32() + val teamCode = "$schoolShortName:$name" + val team = Team(profileId, id, name, Team.TYPE_VIRTUAL, teamCode, -1) + teamList.put(id, team) + return team + } + } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt index c799753c..82c6f659 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/PodlasieFeatures.kt @@ -4,6 +4,15 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie +import pl.szczodrzynski.edziennik.data.api.FEATURE_ALWAYS_NEEDED +import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_PODLASIE_API +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_PODLASIE import pl.szczodrzynski.edziennik.data.api.models.Feature -val PodlasieFeatures = listOf() +const val ENDPOINT_PODLASIE_API_MAIN = 1001 + +val PodlasieFeatures = listOf( + Feature(LOGIN_TYPE_PODLASIE, FEATURE_ALWAYS_NEEDED, listOf( + ENDPOINT_PODLASIE_API_MAIN to LOGIN_METHOD_PODLASIE_API + ), listOf(LOGIN_METHOD_PODLASIE_API)) +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt index 9ebf57b1..af5ff9d7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieApi.kt @@ -98,7 +98,8 @@ open class PodlasieApi(open val data: DataPodlasie, open val lastSync: Long?) { } private fun getSecurityToken(): String { - val format = SimpleDateFormat("yyyy-MM-dd HH", Locale.ENGLISH).format(Calendar.getInstance().time.time) + val format = SimpleDateFormat("yyyy-MM-dd HH", Locale.ENGLISH) + .also { it.timeZone = TimeZone.getTimeZone("Europe/Warsaw") }.format(System.currentTimeMillis()) val instance = MessageDigest.getInstance("SHA-256") val digest = instance.digest("-EYlwYu8u16miVd8tT?oO7cvoUVQrQN0vr!$format".toByteArray()).toHexString() val digest2 = instance.digest(data.apiToken!!.toByteArray()).toHexString() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt index 98cbb539..ce1197b8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/PodlasieData.kt @@ -4,7 +4,10 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data +import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.ENDPOINT_PODLASIE_API_MAIN +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api.PodlasieApiMain import pl.szczodrzynski.edziennik.utils.Utils class PodlasieData(val data: DataPodlasie, val onSuccess: () -> Unit) { @@ -36,6 +39,10 @@ class PodlasieData(val data: DataPodlasie, val onSuccess: () -> Unit) { private fun useEndpoint(endpointId: Int, lastSync: Long?, onSuccess: (endpointId: Int) -> Unit) { Utils.d(TAG, "Using endpoint $endpointId. Last sync time = $lastSync") when (endpointId) { + ENDPOINT_PODLASIE_API_MAIN -> { + data.startProgress(R.string.edziennik_progress_endpoint_data) + PodlasieApiMain(data, lastSync, onSuccess) + } else -> onSuccess(endpointId) } } 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 new file mode 100644 index 00000000..0ddff4e9 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import pl.szczodrzynski.edziennik.asJsonObjectList +import pl.szczodrzynski.edziennik.data.api.PODLASIE_API_USER_ENDPOINT +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.ENDPOINT_PODLASIE_API_MAIN +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.PodlasieApi +import pl.szczodrzynski.edziennik.data.db.entity.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.getJsonArray + +class PodlasieApiMain(override val data: DataPodlasie, + override val lastSync: Long?, + val onSuccess: (endpointId: Int) -> Unit) : PodlasieApi(data, lastSync) { + companion object { + const val TAG = "PodlasieApiTimetable" + } + + init { + apiGet(TAG, PODLASIE_API_USER_ENDPOINT) { json -> + data.getTeam() // Save the class team when it doesn't exist. + + json.getJsonArray("Timetable")?.asJsonObjectList()?.let { PodlasieApiTimetable(data, it) } + + data.setSyncNext(ENDPOINT_PODLASIE_API_MAIN, SYNC_ALWAYS) + onSuccess(ENDPOINT_PODLASIE_API_MAIN) + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt new file mode 100644 index 00000000..a9a12d5d --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiTimetable.kt @@ -0,0 +1,89 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-12 + */ + +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.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Lesson +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import pl.szczodrzynski.edziennik.utils.models.Week + +class PodlasieApiTimetable(val data: DataPodlasie, rows: List) { + init { + val currentWeekStart = Week.getWeekStart() + + if (Date.getToday().weekDay > 4) { + currentWeekStart.stepForward(0, 0, 7) + } + + val getDate = data.arguments?.getString("weekStart") ?: currentWeekStart.stringY_m_d + + val weekStart = Date.fromY_m_d(getDate) + val weekEnd = weekStart.clone().stepForward(0, 0, 6) + + val days = mutableListOf() + var startDate: Date? = null + var endDate: Date? = null + + rows.forEach { lesson -> + val date = lesson.getString("Date")?.let { Date.fromY_m_d(it) } ?: return@forEach + + if ((date > weekEnd || date < weekStart) && data.profile?.empty != true) return@forEach + if (startDate == null) startDate = date.clone() + endDate = date.clone() + if (date.value !in days) days += date.value + + val lessonNumber = lesson.getInt("LessonNumber") ?: return@forEach + val startTime = lesson.getString("TimeFrom")?.let { Time.fromH_m_s(it) } + ?: return@forEach + val endTime = lesson.getString("TimeTo")?.let { Time.fromH_m_s(it) } ?: return@forEach + val subject = lesson.getString("SchoolSubject")?.let { data.getSubject(it) } + ?: return@forEach + + val teacherFirstName = lesson.getString("TeacherFirstName") ?: return@forEach + val teacherLastName = lesson.getString("TeacherLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val team = lesson.getString("Group")?.let { data.getTeam(it) } ?: return@forEach + val classroom = lesson.getString("Room") + + Lesson(data.profileId, -1).also { + it.type = Lesson.TYPE_NORMAL + it.date = date + it.lessonNumber = lessonNumber + it.startTime = startTime + it.endTime = endTime + it.subjectId = subject.id + it.teacherId = teacher.id + it.teamId = team.id + it.classroom = classroom + + it.id = it.buildId() + data.lessonList += it + } + } + + if (startDate != null && endDate != null) { + if (weekEnd > endDate!!) endDate = weekEnd + + while (startDate!! <= endDate!!) { + if (startDate!!.value !in days) { + val lessonDate = startDate!!.clone() + data.lessonList += Lesson(data.profileId, lessonDate.value.toLong()).apply { + type = Lesson.TYPE_NO_LESSONS + date = lessonDate + } + } + startDate!!.stepForward(0, 0, 1) + } + } + + data.toRemove.add(DataRemoveModel.Timetable.between(weekStart, weekEnd)) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt index bedd9a25..9b92d541 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/firstlogin/PodlasieFirstLogin.kt @@ -60,6 +60,7 @@ class PodlasieFirstLogin(val data: DataPodlasie, val onSuccess: () -> Unit) { schoolYear?.split('/')?.get(0)?.toInt()?.let { studentSchoolYearStart = it } + studentClassName = className } EventBus.getDefault().postSticky(FirstLoginFinishedEvent(listOf(profile), data.loginStore))