channel uncompose kinda works

This commit is contained in:
geeksville
2020-04-07 17:42:31 -07:00
parent 17a1631892
commit b83c1a0394
5 changed files with 103 additions and 18 deletions

View File

@@ -7,6 +7,8 @@ import android.os.Bundle
import android.os.RemoteException
import androidx.compose.mutableStateOf
import androidx.core.content.edit
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.geeksville.android.BuildUtils.isEmulator
import com.geeksville.android.Logging
import com.geeksville.mesh.IMeshService
@@ -14,6 +16,55 @@ import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.service.MeshService
import com.geeksville.mesh.ui.getInitials
class UIViewModel : ViewModel(), Logging {
init {
debug("ViewModel created")
}
companion object {
/**
* Return the current channel info
* FIXME, we should sim channels at the MeshService level if we are running on an emulator,
* for now I just fake it by returning a canned channel.
*/
fun getChannel(c: MeshProtos.RadioConfig?): Channel? {
val channel = c?.channelSettings?.let { Channel(it) }
return if (channel == null && isEmulator)
Channel.emulated
else
channel
}
fun getPreferences(context: Context): SharedPreferences =
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
}
override fun onCleared() {
super.onCleared()
debug("ViewModel cleared")
}
var meshService: IMeshService? = null
/// Are we connected to our radio device
val isConnected = MutableLiveData(MeshService.ConnectionState.DISCONNECTED)
/// various radio settings (including the channel)
val radioConfig = MutableLiveData<MeshProtos.RadioConfig?>(null)
/// Set the radio config (also updates our saved copy in preferences)
fun setRadioConfig(context: Context, c: MeshProtos.RadioConfig) {
debug("Setting new radio config!")
meshService?.radioConfig = c.toByteArray()
radioConfig.value = c
getPreferences(context).edit(commit = true) {
this.putString("channel-url", UIState.getChannel()!!.getChannelUrl().toString())
}
}
}
/// FIXME - figure out how to merge this staate with the AppStatus Model
object UIState : Logging {

View File

@@ -5,7 +5,11 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import androidx.compose.Composable
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.ui.core.ContextAmbient
import androidx.ui.foundation.Text
import androidx.ui.input.ImeAction
@@ -19,22 +23,48 @@ import com.geeksville.android.GeeksvilleApplication
import com.geeksville.android.Logging
import com.geeksville.mesh.R
import com.geeksville.mesh.model.Channel
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.model.toHumanString
import com.google.android.material.textfield.TextInputEditText
object ChannelLog : Logging
class ChannelFragment : ScreenFragment("Channel"), Logging {
private val model: UIViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.channel_fragment, container, false)
): View? {
return inflater.inflate(R.layout.channel_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.radioConfig.observe(viewLifecycleOwner, Observer { config ->
val channel = UIViewModel.getChannel(config)
val channelNameEdit = view.findViewById<TextInputEditText>(R.id.channelNameEdit)
if (channel != null) {
channelNameEdit.visibility = View.VISIBLE
channelNameEdit.setText(channel.name)
} else {
channelNameEdit.visibility = View.INVISIBLE
}
val adapter = ArrayAdapter(
requireContext(),
R.layout.dropdown_menu_popup_item,
arrayOf("Item 1", "Item 2", "Item 3", "Item 4")
)
val editTextFilledExposedDropdown =
view.findViewById<AutoCompleteTextView>(R.id.filled_exposed_dropdown)
editTextFilledExposedDropdown.setAdapter(adapter)
})
}
}