File tree Expand file tree Collapse file tree 7 files changed +87
-2
lines changed
data/src/main/java/com/whyranoid/data
domain/src/main/java/com/whyranoid/domain Expand file tree Collapse file tree 7 files changed +87
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -7,7 +7,8 @@ import kotlinx.coroutines.flow.Flow
7
7
import javax.inject.Inject
8
8
9
9
class RunningHistoryRepositoryImpl @Inject constructor(
10
- private val runningHistoryLocalDataSource : RunningHistoryLocalDataSource
10
+ private val runningHistoryLocalDataSource : RunningHistoryLocalDataSource ,
11
+ private val runningHistoryRemoteDataSource : RunningHistoryRemoteDataSource
11
12
) : RunningHistoryRepository {
12
13
override fun getRunningHistory (): Flow <Result <List <RunningHistory >>> {
13
14
return runningHistoryLocalDataSource.getRunningHistory()
@@ -36,4 +37,8 @@ class RunningHistoryRepositoryImpl @Inject constructor(
36
37
)
37
38
)
38
39
}
40
+
41
+ override suspend fun uploadRunningHistory (runningHistory : RunningHistory ): Result <Boolean > {
42
+ return runningHistoryRemoteDataSource.uploadRunningHistory(runningHistory)
43
+ }
39
44
}
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import com.whyranoid.data.account.AccountDataSourceImpl
5
5
import com.whyranoid.data.account.AccountRepositoryImpl
6
6
import com.whyranoid.data.account.RunningHistoryLocalDataSource
7
7
import com.whyranoid.data.account.RunningHistoryLocalDataSourceImpl
8
+ import com.whyranoid.data.account.RunningHistoryRemoteDataSource
9
+ import com.whyranoid.data.account.RunningHistoryRemoteDataSourceImpl
8
10
import com.whyranoid.data.account.RunningHistoryRepositoryImpl
9
11
import com.whyranoid.domain.repository.AccountRepository
10
12
import com.whyranoid.domain.repository.RunningHistoryRepository
@@ -28,4 +30,7 @@ abstract class AccountModule {
28
30
29
31
@Binds
30
32
abstract fun provideRunningHistoryDataSource (runningHistoryLocalDataSourceImpl : RunningHistoryLocalDataSourceImpl ): RunningHistoryLocalDataSource
33
+
34
+ @Binds
35
+ abstract fun bindRunningHistoryRemoteDataSource (runningHistoryRemoteDataSourceImpl : RunningHistoryRemoteDataSourceImpl ): RunningHistoryRemoteDataSource
31
36
}
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change @@ -20,4 +20,6 @@ interface RunningHistoryRepository {
20
20
pace : Double ,
21
21
totalDistance : Double
22
22
): Result <RunningHistory >
23
+
24
+ suspend fun uploadRunningHistory (runningHistory : RunningHistory ): Result <Boolean >
23
25
}
Original file line number Diff line number Diff line change @@ -4,19 +4,27 @@ import com.whyranoid.domain.model.RunningHistory
4
4
import com.whyranoid.domain.repository.AccountRepository
5
5
import com.whyranoid.domain.repository.GroupRepository
6
6
import com.whyranoid.domain.repository.RunnerRepository
7
+ import com.whyranoid.domain.repository.RunningHistoryRepository
7
8
import javax.inject.Inject
8
9
9
10
class FinishRunningUseCase @Inject constructor(
10
11
private val runnerRepository : RunnerRepository ,
11
12
private val accountRepository : AccountRepository ,
12
- private val groupRepository : GroupRepository
13
+ private val groupRepository : GroupRepository ,
14
+ private val runningHistoryRepository : RunningHistoryRepository
13
15
) {
14
16
suspend operator fun invoke (runningHistory : RunningHistory ? = null): Boolean {
15
17
val uid = accountRepository.getUid()
16
18
17
19
runnerRepository.finishRunning(uid)
18
20
19
21
if (runningHistory != null ) {
22
+ val uploadResult = runningHistoryRepository.uploadRunningHistory(runningHistory)
23
+
24
+ if (uploadResult.isFailure) {
25
+ return false
26
+ }
27
+
20
28
groupRepository.getMyGroupList(uid).onSuccess { groupInfos ->
21
29
groupRepository.notifyRunningFinish(
22
30
uid = uid,
You canβt perform that action at this time.
0 commit comments