[Announcements/Liburs] Fix error on marking as read and make announcement show even when there's no Internet connection.

This commit is contained in:
Kacper Ziubryniewicz 2020-01-06 18:24:43 +01:00
parent 4b08ea7a89
commit 26eb2e4381
4 changed files with 13 additions and 9 deletions

View File

@ -26,7 +26,7 @@ open class LibrusApi(open val data: DataLibrus) {
val profile val profile
get() = data.profile get() = data.profile
fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, onSuccess: (json: JsonObject) -> Unit) { fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, ignoreErrors: List<Int> = emptyList(), onSuccess: (json: JsonObject) -> Unit) {
d(tag, "Request: Librus/Api - ${if (data.fakeLogin) FAKE_LIBRUS_API else LIBRUS_API_URL}/$endpoint") d(tag, "Request: Librus/Api - ${if (data.fakeLogin) FAKE_LIBRUS_API else LIBRUS_API_URL}/$endpoint")
@ -65,12 +65,14 @@ open class LibrusApi(open val data: DataLibrus) {
"Nieprawidłowy węzeł." -> ERROR_LIBRUS_API_INCORRECT_ENDPOINT "Nieprawidłowy węzeł." -> ERROR_LIBRUS_API_INCORRECT_ENDPOINT
else -> ERROR_LIBRUS_API_OTHER else -> ERROR_LIBRUS_API_OTHER
}.let { errorCode -> }.let { errorCode ->
if (errorCode !in ignoreErrors) {
data.error(ApiError(tag, errorCode) data.error(ApiError(tag, errorCode)
.withApiResponse(json) .withApiResponse(json)
.withResponse(response)) .withResponse(response))
return return
} }
} }
}
if (json == null) { if (json == null) {
data.error(ApiError(tag, ERROR_RESPONSE_EMPTY) data.error(ApiError(tag, ERROR_RESPONSE_EMPTY)

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.data.api.ERROR_LIBRUS_API_INVALID_REQUEST_PARAMS
import pl.szczodrzynski.edziennik.data.api.POST import pl.szczodrzynski.edziennik.data.api.POST
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
@ -22,7 +23,8 @@ class LibrusApiAnnouncementMarkAsRead(
} }
init { init {
apiGet(TAG, "SchoolNotices/MarkAsRead/${announcement.idString}", method = POST) { apiGet(TAG, "SchoolNotices/MarkAsRead/${announcement.idString}", method = POST,
ignoreErrors = listOf(ERROR_LIBRUS_API_INVALID_REQUEST_PARAMS)) {
announcement.seen = true announcement.seen = true
EventBus.getDefault().postSticky(AnnouncementGetEvent(announcement)) EventBus.getDefault().postSticky(AnnouncementGetEvent(announcement))

View File

@ -70,7 +70,7 @@ class LibrusLoginSynergia(override val data: DataLibrus, val onSuccess: () -> Un
loginWithToken(json.getString("Token")) loginWithToken(json.getString("Token"))
} }
apiGet(TAG, "AutoLoginToken", POST, null, onSuccess) apiGet(TAG, "AutoLoginToken", POST, onSuccess = onSuccess)
} }
private fun loginWithToken(token: String?) { private fun loginWithToken(token: String?) {

View File

@ -109,7 +109,7 @@ public class AnnouncementsFragment extends Fragment {
return; return;
}*/ }*/
AnnouncementsAdapter announcementsAdapter = new AnnouncementsAdapter(activity, announcements, (v, announcement) -> { AnnouncementsAdapter announcementsAdapter = new AnnouncementsAdapter(activity, announcements, (v, announcement) -> {
if (announcement.text == null || (app.profile.getLoginStoreType() == LOGIN_TYPE_LIBRUS && !announcement.seen)) { if (announcement.text == null || (app.profile.getLoginStoreType() == LOGIN_TYPE_LIBRUS && !announcement.seen && app.networkUtils.isOnline())) {
EdziennikTask.Companion.announcementGet(App.profileId, announcement).enqueue(requireContext()); EdziennikTask.Companion.announcementGet(App.profileId, announcement).enqueue(requireContext());
} else { } else {
showAnnouncementDetailsDialog(announcement); showAnnouncementDetailsDialog(announcement);
@ -157,7 +157,7 @@ public class AnnouncementsFragment extends Fragment {
.show(); .show();
DialogAnnouncementBinding b = DialogAnnouncementBinding.bind(dialog.getCustomView()); DialogAnnouncementBinding b = DialogAnnouncementBinding.bind(dialog.getCustomView());
b.text.setText(announcement.teacherFullName+"\n\n"+ (announcement.startDate != null ? announcement.startDate.getFormattedString() : "-") + (announcement.endDate != null ? " do " + announcement.endDate.getFormattedString() : "")+"\n\n" +announcement.text); b.text.setText(announcement.teacherFullName+"\n\n"+ (announcement.startDate != null ? announcement.startDate.getFormattedString() : "-") + (announcement.endDate != null ? " do " + announcement.endDate.getFormattedString() : "")+"\n\n" +announcement.text);
if (!announcement.seen) { if (!announcement.seen && app.profile.getLoginStoreType() != LOGIN_TYPE_LIBRUS) {
announcement.seen = true; announcement.seen = true;
AsyncTask.execute(() -> app.db.metadataDao().setSeen(App.profileId, announcement, true)); AsyncTask.execute(() -> app.db.metadataDao().setSeen(App.profileId, announcement, true));
if (recyclerView.getAdapter() != null) if (recyclerView.getAdapter() != null)