Skip to content

Commit

Permalink
[MERGE] #46 -> develop
Browse files Browse the repository at this point in the history
[FEAT/#46] Data Layer API로 워치 & 폰 데이터 항목 동기화
  • Loading branch information
Marchbreeze authored Aug 28, 2024
2 parents 81daed3 + 79b67db commit 698bcfc
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 18 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object AndroidXDependencies {
const val hilt = "com.google.dagger:hilt-android:${Versions.hiltVersion}"
const val ossLicense =
"com.google.android.gms:play-services-oss-licenses:${Versions.ossVersion}"
const val wearable = "com.google.android.gms:play-services-wearable:${Versions.wearableVersion}"
}

object TestDependencies {
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import org.gradle.api.JavaVersion
object Versions {
const val gradleVersion = "8.0.2"

const val kotlinVersion = "1.8.20"
const val kotlinVersion = "1.9.0"
const val kotlinSerializationJsonVersion = "1.5.1"
const val kotlinDateTimeVersion = "0.4.0"
const val coreKtxVersion = "1.10.1"
Expand All @@ -20,6 +20,7 @@ object Versions {
const val lifecycleVersion = "2.6.1"
const val ossPluginVersion = "0.10.4"
const val ossVersion = "17.0.0"
const val wearableVersion = "18.2.0"
const val splashVersion = "1.0.1"
const val workManagerVersion = "2.8.1"
const val coilVersion = "2.4.0"
Expand Down
1 change: 1 addition & 0 deletions core-di/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
implementation(hilt)
implementation(workManager)
implementation(hiltWorkManager)
implementation(wearable)
}

KaptDependencies.run {
Expand Down
29 changes: 29 additions & 0 deletions core-di/src/main/java/com/kkkk/di/ManagerModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object ManagerModule{

@Provides
@Singleton
fun provideDataClient(@ApplicationContext context: Context): DataClient {
return Wearable.getDataClient(context)
}

@Provides
@Singleton
fun provideWearableDataManager(dataClient: DataClient): WearableDataManager {
return WearableDataManager(dataClient)
}
}
23 changes: 14 additions & 9 deletions core-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,26 @@ android {
}

dependencies {
// Kotlin
implementation(KotlinDependencies.kotlin)
KotlinDependencies.run {
implementation(kotlin)
}

// Lifecycle Ktx
implementation(AndroidXDependencies.lifeCycleKtx)
AndroidXDependencies.run {
implementation(lifeCycleKtx)
implementation(hilt)
}

// Material Design
implementation(MaterialDesignDependencies.materialDesign)
MaterialDesignDependencies.run {
implementation(materialDesign)
}

// Hilt
implementation(AndroidXDependencies.hilt)
kapt(KaptDependencies.hiltAndroidCompiler)
KaptDependencies.run {
kapt(hiltAndroidCompiler)
}

// Test Dependency
testImplementation(TestDependencies.jUnit)
androidTestImplementation(TestDependencies.androidTest)
androidTestImplementation(TestDependencies.espresso)

}
1 change: 1 addition & 0 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
implementation(splashScreen)
implementation(workManager)
implementation(hiltWorkManager)
implementation(wearable)
}

KaptDependencies.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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.onboarding.onbarding.OnboardingViewModel.Companion.SPEED_CALC_INTERVAL
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
Expand All @@ -32,6 +35,7 @@ import kr.genti.presentation.R
import kr.genti.presentation.databinding.FragmentRhythmBinding
import java.io.File
import java.nio.file.Files
import javax.inject.Inject

@AndroidEntryPoint
class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhythm),
Expand All @@ -44,6 +48,9 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
private var rhythmSaveDialog: RhythmSaveDialog? = null
private lateinit var mediaPlayer: MediaPlayer

@Inject
lateinit var wearableDataManager: WearableDataManager

override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
Expand All @@ -53,6 +60,7 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
initChangeLevelBtnListener()
initPlayBtnListener()
initStopBtnListener()
initWearableSyncBtnListener()
observeRhythmLevel()
observeRhythmUrlState()
observeDownloadState()
Expand Down Expand Up @@ -99,6 +107,12 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
}
}

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

