Settings screen, display fixes

This commit is contained in:
Neil Alexander 2021-06-24 18:12:16 +01:00
parent 8f3ee30e74
commit fa9532fb97
7 changed files with 354 additions and 7 deletions

View file

@ -1,11 +1,71 @@
package eu.neilalexander.yggdrasil
import android.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.widget.*
import androidx.core.view.setPadding
import androidx.core.widget.doOnTextChanged
import org.json.JSONObject
class SettingsActivity : AppCompatActivity() {
private lateinit var config: ConfigurationProxy
private lateinit var inflater: LayoutInflater
private lateinit var deviceNameEntry: EditText
private lateinit var publicKeyLabel: TextView
private lateinit var resetConfigurationRow: TableRow
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
config = ConfigurationProxy(applicationContext)
inflater = LayoutInflater.from(this)
deviceNameEntry = findViewById(R.id.deviceNameEntry)
publicKeyLabel = findViewById(R.id.publicKeyLabel)
resetConfigurationRow = findViewById(R.id.resetConfigurationRow)
deviceNameEntry.doOnTextChanged { text, _, _, _ ->
config.updateJSON { cfg ->
var nodeinfo = cfg.optJSONObject("NodeInfo")
if (nodeinfo == null) {
cfg.put("NodeInfo", JSONObject("{}"))
}
cfg.getJSONObject("NodeInfo").put("name", text)
}
}
resetConfigurationRow.setOnClickListener {
var view = inflater.inflate(R.layout.dialog_resetconfig, null)
val builder: AlertDialog.Builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.Theme_MaterialComponents_DayNight_Dialog))
builder.setTitle("Warning")
builder.setView(view)
builder.setPositiveButton("Reset") { dialog, _ ->
config.resetJSON()
updateView()
dialog.dismiss()
}
builder.setNegativeButton("Cancel") { dialog, _ ->
dialog.cancel()
}
builder.show()
}
updateView()
}
fun updateView() {
val nodeinfo = config.getJSON().optJSONObject("NodeInfo")
if (nodeinfo != null) {
deviceNameEntry.setText(nodeinfo.getString("name"), TextView.BufferType.EDITABLE)
} else {
deviceNameEntry.setText("", TextView.BufferType.EDITABLE)
}
publicKeyLabel.text = config.getJSON().getString("PublicKey")
}
}