Skip to content

Commit

Permalink
fix : 불필요한 flow 변환으로 인한 중복 콜렉트 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
nohjunh committed May 11, 2024
1 parent 6f09c93 commit a2c322b
Show file tree
Hide file tree
Showing 46 changed files with 248 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ interface BattleRepository {

suspend fun setBattleId(battleId: String)

suspend fun getBattleId(): Flow<Result<BattleId>>
suspend fun terminateOngoingBattle(): Flow<Result<TerminateBattle>>
suspend fun getBattleId(): Result<BattleId>
suspend fun terminateOngoingBattle(): Result<TerminateBattle>
fun getBattleStream(battleId: String): Flow<BattleEvent>
suspend fun sendRecordData(battleId: String, recordData: RecordData)
suspend fun disposeSocketResources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package online.partyrun.partyrunapplication.core.data.repository

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import online.partyrun.partyrunapplication.core.common.network.apiRequestFlow
import online.partyrun.partyrunapplication.core.datastore.datasource.BattlePreferencesDataSource
import online.partyrun.partyrunapplication.core.model.battle.BattleId
import online.partyrun.partyrunapplication.core.model.battle.BattleStatus
Expand All @@ -15,13 +14,13 @@ import online.partyrun.partyrunapplication.core.network.model.request.toRequestM
import online.partyrun.partyrunapplication.core.network.model.response.toDomainModel
import online.partyrun.partyrunapplication.core.network.model.toDomainModel
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.common.result.mapResultModel
import online.partyrun.partyrunapplication.core.common.result.toResultModel
import javax.inject.Inject

class BattleRepositoryImpl @Inject constructor(
private val realtimeBattleClient: RealtimeBattleClient,
private val battleDataSource: BattleDataSource,
private val battlePreferencesDataSource: BattlePreferencesDataSource
private val battlePreferencesDataSource: BattlePreferencesDataSource,
) : BattleRepository {

override val battleData: Flow<BattleStatus> =
Expand All @@ -31,14 +30,16 @@ class BattleRepositoryImpl @Inject constructor(
battlePreferencesDataSource.setBattleId(battleId)
}

override suspend fun getBattleId(): Flow<Result<BattleId>> {
return apiRequestFlow { battleDataSource.getBattleId() }
.mapResultModel { it.toDomainModel() }
override suspend fun getBattleId(): Result<BattleId> {
return battleDataSource
.getBattleId()
.toResultModel { it.toDomainModel() }
}

override suspend fun terminateOngoingBattle(): Flow<Result<TerminateBattle>> {
return apiRequestFlow { battleDataSource.terminateOngoingBattle() }
.mapResultModel { it.toDomainModel() }
override suspend fun terminateOngoingBattle(): Result<TerminateBattle> {
return battleDataSource
.terminateOngoingBattle()
.toResultModel { it.toDomainModel() }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ interface MatchRepository {


/* REST */
suspend fun registerMatch(runningDistance: RunningDistance): Flow<Result<MatchStatus>>
suspend fun acceptMatch(matchDecision: MatchDecision): Flow<Result<MatchStatus>>
suspend fun declineMatch(matchDecision: MatchDecision): Flow<Result<MatchStatus>>
suspend fun getRunnerIds(): Flow<Result<RunnerIds>>
suspend fun cancelMatchWaitingEvent(): Flow<Result<CancelMatch>>
suspend fun registerMatch(runningDistance: RunningDistance): Result<MatchStatus>
suspend fun acceptMatch(matchDecision: MatchDecision): Result<MatchStatus>
suspend fun declineMatch(matchDecision: MatchDecision): Result<MatchStatus>
suspend fun getRunnerIds(): Result<RunnerIds>
suspend fun cancelMatchWaitingEvent(): Result<CancelMatch>

/* SSE */
fun createMatchEventSourceListener(onEvent: (data: String) -> Unit, onClosed: () -> Unit, onFailure: () -> Unit): EventSourceListener
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package online.partyrun.partyrunapplication.core.data.repository

import kotlinx.coroutines.flow.Flow
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import online.partyrun.partyrunapplication.core.common.Constants.BASE_URL
import online.partyrun.partyrunapplication.core.common.network.apiRequestFlow
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.common.result.mapResultModel
import online.partyrun.partyrunapplication.core.common.result.toResultModel
import online.partyrun.partyrunapplication.core.datastore.datasource.BattlePreferencesDataSource
import online.partyrun.partyrunapplication.core.model.match.CancelMatch
import online.partyrun.partyrunapplication.core.model.match.MatchDecision
Expand All @@ -29,36 +27,40 @@ class MatchRepositoryImpl @Inject constructor(
}

/* REST */
override suspend fun registerMatch(runningDistance: RunningDistance): Flow<Result<MatchStatus>> {
return apiRequestFlow {
matchDataSource.registerMatch(runningDistance.toRequestModel())
}.mapResultModel { it.toDomainModel() }
override suspend fun registerMatch(runningDistance: RunningDistance): Result<MatchStatus> {
return matchDataSource
.registerMatch(runningDistance.toRequestModel())
.toResultModel { it.toDomainModel() }
}

override suspend fun acceptMatch(matchDecision: MatchDecision): Flow<Result<MatchStatus>> {
return apiRequestFlow { matchDataSource.acceptMatch(matchDecision.toRequestModel()) }
.mapResultModel { it.toDomainModel() }
override suspend fun acceptMatch(matchDecision: MatchDecision): Result<MatchStatus> {
return matchDataSource
.acceptMatch(matchDecision.toRequestModel())
.toResultModel { it.toDomainModel() }
}

override suspend fun declineMatch(matchDecision: MatchDecision): Flow<Result<MatchStatus>> {
return apiRequestFlow { matchDataSource.declineMatch(matchDecision.toRequestModel()) }
.mapResultModel { it.toDomainModel() }
override suspend fun declineMatch(matchDecision: MatchDecision): Result<MatchStatus> {
return matchDataSource
.declineMatch(matchDecision.toRequestModel())
.toResultModel { it.toDomainModel() }
}

override suspend fun getRunnerIds(): Flow<Result<RunnerIds>> {
return apiRequestFlow { matchDataSource.getRunnerIds() }
.mapResultModel { it.toDomainModel() }
override suspend fun getRunnerIds(): Result<RunnerIds> {
return matchDataSource
.getRunnerIds()
.toResultModel { it.toDomainModel() }
}

override suspend fun cancelMatchWaitingEvent(): Flow<Result<CancelMatch>> {
return apiRequestFlow { matchDataSource.cancelMatchWaitingEvent() }
.mapResultModel { it.toDomainModel() }
override suspend fun cancelMatchWaitingEvent(): Result<CancelMatch> {
return matchDataSource
.cancelMatchWaitingEvent()
.toResultModel { it.toDomainModel() }
}

override fun createMatchEventSourceListener(
onEvent: (data: String) -> Unit,
onClosed: () -> Unit,
onFailure: () -> Unit
onFailure: () -> Unit,
): EventSourceListener {
return matchDataSource.createMatchEventSourceListener(onEvent, onClosed, onFailure)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ interface MemberRepository {

val userData: Flow<User>

suspend fun getRunnersInfo(runnerIds: RunnerIds): Flow<Result<RunnerInfoData>>
suspend fun getRunnersInfo(runnerIds: RunnerIds): Result<RunnerInfoData>

suspend fun getUserData(): Flow<Result<User>>
suspend fun getUserData(): Result<User>

suspend fun deleteAccount(): Flow<Result<DeleteAccount>>
suspend fun deleteAccount(): Result<DeleteAccount>

suspend fun setUserName(userName: String)

suspend fun setUserProfile(userProfile: String)

suspend fun setUserId(userId: String)

suspend fun updateUserData(userData: User): Flow<Result<Unit>>
suspend fun updateUserData(userData: User): Result<Unit>

suspend fun updateProfileImage(requestBody: RequestBody, fileName: String?): Flow<Result<Unit>>
suspend fun updateProfileImage(requestBody: RequestBody, fileName: String?): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package online.partyrun.partyrunapplication.core.data.repository
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import okhttp3.RequestBody
import online.partyrun.partyrunapplication.core.common.network.apiRequestFlow
import online.partyrun.partyrunapplication.core.datastore.datasource.UserPreferencesDataSource
import online.partyrun.partyrunapplication.core.model.match.RunnerIds
import online.partyrun.partyrunapplication.core.model.match.RunnerInfoData
Expand All @@ -12,31 +11,34 @@ import online.partyrun.partyrunapplication.core.model.user.User
import online.partyrun.partyrunapplication.core.network.datasource.MemberDataSource
import online.partyrun.partyrunapplication.core.network.model.response.toDomainModel
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.common.result.mapResultModel
import online.partyrun.partyrunapplication.core.common.result.toResultModel
import online.partyrun.partyrunapplication.core.network.model.request.toRequestModel
import javax.inject.Inject

class MemberRepositoryImpl @Inject constructor(
private val userPreferencesDataSource: UserPreferencesDataSource,
private val memberDataSource: MemberDataSource
private val memberDataSource: MemberDataSource,
) : MemberRepository {

override val userData: Flow<User> =
userPreferencesDataSource.userData

override suspend fun getRunnersInfo(runnerIds: RunnerIds): Flow<Result<RunnerInfoData>> {
return apiRequestFlow { memberDataSource.getRunnersInfo(runnerIds.runnerIds) } // runnerIds를 List<String>으로 변환하고 쿼리스트링 전달
.mapResultModel { it.toDomainModel() }
override suspend fun getRunnersInfo(runnerIds: RunnerIds): Result<RunnerInfoData> {
return memberDataSource
.getRunnersInfo(runnerIds.runnerIds) // runnerIds를 List<String>으로 변환하고 쿼리스트링 전달
.toResultModel { it.toDomainModel() }
}

override suspend fun getUserData(): Flow<Result<User>> {
return apiRequestFlow { memberDataSource.getUserData() }
.mapResultModel { it.toDomainModel() }
override suspend fun getUserData(): Result<User> {
return memberDataSource
.getUserData()
.toResultModel { it.toDomainModel() }
}

override suspend fun deleteAccount(): Flow<Result<DeleteAccount>> {
return apiRequestFlow { memberDataSource.deleteAccount() }
.mapResultModel { it.toDomainModel() }
override suspend fun deleteAccount(): Result<DeleteAccount> {
return memberDataSource
.deleteAccount()
.toResultModel { it.toDomainModel() }
}


Expand All @@ -52,27 +54,26 @@ class MemberRepositoryImpl @Inject constructor(
userPreferencesDataSource.setUserId(userId)
}

override suspend fun updateUserData(userData: User): Flow<Result<Unit>> {
return apiRequestFlow { memberDataSource.updateUserData(userData.toRequestModel()) }
override suspend fun updateUserData(userData: User): Result<Unit> {
return memberDataSource.updateUserData(userData.toRequestModel()).toResultModel { }
}

override suspend fun updateProfileImage(
requestBody: RequestBody,
fileName: String?
): Flow<Result<Unit>> {
return apiRequestFlow {
memberDataSource.updateProfileImage(
fileName: String?,
): Result<Unit> {
return memberDataSource
.updateProfileImage(
createMultipartBodyPart(
fileName,
requestBody
)
)
}
).toResultModel { }
}

private fun createMultipartBodyPart(
fileName: String?,
requestBody: RequestBody
requestBody: RequestBody,
): MultipartBody.Part {
return MultipartBody.Part.createFormData("profile", fileName, requestBody)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package online.partyrun.partyrunapplication.core.data.repository

import kotlinx.coroutines.flow.Flow
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import online.partyrun.partyrunapplication.core.common.result.Result
Expand All @@ -9,15 +8,15 @@ import online.partyrun.partyrunapplication.core.model.party.PartyCode

interface PartyRepository {
/* REST */
suspend fun createParty(runningDistance: RunningDistance): Flow<Result<PartyCode>>
suspend fun startPartyBattle(code: String): Flow<Result<Unit>>
suspend fun quitParty(code: String): Flow<Result<Unit>>
suspend fun createParty(runningDistance: RunningDistance): Result<PartyCode>
suspend fun startPartyBattle(code: String): Result<Unit>
suspend fun quitParty(code: String): Result<Unit>

/* SSE */
fun createPartyEventSourceListener(
onEvent: (data: String) -> Unit,
onClosed: () -> Unit,
onFailure: () -> Unit
onFailure: () -> Unit,
): EventSourceListener

fun createPartyEventSource(listener: EventSourceListener, code: String): EventSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package online.partyrun.partyrunapplication.core.data.repository

import kotlinx.coroutines.flow.Flow
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import online.partyrun.partyrunapplication.core.common.Constants.BASE_URL
import online.partyrun.partyrunapplication.core.common.network.apiRequestFlow
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.common.result.mapResultModel
import online.partyrun.partyrunapplication.core.common.result.toResultModel
import online.partyrun.partyrunapplication.core.model.match.RunningDistance
import online.partyrun.partyrunapplication.core.model.party.PartyCode
import online.partyrun.partyrunapplication.core.network.datasource.PartyDataSource
Expand All @@ -15,27 +13,27 @@ import online.partyrun.partyrunapplication.core.network.model.response.toDomainM
import javax.inject.Inject

class PartyRepositoryImpl @Inject constructor(
private val partyDataSource: PartyDataSource
private val partyDataSource: PartyDataSource,
) : PartyRepository {

override suspend fun createParty(runningDistance: RunningDistance): Flow<Result<PartyCode>> {
return apiRequestFlow {
partyDataSource.createParty(runningDistance.toRequestModel())
}.mapResultModel { it.toDomainModel() }
override suspend fun createParty(runningDistance: RunningDistance): Result<PartyCode> {
return partyDataSource
.createParty(runningDistance.toRequestModel())
.toResultModel { it.toDomainModel() }
}

override suspend fun startPartyBattle(code: String): Flow<Result<Unit>> {
return apiRequestFlow { partyDataSource.startPartyBattle(code) }
override suspend fun startPartyBattle(code: String): Result<Unit> {
return partyDataSource.startPartyBattle(code).toResultModel { }
}

override suspend fun quitParty(code: String): Flow<Result<Unit>> {
return apiRequestFlow { partyDataSource.quitParty(code) }
override suspend fun quitParty(code: String): Result<Unit> {
return partyDataSource.quitParty(code).toResultModel { }
}

override fun createPartyEventSourceListener(
onEvent: (data: String) -> Unit,
onClosed: () -> Unit,
onFailure: () -> Unit
onFailure: () -> Unit,
): EventSourceListener {
return partyDataSource.createPartyEventSourceListener(onEvent, onClosed, onFailure)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import online.partyrun.partyrunapplication.core.model.my_page.SingleRunningHisto
import online.partyrun.partyrunapplication.core.model.running_result.single.SingleResult

interface ResultRepository {
suspend fun getBattleResults(): Flow<Result<BattleResult>>
suspend fun getSingleResults(): Flow<Result<SingleResult>>
suspend fun getComprehensiveRunRecord(): Flow<Result<ComprehensiveRunRecord>>
suspend fun updateSingleHistory(): Flow<Result<Unit>>
suspend fun updateBattleHistory(): Flow<Result<Unit>>
suspend fun getSingleHistory(): Flow<Result<SingleRunningHistory>>
suspend fun getBattleHistory(): Flow<Result<BattleRunningHistory>>
suspend fun deleteAllHistories(): Flow<Result<Unit>>
suspend fun getBattleResults(): Result<BattleResult>
suspend fun getSingleResults(): Result<SingleResult>
suspend fun getComprehensiveRunRecord(): Result<ComprehensiveRunRecord>
fun updateSingleHistory(): Flow<Result<Unit>>
fun updateBattleHistory(): Flow<Result<Unit>>
fun getSingleHistory(): Flow<Result<SingleRunningHistory>>
fun getBattleHistory(): Flow<Result<BattleRunningHistory>>
fun deleteAllHistories(): Flow<Result<Unit>>

}
Loading

0 comments on commit a2c322b

Please sign in to comment.