From 2808c66840b7d27df6e041bf2d142698e4fa7fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 29 Mar 2021 09:41:50 +0200 Subject: [PATCH] [UI/Login] Fix ignoring API signature errors in login chooser. --- .../szczodrzynski/edziennik/MainActivity.kt | 2 +- .../ui/modules/login/LoginChooserFragment.kt | 34 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 55beea7d..b7b2d6f2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -650,7 +650,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope { return@let } is Throwable -> { - errorSnackbar.addError(result.toApiError(SzkolnyApi.TAG)).show() + errorSnackbar.addError(result.toApiError(TAG)).show() return } is RegisterAvailabilityStatus -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginChooserFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginChooserFragment.kt index 8dee827e..8d1a2a19 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginChooserFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginChooserFragment.kt @@ -16,15 +16,16 @@ import android.view.ViewGroup import android.view.animation.AccelerateDecelerateInterpolator import android.view.animation.Animation import android.view.animation.RotateAnimation +import android.widget.Toast import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.google.firebase.analytics.FirebaseAnalytics import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi +import pl.szczodrzynski.edziennik.data.api.szkolny.response.RegisterAvailabilityStatus import pl.szczodrzynski.edziennik.databinding.LoginChooserFragmentBinding import pl.szczodrzynski.edziennik.ui.dialogs.RegisterUnavailableDialog import pl.szczodrzynski.edziennik.ui.modules.feedback.FeedbackActivity @@ -249,19 +250,38 @@ class LoginChooserFragment : Fragment(), CoroutineScope { }?.let { registerName -> var status = app.config.sync.registerAvailability[registerName] if (status == null || status.nextCheckAt < currentTimeUnix()) { - withContext(Dispatchers.IO) { - val api = SzkolnyApi(app) - api.runCatching(activity) { + val api = SzkolnyApi(app) + val result = withContext(Dispatchers.IO) { + return@withContext api.runCatching({ val availability = getRegisterAvailability() app.config.sync.registerAvailability = availability - status = availability[registerName] + availability[registerName] + }, onError = { + if (it.toErrorCode() == ERROR_API_INVALID_SIGNATURE) { + return@withContext false + } + return@withContext it + }) + } + + when (result) { + false -> { + Toast.makeText(activity, R.string.error_no_api_access, Toast.LENGTH_SHORT).show() + return@let + } + is Throwable -> { + activity.errorSnackbar.addError(result.toApiError(TAG)).show() + return false + } + is RegisterAvailabilityStatus -> { + status = result } } } - if (status?.available != true) { + if (status?.available != true || status.minVersionCode > BuildConfig.VERSION_CODE) { if (status != null) - RegisterUnavailableDialog(activity, status!!) + RegisterUnavailableDialog(activity, status) return false } }