forked from github/szkolny
[API/Podlasie] Add getting events.
This commit is contained in:
parent
0ac8e1d9c1
commit
031cc05209
@ -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<JsonObject>) {
|
||||
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())
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -53,12 +53,15 @@ open class DataRemoveModel {
|
||||
fun futureExceptType(exceptType: Long) = Events(null, exceptType, null)
|
||||
fun futureExceptTypes(exceptTypes: List<Long>) = 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())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,6 +114,9 @@ abstract class EventDao : BaseDao<Event, EventFull> {
|
||||
" 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)
|
||||
|
||||
|
@ -49,6 +49,16 @@ public class Time implements Comparable<Time> {
|
||||
}
|
||||
}
|
||||
|
||||
public static Time fromY_m_d_H_m_s(String dateTime)
|
||||
{
|
||||
try {
|
||||
return new Time(Integer.parseInt(dateTime.substring(11, 13)), Integer.parseInt(dateTime.substring(14, 16)), Integer.parseInt(dateTime.substring(17, 19)));
|
||||
}
|
||||
catch (Exception e) {
|
||||
return new Time(0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public long combineWith(Date date) {
|
||||
if (date == null) {
|
||||
return getInMillis();
|
||||
|
Loading…
Reference in New Issue
Block a user