diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index f55dec05..734f71a9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -5,15 +5,8 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data import pl.szczodrzynski.edziennik.R -import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_EVENTS -import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ME -import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_NORMAL_GRADES -import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_SCHOOLS -import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus -import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiEvents -import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiGrades -import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe -import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools +import pl.szczodrzynski.edziennik.api.v2.librus.* +import pl.szczodrzynski.edziennik.api.v2.librus.data.api.* import pl.szczodrzynski.edziennik.utils.Utils class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { @@ -43,21 +36,25 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { Utils.d(TAG, "Using endpoint $endpointId") when (endpointId) { ENDPOINT_LIBRUS_API_ME -> { - data.startProgress(R.string.edziennik_progress_endpoint_student_info) + data.startProgress(R.string.sync_action_getting_account) LibrusApiMe(data) { onSuccess() } } ENDPOINT_LIBRUS_API_SCHOOLS -> { - data.startProgress(R.string.edziennik_progress_endpoint_school_info) + data.startProgress(R.string.sync_action_syncing_school_info) LibrusApiSchools(data) { onSuccess() } } ENDPOINT_LIBRUS_API_NORMAL_GRADES -> { - data.startProgress(R.string.edziennik_progress_endpoint_grades) + data.startProgress(R.string.sync_action_syncing_grades) LibrusApiGrades(data) { onSuccess() } } ENDPOINT_LIBRUS_API_EVENTS -> { data.startProgress(R.string.sync_action_syncing_events) LibrusApiEvents(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_HOMEWORK -> { + data.startProgress(R.string.sync_action_syncing_homework) + LibrusApiHomework(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt index fc57f248..f495c94c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -54,7 +54,7 @@ class LibrusApiEvents(override val data: DataLibrus, teamId ) - val addedDate = Date.fromIso(event.get("AddDate").asString) + val addedDate = Date.fromIso(event.getString("AddDate") ?: return@forEach) data.eventList.add(eventObject) data.metadataList.add( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt new file mode 100644 index 00000000..093acac4 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2019-10-12. + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_HOMEWORK +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.events.Event +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.getJsonArray +import pl.szczodrzynski.edziennik.getJsonObject +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date + +class LibrusApiHomework(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiHomework" + } + + init { + apiGet(TAG, "HomeWorkAssignments") { json -> + val homeworkList = json.getJsonArray("HomeWorkAssignments") + + homeworkList?.forEach { homeworkEl -> + val homework = homeworkEl.asJsonObject + + val id = homework.getLong("Id") ?: return@forEach + val eventDate = Date.fromY_m_d(homework.getString("DueDate") ?: return@forEach) + val topic = (homework.getString("Topic") ?: "") + "\n" + + (homework.getString("Text") ?: "") + val teacherId = homework.getJsonObject("Teacher")?.getLong("Id") ?: -1 + + val eventObject = Event( + profileId, + id, + eventDate, + null, + topic, + -1, + -1, + false, + teacherId, + -1, + -1 + ) + + val addedDate = Date.fromY_m_d(homework.getString("Date") ?: return@forEach) + + data.eventList.add(eventObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_HOMEWORK, + id, + profile?.empty ?: false, + profile?.empty ?: false, + addedDate.inMillis + )) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_HOMEWORK, SYNC_ALWAYS) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt index e09456f2..efa7baf6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt @@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS class LibrusApiTemplate(override val data: DataLibrus, val onSuccess: () -> Unit) : LibrusApi(data) { @@ -14,13 +15,10 @@ class LibrusApiTemplate(override val data: DataLibrus, } init { - /*apiGet(LibrusApiMe.TAG, "") { json -> + /*apiGet(TAG, "") { json -> - // on error - data.error(TAG, ERROR_LIBRUS_API_, response, json) - - data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) + data.setSyncNext(ENDPOINT_LIBRUS_API_, SYNC_ALWAYS) onSuccess() }*/ } -} \ No newline at end of file +}