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,6 +1,7 @@
package eu.neilalexander.yggdrasil
import android.content.Context
import android.provider.Settings
import mobile.Mobile
import org.json.JSONObject
import java.io.File
@ -21,6 +22,12 @@ object ConfigurationProxy {
return this
}
fun resetJSON() {
val conf = Mobile.generateConfigJSON()
file.writeBytes(conf)
fix()
}
fun updateJSON(fn: (JSONObject) -> Unit) {
json = JSONObject(file.readText(Charsets.UTF_8))
fn(json)

View file

@ -8,6 +8,7 @@ import android.os.Bundle
import android.text.Layout
import android.util.AttributeSet
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.LayoutInflater
import android.view.View
import android.widget.*
@ -67,7 +68,7 @@ class PeersActivity : AppCompatActivity() {
addPeerButton.setOnClickListener {
var view = inflater.inflate(R.layout.dialog_addpeer, null)
var input = view.findViewById<TextInputEditText>(R.id.addPeerInput)
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
val builder: AlertDialog.Builder = AlertDialog.Builder(ContextThemeWrapper(this, R.style.Theme_MaterialComponents_DayNight_Dialog))
builder.setTitle("Add Configured Peer")
builder.setView(view)
builder.setPositiveButton("Add") { dialog, _ ->

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")
}
}