diff options
author | Isidro Henoch <imhenoch@protonmail.com> | 2021-12-21 14:21:16 -0600 |
---|---|---|
committer | Isidro Henoch <imhenoch@protonmail.com> | 2021-12-21 14:21:16 -0600 |
commit | 7c8a65b8d1ae8a049bf67260acb6fafbd95adac9 (patch) | |
tree | 8e536d19e63df3fe53f921858a710746b5df0e2a /androidApp/src/main | |
parent | 090bcc911e569040ceeaef29f7ca8db82694d9b0 (diff) | |
download | etbsa-trackermap-mobile-7c8a65b8d1ae8a049bf67260acb6fafbd95adac9.tar.gz etbsa-trackermap-mobile-7c8a65b8d1ae8a049bf67260acb6fafbd95adac9.tar.bz2 etbsa-trackermap-mobile-7c8a65b8d1ae8a049bf67260acb6fafbd95adac9.zip |
Implements the commands list
- Shows command details
- Sends a selected command to a device
Diffstat (limited to 'androidApp/src/main')
7 files changed, 115 insertions, 15 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt index 7929832..f46a5c9 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -1,9 +1,11 @@ package mx.trackermap.TrackerMap.android import android.app.Application +import mx.trackermap.TrackerMap.android.details.commands.UnitCommandsViewModel import mx.trackermap.TrackerMap.android.details.information.UnitInformationViewModel import mx.trackermap.TrackerMap.android.session.LoginViewModel import mx.trackermap.TrackerMap.android.units.UnitsViewModel +import mx.trackermap.TrackerMap.client.apis.CommandsApi import mx.trackermap.TrackerMap.client.apis.DevicesApi import mx.trackermap.TrackerMap.client.apis.PositionsApi import mx.trackermap.TrackerMap.client.apis.SessionApi @@ -25,10 +27,13 @@ class TrackerApp : Application() { single { SessionApi(get()) } single { DevicesApi(get()) } single { PositionsApi(get()) } + single { CommandsApi(get()) } + single { UnitsController(get(), get()) } viewModel { LoginViewModel(get(), get()) } - viewModel { UnitInformationViewModel() } + viewModel { UnitInformationViewModel(get()) } + viewModel { UnitCommandsViewModel(get()) } single { UnitsViewModel(get()) } } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt index 0391c16..bb0da4f 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsFragment.kt @@ -1,17 +1,23 @@ package mx.trackermap.TrackerMap.android.details.commands import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter import androidx.fragment.app.Fragment import mx.trackermap.TrackerMap.android.databinding.UnitDetailsCommandsBinding import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter +import org.koin.androidx.viewmodel.ext.android.viewModel class UnitCommandsFragment: Fragment() { private var _binding: UnitDetailsCommandsBinding? = null private val binding get() = _binding!! + private val unitCommandsViewModel: UnitCommandsViewModel by viewModel() + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -23,12 +29,36 @@ class UnitCommandsFragment: Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) - binding.unitCommandsText.text = "COMMANDS for ID - $id" + + setupObservers() + setupEvents() } override fun onDestroyView() { super.onDestroyView() _binding = null } + + private fun setupObservers() { + unitCommandsViewModel.commands.observe(this) { commands -> + Log.d("UnitCommandsFragment", "Device commands - $commands") + val context = activity!!.applicationContext + val adapter = ArrayAdapter<String>(context, android.R.layout.simple_list_item_1) + adapter.addAll(commands.map { it.description }) + binding.commandsList.adapter = adapter + } + + val id = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) + unitCommandsViewModel.fetchCommands(id) + } + + private fun setupEvents() { + binding.commandsList.setOnItemClickListener { _, _, index, _ -> + Log.d("UnitCommandsFragment", "Selected item at index - $index") + unitCommandsViewModel.selectCommand(index) + } + binding.sendCommandButton.setOnClickListener { + unitCommandsViewModel.sendCommand() + } + } }
\ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsViewModel.kt new file mode 100644 index 0000000..06c0439 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsViewModel.kt @@ -0,0 +1,50 @@ +package mx.trackermap.TrackerMap.android.details.commands + +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.apis.CommandsApi +import mx.trackermap.TrackerMap.client.models.Command +import org.koin.core.component.KoinComponent + +@DelicateCoroutinesApi +class UnitCommandsViewModel( + private val commandsApi: CommandsApi +) : ViewModel(), KoinComponent { + + var commands = MutableLiveData<List<Command>>() + private var selectedCommand: Command? = null + private var deviceId: Int? = null + + fun fetchCommands(deviceId: Int?) { + this.deviceId = deviceId + viewModelScope.launch { + val commands = commandsApi.commandsGet(deviceId = deviceId) + this@UnitCommandsViewModel.commands.value = commands.toList() + } + } + + fun selectCommand(index: Int) { + val command = commands.value?.get(index) + Log.d("UnitCommandsVM", "Selected Command - $command") + command?.deviceId = this.deviceId + + this.selectedCommand = command + } + + fun sendCommand() { + if (selectedCommand == null) { + Log.d("UnitCommandsVM", "No Command Selection") + return + } + + Log.d("UnitCommandsVM", "Sending command - $selectedCommand") + viewModelScope.launch { + val command = commandsApi.commandsSendPost(selectedCommand!!) + Log.d("UnitCommandsVM", "Command sent - $command") + } + } +}
\ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt index e7100e1..11daaaf 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/information/UnitInformationViewModel.kt @@ -11,9 +11,11 @@ import mx.trackermap.TrackerMap.controllers.UnitsController import org.koin.core.component.KoinComponent import org.koin.core.component.inject -class UnitInformationViewModel: ViewModel(), KoinComponent { - @DelicateCoroutinesApi - private val unitsController: UnitsController by inject() +@DelicateCoroutinesApi +class UnitInformationViewModel constructor( + private val unitsController: UnitsController +) : ViewModel(), KoinComponent { + var unit = MutableLiveData<UnitInformation?>() init { diff --git a/androidApp/src/main/res/layout/unit_details_commands.xml b/androidApp/src/main/res/layout/unit_details_commands.xml index dfd6516..e98b113 100644 --- a/androidApp/src/main/res/layout/unit_details_commands.xml +++ b/androidApp/src/main/res/layout/unit_details_commands.xml @@ -1,19 +1,30 @@ <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:layout_width="match_parent" - android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" - xmlns:tools="http://schemas.android.com/tools"> + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <ListView + android:id="@+id/commandsList" + android:layout_width="0dp" + android:layout_height="0dp" + android:choiceMode="singleChoice" + android:listSelector="@color/darkBackground" + app:layout_constraintBottom_toTopOf="@id/sendCommandButton" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - <TextView - android:id="@+id/unitCommandsText" + <com.google.android.material.button.MaterialButton + android:id="@+id/sendCommandButton" android:layout_width="wrap_content" android:layout_height="wrap_content" - app:layout_constraintTop_toTopOf="parent" - app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" + android:layout_marginBottom="@dimen/margin" + android:text="@string/send_command" app:layout_constraintBottom_toBottomOf="parent" - tools:text="COMMANDS"/> + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/commandsList" /> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file diff --git a/androidApp/src/main/res/values/dimen.xml b/androidApp/src/main/res/values/dimen.xml index 6ce7887..18fd0eb 100644 --- a/androidApp/src/main/res/values/dimen.xml +++ b/androidApp/src/main/res/values/dimen.xml @@ -1,6 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="padding">8dp</dimen> + <dimen name="margin">16dp</dimen> <!-- CardView --> <dimen name="card_border_radius">20dp</dimen> diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index 5692688..7478ddb 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -25,4 +25,5 @@ <string name="units_search">Type to search</string> <string name="key">Key</string> <string name="value">Value</string> + <string name="send_command">Send Command</string> </resources>
\ No newline at end of file |