improve update checker

This commit is contained in:
Bnyro 2022-07-18 14:45:32 +02:00
parent c56a6d872a
commit 599eafddf2
6 changed files with 106 additions and 24 deletions

View File

@ -0,0 +1,17 @@
package com.github.libretube.update
data class Asset(
val browser_download_url: String,
val content_type: String,
val created_at: String,
val download_count: Int,
val id: Int,
val label: Any,
val name: String,
val node_id: String,
val size: Int,
val state: String,
val updated_at: String,
val uploader: Uploader,
val url: String
)

View File

@ -0,0 +1,22 @@
package com.github.libretube.update
data class Author(
val avatar_url: String,
val events_url: String,
val followers_url: String,
val following_url: String,
val gists_url: String,
val gravatar_id: String,
val html_url: String,
val id: Int,
val login: String,
val node_id: String,
val organizations_url: String,
val received_events_url: String,
val repos_url: String,
val site_admin: Boolean,
val starred_url: String,
val subscriptions_url: String,
val type: String,
val url: String
)

View File

@ -0,0 +1,12 @@
package com.github.libretube.update
data class Reactions(
val confused: Int,
val eyes: Int,
val heart: Int,
val hooray: Int,
val laugh: Int,
val rocket: Int,
val total_count: Int,
val url: String
)

View File

@ -0,0 +1,24 @@
package com.github.libretube.update
data class UpdateInfo(
val assets: List<Asset>,
val assets_url: String,
val author: Author,
val body: String,
val created_at: String,
val draft: Boolean,
val html_url: String,
val id: Int,
val mentions_count: Int,
val name: String,
val node_id: String,
val prerelease: Boolean,
val published_at: String,
val reactions: Reactions,
val tag_name: String,
val tarball_url: String,
val target_commitish: String,
val upload_url: String,
val url: String,
val zipball_url: String
)

View File

@ -0,0 +1,22 @@
package com.github.libretube.update
data class Uploader(
val avatar_url: String,
val events_url: String,
val followers_url: String,
val following_url: String,
val gists_url: String,
val gravatar_id: String,
val html_url: String,
val id: Int,
val login: String,
val node_id: String,
val organizations_url: String,
val received_events_url: String,
val repos_url: String,
val site_admin: Boolean,
val starred_url: String,
val subscriptions_url: String,
val type: String,
val url: String
)

View File

@ -1,17 +1,16 @@
package com.github.libretube.util package com.github.libretube.util
import android.util.Log
import com.github.libretube.GITHUB_API_URL import com.github.libretube.GITHUB_API_URL
import com.github.libretube.obj.VersionInfo import com.github.libretube.obj.VersionInfo
import org.json.JSONArray import com.github.libretube.update.UpdateInfo
import org.json.JSONObject import com.google.gson.Gson
import java.io.BufferedReader import java.io.BufferedReader
import java.io.InputStreamReader import java.io.InputStreamReader
import java.net.URL import java.net.URL
import javax.net.ssl.HttpsURLConnection import javax.net.ssl.HttpsURLConnection
fun checkUpdate(): VersionInfo? { fun checkUpdate(): VersionInfo? {
var versionInfo: VersionInfo? = VersionInfo("", "") var versionInfo: VersionInfo? = null
// run http request as thread to make it async // run http request as thread to make it async
val thread = Thread { val thread = Thread {
// otherwise crashes without internet // otherwise crashes without internet
@ -39,25 +38,11 @@ fun getUpdateInfo(): VersionInfo? {
while (br.readLine().also { line = it } != null) json.append(line) while (br.readLine().also { line = it } != null) json.append(line)
// Parse and return the json data // Parse and return the json data
val jsonRoot = JSONObject(json.toString()) val gson = Gson()
if (jsonRoot.has("tag_name") && val updateInfo = gson.fromJson(json.toString(), UpdateInfo::class.java)
jsonRoot.has("html_url") &&
jsonRoot.has("assets")
) {
val updateUrl = jsonRoot.getString("html_url")
val jsonAssets: JSONArray = jsonRoot.getJSONArray("assets")
for (i in 0 until jsonAssets.length()) { return VersionInfo(
val jsonAsset = jsonAssets.getJSONObject(i) updateInfo.html_url,
if (jsonAsset.has("name")) { updateInfo.name
val name = jsonAsset.getString("name") )
if (name.endsWith(".apk")) {
val tagName = jsonRoot.getString("name")
Log.i("VersionInfo", "Latest version: $tagName")
return VersionInfo(updateUrl, tagName)
}
}
}
}
return null
} }