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

refactor: Login 뷰에서 진행하는 네트워크 작업 코루틴으로 변경 #328

Merged
merged 7 commits into from
Sep 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<NaagaAuthDto>
): Response<NaagaAuthDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ 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.fetchResponse
import com.now.naaga.data.remote.retrofit.ServicePool.authService
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<Boolean>) -> 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.now.naaga.presentation.login
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,
Expand All @@ -18,10 +20,14 @@ class LoginViewModel(
val throwable: LiveData<DataThrowable> = _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 {
_throwable.value = it as DataThrowable
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>) -> Unit,
)
): Boolean
}
Loading