aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-02-07 00:17:49 -0600
committerIván Ávalos <avalos@disroot.org>2022-02-07 00:17:49 -0600
commit8d040f0f02b9462d82fd03041529fdea423f02d3 (patch)
treebb6bd455b9bf2c05424a17aa89c5aba92f8c36e7
parent7610b6506ee3612413cc0b9636c24aadb05c7119 (diff)
downloadetbsa-trackermap-mobile-8d040f0f02b9462d82fd03041529fdea423f02d3.tar.gz
etbsa-trackermap-mobile-8d040f0f02b9462d82fd03041529fdea423f02d3.tar.bz2
etbsa-trackermap-mobile-8d040f0f02b9462d82fd03041529fdea423f02d3.zip
Added sendCommand() method to CommandsController, and updated Android app to use controller
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/TrackerApp.kt6
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/commands/UnitCommandsViewModel.kt20
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt4
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<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()
+ 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<Command> {
return commandsApi.commandsGet(deviceId = deviceId).toList()
}
+
+ suspend fun sendCommand(command: Command) {
+ commandsApi.commandsSendPost(command)
+ }
} \ No newline at end of file