From 03c9932b8c58be01f785489931620858e8211f04 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 4 Apr 2020 22:24:15 +0200 Subject: [PATCH] [API/Edudziennik] Add getting whole homework body. --- .../edziennik/data/api/Regexes.kt | 3 ++ .../api/edziennik/edudziennik/Edudziennik.kt | 10 ++++- .../data/web/EdudziennikWebGetHomework.kt | 45 +++++++++++++++++++ .../data/web/EdudziennikWebHomework.kt | 6 ++- .../szczodrzynski/edziennik/data/db/AppDb.kt | 5 ++- .../data/db/migration/Migration85.kt | 12 +++++ 6 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetHomework.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration85.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt index 8511b40d..8ad2a505 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Regexes.kt @@ -171,6 +171,9 @@ object Regexes { val EDUDZIENNIK_ANNOUNCEMENT_DESCRIPTION by lazy { """
.*?

(.*?)

""".toRegex(DOT_MATCHES_ALL) } + val EDUDZIENNIK_HOMEWORK_DESCRIPTION by lazy { + """
(.*?)
""".toRegex(DOT_MATCHES_ALL) + } val EDUDZIENNIK_SUBJECT_ID by lazy { """/Courses/([\w-_]+?)/""".toRegex() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt index 3119c328..17bf771f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/Edudziennik.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikData import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web.EdudziennikWebGetAnnouncement +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web.EdudziennikWebGetHomework import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.firstlogin.EdudziennikFirstLogin import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.login.EdudziennikLogin import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.login.EdudziennikLoginWeb @@ -96,7 +97,14 @@ class Edudziennik(val app: App, val profile: Profile?, val loginStore: LoginStor override fun getAttachment(owner: Any, attachmentId: Long, attachmentName: String) {} override fun getRecipientList() {} - override fun getEvent(eventFull: EventFull) {} + + override fun getEvent(eventFull: EventFull) { + EdudziennikLoginWeb(data) { + EdudziennikWebGetHomework(data, eventFull) { + completed() + } + } + } override fun firstLogin() { EdudziennikFirstLogin(data) { completed() } } override fun cancel() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetHomework.kt new file mode 100644 index 00000000..84171699 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetHomework.kt @@ -0,0 +1,45 @@ +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +import android.text.Html +import org.greenrobot.eventbus.EventBus +import pl.szczodrzynski.edziennik.data.api.Regexes +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikWeb +import pl.szczodrzynski.edziennik.data.api.events.EventGetEvent +import pl.szczodrzynski.edziennik.data.db.full.EventFull +import pl.szczodrzynski.edziennik.get +import pl.szczodrzynski.edziennik.isNotNullNorEmpty + +class EdudziennikWebGetHomework( + override val data: DataEdudziennik, + val event: EventFull, + val onSuccess: () -> Unit +) : EdudziennikWeb(data, null) { + companion object { + const val TAG = "EdudziennikWebGetHomework" + } + + init { + if (event.attachmentNames.isNotNullNorEmpty()) { + val id = event.attachmentNames!![0] + + webGet(TAG, "Homework/$id") { text -> + val description = Regexes.EDUDZIENNIK_HOMEWORK_DESCRIPTION.find(text)?.get(1)?.trim() + + if (description != null) event.topic = Html.fromHtml(description).toString() + + event.homeworkBody = "" + event.attachmentNames = null + + data.eventList += event + data.eventListReplace = true + + EventBus.getDefault().postSticky(EventGetEvent(event)) + onSuccess() + } + } else { + EventBus.getDefault().postSticky(EventGetEvent(event)) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt index f8a1f14e..acabcfa6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebHomework.kt @@ -33,8 +33,8 @@ class EdudziennikWebHomework(override val data: DataEdudziennik, if (doc.getElementsByClass("message").text().trim() != "Brak prac domowych") { doc.getElementsByTag("tr").forEach { homeworkElement -> val dateElement = homeworkElement.getElementsByClass("date").first().child(0) - val id = EDUDZIENNIK_HOMEWORK_ID.find(dateElement.attr("href"))?.get(1)?.crc32() - ?: return@forEach + val idStr = EDUDZIENNIK_HOMEWORK_ID.find(dateElement.attr("href"))?.get(1) ?: return@forEach + val id = idStr.crc32() val date = Date.fromY_m_d(dateElement.text()) val subjectElement = homeworkElement.child(1).child(0) @@ -64,6 +64,8 @@ class EdudziennikWebHomework(override val data: DataEdudziennik, teamId = data.teamClass?.id ?: -1 ) + eventObject.attachmentNames = mutableListOf(idStr) + data.eventList.add(eventObject) data.metadataList.add(Metadata( profileId, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt index 32be6ac3..b57cc4d4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt @@ -43,7 +43,7 @@ import pl.szczodrzynski.edziennik.data.db.migration.* LibrusLesson::class, TimetableManual::class, Metadata::class -], version = 84) +], version = 85) @TypeConverters( ConverterTime::class, ConverterDate::class, @@ -169,7 +169,8 @@ abstract class AppDb : RoomDatabase() { Migration81(), Migration82(), Migration83(), - Migration84() + Migration84(), + Migration85() ).allowMainThreadQueries().build() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration85.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration85.kt new file mode 100644 index 00000000..6f1c79c0 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration85.kt @@ -0,0 +1,12 @@ +package pl.szczodrzynski.edziennik.data.db.migration + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase +import pl.szczodrzynski.edziennik.data.api.LOGIN_TYPE_EDUDZIENNIK +import pl.szczodrzynski.edziennik.data.db.entity.Event + +class Migration85 : Migration(84, 85) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("DELETE FROM events WHERE eventAddedManually = 0 AND eventType = ${Event.TYPE_HOMEWORK} AND profileId IN (SELECT profileId FROM (SELECT profileId FROM profiles WHERE loginStoreType = $LOGIN_TYPE_EDUDZIENNIK) x)") + } +}