feat: update DashboardViewModel and Fragment with service binding
This commit is contained in:
+12
-5
@@ -29,6 +29,13 @@ class DashboardFragment : Fragment() {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setupObservers()
|
||||
setupListeners()
|
||||
viewModel.bindService(requireContext())
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
viewModel.unbindService(requireContext())
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
private fun setupObservers() {
|
||||
@@ -68,6 +75,11 @@ class DashboardFragment : Fragment() {
|
||||
tvYaw.text = String.format("%.2f", attitude.yaw)
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.isRecording.observe(viewLifecycleOwner) { recording ->
|
||||
binding.btnStartRecording.isEnabled = !recording
|
||||
binding.btnStopRecording.isEnabled = recording
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
@@ -79,9 +91,4 @@ class DashboardFragment : Fragment() {
|
||||
viewModel.stopRecording()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
}
|
||||
|
||||
+58
-16
@@ -1,15 +1,22 @@
|
||||
package com.senstools.presentation.viewmodel
|
||||
|
||||
import android.app.Application
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.IBinder
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.senstools.data.datasource.DataCollectionService
|
||||
import com.senstools.domain.model.AttitudeData
|
||||
import com.senstools.domain.model.GPSData
|
||||
import com.senstools.domain.model.SensorData
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class DashboardViewModel : ViewModel() {
|
||||
class DashboardViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
private val _gpsData = MutableLiveData<GPSData>()
|
||||
val gpsData: LiveData<GPSData> = _gpsData
|
||||
@@ -23,27 +30,62 @@ class DashboardViewModel : ViewModel() {
|
||||
private val _isRecording = MutableLiveData<Boolean>()
|
||||
val isRecording: LiveData<Boolean> = _isRecording
|
||||
|
||||
private var dataCollectionService: DataCollectionService? = null
|
||||
private var isBound = false
|
||||
|
||||
private val serviceCallback = object : DataCollectionService.ServiceCallback {
|
||||
override fun onGPSData(gps: GPSData) {
|
||||
_gpsData.postValue(gps)
|
||||
}
|
||||
|
||||
override fun onSensorData(sensor: SensorData) {
|
||||
_sensorData.postValue(sensor)
|
||||
}
|
||||
|
||||
override fun onAttitudeData(attitude: AttitudeData) {
|
||||
_attitudeData.postValue(attitude)
|
||||
}
|
||||
|
||||
override fun onRecordingStateChanged(isRecording: Boolean) {
|
||||
_isRecording.postValue(isRecording)
|
||||
}
|
||||
}
|
||||
|
||||
private val connection = object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
val binder = service as DataCollectionService.LocalBinder
|
||||
dataCollectionService = binder.getService()
|
||||
isBound = true
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
dataCollectionService = null
|
||||
isBound = false
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
_isRecording.value = false
|
||||
}
|
||||
|
||||
fun bindService(context: Context) {
|
||||
val intent = Intent(context, DataCollectionService::class.java)
|
||||
context.bindService(intent, connection, Context.BIND_AUTO_CREATE)
|
||||
}
|
||||
|
||||
fun unbindService(context: Context) {
|
||||
if (isBound) {
|
||||
context.unbindService(connection)
|
||||
isBound = false
|
||||
dataCollectionService = null
|
||||
}
|
||||
}
|
||||
|
||||
fun startRecording() {
|
||||
_isRecording.value = true
|
||||
dataCollectionService?.startCollecting(serviceCallback)
|
||||
}
|
||||
|
||||
fun stopRecording() {
|
||||
_isRecording.value = false
|
||||
}
|
||||
|
||||
fun updateGPSData(data: GPSData) {
|
||||
_gpsData.value = data
|
||||
}
|
||||
|
||||
fun updateSensorData(data: SensorData) {
|
||||
_sensorData.value = data
|
||||
}
|
||||
|
||||
fun updateAttitudeData(data: AttitudeData) {
|
||||
_attitudeData.value = data
|
||||
dataCollectionService?.stopCollecting()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user