diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 05b3d06f..707c8d1b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -1208,3 +1208,7 @@ val SwipeRefreshLayout.onScrollListener: RecyclerView.OnScrollListener this@onScrollListener.isEnabled = true } } + +operator fun Iterable>.get(key: K): V? { + return firstOrNull { it.first == key }?.second +} 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 633370f5..8511b40d 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 @@ -84,6 +84,22 @@ object Regexes { """(.+?) - (.*?).+?.+?\((.+?), .+?(.+?)\)""".toRegex(DOT_MATCHES_ALL) } + val MOBIDZIENNIK_HOMEWORK_ROW by lazy { + """class="rowRolling">(.+?\s*)""".toRegex(DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_HOMEWORK_ITEM by lazy { + """

(.+?):\s*(.+?)\s*

""".toRegex(DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_HOMEWORK_BODY by lazy { + """Treść:(.+?)

""".toRegex(DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_HOMEWORK_ID by lazy { + """zadanieFormularz\(([0-9]+),""".toRegex(DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_HOMEWORK_ATTACHMENT by lazy { + """zalacznik=([0-9]+)'.+?word-break">(.+?)""".toRegex(DOT_MATCHES_ALL) + } + val IDZIENNIK_LOGIN_HIDDEN_FIELDS by lazy { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt index 8b8951a3..2d90c891 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/Mobidziennik.kt @@ -8,10 +8,7 @@ import com.google.gson.JsonObject import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.MobidziennikData -import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web.MobidziennikWebGetAttachment -import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web.MobidziennikWebGetMessage -import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web.MobidziennikWebGetRecipientList -import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web.MobidziennikWebSendMessage +import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web.* import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.firstlogin.MobidziennikFirstLogin import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.login.MobidziennikLogin import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback @@ -25,6 +22,7 @@ import pl.szczodrzynski.edziennik.data.db.full.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.full.EventFull import pl.szczodrzynski.edziennik.data.db.full.MessageFull import pl.szczodrzynski.edziennik.utils.Utils.d +import pl.szczodrzynski.edziennik.utils.models.Date class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginStore, val callback: EdziennikCallback) : EdziennikInterface { companion object { @@ -122,7 +120,18 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto } } - override fun getEvent(eventFull: EventFull) {} + override fun getEvent(eventFull: EventFull) { + val type = if (eventFull.date >= Date.getToday()) + MobidziennikWebHomework.TYPE_CURRENT + else + MobidziennikWebHomework.TYPE_PAST + + login(LOGIN_METHOD_MOBIDZIENNIK_WEB) { + MobidziennikWebHomework(data, 0L, type, eventFull) { + completed() + } + } + } override fun firstLogin() { MobidziennikFirstLogin(data) { completed() } } override fun cancel() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/MobidziennikFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/MobidziennikFeatures.kt index d056fc4b..7396ac52 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/MobidziennikFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/MobidziennikFeatures.kt @@ -17,6 +17,7 @@ const val ENDPOINT_MOBIDZIENNIK_WEB_NOTICES = 2040 const val ENDPOINT_MOBIDZIENNIK_WEB_ATTENDANCE = 2050 const val ENDPOINT_MOBIDZIENNIK_WEB_MANUALS = 2100 const val ENDPOINT_MOBIDZIENNIK_WEB_ACCOUNT_EMAIL = 2200 +const val ENDPOINT_MOBIDZIENNIK_WEB_HOMEWORK = 2300 // not used as an endpoint const val ENDPOINT_MOBIDZIENNIK_API2_MAIN = 3000 val MobidziennikFeatures = listOf( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebHomework.kt new file mode 100644 index 00000000..944b21ea --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebHomework.kt @@ -0,0 +1,79 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2020-3-31. + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web + +import org.greenrobot.eventbus.EventBus +import pl.szczodrzynski.edziennik.data.api.Regexes +import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_HOMEWORK +import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.MobidziennikWeb +import pl.szczodrzynski.edziennik.data.api.events.EventGetEvent +import pl.szczodrzynski.edziennik.data.db.full.EventFull +import pl.szczodrzynski.edziennik.get + +class MobidziennikWebHomework(override val data: DataMobidziennik, + override val lastSync: Long?, + val type: Int = TYPE_CURRENT, + val event: EventFull, + val onSuccess: (endpointId: Int) -> Unit +) : MobidziennikWeb(data, lastSync) { + companion object { + private const val TAG = "MobidziennikWebHomework" + const val TYPE_CURRENT = 0 + const val TYPE_PAST = 1 + } + + init { + val endpoint = when (type) { + TYPE_PAST -> "zadaniadomowearchiwalne" + else -> "zadaniadomowe" + } + webGet(TAG, "/mobile/$endpoint") { text -> + MobidziennikLuckyNumberExtractor(data, text) + + Regexes.MOBIDZIENNIK_HOMEWORK_ROW.findAll(text).forEach { homeworkMatch -> + val tableRow = homeworkMatch[1].ifBlank { return@forEach } + + /*val items = Regexes.MOBIDZIENNIK_HOMEWORK_ITEM.findAll(tableRow).map { match -> + match[1] to match[2].fixWhiteSpaces() + }.toList()*/ + + val id = Regexes.MOBIDZIENNIK_HOMEWORK_ID.find(tableRow)?.get(1)?.toLongOrNull() ?: return@forEach + if (event.id != id) + return@forEach + + //val homeworkBody = Regexes.MOBIDZIENNIK_HOMEWORK_BODY.find(tableRow)?.get(1) ?: "" + + event.attachmentIds = mutableListOf() + event.attachmentNames = mutableListOf() + Regexes.MOBIDZIENNIK_HOMEWORK_ATTACHMENT.findAll(tableRow).forEach { + event.attachmentIds?.add(it[1].toLongOrNull() ?: return@forEach) + event.attachmentNames?.add(it[2]) + } + + event.homeworkBody = "" + } + + //data.eventList.add(eventObject) + //data.metadataList.add( + // Metadata( + // profileId, + // Metadata.TYPE_EVENT, + // eventObject.id, + // profile?.empty ?: false, + // profile?.empty ?: false, + // System.currentTimeMillis() /* no addedDate here though */ + // )) + + // not used as an endpoint + //data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_HOMEWORK, SYNC_ALWAYS) + data.eventList.add(event) + data.eventListReplace = true + + EventBus.getDefault().postSticky(EventGetEvent(event)) + onSuccess(ENDPOINT_MOBIDZIENNIK_WEB_HOMEWORK) + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt index ce8cc3ca..4869903f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt @@ -82,9 +82,15 @@ open class Event( @ColumnInfo(name = "eventIsDone") var isDone: Boolean = false + /** + * Body/text of the event, if this is a [TYPE_HOMEWORK]. + * May be null if the body is not downloaded yet, or the type is not [TYPE_HOMEWORK]. + * May be empty or blank if the homework has no specific body attached, + * or the topic contains the body already. + */ var homeworkBody: String? = null - var attachmentIds: List? = null - var attachmentNames: List? = null + var attachmentIds: MutableList? = null + var attachmentNames: MutableList? = null @Ignore var showAsUnseen = false