Skip to content

Commit

Permalink
[FEAT/#51] DataClient.OnDataChangedListener 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Aug 28, 2024
1 parent 3985343 commit eb52ed9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataEvent
import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.DataMapItem
import com.google.android.gms.wearable.Wearable
import com.kkkk.core.base.BaseFragment
import com.kkkk.core.extension.colorOf
import com.kkkk.core.extension.drawableOf
Expand All @@ -33,13 +38,14 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.presentation.R
import kr.genti.presentation.databinding.FragmentRhythmBinding
import timber.log.Timber
import java.io.File
import java.nio.file.Files
import javax.inject.Inject

@AndroidEntryPoint
class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhythm),
SensorEventListener {
SensorEventListener, DataClient.OnDataChangedListener {
private lateinit var sensorManager: SensorManager
private var stepDetectorSensor: Sensor? = null

Expand Down Expand Up @@ -271,13 +277,17 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
stepDetectorSensor?.let {
sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL)
}
Timber.tag("okhttp").d("LISTENER : ADDED")
Wearable.getDataClient(requireActivity()).addListener(this)
}

override fun onPause() {
super.onPause()
if (::sensorManager.isInitialized) {
sensorManager.unregisterListener(this)
}
Timber.tag("okhttp").d("LISTENER : REMOVED")
Wearable.getDataClient(requireActivity()).removeListener(this)
}

override fun onSensorChanged(event: SensorEvent?) {
Expand All @@ -295,6 +305,23 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
stepDetectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
}

override fun onDataChanged(dataEvents: DataEventBuffer) {
Timber.tag("okhttp").d("LISTENER : ON DATA CHANGED")

dataEvents.forEach { event ->
if (event.type == DataEvent.TYPE_CHANGED) {
event.dataItem.also { item ->
if (item.uri.path?.compareTo(PATH_RECORD) == 0) {
DataMapItem.fromDataItem(item).dataMap.apply {
val record = getInt(KEY_RECORD)
Timber.tag("okhttp").d("LISTENER : DATA RECEIVED : $record")
}
}
}
}
}
}

private fun calculateSpeed() {
val currentTime = System.currentTimeMillis()
val lastStepTime = viewModel.lastStepTime.value
Expand All @@ -321,6 +348,9 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
private const val DRAWABLE = "drawable"
private const val RAW = "raw"

const val KEY_RECORD = "KEY_RECORD"
const val PATH_RECORD = "/record"

private const val FLOAT_120 = 120.00000000000000000000F

private const val SUCCESS_CODE = 200
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.kkkk.stempo.presentation.home

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kkkk.domain.repository.UserRepository
Expand Down

0 comments on commit eb52ed9

Please sign in to comment.