forked from Schanbro/WeekFocus
refactor: move update code to folder update
This commit is contained in:
1 parent
8d38c319a2
commit
0fcc9ba514
4 files changed
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package de.schanbro.weekfocus.update
|
||||
|
||||
import android.content.Context
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.json.JSONObject
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
|
||||
data class ReleaseInfo(
|
||||
val version: String,
|
||||
val notes: String,
|
||||
val apkUrl: String
|
||||
)
|
||||
|
||||
object UpdateChecker {
|
||||
|
||||
private const val LATEST_RELEASE_URL =
|
||||
"https://git.schamagusa.de/api/v1/repos/robin/WeekFocus/releases/latest"
|
||||
|
||||
/** Liefert das neueste stabile Release oder null (bei Fehler / ohne APK). */
|
||||
suspend fun fetchLatestRelease(): ReleaseInfo? = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val conn = URL(LATEST_RELEASE_URL).openConnection() as HttpURLConnection
|
||||
conn.connectTimeout = 10_000
|
||||
conn.readTimeout = 10_000
|
||||
conn.setRequestProperty("Accept", "application/json")
|
||||
try {
|
||||
if (conn.responseCode != HttpURLConnection.HTTP_OK) return@withContext null
|
||||
|
||||
val json = JSONObject(conn.inputStream.bufferedReader().use { it.readText() })
|
||||
val assets = json.getJSONArray("assets")
|
||||
val apk = (0 until assets.length())
|
||||
.map { assets.getJSONObject(it) }
|
||||
.firstOrNull { it.getString("name").endsWith(".apk", ignoreCase = true) }
|
||||
?: return@withContext null
|
||||
|
||||
ReleaseInfo(
|
||||
version = json.getString("tag_name"),
|
||||
notes = json.optString("body", ""),
|
||||
apkUrl = apk.getString("browser_download_url")
|
||||
)
|
||||
} finally {
|
||||
conn.disconnect()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null // Netzwerk- oder Parse-Fehler: still ignorieren
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
fun currentVersion(context: Context): String =
|
||||
context.packageManager.getPackageInfo(context.packageName, 0).versionName ?: "0"
|
||||
|
||||
/** Numerischer Vergleich pro Segment, damit 1.2.11 > 1.2.9 gilt. */
|
||||
fun isNewer(remote: String, local: String): Boolean {
|
||||
val r = parse(remote)
|
||||
val l = parse(local)
|
||||
for (i in 0 until maxOf(r.size, l.size)) {
|
||||
val a = r.getOrElse(i) { 0 }
|
||||
val b = l.getOrElse(i) { 0 }
|
||||
if (a != b) return a > b
|
||||
}
|
||||
// Gleiche Nummer: stabiles Release ist neuer als eine installierte Beta
|
||||
return local.contains("-") && !remote.contains("-")
|
||||
}
|
||||
|
||||
private fun parse(version: String): List<Int> =
|
||||
version.trim().removePrefix("v").substringBefore("-")
|
||||
.split(".").map { it.toIntOrNull() ?: 0 }
|
||||
}
|
||||
Reference in new issue
Block a user