Skip to content

Commit

Permalink
Merge pull request #227 from SWM-KAWAI-MANS/PAR-382
Browse files Browse the repository at this point in the history
#PAR-382 : 싱글, 대결모드 조회 API Combine 처리 + Shimmer Effect 적용
  • Loading branch information
nohjunh authored Oct 12, 2023
2 parents 4e857e2 + ed805fa commit 34a2dd1
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ fun SetUpMainNavGraph(
}
}
},
navigateToBattleResult = { isFromMyPage ->
navController.navigate("${MainNavRoutes.BattleResult.route}?isFromMyPage=$isFromMyPage") {
popUpTo(MainNavRoutes.MyPage.route) {
inclusive = false
}
}
},
onShowSnackbar = onShowSnackbar
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package online.partyrun.partyrunapplication.core.data.repository
import kotlinx.coroutines.flow.Flow
import online.partyrun.partyrunapplication.core.model.running_result.battle.BattleResult
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.model.my_page.BattleRunningHistory
import online.partyrun.partyrunapplication.core.model.my_page.ComprehensiveRunRecord
import online.partyrun.partyrunapplication.core.model.my_page.SingleRunningHistory
import online.partyrun.partyrunapplication.core.model.running_result.single.SingleResult
Expand All @@ -12,4 +13,6 @@ interface ResultRepository {
suspend fun getSingleResults(): Flow<Result<SingleResult>>
suspend fun getComprehensiveRunRecord(): Flow<Result<ComprehensiveRunRecord>>
suspend fun getSingleHistory(): Flow<Result<SingleRunningHistory>>
suspend fun getBattleHistory(): Flow<Result<BattleRunningHistory>>

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import online.partyrun.partyrunapplication.core.network.model.response.toDomainM
import online.partyrun.partyrunapplication.core.common.result.Result
import online.partyrun.partyrunapplication.core.common.result.mapResultModel
import online.partyrun.partyrunapplication.core.datastore.datasource.SinglePreferencesDataSource
import online.partyrun.partyrunapplication.core.model.my_page.BattleRunningHistory
import online.partyrun.partyrunapplication.core.model.my_page.ComprehensiveRunRecord
import online.partyrun.partyrunapplication.core.model.my_page.SingleRunningHistory
import online.partyrun.partyrunapplication.core.model.running_result.single.SingleResult
Expand Down Expand Up @@ -47,4 +48,10 @@ class ResultRepositoryImpl @Inject constructor(
}.mapResultModel { it.toDomainModel() }
}

