diff --git a/app/build.gradle b/app/build.gradle index e1fe106c..3672b1eb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index a3fa1f6d..5b613921 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -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(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 { diff --git a/app/src/main/java/com/geeksville/mesh/ui/Channel.kt b/app/src/main/java/com/geeksville/mesh/ui/Channel.kt index 66677122..da48ac3e 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/Channel.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/Channel.kt @@ -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(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(R.id.filled_exposed_dropdown) + editTextFilledExposedDropdown.setAdapter(adapter) + }) } } diff --git a/app/src/main/res/layout/channel_fragment.xml b/app/src/main/res/layout/channel_fragment.xml index 22120ee8..18fd4768 100644 --- a/app/src/main/res/layout/channel_fragment.xml +++ b/app/src/main/res/layout/channel_fragment.xml @@ -4,7 +4,6 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:hint="channel name" + android:text="Unset" /> - - @@ -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" diff --git a/app/src/main/res/layout/dropdown_menu_popup_item.xml b/app/src/main/res/layout/dropdown_menu_popup_item.xml new file mode 100644 index 00000000..1909d58d --- /dev/null +++ b/app/src/main/res/layout/dropdown_menu_popup_item.xml @@ -0,0 +1,8 @@ + + \ No newline at end of file