Refine NNNet UI and rename Android package
Some checks failed
Android CI / build (push) Has been cancelled
Some checks failed
Android CI / build (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package pro.nnnteam.nnnet
|
||||
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.widget.ImageButton
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.google.android.material.switchmaterial.SwitchMaterial
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import pro.nnnteam.nnnet.update.UpdateInfo
|
||||
import pro.nnnteam.nnnet.update.UpdateManager
|
||||
|
||||
class SettingsActivity : AppCompatActivity() {
|
||||
private val prefs by lazy {
|
||||
getSharedPreferences(UpdateManager.PREFS_NAME, MODE_PRIVATE)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_settings)
|
||||
|
||||
findViewById<ImageButton>(R.id.backButton).setOnClickListener { finish() }
|
||||
|
||||
val autoUpdateSwitch = findViewById<SwitchMaterial>(R.id.autoUpdateSwitch)
|
||||
val versionText = findViewById<TextView>(R.id.versionText)
|
||||
autoUpdateSwitch.isChecked = prefs.getBoolean(UpdateManager.KEY_AUTO_UPDATE, false)
|
||||
autoUpdateSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
prefs.edit().putBoolean(UpdateManager.KEY_AUTO_UPDATE, isChecked).apply()
|
||||
}
|
||||
|
||||
versionText.text = getString(
|
||||
R.string.current_version,
|
||||
packageManager.getPackageInfo(packageName, 0).versionName,
|
||||
currentVersionCode()
|
||||
)
|
||||
|
||||
findViewById<android.view.View>(R.id.checkUpdatesButton).setOnClickListener {
|
||||
checkForUpdates()
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForUpdates() {
|
||||
lifecycleScope.launch {
|
||||
val updateInfo = withContext(Dispatchers.IO) { UpdateManager.fetchUpdateInfo() }
|
||||
if (updateInfo == null) {
|
||||
Toast.makeText(this@SettingsActivity, R.string.update_check_failed, Toast.LENGTH_SHORT).show()
|
||||
return@launch
|
||||
}
|
||||
if (updateInfo.versionCode > currentVersionCode()) {
|
||||
showUpdateDialog(updateInfo)
|
||||
} else {
|
||||
Toast.makeText(this@SettingsActivity, R.string.latest_version_installed, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showUpdateDialog(updateInfo: UpdateInfo) {
|
||||
lifecycleScope.launch {
|
||||
val releaseNotes = withContext(Dispatchers.IO) {
|
||||
UpdateManager.fetchReleaseNotes(updateInfo.releaseNotesPath)
|
||||
}
|
||||
AlertDialog.Builder(this@SettingsActivity)
|
||||
.setTitle(updateInfo.releaseNotesTitle)
|
||||
.setMessage(
|
||||
buildString {
|
||||
append(getString(R.string.update_available_message, updateInfo.versionName))
|
||||
if (!releaseNotes.isNullOrBlank()) {
|
||||
append("\n\n")
|
||||
append(releaseNotes.trim())
|
||||
}
|
||||
}
|
||||
)
|
||||
.setPositiveButton(R.string.download_update) { _, _ ->
|
||||
val url = UpdateManager.buildDownloadUrl(updateInfo.apkPath)
|
||||
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
|
||||
}
|
||||
.setNegativeButton(R.string.later, null)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
private fun currentVersionCode(): Int {
|
||||
val packageInfo = packageManager.getPackageInfo(packageName, 0)
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
packageInfo.longVersionCode.toInt()
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
packageInfo.versionCode
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user