Skip to content

Commit

Permalink
Latest version checking.
Browse files Browse the repository at this point in the history
* Handle exceptions by returning no version.
  • Loading branch information
zegkljan committed Oct 17, 2021
1 parent d307af6 commit e19bd0c
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -107,19 +111,24 @@ class VersionCheckFragment : Fragment() {
Navigation.findNavController(requireActivity(), R.id.fragment_container).navigate(VersionCheckFragmentDirections.actionVersionCheckToPermissions())
}

private suspend fun getReleases(): List<Release> {
private suspend fun getLatestRelease(): Release? {
val client = HttpClient(Android) {
engine {
connectTimeout = 500
socketTimeout = 500
}
}

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<Release>()
for (i in 0 until jsonReleases.length()) {
Expand All @@ -129,7 +138,7 @@ class VersionCheckFragment : Fragment() {
releases.add(Release(ver, url))
}
releases.sort()
return releases
return releases.last()
}

companion object {
Expand Down

0 comments on commit e19bd0c

Please sign in to comment.