Merge pull request #81 from szkolny-eu/hotfix/mobidziennik-event-description

[API/Mobidziennik] Implement getting full event description.
This commit is contained in:
Kuba Szczodrzyński 2021-10-01 21:18:52 +02:00 committed by GitHub
commit 591abb4bb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 147 additions and 36 deletions

View File

@ -16,6 +16,10 @@ object Regexes {
"""[^0-9]""".toRegex()
}
val HTML_BR by lazy {
"""<br\s?/?>""".toRegex()
}
val MOBIDZIENNIK_GRADES_SUBJECT_NAME by lazy {
@ -128,6 +132,11 @@ object Regexes {
}
val MOBIDZIENNIK_EVENT_CONTENT by lazy {
"""<h1>(.+?) <small>\(wpisał\(a\) (.+?) w dniu ([0-9-]{10})\).+?<strong>(.+?)</strong><br""".toRegex(DOT_MATCHES_ALL)
}
val IDZIENNIK_LOGIN_HIDDEN_FIELDS by lazy {
"""<input type="hidden".+?name="([A-z0-9_]+)?".+?value="([A-z0-9_+-/=]+)?".+?>""".toRegex(DOT_MATCHES_ALL)

View File

@ -120,10 +120,17 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto
override fun getEvent(eventFull: EventFull) {
login(LOGIN_METHOD_MOBIDZIENNIK_WEB) {
if (eventFull.isHomework) {
MobidziennikWebGetHomework(data, eventFull) {
completed()
}
}
else {
MobidziennikWebGetEvent(data, eventFull) {
completed()
}
}
}
}
override fun firstLogin() { MobidziennikFirstLogin(data) { completed() } }

View File

@ -81,6 +81,7 @@ class MobidziennikWebCalendar(override val data: DataMobidziennik,
subjectId = -1,
teamId = data.teamClass?.id ?: -1
)
eventObject.isDownloaded = false
data.eventList.add(eventObject)
data.metadataList.add(

View File

@ -0,0 +1,57 @@
/*
* Copyright (c) Kuba Szczodrzyński 2021-10-1.
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.web
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.data.api.POST
import pl.szczodrzynski.edziennik.data.api.Regexes
import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.DataMobidziennik
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
import pl.szczodrzynski.edziennik.utils.models.Date
class MobidziennikWebGetEvent(
override val data: DataMobidziennik,
val event: EventFull,
val onSuccess: () -> Unit
) : MobidziennikWeb(data, null) {
companion object {
private const val TAG = "MobidziennikWebGetEvent"
}
init {
val params = listOf(
"typ" to "kalendarz",
"uczen" to data.studentId,
"id" to event.id,
)
webGet(TAG, "/dziennik/ajaxkalendarzklasowy", method = POST, parameters = params) { text ->
Regexes.MOBIDZIENNIK_EVENT_CONTENT.find(text)?.let {
val topic = it[1]
val teacherName = it[2]
val teacher = data.getTeacherByLastFirst(teacherName)
val addedDate = Date.fromY_m_d(it[3])
val body = it[4]
.replace("\n", "")
.replace(Regexes.HTML_BR, "\n")
event.topic = topic
event.homeworkBody = body
event.teacherId = teacher.id
event.addedDate = addedDate.inMillis
event.isDownloaded = true
}
data.eventList.add(event)
data.eventListReplace = true
EventBus.getDefault().postSticky(EventGetEvent(event))
onSuccess()
}
}
}

View File

@ -437,11 +437,13 @@ abstract class Data(val app: App, val profile: Profile?, val loginStore: LoginSt
}
fun getTeacherByLastFirst(nameLastFirst: String, loginId: String? = null): Teacher {
// comparing full name is safer than splitting and swapping
val teacher = teacherList.singleOrNull { it.fullNameLastFirst == nameLastFirst }
val nameParts = nameLastFirst.split(" ")
return if (nameParts.size == 1)
getTeacher(nameParts[0], "", loginId)
validateTeacher(teacher, nameParts[0], "", loginId)
else
getTeacher(nameParts[1], nameParts[0], loginId)
validateTeacher(teacher, nameParts[1], nameParts[0], loginId)
}
fun getTeacherByFirstLast(nameFirstLast: String, loginId: String? = null): Teacher {

View File

@ -70,7 +70,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
private fun eventNotifications() {
for (event in app.db.eventDao().getNotNotifiedNow().filter { it.date >= today }) {
val text = if (event.type == Event.TYPE_HOMEWORK)
val text = if (event.isHomework)
app.getString(
if (event.subjectLongName.isNullOrEmpty())
R.string.notification_homework_no_subject_format
@ -98,7 +98,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
event.time?.stringHM ?: app.getString(R.string.event_all_day),
event.topic.take(200)
)
val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT
val type = if (event.isHomework) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT
notifications += Notification(
id = Notification.buildId(event.profileId, type, event.id),
title = app.getNotificationTitle(type),
@ -107,7 +107,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
type = type,
profileId = event.profileId,
profileName = profiles.singleOrNull { it.id == event.profileId }?.name,
viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
viewId = if (event.isHomework) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
addedDate = event.addedDate
).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong())
}
@ -132,7 +132,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
event.time?.stringHM ?: app.getString(R.string.event_all_day),
event.topic.take(200)
)
val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT
val type = if (event.isHomework) Notification.TYPE_NEW_HOMEWORK else Notification.TYPE_NEW_EVENT
notifications += Notification(
id = Notification.buildId(event.profileId, type, event.id),
title = app.getNotificationTitle(type),
@ -141,7 +141,7 @@ class Notifications(val app: App, val notifications: MutableList<Notification>,
type = type,
profileId = event.profileId,
profileName = profiles.singleOrNull { it.id == event.profileId }?.name,
viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
viewId = if (event.isHomework) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
addedDate = event.addedDate
).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong())
}

View File

@ -43,7 +43,7 @@ import pl.szczodrzynski.edziennik.data.db.migration.*
LibrusLesson::class,
TimetableManual::class,
Metadata::class
], version = 93)
], version = 94)
@TypeConverters(
ConverterTime::class,
ConverterDate::class,
@ -178,7 +178,8 @@ abstract class AppDb : RoomDatabase() {
Migration90(),
Migration91(),
Migration92(),
Migration93()
Migration93(),
Migration94()
).allowMainThreadQueries().build()
}
}

View File

@ -4,6 +4,15 @@
package pl.szczodrzynski.edziennik.data.db.dao;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_ANNOUNCEMENT;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_ATTENDANCE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_EVENT;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_GRADE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_HOMEWORK;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_LESSON_CHANGE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_MESSAGE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_NOTICE;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Insert;
@ -23,15 +32,6 @@ import pl.szczodrzynski.edziennik.data.db.entity.Notice;
import pl.szczodrzynski.edziennik.data.db.full.LessonFull;
import pl.szczodrzynski.edziennik.utils.models.UnreadCounter;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_ANNOUNCEMENT;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_ATTENDANCE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_EVENT;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_GRADE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_HOMEWORK;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_LESSON_CHANGE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_MESSAGE;
import static pl.szczodrzynski.edziennik.data.db.entity.Metadata.TYPE_NOTICE;
@Dao
public abstract class MetadataDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
@ -78,8 +78,8 @@ public abstract class MetadataDao {
}
}
if (o instanceof Event) {
if (add(new Metadata(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen, false)) == -1) {
updateSeen(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen);
if (add(new Metadata(profileId, ((Event) o).isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen, false)) == -1) {
updateSeen(profileId, ((Event) o).isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), seen);
}
}
if (o instanceof LessonFull) {
@ -117,8 +117,8 @@ public abstract class MetadataDao {
}
}
if (o instanceof Event) {
if (add(new Metadata(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), false, notified)) == -1) {
updateNotified(profileId, ((Event) o).getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), notified);
if (add(new Metadata(profileId, ((Event) o).isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), false, notified)) == -1) {
updateNotified(profileId, ((Event) o).isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, ((Event) o).getId(), notified);
}
}
if (o instanceof LessonFull) {
@ -141,9 +141,9 @@ public abstract class MetadataDao {
@Transaction
public void setBoth(int profileId, Event o, boolean seen, boolean notified, long addedDate) {
if (o != null) {
if (add(new Metadata(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen, notified)) == -1) {
updateSeen(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen);
updateNotified(profileId, o.getType() == Event.TYPE_HOMEWORK ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), notified);
if (add(new Metadata(profileId, o.isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen, notified)) == -1) {
updateSeen(profileId, o.isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), seen);
updateNotified(profileId, o.isHomework() ? TYPE_HOMEWORK : TYPE_EVENT, o.getId(), notified);
}
}
}

View File

@ -83,6 +83,13 @@ open class Event(
@ColumnInfo(name = "eventIsDone")
var isDone: Boolean = false
/**
* Whether the full contents of the event are already stored locally.
* There may be a need to download the full topic or body.
*/
@ColumnInfo(name = "eventIsDownloaded")
var isDownloaded: Boolean = true
/**
* 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].
@ -123,6 +130,9 @@ open class Event(
it.timeInMillis += 45 * MINUTE * 1000
}
val isHomework
get() = type == TYPE_HOMEWORK
@Ignore
fun withMetadata(metadata: Metadata) = EventFull(this, metadata)
}

View File

@ -26,6 +26,7 @@ class EventFull(
sharedBy = it.sharedBy
sharedByName = it.sharedByName
blacklisted = it.blacklisted
isDownloaded = it.isDownloaded
homeworkBody = it.homeworkBody
attachmentIds = it.attachmentIds
attachmentNames = it.attachmentNames

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) Kuba Szczodrzyński 2021-10-1.
*/
package pl.szczodrzynski.edziennik.data.db.migration
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import pl.szczodrzynski.edziennik.data.db.entity.Event
import pl.szczodrzynski.edziennik.data.db.entity.LoginStore
class Migration94 : Migration(93, 94) {
override fun migrate(database: SupportSQLiteDatabase) {
// events - is downloaded flag
// get all profiles using Mobidziennik
database.execSQL("CREATE TABLE _94_ids (id INTEGER NOT NULL);")
database.execSQL("INSERT INTO _94_ids SELECT profileId FROM profiles JOIN loginStores USING(loginStoreId) WHERE loginStores.loginStoreType = ${LoginStore.LOGIN_TYPE_MOBIDZIENNIK};")
database.execSQL("ALTER TABLE events ADD COLUMN eventIsDownloaded INT NOT NULL DEFAULT 1;")
// set isDownloaded = 0 for information events in Mobidziennik
database.execSQL("UPDATE events SET eventIsDownloaded = 0 WHERE profileId IN (SELECT id FROM _94_ids) AND eventType = ${Event.TYPE_INFORMATION};")
database.execSQL("DROP TABLE _94_ids;")
}
}

View File

@ -139,13 +139,13 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
val metadata = Metadata(
event.profileId,
if (event.type == Event.TYPE_HOMEWORK) Metadata.TYPE_HOMEWORK else Metadata.TYPE_EVENT,
if (event.isHomework) Metadata.TYPE_HOMEWORK else Metadata.TYPE_EVENT,
event.id,
false,
true
)
val type = if (event.type == Event.TYPE_HOMEWORK) Notification.TYPE_NEW_SHARED_HOMEWORK else Notification.TYPE_NEW_SHARED_EVENT
val type = if (event.isHomework) Notification.TYPE_NEW_SHARED_HOMEWORK else Notification.TYPE_NEW_SHARED_EVENT
val notificationFilter = app.config.getFor(event.profileId).sync.notificationFilter
if (!notificationFilter.contains(type)) {
@ -156,7 +156,7 @@ class SzkolnyAppFirebase(val app: App, val profiles: List<Profile>, val message:
type = type,
profileId = profile.id,
profileName = profile.name,
viewId = if (event.type == Event.TYPE_HOMEWORK) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
viewId = if (event.isHomework) MainActivity.DRAWER_ITEM_HOMEWORK else MainActivity.DRAWER_ITEM_AGENDA,
addedDate = event.addedDate
).addExtra("eventId", event.id).addExtra("eventDate", event.date.value.toLong())
notificationList += notification

View File

@ -22,7 +22,6 @@ import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.events.EventGetEvent
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.db.entity.Event
import pl.szczodrzynski.edziennik.data.db.full.EventFull
import pl.szczodrzynski.edziennik.databinding.DialogEventDetailsBinding
import pl.szczodrzynski.edziennik.ui.modules.timetable.TimetableFragment
@ -227,7 +226,7 @@ class EventDetailsDialog(
)
}
if (event.homeworkBody == null && !event.addedManually && event.type == Event.TYPE_HOMEWORK) {
if (!event.addedManually && (!event.isDownloaded || event.isHomework && event.homeworkBody == null)) {
b.bodyTitle.isVisible = true
b.bodyProgressBar.isVisible = true
b.body.isVisible = false

View File

@ -17,7 +17,6 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.config.Config
import pl.szczodrzynski.edziennik.data.db.entity.Event
import pl.szczodrzynski.edziennik.databinding.LabFragmentBinding
import pl.szczodrzynski.edziennik.ui.dialogs.profile.ProfileRemoveDialog
import pl.szczodrzynski.edziennik.ui.modules.base.lazypager.LazyFragment
@ -55,7 +54,7 @@ class LabPageFragment : LazyFragment(), CoroutineScope {
b.last10unseen.onClick {
launch(Dispatchers.Default) {
val events = app.db.eventDao().getAllNow(App.profileId)
val ids = events.sortedBy { it.date }.filter { it.type == Event.TYPE_HOMEWORK }.takeLast(10)
val ids = events.sortedBy { it.date }.filter { it.isHomework }.takeLast(10)
ids.forEach {
app.db.metadataDao().setSeen(App.profileId, it, false)
}

View File

@ -27,7 +27,6 @@ import com.mikepenz.iconics.utils.colorInt
import com.mikepenz.iconics.utils.sizeDp
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.db.entity.Event.Companion.TYPE_HOMEWORK
import pl.szczodrzynski.edziennik.data.db.entity.Lesson
import pl.szczodrzynski.edziennik.data.db.entity.Lesson.Companion.TYPE_NO_LESSONS
import pl.szczodrzynski.edziennik.ui.widgets.LessonDialogActivity
@ -355,7 +354,7 @@ class WidgetTimetableProvider : AppWidgetProvider() {
for (event in events) {
if (event.time == null || event.time != lesson.displayStartTime)
continue
model.eventColors.add(if (event.type == TYPE_HOMEWORK) ItemWidgetTimetableModel.EVENT_COLOR_HOMEWORK else event.eventColor)
model.eventColors.add(if (event.isHomework) ItemWidgetTimetableModel.EVENT_COLOR_HOMEWORK else event.eventColor)
}
models += model