override suspend fun getBattleHistory(): Flow<Result<BattleRunningHistory>> {
return apiRequestFlow {
resultDataSource.getBattleHistory()
}.mapResultModel { it.toDomainModel() }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package online.partyrun.partyrunapplication.core.domain.my_page

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import online.partyrun.partyrunapplication.core.data.repository.ResultRepository
import online.partyrun.partyrunapplication.core.model.my_page.CombinedRunningHistory
import online.partyrun.partyrunapplication.core.common.result.Result
import javax.inject.Inject

class GetRunningHistoryUseCase @Inject constructor(
private val resultRepository: ResultRepository
) {
suspend operator fun invoke(): Flow<Result<CombinedRunningHistory>> {
return combine(
resultRepository.getSingleHistory(),
resultRepository.getBattleHistory()
) { single, battle ->
when {
single is Result.Loading || battle is Result.Loading -> Result.Loading
single is Result.Success && battle is Result.Success ->
Result.Success(CombinedRunningHistory(single.data, battle.data))

else -> Result.Failure("Failed to fetch combined running history", code = -1)
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package online.partyrun.partyrunapplication.core.model.my_page

data class BattleRunningHistory(
val history: List<RunningHistoryDetail>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package online.partyrun.partyrunapplication.core.model.my_page

data class CombinedRunningHistory(
val singleRunningHistory: SingleRunningHistory = SingleRunningHistory(emptyList()),
val battleRunningHistory: BattleRunningHistory = BattleRunningHistory(emptyList())
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ fun NavGraphBuilder.myPageRoute(
navigateToMyPage: () -> Unit,
navigateToProfile: () -> Unit,
navigateToSingleResult: (Boolean) -> Unit,
navigateToBattleResult: (Boolean) -> Unit,
onShowSnackbar: (String) -> Unit
) {
composable(route = MainNavRoutes.MyPage.route) {
MyPageScreen(
navigateToSettings = navigateToSettings,
navigateToProfile = navigateToProfile,
navigateToSingleResult = navigateToSingleResult,
navigateToBattleResult = navigateToBattleResult,
onShowSnackbar = onShowSnackbar
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ fun NavGraphBuilder.runningResultRoute(
navigateToTopLevel: () -> Unit,
navigateToBack: () -> Unit
) {
composable(route = MainNavRoutes.BattleResult.route) {
composable(
route = "${MainNavRoutes.BattleResult.route}?isFromMyPage={isFromMyPage}",
arguments = listOf(
navArgument("isFromMyPage") {
type = NavType.BoolType
defaultValue = false
}
)
) { backStackEntry ->
val isFromMyPage = backStackEntry.arguments?.getBoolean("isFromMyPage") ?: false
BattleResultScreen(
isFromMyPage = isFromMyPage,
navigateToTopLevel = navigateToTopLevel,
navigateToBack = navigateToBack
)
}

composable(
route = "${MainNavRoutes.SingleResult.route}?isFromMyPage={isFromMyPage}",
arguments = listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package online.partyrun.partyrunapplication.core.network.datasource

import online.partyrun.partyrunapplication.core.common.network.ApiResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleHistoryResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleResultResponse
import online.partyrun.partyrunapplication.core.network.model.response.ComprehensiveRunRecordResponse
import online.partyrun.partyrunapplication.core.network.model.response.SingleHistoryResponse
Expand All @@ -11,4 +12,5 @@ interface ResultDataSource {
suspend fun getSingleResults(singleId: String): ApiResponse<SingleResultResponse>
suspend fun getComprehensiveRunRecord(): ApiResponse<ComprehensiveRunRecordResponse>
suspend fun getSingleHistory(): ApiResponse<SingleHistoryResponse>
suspend fun getBattleHistory(): ApiResponse<BattleHistoryResponse>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package online.partyrun.partyrunapplication.core.network.datasource

import online.partyrun.partyrunapplication.core.common.network.ApiResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleHistoryResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleResultResponse
import online.partyrun.partyrunapplication.core.network.model.response.ComprehensiveRunRecordResponse
import online.partyrun.partyrunapplication.core.network.model.response.SingleHistoryResponse
Expand All @@ -23,4 +24,7 @@ class ResultDataSourceImpl @Inject constructor(
override suspend fun getSingleHistory(): ApiResponse<SingleHistoryResponse> =
resultApi.getSingleHistory()

override suspend fun getBattleHistory(): ApiResponse<BattleHistoryResponse> =
resultApi.getBattleHistory()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package online.partyrun.partyrunapplication.core.network.model.response

import com.google.gson.annotations.SerializedName
import online.partyrun.partyrunapplication.core.model.my_page.BattleRunningHistory

data class BattleHistoryResponse(
@SerializedName("history")
val history: List<RunningHistoryDetailResponse>?
)

fun BattleHistoryResponse.toDomainModel(): BattleRunningHistory {
val transformedHistory = history?.map { it.toDomainModel() }?.reversed() ?: emptyList()
return BattleRunningHistory(history = transformedHistory)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import online.partyrun.partyrunapplication.core.network.model.util.formatDate
import online.partyrun.partyrunapplication.core.network.model.util.formatDistanceWithComma
import java.time.LocalDateTime

data class SingleHistoryDetailResponse(
data class RunningHistoryDetailResponse(
@SerializedName("id")
val id: String?,
@SerializedName("startTime")
Expand All @@ -20,7 +20,7 @@ data class SingleHistoryDetailResponse(
val distance: Double?
)

fun SingleHistoryDetailResponse.toDomainModel(): RunningHistoryDetail {
fun RunningHistoryDetailResponse.toDomainModel(): RunningHistoryDetail {
val parsedDate = startTime?.let {
LocalDateTime.parse(
it,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import online.partyrun.partyrunapplication.core.model.my_page.SingleRunningHisto

data class SingleHistoryResponse(
@SerializedName("history")
val history: List<SingleHistoryDetailResponse>?
val history: List<RunningHistoryDetailResponse>?
)

fun SingleHistoryResponse.toDomainModel(): SingleRunningHistory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package online.partyrun.partyrunapplication.core.network.service

import online.partyrun.partyrunapplication.core.common.network.ApiResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleHistoryResponse
import online.partyrun.partyrunapplication.core.network.model.response.BattleResultResponse
import online.partyrun.partyrunapplication.core.network.model.response.ComprehensiveRunRecordResponse
import online.partyrun.partyrunapplication.core.network.model.response.SingleHistoryResponse
Expand All @@ -26,4 +27,7 @@ interface ResultApiService {
@GET("/api/mypage/singles")
suspend fun getSingleHistory(): ApiResponse<SingleHistoryResponse>

@GET("/api/mypage/battles")
suspend fun getBattleHistory(): ApiResponse<BattleHistoryResponse>

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import online.partyrun.partyrunapplication.core.designsystem.component.PartyRunGradientRoundedRect
import online.partyrun.partyrunapplication.core.designsystem.component.PartyRunTopAppBar
import online.partyrun.partyrunapplication.core.designsystem.icon.PartyRunIcons
import online.partyrun.partyrunapplication.core.model.my_page.BattleRunningHistory
import online.partyrun.partyrunapplication.core.model.my_page.SingleRunningHistory
import online.partyrun.partyrunapplication.core.model.user.User
import online.partyrun.partyrunapplication.core.ui.ProfileSection
Expand All @@ -57,6 +58,7 @@ fun MyPageScreen(
navigateToSettings: () -> Unit = {},
navigateToProfile: () -> Unit = {},
navigateToSingleResult: (Boolean) -> Unit = {},
navigateToBattleResult: (Boolean) -> Unit = {},
onShowSnackbar: (String) -> Unit
) {
val myPageProfileState by myPageViewModel.myPageProfileState.collectAsStateWithLifecycle()
Expand All @@ -68,6 +70,7 @@ fun MyPageScreen(
navigateToSettings = navigateToSettings,
navigateToProfile = navigateToProfile,
navigateToSingleResult = navigateToSingleResult,
navigateToBattleResult = navigateToBattleResult,
onShowSnackbar = onShowSnackbar,
myPageSnackbarMessage = myPageSnackbarMessage
)
Expand All @@ -82,6 +85,7 @@ fun Content(
navigateToSettings: () -> Unit,
navigateToProfile: () -> Unit,
navigateToSingleResult: (Boolean) -> Unit,
navigateToBattleResult: (Boolean) -> Unit,
onShowSnackbar: (String) -> Unit,
myPageSnackbarMessage: String
) {
Expand Down Expand Up @@ -112,7 +116,8 @@ fun Content(
userData = myPageProfileState.user,
myPageViewModel = myPageViewModel,
navigateToProfile = navigateToProfile,
navigateToSingleResult = navigateToSingleResult
navigateToSingleResult = navigateToSingleResult,
navigateToBattleResult = navigateToBattleResult
)

is MyPageProfileState.LoadFailed -> LoadingBody()
Expand Down Expand Up @@ -161,20 +166,19 @@ private fun MyPageBody(
userData: User,
myPageViewModel: MyPageViewModel,
navigateToProfile: () -> Unit,
navigateToSingleResult: (Boolean) -> Unit
navigateToSingleResult: (Boolean) -> Unit,
navigateToBattleResult: (Boolean) -> Unit
) {
LaunchedEffect(Unit) {
myPageViewModel.getComprehensiveRunRecord()
myPageViewModel.getSingleRunningHistory()
myPageViewModel.getRunningHistory()
}

LaunchedEffect(Unit) {// 러닝 기록 상세 보기 클릭
myPageViewModel.saveIdCompleteEvent.collect { modeType ->
when (modeType) {
ModeType.SINGLE -> navigateToSingleResult(true) // MyPage로부터 Result로의 이동은 true
else -> {
// Battle
}
ModeType.BATTLE -> navigateToBattleResult(true)
}
}
}
Expand Down Expand Up @@ -214,21 +218,10 @@ private fun MyPageBody(
)

Spacer(modifier = Modifier.height(30.dp))
Column(
modifier = Modifier
.fillMaxWidth()
.padding(start = 20.dp)
) {
Text(
text = stringResource(id = R.string.battle_title),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onPrimary,
)
Spacer(modifier = Modifier.height(30.dp))

EmptyRunningHistory()

}
BattleRunningHistoryRow(
runningHistoryState = runningHistoryState,
myPageViewModel = myPageViewModel
)

Spacer(modifier = Modifier.height(80.dp))
}
Expand All @@ -242,11 +235,27 @@ private fun SingleRunningHistoryRow(
when (runningHistoryState) {
is RunningHistoryState.Loading -> ShimmerRunningHistory(isSingleData = true)
is RunningHistoryState.Success -> SingleRunningHistoryBody(
singleRunningHistory = runningHistoryState.singleRunningHistory,
singleRunningHistory = runningHistoryState.combinedRunningHistory.singleRunningHistory,
myPageViewModel = myPageViewModel
)

RunningHistoryState.LoadFailed -> ShimmerRunningHistory(isSingleData = true)
RunningHistoryState.LoadFailed -> EmptyRunningHistory()
}
}

@Composable
private fun BattleRunningHistoryRow(
runningHistoryState: RunningHistoryState,
myPageViewModel: MyPageViewModel
) {
when (runningHistoryState) {
is RunningHistoryState.Loading -> ShimmerRunningHistory(isSingleData = false)
is RunningHistoryState.Success -> BattleRunningHistoryBody(
battleRunningHistory = runningHistoryState.combinedRunningHistory.battleRunningHistory,
myPageViewModel = myPageViewModel
)

RunningHistoryState.LoadFailed -> EmptyRunningHistory()
}
}

Expand Down Expand Up @@ -286,6 +295,42 @@ private fun SingleRunningHistoryBody(
}
}

@Composable
private fun BattleRunningHistoryBody(
battleRunningHistory: BattleRunningHistory,
myPageViewModel: MyPageViewModel
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(start = 20.dp)
) {
Text(
text = stringResource(id = R.string.battle_title),
style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onPrimary,
)
Spacer(modifier = Modifier.height(30.dp))

if (battleRunningHistory.history.isEmpty()) {
EmptyRunningHistory()
} else {
LazyRow(
horizontalArrangement = Arrangement.spacedBy(20.dp)
) {
items(battleRunningHistory.history) { data ->
RunningDataCard(
runningHistoryDetail = data,
isSingleData = false
) {
myPageViewModel.saveBattleId(data.id)
}
}
}
}
}
}

@Composable
private fun ComprehensiveRunRecordRect(
myPageComprehensiveRunRecordState: MyPageComprehensiveRunRecordState
Expand Down
Loading

0 comments on commit 34a2dd1

Please sign in to comment.