From 031cc05209817fe00d7858cce1ef2d7ff484b65b Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 13 May 2020 22:17:50 +0200 Subject: [PATCH] [API/Podlasie] Add getting events. --- .../podlasie/data/api/PodlasieApiEvents.kt | 72 +++++++++++++++++++ .../podlasie/data/api/PodlasieApiMain.kt | 1 + .../data/api/models/DataRemoveModel.kt | 3 + .../edziennik/data/db/dao/EventDao.kt | 3 + .../edziennik/utils/models/Time.java | 10 +++ 5 files changed, 89 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt new file mode 100644 index 00000000..e4dac6cc --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiEvents.kt @@ -0,0 +1,72 @@ +/* + * 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.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Event +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import java.util.* + +class PodlasieApiEvents(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { event -> + val id = event.getLong("ExternalId") ?: return@forEach + val date = event.getString("DateFrom")?.let { Date.fromY_m_d(it) } ?: return@forEach + val time = event.getString("DateFrom")?.let { Time.fromY_m_d_H_m_s(it) } + ?: return@forEach + val name = event.getString("Name") ?: "" + val description = event.getString("Description") ?: "" + + val type = when (event.getString("Category")?.toLowerCase(Locale.getDefault())) { + "klasówka" -> Event.TYPE_EXAM + "praca domowa" -> Event.TYPE_HOMEWORK + "wycieczka" -> Event.TYPE_EXCURSION + else -> Event.TYPE_DEFAULT + } + + val teacherFirstName = event.getString("PersonEnteringDataFirstName") ?: return@forEach + val teacherLastName = event.getString("PersonEnteringDataLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val lessonList = data.db.timetableDao().getAllForDateNow(data.profileId, date) + val lesson = lessonList.firstOrNull { it.startTime == time } + + val addedDate = event.getString("CreateDate")?.let { Date.fromIso(it) } + ?: System.currentTimeMillis() + + val eventObject = Event( + profileId = data.profileId, + id = id, + date = date, + time = time, + topic = "$name\n\n$description", + color = null, + type = type, + teacherId = teacher.id, + subjectId = lesson?.subjectId ?: -1, + teamId = data.teamClass?.id ?: -1, + addedDate = addedDate + ) + + data.eventList.add(eventObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_EVENT, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false + )) + } + + data.toRemove.add(DataRemoveModel.Events.future()) + } +} 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 a0a1c6cd..2c0613e6 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 @@ -28,6 +28,7 @@ class PodlasieApiMain(override val data: DataPodlasie, json.getJsonArray("Timetable")?.asJsonObjectList()?.let { PodlasieApiTimetable(data, it) } json.getJsonArray("Marks")?.asJsonObjectList()?.let { PodlasieApiGrades(data, it) } json.getJsonArray("MarkFinal")?.asJsonObjectList()?.let { PodlasieApiFinalGrades(data, it) } + json.getJsonArray("News")?.asJsonObjectList()?.let { PodlasieApiEvents(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/models/DataRemoveModel.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt index fabf5f39..2f9c8fd9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt @@ -53,12 +53,15 @@ open class DataRemoveModel { fun futureExceptType(exceptType: Long) = Events(null, exceptType, null) fun futureExceptTypes(exceptTypes: List) = Events(null, null, exceptTypes) fun futureWithType(type: Long) = Events(type, null, null) + fun future() = Events(null, null, null) } fun commit(profileId: Int, dao: EventDao) { type?.let { dao.dontKeepFutureWithType(profileId, Date.getToday(), it) } exceptType?.let { dao.dontKeepFutureExceptType(profileId, Date.getToday(), it) } exceptTypes?.let { dao.dontKeepFutureExceptTypes(profileId, Date.getToday(), it) } + if (type == null && exceptType == null && exceptTypes == null) + dao.dontKeepFuture(profileId, Date.getToday()) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt index 44b2962d..a532602b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt @@ -114,6 +114,9 @@ abstract class EventDao : BaseDao { " AND " + filter)) } + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate") + abstract fun dontKeepFuture(profileId: Int, todayDate: Date) + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") abstract fun dontKeepFutureWithType(profileId: Int, todayDate: Date, type: Long) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java index d19e317f..04b0c8a3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Time.java @@ -49,6 +49,16 @@ public class Time implements Comparable