From e19bd0cda99d3a5bcb3e13d534498c67dddfe0bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=BDegklitz?= Date: Mon, 18 Oct 2021 01:40:56 +0200 Subject: [PATCH] Latest version checking. * Handle exceptions by returning no version. --- .../fragments/VersionCheckFragment.kt | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/cz/zegkljan/videoreferee/fragments/VersionCheckFragment.kt b/app/src/main/java/cz/zegkljan/videoreferee/fragments/VersionCheckFragment.kt index 61ebf77..824f713 100644 --- a/app/src/main/java/cz/zegkljan/videoreferee/fragments/VersionCheckFragment.kt +++ b/app/src/main/java/cz/zegkljan/videoreferee/fragments/VersionCheckFragment.kt @@ -74,7 +74,11 @@ class VersionCheckFragment : Fragment() { val version = Version.fromVersionString(info.versionName) lifecycleScope.launch { - val latest = getReleases().last() + val latest = getLatestRelease() + if (latest == null) { + leave() + return@launch + } if (version < latest.version) { AlertDialog.Builder(requireActivity()).apply { @@ -107,7 +111,7 @@ class VersionCheckFragment : Fragment() { Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(VersionCheckFragmentDirections.actionVersionCheckToPermissions()) } - private suspend fun getReleases(): List { + private suspend fun getLatestRelease(): Release? { val client = HttpClient(Android) { engine { connectTimeout = 500 @@ -115,11 +119,16 @@ class VersionCheckFragment : Fragment() { } } - val response: HttpResponse = client.request(GIT_RELEASES) { - method = HttpMethod.Get - header("Accept", "application/vnd.github.v3+json") + val res: String + try { + val response: HttpResponse = client.request(GIT_RELEASES) { + method = HttpMethod.Get + header("Accept", "application/vnd.github.v3+json") + } + res = response.receive() + } catch (e: Exception) { + return null } - val res: String = response.receive() val jsonReleases = JSONTokener(res).nextValue() as JSONArray val releases = mutableListOf() for (i in 0 until jsonReleases.length()) { @@ -129,7 +138,7 @@ class VersionCheckFragment : Fragment() { releases.add(Release(ver, url)) } releases.sort() - return releases + return releases.last() } companion object {