Make dialog translatable and replace handlers with coroutines

This commit is contained in:
PabloG02 2023-01-26 14:35:07 +01:00
parent c623278d09
commit 1cc0b42720
2 changed files with 23 additions and 18 deletions

View File

@ -11,8 +11,6 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.text.Html
import android.widget.ImageView
import android.widget.Toast
@ -26,6 +24,7 @@ import com.google.android.material.badge.BadgeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONArray
import org.json.JSONObject
import org.json.JSONTokener
@ -77,41 +76,43 @@ class AppUpdater : BroadcastReceiver() {
private const val baseUrl = "https://skyline-builds.alula.gay"
private const val branch = "ftx1"
fun checkForUpdates(applicationContext : Context) {
val myHandler = Handler(Looper.getMainLooper())
val builder = AlertDialog.Builder(applicationContext)
CoroutineScope(Dispatchers.IO).launch {
val newestBuild = checkRemoteForUpdates()
if (newestBuild != null) {
val remoteBuildGitHash = newestBuild.getJSONObject("commit").getString("id")
val commit = newestBuild.getJSONObject("commit")
val remoteBuildGitHash = commit.getString("id")
if (BuildConfig.GIT_HASH != remoteBuildGitHash) {
val id = newestBuild.get("id")
val apkName = newestBuild.get("apkName")
val uri = Uri.parse("$baseUrl/cache/${id}/${apkName}")
var changelog = "<b>Changelog</b><p>${newestBuild.getJSONObject("commit").getString("message").substringBefore("\n")}</p>"
if (newestBuild.getJSONObject("commit").getString("message").contains("\n"))
changelog += "<p>${newestBuild.getJSONObject("commit").getString("message").substringAfter("\n")}</p>"
val message = commit.getString("message")
var changelog = "<b>${applicationContext.getString(R.string.changelog)}</b><p>${message.substringBefore("\n")}</p>"
if (message.contains("\n"))
changelog += "<p>${message.substringAfter("\n")}</p>"
myHandler.post {
builder.setTitle("New version ${newestBuild.get("runNumber")}")
withContext(Dispatchers.Main) {
builder.setTitle("${applicationContext.getString(R.string.new_version)} ${newestBuild.get("runNumber")}")
.setMessage(Html.fromHtml(changelog, 0))
.setCancelable(true)
.setPositiveButton("Update") { dialogInterface, _ ->
.setPositiveButton(applicationContext.getString(R.string.update)) { dialogInterface, _ ->
val receiver = AppUpdater()
applicationContext.registerReceiver(receiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
receiver.downloadApk(applicationContext, uri)
dialogInterface.dismiss()
}.setNegativeButton(applicationContext.getString(R.string.cancel)){ dialogInterface, _ ->
dialogInterface.cancel()
}.show()
}
} else {
myHandler.post {
builder.setTitle("No updates available")
.setMessage(Html.fromHtml("<b>Changelog</b><p>${newestBuild.getJSONObject("commit").getString("message")}</p>", 0))
withContext(Dispatchers.Main) {
builder.setTitle(applicationContext.getString(R.string.no_updates_available))
.setMessage(Html.fromHtml("<b>${applicationContext.getString(R.string.changelog)}</b><p>${commit.getString("message")}</p>", 0))
.setCancelable(true)
.setPositiveButton("Close") { dialogInterface, _ ->
.setPositiveButton(applicationContext.getString(R.string.close)) { dialogInterface, _ ->
dialogInterface.dismiss()
}.show()
}
@ -120,7 +121,6 @@ class AppUpdater : BroadcastReceiver() {
}
}
@com.google.android.material.badge.ExperimentalBadgeUtils
fun notifyUpdateBadge(context : Context, icon : ImageView) {
CoroutineScope(Dispatchers.IO).launch {
@ -139,8 +139,7 @@ class AppUpdater : BroadcastReceiver() {
}
}
fun checkRemoteForUpdates() : JSONObject? {
private fun checkRemoteForUpdates() : JSONObject? {
val url = URL("$baseUrl/builds")
try {
val response = url.readText()

View File

@ -12,6 +12,12 @@
<!-- Toolbar - Share Logs -->
<string name="log_share_prompt">Share log file</string>
<string name="logs_not_found">No logs were created during the last run</string>
<!-- Updater -->
<string name="new_version">New version</string>
<string name="changelog">Changelog</string>
<string name="update">Update</string>
<string name="no_updates_available">No updates available</string>
<string name="close">Close</string>
<!-- Main -->
<string name="all">All</string>
<string name="metadata_missing">Metadata Missing</string>