[API/Podlasie] Fix saving class team, add main endpoint and getting the timetable.

This commit is contained in:
Kacper Ziubryniewicz 2020-05-13 16:56:42 +02:00
parent 3c2f85f263
commit fcffa2afeb
7 changed files with 179 additions and 7 deletions

View File

@ -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
}
}
}

View File

@ -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<Feature>()
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))
)

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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)
}
}
}

View File

@ -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<JsonObject>) {
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<Int>()
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))
}
}

View File

@ -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))