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

@ -76,9 +76,12 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation "androidx.fragment:fragment-ktx:1.2.4"
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

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

View file

@ -4,7 +4,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/channelNameView"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
@ -14,15 +13,16 @@
android:layout_marginTop="16dp"
android:layout_marginEnd="64dp"
android:hint="Channel Name"
app:helperText="Channel Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/channelNameEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint" />
android:hint="channel name"
android:text="Unset" />
</com.google.android.material.textfield.TextInputLayout>
<ImageView
@ -43,28 +43,21 @@
style="@android:style/Widget.Material.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:contentDescription="Lock/Unlock button"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/channelOptions"
app:srcCompat="@drawable/ic_twotone_lock_open_24" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="205dp" />
<ImageButton
android:id="@+id/shareButton"
style="@android:style/Widget.Material.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="96dp"
android:contentDescription="Share button"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/lockButton"
app:srcCompat="@drawable/ic_twotone_share_24" />
@ -73,7 +66,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="64dp"
android:hint="Channel options"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1" />