[Mobidziennik] Fix syncing message lists.

This commit is contained in:
Kuba Szczodrzyński 2022-02-05 17:39:15 +01:00
parent e314fafaff
commit aeed735521
No known key found for this signature in database
GPG Key ID: 70CB8A85BA1633CB
3 changed files with 61 additions and 14 deletions

View File

@ -7,10 +7,12 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik
import android.util.LongSparseArray
import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.data.api.LOGIN_METHOD_MOBIDZIENNIK_WEB
import pl.szczodrzynski.edziennik.data.api.Regexes
import pl.szczodrzynski.edziennik.data.api.models.Data
import pl.szczodrzynski.edziennik.data.db.entity.LoginStore
import pl.szczodrzynski.edziennik.data.db.entity.Profile
import pl.szczodrzynski.edziennik.ext.currentTimeUnix
import pl.szczodrzynski.edziennik.ext.get
import pl.szczodrzynski.edziennik.ext.isNotNullNorEmpty
import pl.szczodrzynski.edziennik.utils.models.Date
import pl.szczodrzynski.edziennik.utils.models.Time
@ -35,6 +37,31 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da
override fun generateUserCode() = "$loginServerName:$loginUsername:$studentId"
fun parseDateTime(dateStr: String): Pair<Date, Time> {
// pt, 4 lut, 09:11
val dateParts = dateStr.split(',', ' ').filter { it.isNotEmpty() }
// [pt], [4], [lut], [09:11]
val date = Date.getToday()
date.day = dateParts[1].toIntOrNull() ?: 1
date.month = when (dateParts[2]) {
"sty" -> 1
"lut" -> 2
"mar" -> 3
"kwi" -> 4
"maj" -> 5
"cze" -> 6
"lip" -> 7
"sie" -> 8
"wrz" -> 9
"paź" -> 10
"lis" -> 11
"gru" -> 12
else -> 1
}
val time = Time.fromH_m(dateParts[3])
return date to time
}
val teachersMap = LongSparseArray<String>()
val subjectsMap = LongSparseArray<String>()

View File

@ -36,6 +36,10 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik,
val doc = Jsoup.parse(text)
val today = Date.getToday()
var currentYear = today.year
var currentMonth = today.month
val list = doc.getElementsByClass("spis").first()?.getElementsByClass("podswietl")
list?.forEach { item ->
val id = item.attr("rel").toLongOrNull() ?: return@forEach
@ -47,15 +51,20 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik,
}
val subject = subjectEl?.ownText() ?: ""
val addedDateEl = item.select("td:eq(1) small").first()
val addedDate = Date.fromIsoHm(addedDateEl?.text())
val addedDateEl = item.select("td:eq(4)").first()
val (date, time) = data.parseDateTime(addedDateEl?.text()?.trim() ?: "")
if (date.month > currentMonth) {
currentYear--
}
currentMonth = date.month
date.year = currentYear
val senderEl = item.select("td:eq(2)").first()
val senderEl = item.select("td:eq(3)").first()
val senderName = senderEl?.ownText().fixName()
val senderId = data.teacherList.singleOrNull { it.fullNameLastFirst == senderName }?.id
data.messageRecipientIgnoreList.add(MessageRecipient(profileId, -1, id))
val isRead = item.select("td:eq(3) span").first()?.hasClass("wiadomosc_przeczytana") == true
val isRead = item.select("td:eq(5) span").first()?.hasClass("wiadomosc_przeczytana") == true
val message = Message(
profileId = profileId,
@ -64,7 +73,7 @@ class MobidziennikWebMessagesInbox(override val data: DataMobidziennik,
subject = subject,
body = null,
senderId = senderId,
addedDate = addedDate
addedDate = date.combineWith(time)
)
if (hasAttachments)

View File

@ -40,23 +40,29 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik,
val doc = Jsoup.parse(text)
val today = Date.getToday()
var currentYear = today.year
var currentMonth = today.month
val list = doc.getElementsByClass("spis").first()?.getElementsByClass("podswietl")
list?.forEach { item ->
val id = item.attr("rel").toLongOrNull() ?: return@forEach
val subjectEl = item.select("td:eq(0)").first()
var hasAttachments = false
if (subjectEl?.getElementsByTag("a")?.size ?: 0 > 0) {
hasAttachments = true
}
val subject = subjectEl?.ownText() ?: ""
val readByString = item.select("td:eq(2)").first()?.text() ?: ""
val attachmentsEl = item.select("td:eq(1)").first()
var hasAttachments = false
if (attachmentsEl?.getElementsByTag("a")?.size ?: 0 > 0) {
hasAttachments = true
}
val readByString = item.select("td:eq(4)").first()?.text() ?: ""
val (readBy, sentTo) = Regexes.MOBIDZIENNIK_MESSAGE_SENT_READ_BY.find(readByString).let {
(it?.get(1)?.toIntOrNull() ?: 0) to (it?.get(2)?.toIntOrNull() ?: 0)
}
val recipientEl = item.select("td:eq(1) a span").first()
val recipientEl = item.select("td:eq(2) a span").first()
val recipientNames = recipientEl?.ownText()?.split(", ")
val readState = when (readBy) {
0 -> 0
@ -69,8 +75,13 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik,
data.messageRecipientIgnoreList.add(MessageRecipient(profileId, recipientId, -1, readState, id))
}
val addedDateEl = item.select("td:eq(3) small").first()
val addedDate = Date.fromIsoHm(addedDateEl?.text())
val addedDateEl = item.select("td:eq(3)").first()
val (date, time) = data.parseDateTime(addedDateEl?.text()?.trim() ?: "")
if (date.month > currentMonth) {
currentYear--
}
currentMonth = date.month
date.year = currentYear
val message = Message(
profileId = profileId,
@ -79,7 +90,7 @@ class MobidziennikWebMessagesSent(override val data: DataMobidziennik,
subject = subject,
body = null,
senderId = null,
addedDate = addedDate
addedDate = date.combineWith(time)
)
if (hasAttachments)