editing and saving channels kinda works

This commit is contained in:
geeksville 2020-04-09 16:33:42 -07:00
parent 1d32dad6de
commit 2b588ac7e7
7 changed files with 137 additions and 64 deletions

View file

@ -11,9 +11,7 @@ import java.net.MalformedURLException
data class Channel(
var name: String,
var modemConfig: MeshProtos.ChannelSettings.ModemConfig,
var settings: MeshProtos.ChannelSettings? = MeshProtos.ChannelSettings.getDefaultInstance()
val settings: MeshProtos.ChannelSettings = MeshProtos.ChannelSettings.getDefaultInstance()
) {
companion object {
// Placeholder when emulating
@ -37,10 +35,11 @@ data class Channel(
}
}
constructor(c: MeshProtos.ChannelSettings) : this(c.name, c.modemConfig, c)
constructor(url: Uri) : this(urlToSettings(url))
val name: String get() = settings.name
val modemConfig: MeshProtos.ChannelSettings.ModemConfig get() = settings.modemConfig
/// Can this channel be changed right now?
var editable = false

View file

@ -1,12 +1,13 @@
package com.geeksville.mesh.model
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.os.RemoteException
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.geeksville.android.BuildUtils.isEmulator
import com.geeksville.android.Logging
import com.geeksville.mesh.IMeshService
@ -22,7 +23,7 @@ fun getInitials(name: String): String {
return words
}
class UIViewModel : ViewModel(), Logging {
class UIViewModel(app: Application) : AndroidViewModel(app), Logging {
init {
debug("ViewModel created")
}
@ -46,6 +47,8 @@ class UIViewModel : ViewModel(), Logging {
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
}
private val context = app.applicationContext
var meshService: IMeshService? = null
val nodeDB = NodeDB(this)
@ -67,7 +70,7 @@ class UIViewModel : ViewModel(), Logging {
}
/// Set the radio config (also updates our saved copy in preferences)
fun setRadioConfig(context: Context, c: MeshProtos.RadioConfig) {
fun setRadioConfig(c: MeshProtos.RadioConfig) {
debug("Setting new radio config!")
meshService?.radioConfig = c.toByteArray()
radioConfig.value = c
@ -77,6 +80,15 @@ class UIViewModel : ViewModel(), Logging {
}
}
/** Update just the channel settings portion of our config (both in the device and in saved preferences) */
fun setChannel(c: MeshProtos.ChannelSettings) {
// When running on the emulator, radio config might not really be available, in that case, just ignore attempts to change the config
radioConfig.value?.toBuilder()?.let { config ->
config.channelSettings = c
setRadioConfig(config.build())
}
}
/// Kinda ugly - created in the activity but used from Compose - figure out if there is a cleaner way GIXME
// lateinit var googleSignInClient: GoogleSignInClient
@ -91,7 +103,7 @@ class UIViewModel : ViewModel(), Logging {
var requestedChannelUrl: Uri? = null
// clean up all this nasty owner state management FIXME
fun setOwner(context: Context, s: String? = null) {
fun setOwner(s: String? = null) {
if (s != null) {
ownerName.value = s