Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#51] 워치 걸음 측정 폰으로 전송 구현 #52

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core-di/src/main/java/com/kkkk/di/ManagerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.kkkk.di
import android.content.Context
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.Wearable
import com.kkkk.presentation.manager.WearableDataManager
import com.kkkk.presentation.manager.PhoneDataManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -23,7 +23,7 @@ object ManagerModule{

@Provides
@Singleton
fun provideWearableDataManager(dataClient: DataClient): WearableDataManager {
return WearableDataManager(dataClient)
fun providePhoneDataManager(dataClient: DataClient): PhoneDataManager {
return PhoneDataManager(dataClient)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
initBnvItemSelectedListener()
}

fun initBnvItemIconTintList() {
private fun initBnvItemIconTintList() {
with(binding.bnvMain) {
itemIconTintList = null
selectedItemId = R.id.menu_rhythm
Expand Down
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 @@ -23,23 +28,24 @@ import com.kkkk.core.extension.stringOf
import com.kkkk.core.extension.toast
import com.kkkk.core.state.UiState
import com.kkkk.presentation.main.rhythm.RhythmViewModel.Companion.LEVEL_UNDEFINED
import com.kkkk.presentation.manager.WearableDataManager
import com.kkkk.presentation.manager.WearableDataManager.Companion.KEY_BPM
import com.kkkk.presentation.manager.WearableDataManager.Companion.PATH_BPM
import com.kkkk.presentation.manager.PhoneDataManager
import com.kkkk.presentation.manager.PhoneDataManager.Companion.KEY_BPM
import com.kkkk.presentation.manager.PhoneDataManager.Companion.PATH_BPM
import com.kkkk.presentation.onboarding.onbarding.OnboardingViewModel.Companion.SPEED_CALC_INTERVAL
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
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 All @@ -49,7 +55,7 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
private lateinit var mediaPlayer: MediaPlayer

@Inject
lateinit var wearableDataManager: WearableDataManager
lateinit var phoneDataManager: PhoneDataManager

override fun onViewCreated(
view: View,
Expand Down Expand Up @@ -109,7 +115,7 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy

private fun initWearableSyncBtnListener() {
binding.tvRhythmTitle.setOnSingleClickListener {
wearableDataManager.sendIntToWearable(PATH_BPM, KEY_BPM, viewModel.getBpmFromDataStore())
phoneDataManager.sendIntToWearable(PATH_BPM, KEY_BPM, viewModel.getBpmFromDataStore())
}
}

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,24 @@ 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")
// TODO 여기서 기록 받아서 서버통신으로 기록
}
}
}
}
}
}

private fun calculateSpeed() {
val currentTime = System.currentTimeMillis()
val lastStepTime = viewModel.lastStepTime.value
Expand All @@ -321,6 +349,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
Expand Up @@ -10,7 +10,7 @@ import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class WearableDataManager @Inject constructor(
class PhoneDataManager @Inject constructor(
private val dataClient: DataClient
) {

Expand Down
11 changes: 11 additions & 0 deletions stempo/src/main/java/com/kkkk/stempo/presentation/WatchActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.DataMapItem
import com.google.android.gms.wearable.Wearable
import com.kkkk.stempo.presentation.home.HomeScreen
import com.kkkk.stempo.presentation.manager.WearableDataManager
import com.kkkk.stempo.presentation.manager.WearableDataManager.Companion.KEY_RECORD
import com.kkkk.stempo.presentation.manager.WearableDataManager.Companion.PATH_RECORD
import com.kkkk.stempo.presentation.theme.StempoandroidTheme
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
Expand All @@ -34,6 +37,13 @@ class WatchActivity : ComponentActivity(), DataClient.OnDataChangedListener {
HomeScreen()
}
}

// TODO 이 함수로 정지 시 결과값 전송
WearableDataManager(Wearable.getDataClient(this)).sendIntToPhone(
PATH_RECORD,
KEY_RECORD,
50
)
}

override fun onResume() {
Expand All @@ -57,6 +67,7 @@ class WatchActivity : ComponentActivity(), DataClient.OnDataChangedListener {
DataMapItem.fromDataItem(item).dataMap.apply {
val bpm = getInt(KEY_BPM)
Timber.tag("okhttp").d("LISTENER : DATA RECEIVED : $bpm")
// TODO 여기서 bpm 받아서 초기값으로 설정
}
}
}
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.kkkk.stempo.presentation.manager

import com.google.android.gms.tasks.Task
import com.google.android.gms.wearable.DataClient
import com.google.android.gms.wearable.DataItem
import com.google.android.gms.wearable.PutDataMapRequest
import com.google.android.gms.wearable.PutDataRequest
import timber.log.Timber

class WearableDataManager(private val dataClient: DataClient) {

fun sendIntToPhone(path: String, key: String, value: Int): Task<DataItem> {

Timber.tag("okhttp").d("START SENDING DATA TO PHONE")

val putDataReq: PutDataRequest = PutDataMapRequest.create(path).run {
dataMap.putInt(key, value)
asPutDataRequest().setUrgent()
}

return dataClient.putDataItem(putDataReq).addOnSuccessListener { dataItem ->
Timber.tag("okhttp").d("SEND DATA TO PHONE SUCCESS : ${dataItem.uri}")
}.addOnFailureListener { exception ->
Timber.tag("okhttp").d("SEND DATA TO PHONE FAIL : ${exception.message}")
}
}

companion object {
const val KEY_RECORD = "KEY_RECORD"

const val PATH_RECORD = "/record"
}
}
Loading