forked from Schanbro/WeekFocus
feat: settings
This commit is contained in:
1 parent
6be81093e0
commit
8f65bb05b7
7 files changed
+481
-105
No files matched your search
@@ -6,6 +6,7 @@ import kotlinx.coroutines.withContext
|
||||
import org.json.JSONObject
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import org.json.JSONArray
|
||||
|
||||
data class ReleaseInfo(
|
||||
val version: String,
|
||||
@@ -15,37 +16,53 @@ data class ReleaseInfo(
|
||||
|
||||
object UpdateChecker {
|
||||
|
||||
private const val LATEST_RELEASE_URL =
|
||||
"https://git.schamagusa.de/api/v1/repos/robin/WeekFocus/releases/latest"
|
||||
private const val RELEASES_URL =
|
||||
"https://git.schamagusa.de/api/v1/repos/robin/WeekFocus/releases"
|
||||
|
||||
/** 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")
|
||||
suspend fun fetchLatestRelease(includePrereleases: Boolean = false): ReleaseInfo? =
|
||||
withContext(Dispatchers.IO) {
|
||||
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()
|
||||
if (includePrereleases) {
|
||||
val body = httpGet("$RELEASES_URL?limit=10") ?: return@withContext null
|
||||
val array = JSONArray(body)
|
||||
(0 until array.length())
|
||||
.map { array.getJSONObject(it) }
|
||||
.firstNotNullOfOrNull { toRelease(it) }
|
||||
} else {
|
||||
val body = httpGet("$RELEASES_URL/latest") ?: return@withContext null
|
||||
toRelease(JSONObject(body))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null // Netzwerk- oder Parse-Fehler: still ignorieren
|
||||
}
|
||||
|
||||
private fun httpGet(url: String): String? {
|
||||
val conn = URL(url).openConnection() as HttpURLConnection
|
||||
conn.connectTimeout = 10_000
|
||||
conn.readTimeout = 10_000
|
||||
conn.setRequestProperty("Accept", "application/json")
|
||||
return try {
|
||||
if (conn.responseCode == HttpURLConnection.HTTP_OK) {
|
||||
conn.inputStream.bufferedReader().use { it.readText() }
|
||||
} else null
|
||||
} finally {
|
||||
conn.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
private fun toRelease(json: JSONObject): ReleaseInfo? {
|
||||
if (json.optBoolean("draft", false)) return null
|
||||
val assets = json.getJSONArray("assets")
|
||||
val apk = (0 until assets.length())
|
||||
.map { assets.getJSONObject(it) }
|
||||
.firstOrNull { it.getString("name").endsWith(".apk", ignoreCase = true) }
|
||||
?: return null
|
||||
return ReleaseInfo(
|
||||
version = json.getString("tag_name"),
|
||||
notes = json.optString("body", ""),
|
||||
apkUrl = apk.getString("browser_download_url")
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
|
||||
Reference in new issue
Block a user