From 8d040f0f02b9462d82fd03041529fdea423f02d3 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 7 Feb 2022 00:17:49 -0600 Subject: Added sendCommand() method to CommandsController, and updated Android app to use controller --- .../mx/trackermap/TrackerMap/android/TrackerApp.kt | 6 ++---- .../details/commands/UnitCommandsViewModel.kt | 20 ++++++++++++++------ .../TrackerMap/controllers/CommandsController.kt | 4 ++++ 3 files changed, 20 insertions(+), 10 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 cdeb85b..f8c236e 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt @@ -27,10 +27,7 @@ import mx.trackermap.TrackerMap.android.session.UserInformationViewModel import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.apis.* import mx.trackermap.TrackerMap.client.infrastructure.SessionManager -import mx.trackermap.TrackerMap.controllers.GeofencesController -import mx.trackermap.TrackerMap.controllers.ReportController -import mx.trackermap.TrackerMap.controllers.SessionController -import mx.trackermap.TrackerMap.controllers.UnitsController +import mx.trackermap.TrackerMap.controllers.* import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidLogger import org.koin.androidx.viewmodel.dsl.viewModel @@ -63,6 +60,7 @@ open class TrackerApp : Application() { factory { UnitsController(get(), get()) } factory { GeofencesController(get()) } factory { ReportController(get(), get()) } + factory { CommandsController(get()) } viewModel { LoginViewModel() } viewModel { UnitInformationViewModel(get()) } 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 index 3bb7f11..312d67e 100644 --- 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 @@ -25,22 +25,28 @@ import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.launch import mx.trackermap.TrackerMap.client.apis.CommandsApi import mx.trackermap.TrackerMap.client.models.Command +import mx.trackermap.TrackerMap.controllers.CommandsController import org.koin.core.component.KoinComponent +import org.koin.core.component.inject @DelicateCoroutinesApi class UnitCommandsViewModel( private val commandsApi: CommandsApi ) : ViewModel(), KoinComponent { + private val commandsController: CommandsController by inject() + var commands = MutableLiveData>() 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() + deviceId?.let { + viewModelScope.launch { + val commands = commandsController.fetchCommands(it) + this@UnitCommandsViewModel.commands.value = commands.toList() + } } } @@ -59,9 +65,11 @@ class UnitCommandsViewModel( } Log.d("UnitCommandsVM", "Sending command - $selectedCommand") - viewModelScope.launch { - val command = commandsApi.commandsSendPost(selectedCommand!!) - Log.d("UnitCommandsVM", "Command sent - $command") + selectedCommand?.let { + viewModelScope.launch { + val command = commandsController.sendCommand(it) + Log.d("UnitCommandsVM", "Command sent - $command") + } } } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt index c483f67..03ba5ab 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt @@ -10,4 +10,8 @@ class CommandsController( suspend fun fetchCommands(deviceId: Int): List { return commandsApi.commandsGet(deviceId = deviceId).toList() } + + suspend fun sendCommand(command: Command) { + commandsApi.commandsSendPost(command) + } } \ No newline at end of file -- cgit v1.2.3