Commit 5bbdf2db authored by sivl0509's avatar sivl0509
Browse files

make the application tutorial ready

parent 0745c704
......@@ -22,70 +22,22 @@ class Sensors : SensorEventListener {
return
}
if (event.sensor.type == Sensor.TYPE_HEART_RATE) {
if (event.accuracy == SensorManager.SENSOR_STATUS_NO_CONTACT) {
mSensorDataListener.sensorHeartRateError("Heart rate sensor not in contact with user.")
} else if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
mSensorDataListener.sensorHeartRateError("Heart rate sensor can not read values.")
} else {
mSensorDataListener.heartRateValueChanged(event.values[0].toInt())
}
} else if (event.sensor.type == Sensor.TYPE_STEP_COUNTER) {
mSensorDataListener.stepCounterValueChanged(event.values[0].toInt())
} else if (event.sensor.type == Sensor.TYPE_STEP_DETECTOR) {
mSensorDataListener.stepDetected()
}
// TODO
}
fun createSensor(context: Context, sensorDataListener: SensorDataListener) {
mSensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE)
mStepCounterSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
mSensorDataListener = sensorDataListener
// TODO
}
fun start() {
mHeartRateSensor?.also { heartRate ->
mSensorManager.registerListener(
this,
heartRate,
SensorManager.SENSOR_DELAY_NORMAL,
SensorManager.SENSOR_DELAY_FASTEST
)
}
mStepCounterSensor?.also { stepCounter ->
mSensorManager.registerListener(
this,
stepCounter,
SensorManager.SENSOR_DELAY_NORMAL,
SensorManager.SENSOR_DELAY_FASTEST
)
}
mStepDetectorSensor?.also { stepDetector ->
mSensorManager.registerListener(
this,
stepDetector,
SensorManager.SENSOR_DELAY_NORMAL,
SensorManager.SENSOR_DELAY_FASTEST
)
}
// TODO
}
fun stop() {
mSensorManager.unregisterListener(this, mHeartRateSensor)
mSensorManager.unregisterListener(this, mStepCounterSensor)
mSensorManager.unregisterListener(this, mStepDetectorSensor)
// TODO
}
interface SensorDataListener {
fun heartRateValueChanged(bpm: Int)
fun stepCounterValueChanged(stepCount: Int)
fun stepDetected();
fun sensorHeartRateError(msg: String)
// TODO
}
}
\ No newline at end of file
......@@ -20,7 +20,6 @@ class SessionActivity : AppCompatActivity() {
private lateinit var mBinding: SessionActivityBinding
private lateinit var mViewModel: SessionViewModel
private val mSensorPermissionRequestCode = 0
private val mBtnStartSessionId = "Start"
private val mBtnStopSessionId = "Stop"
......@@ -38,7 +37,7 @@ class SessionActivity : AppCompatActivity() {
}
override fun onDestroy() {
mViewModel.stopSensors()
// TODO
super.onDestroy()
}
......@@ -63,36 +62,13 @@ class SessionActivity : AppCompatActivity() {
}
private fun checkSensorPermissions() {
var permissions: MutableList<String> = ArrayList()
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.BODY_SENSORS
) != PackageManager.PERMISSION_GRANTED
) {
permissions.add(Manifest.permission.BODY_SENSORS)
}
if (permissions.size > 0) {
// Sensor permission is not granted
requestSensorPermissions(permissions.toTypedArray())
}
// TODO
}
private fun initUiElements() {
mBinding.editFeedback.setTextColor(ContextCompat.getColor(this, R.color.teal_700))
mViewModel.getBpmLiveData().observe(this, { bpm: String ->
mBinding.editHeartRate.text = bpm
})
mViewModel.getStepCountLiveData().observe(this, { steCount: String ->
mBinding.editStepCounter.text = steCount
})
mViewModel.getSensorHeartRateFeedback().observe(this, { heartRateFeedback: String ->
mBinding.editFeedback.text = heartRateFeedback
})
// TODO
mBinding.buttonSessionStartStop.setOnClickListener {
if (mBinding.buttonSessionStartStop.text.equals(mBtnStartSessionId)) {
......@@ -108,21 +84,13 @@ class SessionActivity : AppCompatActivity() {
}
private fun requestSensorPermissions(permissions: Array<String>) {
ActivityCompat.requestPermissions(
this,
permissions,
mSensorPermissionRequestCode
)
// TODO
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
mSensorPermissionRequestCode -> {
checkSensorPermissions()
}
}
// TODO
}
}
\ No newline at end of file
......@@ -15,84 +15,33 @@ import kotlin.collections.ArrayList
class SessionViewModel(context: Context) : ViewModel(), Sensors.SensorDataListener {
private val mRepository: SessionRepository by lazy { StayFitApplication.repository }
private var mSensors: Sensors = Sensors()
private var mStartTime: String = ""
private var mEndTime: String = ""
private var mStepsSensorCounter: Int = 0
private var mSteps: Int = 0
private var mBpm: MutableList<Int> = ArrayList()
private var mBpmLiveData = MutableLiveData<String>("-")
private var mStepCountLiveData = MutableLiveData<String>("-")
private var mSensorHeartRateFeedback = MutableLiveData<String>("Start a new session.")
init {
mSensors.createSensor(context, this)
// TODO
}
fun getBpmLiveData(): LiveData<String> = mBpmLiveData
fun getStepCountLiveData(): LiveData<String> = mStepCountLiveData
fun getSensorHeartRateFeedback(): LiveData<String> = mSensorHeartRateFeedback
private suspend fun saveSession() {
mRepository.insert(Session(0, mStartTime, mEndTime, mBpm as ArrayList<Int>, mSteps))
// TODO
clearFields()
}
private fun clearFields() {
mStartTime = ""
mEndTime = ""
mBpm.clear()
mStepsSensorCounter = 0
mSteps = 0
mSensorHeartRateFeedback.value = "Start a new session."
mStepCountLiveData.value = "-"
mBpmLiveData.value = "-"
// TODO
}
private fun updateStepsLiveData() {
mStepCountLiveData.value = mSteps.toString()
// TODO
}
fun startSession() {
mStartTime = Date().toDateTimeStringFormatter()
mSensors.start()
// TODO
}
suspend fun stopSession() {
mEndTime = Date().toDateTimeStringFormatter()
mSensors.stop()
// TODO
saveSession()
}
fun stopSensors() {
mSensors.stop()
}
override fun heartRateValueChanged(bpm: Int) {
mSensorHeartRateFeedback.value = ""
mBpmLiveData.value = bpm.toString()
mBpm.add(bpm)
}
override fun stepCounterValueChanged(stepCount: Int) {
if (mStepsSensorCounter < 1) {
mStepsSensorCounter = stepCount
}
mSteps = stepCount - mStepsSensorCounter
updateStepsLiveData()
}
override fun stepDetected() {
mSteps += 1
updateStepsLiveData()
}
override fun sensorHeartRateError(msg: String) {
mSensorHeartRateFeedback.value = msg
mBpmLiveData.value = "-"
// TODO
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment