diff --git a/app/src/main/java/com/github/libretube/update/Asset.kt b/app/src/main/java/com/github/libretube/update/Asset.kt new file mode 100644 index 000000000..84525a625 --- /dev/null +++ b/app/src/main/java/com/github/libretube/update/Asset.kt @@ -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 +) diff --git a/app/src/main/java/com/github/libretube/update/Author.kt b/app/src/main/java/com/github/libretube/update/Author.kt new file mode 100644 index 000000000..1ca9b5d23 --- /dev/null +++ b/app/src/main/java/com/github/libretube/update/Author.kt @@ -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 +) diff --git a/app/src/main/java/com/github/libretube/update/Reactions.kt b/app/src/main/java/com/github/libretube/update/Reactions.kt new file mode 100644 index 000000000..5f4b2daef --- /dev/null +++ b/app/src/main/java/com/github/libretube/update/Reactions.kt @@ -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 +) diff --git a/app/src/main/java/com/github/libretube/update/UpdateInfo.kt b/app/src/main/java/com/github/libretube/update/UpdateInfo.kt new file mode 100644 index 000000000..a3e239ea4 --- /dev/null +++ b/app/src/main/java/com/github/libretube/update/UpdateInfo.kt @@ -0,0 +1,24 @@ +package com.github.libretube.update + +data class UpdateInfo( + val assets: List, + 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 +) \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/update/Uploader.kt b/app/src/main/java/com/github/libretube/update/Uploader.kt new file mode 100644 index 000000000..a9c081a10 --- /dev/null +++ b/app/src/main/java/com/github/libretube/update/Uploader.kt @@ -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 +) diff --git a/app/src/main/java/com/github/libretube/util/UpdateChecker.kt b/app/src/main/java/com/github/libretube/util/UpdateChecker.kt index d42880d27..1620f0a6b 100644 --- a/app/src/main/java/com/github/libretube/util/UpdateChecker.kt +++ b/app/src/main/java/com/github/libretube/util/UpdateChecker.kt @@ -1,17 +1,16 @@ package com.github.libretube.util -import android.util.Log import com.github.libretube.GITHUB_API_URL import com.github.libretube.obj.VersionInfo -import org.json.JSONArray -import org.json.JSONObject +import com.github.libretube.update.UpdateInfo +import com.google.gson.Gson import java.io.BufferedReader import java.io.InputStreamReader import java.net.URL import javax.net.ssl.HttpsURLConnection fun checkUpdate(): VersionInfo? { - var versionInfo: VersionInfo? = VersionInfo("", "") + var versionInfo: VersionInfo? = null // run http request as thread to make it async val thread = Thread { // otherwise crashes without internet @@ -39,25 +38,11 @@ fun getUpdateInfo(): VersionInfo? { while (br.readLine().also { line = it } != null) json.append(line) // Parse and return the json data - val jsonRoot = JSONObject(json.toString()) - if (jsonRoot.has("tag_name") && - jsonRoot.has("html_url") && - jsonRoot.has("assets") - ) { - val updateUrl = jsonRoot.getString("html_url") - val jsonAssets: JSONArray = jsonRoot.getJSONArray("assets") + val gson = Gson() + val updateInfo = gson.fromJson(json.toString(), UpdateInfo::class.java) - for (i in 0 until jsonAssets.length()) { - val jsonAsset = jsonAssets.getJSONObject(i) - if (jsonAsset.has("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 + return VersionInfo( + updateInfo.html_url, + updateInfo.name + ) }