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: suspendOnXXX() 함수 제거 #949

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
@@ -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
@@ -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)
}
}
@@ -38,7 +38,7 @@ class KakaoAuthRepository
}

override suspend fun withdrawAccount(): ApiResult<Unit> {
return remoteAuthDataSource.withdraw().suspendOnSuccess {
return remoteAuthDataSource.withdraw().onSuccess {
localAuthDataSource.removeAuthToken()
}
}
Original file line number Diff line number Diff line change
@@ -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
@@ -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,
Original file line number Diff line number Diff line change
@@ -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
@@ -55,7 +55,7 @@ class AccessTokenInterceptor
runBlocking(Dispatchers.IO) {
refreshTokenService.postRefreshToken()
.map { it.toAuthToken() }
.suspendOnSuccess {
.onSuccess {
odyDatastore.setAuthToken(it)
}
}
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
@@ -116,10 +115,10 @@ class MeetingCreationViewModel
viewModelScope.launch {
startLoading()
locationHelper.getCurrentCoordinate()
.suspendOnSuccess { location ->
.onSuccess { location ->
fetchAddressesByCoordinate(location)
}
.suspendOnUnexpected {
.onUnexpected {
_defaultLocationError.emit(Unit)
}
stopLoading()
@@ -140,7 +139,7 @@ class MeetingCreationViewModel
}.onFailure { code, errorMessage ->
handleError()
analyticsHelper.logNetworkErrorEvent(TAG, "$code $errorMessage")
}.suspendOnUnexpected {
}.onUnexpected {
_defaultLocationError.emit(Unit)
}.onNetworkError {
handleNetworkError()
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
@@ -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 {
Original file line number Diff line number Diff line change
@@ -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
@@ -65,10 +64,10 @@ class MeetingJoinViewModel
viewModelScope.launch {
startLoading()
locationHelper.getCurrentCoordinate()
.suspendOnSuccess { location ->
.onSuccess { location ->
fetchAddressesByCoordinate(location)
}
.suspendOnUnexpected {
.onUnexpected {
_defaultLocationError.emit(Unit)
}
stopLoading()
@@ -89,7 +88,7 @@ class MeetingJoinViewModel
}.onFailure { code, errorMessage ->
handleError()
analyticsHelper.logNetworkErrorEvent(TAG, "$code $errorMessage")
}.suspendOnUnexpected {
}.onUnexpected {
_defaultLocationError.emit(Unit)
}.onNetworkError {
handleNetworkError()
@@ -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)
Original file line number Diff line number Diff line change
@@ -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
@@ -55,7 +55,7 @@ class LoginViewModel
viewModelScope.launch {
startLoading()
authRepository.login(context)
.suspendOnSuccess {
.onSuccess {
navigateToMeetings()
matesEtaRepository.reserveAllEtaReservation()
}.onFailure { code, errorMessage ->
Original file line number Diff line number Diff line change
@@ -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
@@ -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()
@@ -275,7 +273,7 @@ class MeetingRoomViewModel

startLoading()
meetingRepository.exitMeeting(_meeting.value.id)
.suspendOnSuccess {
.onSuccess {
matesEtaRepository.deleteEtaReservation(meetingId)
_exitMeetingRoomEvent.emit(Unit)
}.onFailure { code, errorMessage ->
Original file line number Diff line number Diff line change
@@ -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
@@ -42,7 +42,7 @@ class SettingViewModel
viewModelScope.launch {
startLoading()
authRepository.withdrawAccount()
.suspendOnSuccess {
.onSuccess {
_loginNavigateEvent.emit(LoginNavigatedReason.WITHDRAWAL)
matesEtaRepository.clearEtaFetchingJob()
matesEtaRepository.clearEtaReservation(isReservationPending = false)