From 33cfaef4542dc20e80bcef67164829788265816a Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Mon, 14 Oct 2019 13:32:58 +0200 Subject: [PATCH] [APIv2/Librus] Add getting the lucky number --- .../api/v2/librus/data/LibrusData.kt | 4 ++ .../librus/data/api/LibrusApiLuckyNumber.kt | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiLuckyNumber.kt 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 cab131f4..b5e9c245 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 @@ -67,6 +67,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_announcements) LibrusApiAnnouncements(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_LUCKY_NUMBER -> { + data.startProgress(R.string.edziennik_progress_endpoint_lucky_number) + LibrusApiLuckyNumber(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiLuckyNumber.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiLuckyNumber.kt new file mode 100644 index 00000000..596fe4a7 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiLuckyNumber.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-14 + */ + +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_LUCKY_NUMBER +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.luckynumber.LuckyNumber +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getJsonObject +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date + +class LibrusApiLuckyNumber(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiLuckyNumber" + } + + init { + apiGet(TAG, "LuckyNumbers") { json -> + if (json.isJsonNull) { + profile?.luckyNumberEnabled = false + } else { + profile?.also { profile -> + profile.luckyNumber = -1 + profile.luckyNumberDate = Date.getToday() + + json.getJsonObject("LuckyNumber")?.also { luckyNumber -> + profile.luckyNumber = luckyNumber.getInt("LuckyNumber") ?: -1 + profile.luckyNumberDate = Date.fromY_m_d(luckyNumber.getString("LuckyNumberDay")) + } + + data.luckyNumberList.add(LuckyNumber( + profileId, + profile.luckyNumberDate ?: Date.getToday(), + profile.luckyNumber + )) + } + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_LUCKY_NUMBER, SYNC_ALWAYS) + onSuccess() + } + } +}