Skip to content

Commit af2f6ff

Browse files
authored
πŸ”€ #62 from boostcampwm-2022/feat/running_tab
λŸ¬λ‹ νžˆμŠ€ν† λ¦¬ μ„œλ²„ μ—…λ‘œλ“œ κ΅¬ν˜„
2 parents d34e8cd + c5b1d20 commit af2f6ff

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.whyranoid.data.account
2+
3+
import com.whyranoid.domain.model.RunningHistory
4+
5+
interface RunningHistoryRemoteDataSource {
6+
7+
suspend fun uploadRunningHistory(runningHistory: RunningHistory): Result<Boolean>
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.whyranoid.data.account
2+
3+
import com.google.firebase.firestore.FirebaseFirestore
4+
import com.whyranoid.data.constant.CollectionId
5+
import com.whyranoid.data.model.toRunningHistoryResponse
6+
import com.whyranoid.domain.model.RunningHistory
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.tasks.await
9+
import kotlinx.coroutines.withContext
10+
import javax.inject.Inject
11+
12+
class RunningHistoryRemoteDataSourceImpl @Inject constructor(private val firebaseDB: FirebaseFirestore) :
13+
RunningHistoryRemoteDataSource {
14+
override suspend fun uploadRunningHistory(runningHistory: RunningHistory): Result<Boolean> {
15+
val runningHistoryResponse = runningHistory.toRunningHistoryResponse()
16+
var uploadSuccess = false
17+
18+
return runCatching {
19+
withContext(Dispatchers.IO) {
20+
val task = firebaseDB.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
21+
.document(runningHistoryResponse.historyId)
22+
.set(runningHistoryResponse)
23+
.addOnSuccessListener {
24+
uploadSuccess = true
25+
}
26+
.addOnFailureListener { exception ->
27+
throw exception
28+
}
29+
30+
task.await()
31+
}
32+
uploadSuccess
33+
}
34+
}
35+
}

β€Ždata/src/main/java/com/whyranoid/data/account/RunningHistoryRepositoryImpl.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import kotlinx.coroutines.flow.Flow
77
import javax.inject.Inject
88

99
class RunningHistoryRepositoryImpl @Inject constructor(
10-
private val runningHistoryLocalDataSource: RunningHistoryLocalDataSource
10+
private val runningHistoryLocalDataSource: RunningHistoryLocalDataSource,
11+
private val runningHistoryRemoteDataSource: RunningHistoryRemoteDataSource
1112
) : RunningHistoryRepository {
1213
override fun getRunningHistory(): Flow<Result<List<RunningHistory>>> {
1314
return runningHistoryLocalDataSource.getRunningHistory()
@@ -36,4 +37,8 @@ class RunningHistoryRepositoryImpl @Inject constructor(
3637
)
3738
)
3839
}
40+
41+
override suspend fun uploadRunningHistory(runningHistory: RunningHistory): Result<Boolean> {
42+
return runningHistoryRemoteDataSource.uploadRunningHistory(runningHistory)
43+
}
3944
}

β€Ždata/src/main/java/com/whyranoid/data/di/AccountModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.whyranoid.data.account.AccountDataSourceImpl
55
import com.whyranoid.data.account.AccountRepositoryImpl
66
import com.whyranoid.data.account.RunningHistoryLocalDataSource
77
import com.whyranoid.data.account.RunningHistoryLocalDataSourceImpl
8+
import com.whyranoid.data.account.RunningHistoryRemoteDataSource
9+
import com.whyranoid.data.account.RunningHistoryRemoteDataSourceImpl
810
import com.whyranoid.data.account.RunningHistoryRepositoryImpl
911
import com.whyranoid.domain.repository.AccountRepository
1012
import com.whyranoid.domain.repository.RunningHistoryRepository
@@ -28,4 +30,7 @@ abstract class AccountModule {
2830

2931
@Binds
3032
abstract fun provideRunningHistoryDataSource(runningHistoryLocalDataSourceImpl: RunningHistoryLocalDataSourceImpl): RunningHistoryLocalDataSource
33+
34+
@Binds
35+
abstract fun bindRunningHistoryRemoteDataSource(runningHistoryRemoteDataSourceImpl: RunningHistoryRemoteDataSourceImpl): RunningHistoryRemoteDataSource
3136
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.whyranoid.data.model
2+
3+
import com.whyranoid.domain.model.RunningHistory
4+
5+
data class RunningHistoryResponse(
6+
val historyId: String = "",
7+
val startedAt: Long = 0L,
8+
val finishedAt: Long = 0L,
9+
val totalRunningTime: Int = 0,
10+
val pace: Double = 0.0,
11+
val totalDistance: Double = 0.0
12+
)
13+
14+
fun RunningHistory.toRunningHistoryResponse() =
15+
RunningHistoryResponse(
16+
historyId = historyId,
17+
startedAt = startedAt,
18+
finishedAt = finishedAt,
19+
totalRunningTime = totalRunningTime,
20+
pace = pace,
21+
totalDistance = totalDistance
22+
)

β€Ždomain/src/main/java/com/whyranoid/domain/repository/RunningHistoryRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ interface RunningHistoryRepository {
2020
pace: Double,
2121
totalDistance: Double
2222
): Result<RunningHistory>
23+
24+
suspend fun uploadRunningHistory(runningHistory: RunningHistory): Result<Boolean>
2325
}

β€Ždomain/src/main/java/com/whyranoid/domain/usecase/FinishRunningUseCase.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ import com.whyranoid.domain.model.RunningHistory
44
import com.whyranoid.domain.repository.AccountRepository
55
import com.whyranoid.domain.repository.GroupRepository
66
import com.whyranoid.domain.repository.RunnerRepository
7+
import com.whyranoid.domain.repository.RunningHistoryRepository
78
import javax.inject.Inject
89

910
class FinishRunningUseCase @Inject constructor(
1011
private val runnerRepository: RunnerRepository,
1112
private val accountRepository: AccountRepository,
12-
private val groupRepository: GroupRepository
13+
private val groupRepository: GroupRepository,
14+
private val runningHistoryRepository: RunningHistoryRepository
1315
) {
1416
suspend operator fun invoke(runningHistory: RunningHistory? = null): Boolean {
1517
val uid = accountRepository.getUid()
1618

1719
runnerRepository.finishRunning(uid)
1820

1921
if (runningHistory != null) {
22+
val uploadResult = runningHistoryRepository.uploadRunningHistory(runningHistory)
23+
24+
if (uploadResult.isFailure) {
25+
return false
26+
}
27+
2028
groupRepository.getMyGroupList(uid).onSuccess { groupInfos ->
2129
groupRepository.notifyRunningFinish(
2230
uid = uid,

0 commit comments

Comments
Β (0)