private fun observeRhythmLevel() {
viewModel.rhythmLevel.flowWithLifecycle(lifecycle).distinctUntilChanged().onEach { level ->
if (level == LEVEL_UNDEFINED) return@onEach
Expand All @@ -107,7 +121,7 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
switchPlayingState(false)
}
setUiWithCurrentLevel()
viewModel.postToGetRhythmUrlFromServer(level)
viewModel.postToGetRhythmUrlFromServer()
}.launchIn(lifecycleScope)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ constructor(
var tempRhythmLevel = MutableLiveData<Int>(1)
var bpm = 50
var filename: String = "stempo_level_1"
var isSubmitted: Boolean = true
private var isSubmitted: Boolean = true

private val _rhythmLevel = MutableStateFlow<Int>(LEVEL_UNDEFINED)
val rhythmLevel: StateFlow<Int> = _rhythmLevel
Expand Down Expand Up @@ -93,7 +93,7 @@ constructor(
_rhythmLevel.value = tempRhythmLevel.value ?: 1
}

fun postToGetRhythmUrlFromServer(level: Int) {
fun postToGetRhythmUrlFromServer() {
_rhythmUrlState.value = UiState.Loading
viewModelScope.launch {
rhythmRepository.postToGetRhythmUrl(bpm)
Expand Down Expand Up @@ -142,6 +142,8 @@ constructor(
_lastStepTime.value = 0L
}

fun getBpmFromDataStore() = userRepository.getBpm()

private fun setBpm(level: Int) = 40 + level * 10

private fun setBpmLevel(bpm: Int) =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kkkk.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
import javax.inject.Inject
import javax.inject.Singleton

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

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

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

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 WEARABLE SUCCESS : ${dataItem.uri}")
}.addOnFailureListener { exception ->
Timber.tag("okhttp").d("SEND DATA TO WEARABLE FAIL : ${exception.message}")
}
}

companion object {
const val KEY_BPM = "KEY_BPM"

const val PATH_BPM = "/bpm"
}
}
5 changes: 5 additions & 0 deletions stempo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,15 @@ dependencies {
AndroidXDependencies.run {
implementation(hilt)
implementation(hiltWorkManager)
implementation(wearable)
}

KaptDependencies.run {
kapt(hiltCompiler)
kapt(hiltWorkManagerCompiler)
}

ThirdPartyDependencies.run {
implementation(timber)
}
}
6 changes: 2 additions & 4 deletions stempo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
android:supportsRtl="true"
android:theme="@android:style/Theme.DeviceDefault"
android:usesCleartextTraffic="true">

<uses-library
android:name="com.google.android.wearable"
android:required="true" />

<!--
Set to true if your app is Standalone, that is, it does not require the handheld
app to run.
-->
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
Expand All @@ -39,6 +36,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
8 changes: 8 additions & 0 deletions stempo/src/main/java/com/kkkk/stempo/presentation/MyApp.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.kkkk.stempo.presentation

import android.app.Application
import com.kkkk.di.BuildConfig
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber

@HiltAndroidApp
class MyApp : Application() {
override fun onCreate() {
super.onCreate()

initTimber()
}

private fun initTimber() {
if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
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.stempo.presentation.home.HomeScreen
import com.kkkk.stempo.presentation.theme.StempoandroidTheme
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class WatchActivity : ComponentActivity() {
class WatchActivity : ComponentActivity(), DataClient.OnDataChangedListener {
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()

Expand All @@ -29,4 +35,38 @@ class WatchActivity : ComponentActivity() {
}
}
}

override fun onResume() {
super.onResume()
Timber.tag("okhttp").d("LISTENER : ADDED")
Wearable.getDataClient(this).addListener(this)
}

override fun onPause() {
super.onPause()
Wearable.getDataClient(this).removeListener(this)
}

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_BPM) == 0) {
DataMapItem.fromDataItem(item).dataMap.apply {
val bpm = getInt(KEY_BPM)
Timber.tag("okhttp").d("LISTENER : DATA RECEIVED : $bpm")
}
}
}
}
}
}

companion object {
const val KEY_BPM = "KEY_BPM"

const val PATH_BPM = "/bpm"
}
}

0 comments on commit 698bcfc

Please sign in to comment.