From e472d34f4d8ab99814682a49a54bbf619ebc9c6f Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Thu, 26 Dec 2019 22:59:43 +0100 Subject: [PATCH] [Announcements] Add idString column and add getting attachments in Edudziennik. --- .../edziennik/data/api/Regexes.kt | 7 ++ .../edziennik/edudziennik/DataEdudziennik.kt | 3 + .../api/edziennik/edudziennik/Edudziennik.kt | 11 +++ .../edudziennik/EdudziennikFeatures.kt | 13 ++++ .../edudziennik/data/EdudziennikData.kt | 4 + .../data/web/EdudziennikWebAnnouncements.kt | 73 +++++++++++++++++++ .../data/web/EdudziennikWebGetAnnouncement.kt | 36 +++++++++ .../data/api/edziennik/idziennik/Idziennik.kt | 5 ++ .../data/web/IdziennikWebAnnouncements.kt | 3 +- .../data/api/edziennik/librus/Librus.kt | 5 ++ .../librus/data/api/LibrusApiAnnouncements.kt | 6 +- .../edziennik/mobidziennik/Mobidziennik.kt | 5 ++ .../data/api/edziennik/template/Template.kt | 5 ++ .../data/api/edziennik/vulcan/Vulcan.kt | 5 ++ .../data/api/events/AnnouncementGetEvent.kt | 9 +++ .../data/api/interfaces/EdziennikInterface.kt | 2 + .../edziennik/data/api/models/Data.kt | 5 ++ .../edziennik/data/api/task/EdziennikTask.kt | 4 + .../edziennik/data/db/AppDb.java | 12 ++- .../modules/announcements/Announcement.java | 9 ++- .../announcements/AnnouncementDao.java | 3 + .../announcements/AnnouncementsFragment.java | 68 +++++++++++++---- 22 files changed, 273 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetAnnouncement.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/events/AnnouncementGetEvent.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 8ca1af0b..1d2e634e 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 @@ -113,6 +113,10 @@ object Regexes { """\((.+?)\) (.+)""".toRegex() } + val EDUDZIENNIK_ANNOUNCEMENT_DESCRIPTION by lazy { + """
.*?

(.*?)

""".toRegex(DOT_MATCHES_ALL) + } + val EDUDZIENNIK_SUBJECT_ID by lazy { """/Courses/([\w-_]+?)/""".toRegex() } @@ -125,6 +129,9 @@ object Regexes { val EDUDZIENNIK_EVENT_TYPE_ID by lazy { """/GradeLabels/([\w-_]+?)/""".toRegex() } + val EDUDZIENNIK_ANNOUNCEMENT_ID by lazy { + """/Announcement/([\w-_]+?)/""".toRegex() + } val EDUDZIENNIK_SCHOOL_DETAIL_ID by lazy { """ Unit) { data.startProgress(R.string.edziennik_progress_endpoint_attendance) EdudziennikWebAttendance(data, onSuccess) } + ENDPOINT_EDUDZIENNIK_WEB_ANNOUNCEMENTS -> { + data.startProgress(R.string.edziennik_progress_endpoint_announcements) + EdudziennikWebAnnouncements(data, onSuccess) + } ENDPOINT_EDUDZIENNIK_WEB_LUCKY_NUMBER -> { data.startProgress(R.string.edziennik_progress_endpoint_lucky_number) EdudziennikWebLuckyNumber(data, onSuccess) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt new file mode 100644 index 00000000..98d95c15 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebAnnouncements.kt @@ -0,0 +1,73 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-26 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +import org.jsoup.Jsoup +import pl.szczodrzynski.edziennik.crc32 +import pl.szczodrzynski.edziennik.data.api.Regexes.EDUDZIENNIK_ANNOUNCEMENT_ID +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.DataEdudziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.ENDPOINT_EDUDZIENNIK_WEB_ANNOUNCEMENTS +import pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.EdudziennikWeb +import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.get +import pl.szczodrzynski.edziennik.splitName +import pl.szczodrzynski.edziennik.utils.models.Date + +class EdudziennikWebAnnouncements(override val data: DataEdudziennik, + val onSuccess: () -> Unit) : EdudziennikWeb(data) { + companion object { + const val TAG = "EdudziennikWebAnnouncements" + } + + init { data.profile?.also { profile -> + webGet(TAG, data.schoolClassEndpoint + "Announcements") { text -> + val doc = Jsoup.parse(text) + + doc.select("table.list tbody tr").forEach { announcementElement -> + val titleElement = announcementElement.child(0).child(0) + + val longId = EDUDZIENNIK_ANNOUNCEMENT_ID.find(titleElement.attr("href"))?.get(1) + ?: return@forEach + val id = longId.crc32() + val subject = titleElement.text() + + val teacherName = announcementElement.child(1).text() + val teacher = teacherName.splitName()?.let { (teacherFirstName, teacherLastName) -> + data.getTeacher(teacherFirstName, teacherLastName) + } ?: return@forEach + + val dateString = announcementElement.getElementsByClass("datetime").first().text() + val startDate = Date.fromY_m_d(dateString) + val addedDate = Date.fromIsoHm(dateString) + + val announcementObject = Announcement( + profileId, + id, + subject, + null, + startDate, + null, + teacher.id, + longId + ) + + data.announcementIgnoreList.add(announcementObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_ANNOUNCEMENT, + id, + profile.empty, + profile.empty, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_EDUDZIENNIK_WEB_ANNOUNCEMENTS, SYNC_ALWAYS) + onSuccess() + } + } ?: onSuccess() } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetAnnouncement.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetAnnouncement.kt new file mode 100644 index 00000000..3b35ef0a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/edudziennik/data/web/EdudziennikWebGetAnnouncement.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-26 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.edudziennik.data.web + +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.AnnouncementGetEvent +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull +import pl.szczodrzynski.edziennik.get + +class EdudziennikWebGetAnnouncement( + override val data: DataEdudziennik, + announcement: AnnouncementFull, + val onSuccess: () -> Unit +) : EdudziennikWeb(data) { + companion object { + const val TAG = "EdudziennikWebGetAnnouncement" + } + + init { + webGet(TAG, "Announcement/${announcement.idString}") { text -> + val description = Regexes.EDUDZIENNIK_ANNOUNCEMENT_DESCRIPTION.find(text)?.get(1)?.trim() ?: "" + + announcement.text = description + + EventBus.getDefault().postSticky(AnnouncementGetEvent(announcement)) + + data.announcementList.add(announcement) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt index 98010dc8..ae17bed8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt @@ -15,6 +15,7 @@ import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.prepare +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -71,6 +72,10 @@ class Idziennik(val app: App, val profile: Profile?, val loginStore: LoginStore, } + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebAnnouncements.kt index 20844220..f8c6573b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebAnnouncements.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebAnnouncements.kt @@ -54,7 +54,8 @@ class IdziennikWebAnnouncements(override val data: DataIdziennik, jAnnouncement.get("Tresc").asString, startDate, null, - rTeacher.id + rTeacher.id, + null ) data.announcementList.add(announcementObject) data.metadataList.add(Metadata( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt index bf33bbdb..24eba49d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/Librus.kt @@ -16,6 +16,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.librus.login.* import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.models.ApiError +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -106,6 +107,10 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va } } + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { LibrusLoginPortal(data) { LibrusLoginApi(data) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiAnnouncements.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiAnnouncements.kt index 2dd3a5ec..e8dabc11 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiAnnouncements.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiAnnouncements.kt @@ -24,7 +24,8 @@ class LibrusApiAnnouncements(override val data: DataLibrus, val announcements = json.getJsonArray("SchoolNotices").asJsonObjectList() announcements?.forEach { announcement -> - val id = announcement.getString("Id")?.crc32() ?: return@forEach + val longId = announcement.getString("Id") ?: return@forEach + val id = longId.crc32() val subject = announcement.getString("Subject") ?: "" val text = announcement.getString("Content") ?: "" val startDate = Date.fromY_m_d(announcement.getString("StartDate")) @@ -41,7 +42,8 @@ class LibrusApiAnnouncements(override val data: DataLibrus, text, startDate, endDate, - teacherId + teacherId, + longId ) data.announcementList.add(announcementObject) 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 4b8b822d..249c4558 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 @@ -18,6 +18,7 @@ import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.mobidziennikLoginMethods import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.prepare +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -80,6 +81,10 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto } + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { MobidziennikLoginWeb(data) { MobidziennikWebGetAttachment(data, message, attachmentId, attachmentName) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt index e850b741..fc65a76f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/template/Template.kt @@ -15,6 +15,7 @@ import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.prepare import pl.szczodrzynski.edziennik.data.api.templateLoginMethods +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -71,6 +72,10 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, } + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt index c30f1fd0..eed4de82 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/Vulcan.kt @@ -17,6 +17,7 @@ import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.prepare import pl.szczodrzynski.edziennik.data.api.vulcanLoginMethods +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -77,6 +78,10 @@ class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, va } + override fun getAnnouncement(announcement: AnnouncementFull) { + + } + override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/events/AnnouncementGetEvent.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/events/AnnouncementGetEvent.kt new file mode 100644 index 00000000..9278844a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/events/AnnouncementGetEvent.kt @@ -0,0 +1,9 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-26 + */ + +package pl.szczodrzynski.edziennik.data.api.events + +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull + +data class AnnouncementGetEvent(val announcement: AnnouncementFull) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt index e3e11066..d12eb060 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/interfaces/EdziennikInterface.kt @@ -5,6 +5,7 @@ package pl.szczodrzynski.edziennik.data.api.interfaces import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -12,6 +13,7 @@ interface EdziennikInterface { fun sync(featureIds: List, viewId: Int? = null, arguments: JsonObject? = null) fun getMessage(message: MessageFull) fun markAllAnnouncementsAsRead() + fun getAnnouncement(announcement: AnnouncementFull) fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) fun firstLogin() fun cancel() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt index fddb4be0..7596bb65 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt @@ -149,6 +149,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val attendanceList = mutableListOf() val announcementList = mutableListOf() + val announcementIgnoreList = mutableListOf() val luckyNumberList = mutableListOf() @@ -203,8 +204,10 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) noticeList.clear() attendanceList.clear() announcementList.clear() + announcementIgnoreList.clear() luckyNumberList.clear() teacherAbsenceList.clear() + messageList.clear() messageIgnoreList.clear() messageRecipientList.clear() messageRecipientIgnoreList.clear() @@ -315,6 +318,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.attendanceDao().addAll(attendanceList) if (announcementList.isNotEmpty()) db.announcementDao().addAll(announcementList) + if (announcementIgnoreList.isNotEmpty()) + db.announcementDao().addAllIgnore(announcementIgnoreList) if (luckyNumberList.isNotEmpty()) db.luckyNumberDao().addAll(luckyNumberList) if (teacherAbsenceList.isNotEmpty()) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/EdziennikTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/EdziennikTask.kt index 760703ad..d0c80bda 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/EdziennikTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/EdziennikTask.kt @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.template.Template import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.Vulcan import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.messages.Message import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull @@ -26,6 +27,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa fun syncProfileList(profileList: List) = EdziennikTask(-1, SyncProfileListRequest(profileList)) fun messageGet(profileId: Int, message: MessageFull) = EdziennikTask(profileId, MessageGetRequest(message)) fun announcementsRead(profileId: Int) = EdziennikTask(profileId, AnnouncementsReadRequest()) + fun announcementGet(profileId: Int, announcement: AnnouncementFull) = EdziennikTask(profileId, AnnouncementGetRequest(announcement)) fun attachmentGet(profileId: Int, message: Message, attachmentId: Long, attachmentName: String) = EdziennikTask(profileId, AttachmentGetRequest(message, attachmentId, attachmentName)) } @@ -77,6 +79,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa is MessageGetRequest -> edziennikInterface?.getMessage(request.message) is FirstLoginRequest -> edziennikInterface?.firstLogin() is AnnouncementsReadRequest -> edziennikInterface?.markAllAnnouncementsAsRead() + is AnnouncementGetRequest -> edziennikInterface?.getAnnouncement(request.announcement) is AttachmentGetRequest -> edziennikInterface?.getAttachment(request.message, request.attachmentId, request.attachmentName) } } @@ -95,5 +98,6 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa data class SyncProfileListRequest(val profileList: List) data class MessageGetRequest(val message: MessageFull) class AnnouncementsReadRequest + data class AnnouncementGetRequest(val announcement: AnnouncementFull) data class AttachmentGetRequest(val message: Message, val attachmentId: Long, val attachmentName: String) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java index 7144ccb3..cd9abbf4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.java @@ -108,7 +108,7 @@ import pl.szczodrzynski.edziennik.utils.models.Date; AttendanceType.class, pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson.class, ConfigEntry.class, - Metadata.class}, version = 69) + Metadata.class}, version = 70) @TypeConverters({ ConverterTime.class, ConverterDate.class, @@ -829,6 +829,13 @@ public abstract class AppDb extends RoomDatabase { database.execSQL("ALTER TABLE loginStores ADD COLUMN loginStoreMode INTEGER NOT NULL DEFAULT 0"); } }; + private static final Migration MIGRATION_69_70 = new Migration(69, 70) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("ALTER TABLE announcements ADD COLUMN announcementIdString TEXT DEFAULT NULL"); + database.execSQL("DELETE FROM announcements"); + } + }; public static AppDb getDatabase(final Context context) { @@ -895,7 +902,8 @@ public abstract class AppDb extends RoomDatabase { MIGRATION_65_66, MIGRATION_66_67, MIGRATION_67_68, - MIGRATION_68_69 + MIGRATION_68_69, + MIGRATION_69_70 ) .allowMainThreadQueries() //.fallbackToDestructiveMigration() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/Announcement.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/Announcement.java index 49ba227c..66f6fbff 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/Announcement.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/Announcement.java @@ -5,6 +5,7 @@ import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.Ignore; import androidx.room.Index; + import pl.szczodrzynski.edziennik.utils.models.Date; @Entity(tableName = "announcements", @@ -18,6 +19,7 @@ public class Announcement { @ColumnInfo(name = "announcementSubject") public String subject; + @Nullable @ColumnInfo(name = "announcementText") public String text; @Nullable @@ -29,10 +31,14 @@ public class Announcement { public long teacherId; + @Nullable + @ColumnInfo(name = "announcementIdString") + public String idString; + @Ignore public Announcement() {} - public Announcement(int profileId, long id, String subject, String text, @Nullable Date startDate, @Nullable Date endDate, long teacherId) { + public Announcement(int profileId, long id, String subject, String text, @Nullable Date startDate, @Nullable Date endDate, long teacherId, @Nullable String idString) { this.profileId = profileId; this.id = id; this.subject = subject; @@ -40,6 +46,7 @@ public class Announcement { this.startDate = startDate; this.endDate = endDate; this.teacherId = teacherId; + this.idString = idString; } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java index e14c966a..1b297070 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/announcements/AnnouncementDao.java @@ -23,6 +23,9 @@ public abstract class AnnouncementDao { @Insert(onConflict = OnConflictStrategy.REPLACE) public abstract void addAll(List announcementList); + @Insert(onConflict = OnConflictStrategy.IGNORE) + public abstract void addAllIgnore(List announcementList); + @Query("DELETE FROM announcements WHERE profileId = :profileId") public abstract void clear(int profileId); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsFragment.java index d37aff4d..0834d453 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/announcements/AnnouncementsFragment.java @@ -17,9 +17,16 @@ import androidx.recyclerview.widget.RecyclerView; import com.afollestad.materialdialogs.MaterialDialog; import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.greenrobot.eventbus.ThreadMode; + import pl.szczodrzynski.edziennik.App; import pl.szczodrzynski.edziennik.MainActivity; import pl.szczodrzynski.edziennik.R; +import pl.szczodrzynski.edziennik.data.api.events.AnnouncementGetEvent; +import pl.szczodrzynski.edziennik.data.api.task.EdziennikTask; +import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull; import pl.szczodrzynski.edziennik.databinding.DialogAnnouncementBinding; import pl.szczodrzynski.edziennik.databinding.FragmentAnnouncementsBinding; import pl.szczodrzynski.edziennik.utils.SimpleDividerItemDecoration; @@ -33,6 +40,7 @@ public class AnnouncementsFragment extends Fragment { private App app = null; private MainActivity activity = null; private FragmentAnnouncementsBinding b = null; + private boolean dialogOpened = false; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -97,20 +105,13 @@ public class AnnouncementsFragment extends Fragment { return; }*/ AnnouncementsAdapter announcementsAdapter = new AnnouncementsAdapter(activity, announcements, (v, announcement) -> { - MaterialDialog dialog = new MaterialDialog.Builder(activity) - .title(announcement.subject) - .customView(R.layout.dialog_announcement, true) - .positiveText(R.string.ok) - .show(); - DialogAnnouncementBinding b = DialogAnnouncementBinding.bind(dialog.getCustomView()); - b.text.setText(announcement.teacherFullName+"\n\n"+ (announcement.startDate != null ? announcement.startDate.getFormattedString() : "-")+" do "+ (announcement.endDate != null ? announcement.endDate.getFormattedString() : "-")+"\n\n" +announcement.text); - if (!announcement.seen) { - announcement.seen = true; - AsyncTask.execute(() -> { - app.db.metadataDao().setSeen(App.profileId, announcement, true); - }); - if (recyclerView.getAdapter() != null) - recyclerView.getAdapter().notifyDataSetChanged(); + if (!dialogOpened) { + dialogOpened = true; + if (announcement.text == null) { + EdziennikTask.Companion.announcementGet(App.profileId, announcement).enqueue(requireContext()); + } else { + showAnnouncementDetailsDialog(announcement); + } } }); @@ -128,4 +129,43 @@ public class AnnouncementsFragment extends Fragment { } }); } + + @Override + public void onStart() { + EventBus.getDefault().register(this); + super.onStart(); + } + + @Override + public void onStop() { + EventBus.getDefault().unregister(this); + super.onStop(); + } + + @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) + public void onAnnouncementGetEvent(AnnouncementGetEvent event) { + EventBus.getDefault().removeStickyEvent(event); + showAnnouncementDetailsDialog(event.getAnnouncement()); + } + + private void showAnnouncementDetailsDialog(AnnouncementFull announcement) { + if (!dialogOpened) { + MaterialDialog dialog = new MaterialDialog.Builder(activity) + .title(announcement.subject) + .customView(R.layout.dialog_announcement, true) + .positiveText(R.string.ok) + .dismissListener(v -> dialogOpened = false) + .show(); + DialogAnnouncementBinding b = DialogAnnouncementBinding.bind(dialog.getCustomView()); + b.text.setText(announcement.teacherFullName+"\n\n"+ (announcement.startDate != null ? announcement.startDate.getFormattedString() : "-")+" do "+ (announcement.endDate != null ? announcement.endDate.getFormattedString() : "-")+"\n\n" +announcement.text); + if (!announcement.seen) { + announcement.seen = true; + AsyncTask.execute(() -> { + app.db.metadataDao().setSeen(App.profileId, announcement, true); + }); + if (recyclerView.getAdapter() != null) + recyclerView.getAdapter().notifyDataSetChanged(); + } + } + } }