From fa8ab2cecb3faa95496a8228573f0eaa0454e7e5 Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:03:45 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20service=EC=99=80=20repository?= =?UTF-8?q?=EC=97=90=20suspend=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../now/naaga/data/remote/retrofit/service/AuthService.kt | 6 +++--- .../main/java/com/now/domain/repository/AuthRepository.kt | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/data/remote/retrofit/service/AuthService.kt b/android/app/src/main/java/com/now/naaga/data/remote/retrofit/service/AuthService.kt index 73fdde4e0..2de427ba2 100644 --- a/android/app/src/main/java/com/now/naaga/data/remote/retrofit/service/AuthService.kt +++ b/android/app/src/main/java/com/now/naaga/data/remote/retrofit/service/AuthService.kt @@ -2,13 +2,13 @@ package com.now.naaga.data.remote.retrofit.service import com.now.naaga.data.remote.dto.NaagaAuthDto import com.now.naaga.data.remote.dto.PlatformAuthDto -import retrofit2.Call +import retrofit2.Response import retrofit2.http.Body import retrofit2.http.POST interface AuthService { @POST("/auth") - fun requestAuth( + suspend fun requestAuth( @Body platformAuthDto: PlatformAuthDto, - ): Call + ): Response } diff --git a/android/domain/src/main/java/com/now/domain/repository/AuthRepository.kt b/android/domain/src/main/java/com/now/domain/repository/AuthRepository.kt index 2b3fb8d97..045223603 100644 --- a/android/domain/src/main/java/com/now/domain/repository/AuthRepository.kt +++ b/android/domain/src/main/java/com/now/domain/repository/AuthRepository.kt @@ -3,8 +3,7 @@ package com.now.domain.repository import com.now.domain.model.PlatformAuth interface AuthRepository { - fun getToken( + suspend fun getToken( platformAuth: PlatformAuth, - callback: (Result) -> Unit, - ) + ): Boolean } From b718f81216ad2d2f4a9cdaa0ffbb69684dcff44f Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:04:07 +0900 Subject: [PATCH 2/7] =?UTF-8?q?refactor:=20repository=20=EB=82=B4=20?= =?UTF-8?q?=EC=BD=94=EB=A3=A8=ED=8B=B4=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/repository/DefaultAuthRepository.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt index 09bf224a5..5271934f3 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt @@ -5,21 +5,28 @@ import com.now.domain.repository.AuthRepository import com.now.naaga.data.local.AuthDataSource import com.now.naaga.data.mapper.toDto import com.now.naaga.data.remote.retrofit.ServicePool +import com.now.naaga.data.remote.retrofit.ServicePool.authService import com.now.naaga.data.remote.retrofit.fetchResponse +import com.now.naaga.util.getValueOrThrow +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext -class DefaultAuthRepository(private val authDataSource: AuthDataSource) : AuthRepository { - override fun getToken( - platformAuth: PlatformAuth, - callback: (Result) -> Unit, - ) { - val call = ServicePool.authService.requestAuth(platformAuth.toDto()) - call.fetchResponse( - onSuccess = { naagaAuthDto -> +class DefaultAuthRepository( + private val authDataSource: AuthDataSource, + private val dispatcher: CoroutineDispatcher = Dispatchers.IO +) : AuthRepository { + + override suspend fun getToken(platformAuth: PlatformAuth): Boolean { + return withContext(dispatcher) { + val response = authService.requestAuth(platformAuth.toDto()) + runCatching { + val naagaAuthDto = response.getValueOrThrow() authDataSource.setAccessToken(naagaAuthDto.accessToken) authDataSource.setRefreshToken(naagaAuthDto.refreshToken) - callback(Result.success(true)) - }, - onFailure = { callback(Result.failure(it)) }, - ) + return@withContext true + } + return@withContext false + } } } From a6f62ec0bf5bde0809b26b258c07a9a66e29ccb9 Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:05:00 +0900 Subject: [PATCH 3/7] =?UTF-8?q?refactor:=20repository=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EC=8B=9C=20Dispatcher=20=EC=A0=84=ED=95=B4=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/presentation/login/LoginActivity.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt b/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt index e81fd810d..cf0894597 100644 --- a/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt +++ b/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt @@ -17,6 +17,7 @@ import com.now.naaga.data.repository.DefaultAuthRepository import com.now.naaga.databinding.ActivityLoginBinding import com.now.naaga.presentation.beginadventure.BeginAdventureActivity import com.now.naaga.util.loginWithKakao +import kotlinx.coroutines.Dispatchers class LoginActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalyticsDelegate() { private lateinit var binding: ActivityLoginBinding @@ -35,7 +36,7 @@ class LoginActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytics } private fun initViewModel() { - val factory = LoginViewModelFactory(DefaultAuthRepository(NaagaApplication.authDataSource)) + val factory = LoginViewModelFactory(DefaultAuthRepository(NaagaApplication.authDataSource, Dispatchers.IO)) viewModel = ViewModelProvider(this, factory)[LoginViewModel::class.java] } From 38994e793d8ed7f8ef8210edc0452a0ef91e8916 Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:05:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?refactor:=20=EC=BD=94=EB=A3=A8=ED=8B=B4=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../naaga/presentation/login/LoginViewModel.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt b/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt index 788c29670..d8d56f6c9 100644 --- a/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt +++ b/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt @@ -1,12 +1,15 @@ package com.now.naaga.presentation.login +import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.now.domain.model.AuthPlatformType import com.now.domain.model.PlatformAuth import com.now.domain.repository.AuthRepository import com.now.naaga.data.throwable.DataThrowable +import kotlinx.coroutines.launch class LoginViewModel( private val authRepository: AuthRepository, @@ -18,10 +21,15 @@ class LoginViewModel( val throwable: LiveData = _throwable fun signIn(token: String, platformType: AuthPlatformType) { - authRepository.getToken(PlatformAuth(token, platformType)) { result -> - result - .onSuccess { _isLoginSucceed.value = it } - .onFailure { _throwable.value = it as DataThrowable } + viewModelScope.launch { + runCatching { + authRepository.getToken(PlatformAuth(token, platformType)) + }.onSuccess { status -> + _isLoginSucceed.value = status + }.onFailure { + Log.d("test", "${it.message} , ${it}") + _throwable.value = it as DataThrowable + } } } } From 79723e95053ae3fe83179c77342cfc4d749be96d Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:15:41 +0900 Subject: [PATCH 5/7] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=9D=B8=EC=9E=90=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/presentation/login/LoginActivity.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt b/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt index cf0894597..e81fd810d 100644 --- a/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt +++ b/android/app/src/main/java/com/now/naaga/presentation/login/LoginActivity.kt @@ -17,7 +17,6 @@ import com.now.naaga.data.repository.DefaultAuthRepository import com.now.naaga.databinding.ActivityLoginBinding import com.now.naaga.presentation.beginadventure.BeginAdventureActivity import com.now.naaga.util.loginWithKakao -import kotlinx.coroutines.Dispatchers class LoginActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalyticsDelegate() { private lateinit var binding: ActivityLoginBinding @@ -36,7 +35,7 @@ class LoginActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytics } private fun initViewModel() { - val factory = LoginViewModelFactory(DefaultAuthRepository(NaagaApplication.authDataSource, Dispatchers.IO)) + val factory = LoginViewModelFactory(DefaultAuthRepository(NaagaApplication.authDataSource)) viewModel = ViewModelProvider(this, factory)[LoginViewModel::class.java] } From 22c4907188567b9bf76bcf29c999fc7eeec0ca70 Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:17:42 +0900 Subject: [PATCH 6/7] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=A1=9C=EA=B7=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/presentation/login/LoginViewModel.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt b/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt index d8d56f6c9..0ffa3d58c 100644 --- a/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt +++ b/android/app/src/main/java/com/now/naaga/presentation/login/LoginViewModel.kt @@ -1,6 +1,5 @@ package com.now.naaga.presentation.login -import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel @@ -27,7 +26,6 @@ class LoginViewModel( }.onSuccess { status -> _isLoginSucceed.value = status }.onFailure { - Log.d("test", "${it.message} , ${it}") _throwable.value = it as DataThrowable } } From a334efafa6cd9e1a87ec158f017889c1cfbb1798 Mon Sep 17 00:00:00 2001 From: hyunji1203 Date: Thu, 14 Sep 2023 18:20:22 +0900 Subject: [PATCH 7/7] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/now/naaga/data/repository/DefaultAuthRepository.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt index 5271934f3..da7e225e9 100644 --- a/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt +++ b/android/app/src/main/java/com/now/naaga/data/repository/DefaultAuthRepository.kt @@ -4,9 +4,7 @@ import com.now.domain.model.PlatformAuth import com.now.domain.repository.AuthRepository import com.now.naaga.data.local.AuthDataSource import com.now.naaga.data.mapper.toDto -import com.now.naaga.data.remote.retrofit.ServicePool import com.now.naaga.data.remote.retrofit.ServicePool.authService -import com.now.naaga.data.remote.retrofit.fetchResponse import com.now.naaga.util.getValueOrThrow import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers