Skip to content

Commit 2650104

Browse files
committed
✨ RunningHistoryRemoteDataSource 구현
1 parent d34e8cd commit 2650104

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
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+
}
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+
)

0 commit comments

Comments
 (0)