Skip to content

Commit

Permalink
refactor: ApiResult 확장 함수의 suspend 키워드 제거 및 inline 키워드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kimhm0728 committed Jan 12, 2025
1 parent 848b444 commit 9722cd2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.mulberry.ody.data.auth.source.local.LocalAuthDataSource
import com.mulberry.ody.data.auth.source.remote.RemoteAuthDataSource
import com.mulberry.ody.domain.apiresult.ApiResult
import com.mulberry.ody.domain.apiresult.getOrNull
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.model.AuthToken
import com.mulberry.ody.domain.repository.ody.AuthRepository
import javax.inject.Inject
Expand All @@ -27,7 +27,7 @@ class KakaoAuthRepository

override suspend fun login(context: Context): ApiResult<AuthToken> {
val fcmToken = localAuthDataSource.fetchFCMToken().getOrNull() ?: return ApiResult.Unexpected(Exception("FCM 토큰이 존재하지 않습니다."))
return remoteAuthDataSource.login(fcmToken, context).suspendOnSuccess {
return remoteAuthDataSource.login(fcmToken, context).onSuccess {
localAuthDataSource.postAuthToken(it)
}
}
Expand All @@ -38,7 +38,7 @@ class KakaoAuthRepository
}

override suspend fun withdrawAccount(): ApiResult<Unit> {
return remoteAuthDataSource.withdraw().suspendOnSuccess {
return remoteAuthDataSource.withdraw().onSuccess {
localAuthDataSource.removeAuthToken()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import com.mulberry.ody.domain.apiresult.fold
import com.mulberry.ody.domain.apiresult.getOrNull
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.suspendFold
import com.mulberry.ody.domain.model.MateEtaInfo
import com.mulberry.ody.domain.repository.ody.MeetingRepository
import com.mulberry.ody.presentation.common.analytics.AnalyticsHelper
Expand Down Expand Up @@ -88,7 +88,7 @@ class EtaDashboardService : Service() {
}

private suspend fun getLocation(meetingId: Long): MateEtaInfo? {
return geoLocationHelper.getCurrentCoordinate().suspendFold(
return geoLocationHelper.getCurrentCoordinate().fold(
onSuccess = { location ->
updateMatesEta(
meetingId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.mulberry.ody.data.remote.core.entity.login.mapper.toAuthToken
import com.mulberry.ody.data.remote.core.service.RefreshTokenService
import com.mulberry.ody.domain.apiresult.ApiResult
import com.mulberry.ody.domain.apiresult.map
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.model.AuthToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -55,7 +55,7 @@ class AccessTokenInterceptor
runBlocking(Dispatchers.IO) {
refreshTokenService.postRefreshToken()
.map { it.toAuthToken() }
.suspendOnSuccess {
.onSuccess {
odyDatastore.setAuthToken(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,35 @@ package com.mulberry.ody.domain.apiresult

import java.lang.IllegalArgumentException

fun <T> ApiResult<T>.onSuccess(block: (T) -> Unit): ApiResult<T> {
inline fun <T> ApiResult<T>.onSuccess(block: (T) -> Unit): ApiResult<T> {
if (this is ApiResult.Success) {
block(this.data)
}
return this
}

fun <T> ApiResult<T>.onFailure(block: (code: Int?, errorMessage: String?) -> Unit): ApiResult<T> {
inline fun <T> ApiResult<T>.onFailure(block: (code: Int?, errorMessage: String?) -> Unit): ApiResult<T> {
if (this is ApiResult.Failure) {
block(this.code, this.errorMessage)
}
return this
}

fun <T> ApiResult<T>.onNetworkError(block: (exception: Exception) -> Unit): ApiResult<T> {
inline fun <T> ApiResult<T>.onNetworkError(block: (exception: Exception) -> Unit): ApiResult<T> {
if (this is ApiResult.NetworkError) {
block(this.exception)
}
return this
}

fun <T> ApiResult<T>.onUnexpected(block: (t: Throwable) -> Unit): ApiResult<T> {
inline fun <T> ApiResult<T>.onUnexpected(block: (t: Throwable) -> Unit): ApiResult<T> {
if (this is ApiResult.Unexpected) {
block(this.t)
}
return this
}

suspend fun <T> ApiResult<T>.suspendOnSuccess(block: suspend (T) -> Unit): ApiResult<T> {
if (this is ApiResult.Success) {
block(this.data)
}
return this
}

suspend fun <T> ApiResult<T>.suspendOnFailure(block: suspend (code: Int?, errorMessage: String?) -> Unit): ApiResult<T> {
if (this is ApiResult.Failure) {
block(this.code, this.errorMessage)
}
return this
}

suspend fun <T> ApiResult<T>.suspendOnNetworkError(block: suspend (exception: Exception) -> Unit): ApiResult<T> {
if (this is ApiResult.NetworkError) {
block(this.exception)
}
return this
}

suspend fun <T> ApiResult<T>.suspendOnUnexpected(block: suspend (t: Throwable) -> Unit): ApiResult<T> {
if (this is ApiResult.Unexpected) {
block(this.t)
}
return this
}

suspend fun <T, R> ApiResult<T>.map(block: suspend (T) -> R): ApiResult<R> {
inline fun <T, R> ApiResult<T>.map(block: (T) -> R): ApiResult<R> {
return when (this) {
is ApiResult.Success -> ApiResult.Success(block(this.data))
is ApiResult.Failure -> ApiResult.Failure(this.code, this.errorMessage)
Expand Down Expand Up @@ -97,17 +69,17 @@ fun <T> ApiResult<T>.exceptionOrNull(): Throwable? {
return exception
}

suspend fun <R, T> ApiResult<T>.suspendFold(
onSuccess: suspend (data: T) -> R,
onFailure: suspend (t: Throwable) -> R,
inline fun <R, T> ApiResult<T>.fold(
onSuccess: (data: T) -> R,
onFailure: (t: Throwable) -> R,
): R {
return when (val exception = exceptionOrNull()) {
null -> onSuccess(this.getOrNull() as T)
else -> onFailure(exception)
}
}

suspend fun <T, R> ApiResult<T>.flatMap(block: suspend (T) -> ApiResult<R>): ApiResult<R> {
inline fun <T, R> ApiResult<T>.flatMap(block: (T) -> ApiResult<R>): ApiResult<R> {
return when (this) {
is ApiResult.Success -> block(this.data)
is ApiResult.Failure -> ApiResult.Failure(this.code, this.errorMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.suspendOnUnexpected
import com.mulberry.ody.domain.apiresult.onUnexpected
import com.mulberry.ody.domain.model.Address
import com.mulberry.ody.domain.model.MeetingCreationInfo
import com.mulberry.ody.domain.repository.location.AddressRepository
Expand Down Expand Up @@ -116,10 +115,10 @@ class MeetingCreationViewModel
viewModelScope.launch {
startLoading()
locationHelper.getCurrentCoordinate()
.suspendOnSuccess { location ->
.onSuccess { location ->
fetchAddressesByCoordinate(location)
}
.suspendOnUnexpected {
.onUnexpected {
_defaultLocationError.emit(Unit)
}
stopLoading()
Expand All @@ -140,7 +139,7 @@ class MeetingCreationViewModel
}.onFailure { code, errorMessage ->
handleError()
analyticsHelper.logNetworkErrorEvent(TAG, "$code $errorMessage")
}.suspendOnUnexpected {
}.onUnexpected {
_defaultLocationError.emit(Unit)
}.onNetworkError {
handleNetworkError()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mulberry.ody.presentation.invitecode

import androidx.lifecycle.viewModelScope
import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.suspendOnFailure
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.repository.ody.MeetingRepository
import com.mulberry.ody.presentation.common.BaseViewModel
import com.mulberry.ody.presentation.common.analytics.AnalyticsHelper
Expand Down Expand Up @@ -51,9 +51,9 @@ class InviteCodeViewModel
val inviteCode = inviteCode.value.ifBlank { return@launch }
startLoading()
meetingRepository.fetchInviteCodeValidity(inviteCode)
.suspendOnSuccess {
.onSuccess {
_navigateAction.emit(InviteCodeNavigateAction.CodeNavigateToJoin)
}.suspendOnFailure { code, errorMessage ->
}.onFailure { code, errorMessage ->
_invalidCodeEvent.emit(errorMessage.toString())
analyticsHelper.logNetworkErrorEvent(TAG, "$code $errorMessage")
}.onNetworkError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.suspendOnUnexpected
import com.mulberry.ody.domain.apiresult.onUnexpected
import com.mulberry.ody.domain.model.Address
import com.mulberry.ody.domain.model.MeetingJoinInfo
import com.mulberry.ody.domain.repository.location.AddressRepository
Expand Down Expand Up @@ -65,10 +64,10 @@ class MeetingJoinViewModel
viewModelScope.launch {
startLoading()
locationHelper.getCurrentCoordinate()
.suspendOnSuccess { location ->
.onSuccess { location ->
fetchAddressesByCoordinate(location)
}
.suspendOnUnexpected {
.onUnexpected {
_defaultLocationError.emit(Unit)
}
stopLoading()
Expand All @@ -89,7 +88,7 @@ class MeetingJoinViewModel
}.onFailure { code, errorMessage ->
handleError()
analyticsHelper.logNetworkErrorEvent(TAG, "$code $errorMessage")
}.suspendOnUnexpected {
}.onUnexpected {
_defaultLocationError.emit(Unit)
}.onNetworkError {
handleNetworkError()
Expand All @@ -106,7 +105,7 @@ class MeetingJoinViewModel
viewModelScope.launch {
startLoading()
joinRepository.postMates(meetingJoinInfo)
.suspendOnSuccess {
.onSuccess {
matesEtaRepository.reserveEtaFetchingJob(it.meetingId, it.meetingDateTime)
_navigateAction.emit(MeetingJoinNavigateAction.JoinNavigateToRoom(it.meetingId))
_navigateAction.emit(MeetingJoinNavigateAction.JoinNavigateToJoinComplete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.repository.ody.AuthRepository
import com.mulberry.ody.domain.repository.ody.MatesEtaRepository
import com.mulberry.ody.presentation.common.BaseViewModel
Expand Down Expand Up @@ -55,7 +55,7 @@ class LoginViewModel
viewModelScope.launch {
startLoading()
authRepository.login(context)
.suspendOnSuccess {
.onSuccess {
navigateToMeetings()
matesEtaRepository.reserveAllEtaReservation()
}.onFailure { code, errorMessage ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.apiresult.onUnexpected
import com.mulberry.ody.domain.apiresult.suspendOnFailure
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.model.MateEtaInfo
import com.mulberry.ody.domain.model.Nudge
import com.mulberry.ody.domain.repository.image.ImageStorage
Expand Down Expand Up @@ -145,9 +143,9 @@ class MeetingRoomViewModel
mateNickname: String,
) {
meetingRepository.postNudge(Nudge(nudgeId, mateId))
.suspendOnSuccess {
.onSuccess {
_nudgeSuccessMate.emit(mateNickname)
}.suspendOnFailure { code, errorMessage ->
}.onFailure { code, errorMessage ->
when (code) {
400 -> _expiredNudgeTimeLimit.emit(Unit)
else -> handleError()
Expand Down Expand Up @@ -275,7 +273,7 @@ class MeetingRoomViewModel

startLoading()
meetingRepository.exitMeeting(_meeting.value.id)
.suspendOnSuccess {
.onSuccess {
matesEtaRepository.deleteEtaReservation(meetingId)
_exitMeetingRoomEvent.emit(Unit)
}.onFailure { code, errorMessage ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.mulberry.ody.presentation.setting
import androidx.lifecycle.viewModelScope
import com.mulberry.ody.domain.apiresult.onFailure
import com.mulberry.ody.domain.apiresult.onNetworkError
import com.mulberry.ody.domain.apiresult.suspendOnSuccess
import com.mulberry.ody.domain.apiresult.onSuccess
import com.mulberry.ody.domain.repository.ody.AuthRepository
import com.mulberry.ody.domain.repository.ody.MatesEtaRepository
import com.mulberry.ody.presentation.common.BaseViewModel
Expand Down Expand Up @@ -42,7 +42,7 @@ class SettingViewModel
viewModelScope.launch {
startLoading()
authRepository.withdrawAccount()
.suspendOnSuccess {
.onSuccess {
_loginNavigateEvent.emit(LoginNavigatedReason.WITHDRAWAL)
matesEtaRepository.clearEtaFetchingJob()
matesEtaRepository.clearEtaReservation(isReservationPending = false)
Expand Down

0 comments on commit 9722cd2

Please sign in to comment.