Skip to content

Commit 0cab66b

Browse files
committed
✨ GroupNotificationDataSource 추상화
1 parent 43781d2 commit 0cab66b

File tree

3 files changed

+140
-117
lines changed

3 files changed

+140
-117
lines changed

data/src/main/java/com/whyranoid/data/di/GroupModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.whyranoid.data.di
33
import com.whyranoid.data.group.GroupDataSource
44
import com.whyranoid.data.group.GroupDataSourceImpl
55
import com.whyranoid.data.group.GroupRepositoryImpl
6+
import com.whyranoid.data.groupnotification.GroupNotificationDataSource
7+
import com.whyranoid.data.groupnotification.GroupNotificationDataSourceImpl
68
import com.whyranoid.domain.repository.GroupRepository
79
import dagger.Binds
810
import dagger.Module
@@ -16,6 +18,9 @@ abstract class GroupModule {
1618
@Binds
1719
abstract fun bindGroupDataSource(groupDataSourceImpl: GroupDataSourceImpl): GroupDataSource
1820

21+
@Binds
22+
abstract fun bindGroupNotificationDataSource(groupNotificationDataSourceImpl: GroupNotificationDataSourceImpl): GroupNotificationDataSource
23+
1924
@Binds
2025
abstract fun bindGroupRepository(groupRepositoryImpl: GroupRepositoryImpl): GroupRepository
2126
}
Lines changed: 4 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,18 @@
11
package com.whyranoid.data.groupnotification
22

3-
import com.google.firebase.firestore.FirebaseFirestore
4-
import com.whyranoid.data.constant.CollectionId.FINISH_NOTIFICATION
5-
import com.whyranoid.data.constant.CollectionId.GROUP_NOTIFICATIONS_COLLECTION
6-
import com.whyranoid.data.constant.CollectionId.RUNNING_HISTORY_COLLECTION
7-
import com.whyranoid.data.constant.CollectionId.START_NOTIFICATION
8-
import com.whyranoid.data.model.FinishNotificationResponse
9-
import com.whyranoid.data.model.StartNotificationResponse
10-
import com.whyranoid.data.model.toStartNotification
11-
import com.whyranoid.domain.model.FinishNotification
123
import com.whyranoid.domain.model.GroupNotification
134
import com.whyranoid.domain.model.RunningHistory
14-
import com.whyranoid.domain.model.StartNotification
15-
import kotlinx.coroutines.Dispatchers
16-
import kotlinx.coroutines.FlowPreview
17-
import kotlinx.coroutines.channels.awaitClose
185
import kotlinx.coroutines.flow.Flow
19-
import kotlinx.coroutines.flow.callbackFlow
20-
import kotlinx.coroutines.flow.flattenMerge
21-
import kotlinx.coroutines.flow.flowOf
22-
import kotlinx.coroutines.withContext
23-
import java.util.UUID
24-
import javax.inject.Inject
256

26-
class GroupNotificationDataSource @Inject constructor(
27-
private val db: FirebaseFirestore
28-
) {
7+
interface GroupNotificationDataSource {
298

30-
@OptIn(FlowPreview::class)
31-
fun getGroupNotifications(groupId: String): Flow<List<GroupNotification>> {
32-
val startNotificationFlow = getGroupStartNotifications(groupId)
33-
val finishNotificationFlow = getGroupFinishNotifications(groupId)
9+
fun getGroupNotifications(groupId: String): Flow<List<GroupNotification>>
3410

35-
return flowOf(
36-
startNotificationFlow,
37-
finishNotificationFlow
38-
).flattenMerge()
39-
}
40-
41-
private fun getGroupStartNotifications(groupId: String): Flow<List<GroupNotification>> =
42-
callbackFlow {
43-
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
44-
.document(groupId)
45-
.collection(START_NOTIFICATION)
46-
.addSnapshotListener { snapshot, error ->
47-
val startNotificationList = mutableListOf<GroupNotification>()
48-
snapshot?.forEach { document ->
49-
val startNotification =
50-
document.toObject(StartNotificationResponse::class.java)
51-
startNotificationList.add(startNotification.toStartNotification())
52-
}
53-
trySend(startNotificationList)
54-
}
55-
56-
awaitClose()
57-
}
58-
59-
suspend fun notifyRunningStart(uid: String, groupIdList: List<String>) {
60-
withContext(Dispatchers.IO) {
61-
groupIdList.forEach { groupId ->
62-
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
63-
.document(groupId)
64-
.collection(START_NOTIFICATION)
65-
.document(UUID.randomUUID().toString())
66-
.set(
67-
StartNotification(
68-
startedAt = System.currentTimeMillis(),
69-
uid = uid
70-
)
71-
)
72-
}
73-
}
74-
}
11+
suspend fun notifyRunningStart(uid: String, groupIdList: List<String>)
7512

7613
suspend fun notifyRunningFinish(
7714
uid: String,
7815
runningHistory: RunningHistory,
7916
groupIdList: List<String>
80-
) {
81-
withContext(Dispatchers.IO) {
82-
groupIdList.forEach { groupId ->
83-
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
84-
.document(groupId)
85-
.collection(FINISH_NOTIFICATION)
86-
.document(UUID.randomUUID().toString())
87-
.set(
88-
FinishNotificationResponse(
89-
uid = uid,
90-
historyId = runningHistory.historyId
91-
)
92-
)
93-
}
94-
}
95-
}
96-
97-
private fun getGroupFinishNotifications(groupId: String): Flow<List<GroupNotification>> =
98-
callbackFlow {
99-
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
100-
.document(groupId)
101-
.collection(FINISH_NOTIFICATION)
102-
.addSnapshotListener { snapshot, error ->
103-
val finishNotificationList = mutableListOf<GroupNotification>()
104-
snapshot?.forEach { document ->
105-
val finishNotificationResponse =
106-
document.toObject(FinishNotificationResponse::class.java)
107-
108-
finishNotificationResponse.let { finishNotificationResponse ->
109-
db.collection(RUNNING_HISTORY_COLLECTION)
110-
.document(finishNotificationResponse.historyId)
111-
.get()
112-
.addOnSuccessListener {
113-
val runningHistory = it.toObject(RunningHistory::class.java)
114-
115-
runningHistory?.let {
116-
finishNotificationList.add(
117-
FinishNotification(
118-
uid = finishNotificationResponse.uid,
119-
runningHistory = runningHistory
120-
)
121-
)
122-
}
123-
trySend(finishNotificationList)
124-
}
125-
}
126-
}
127-
}
128-
129-
awaitClose()
130-
}
17+
)
13118
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.whyranoid.data.groupnotification
2+
3+
import com.google.firebase.firestore.FirebaseFirestore
4+
import com.whyranoid.data.constant.CollectionId.FINISH_NOTIFICATION
5+
import com.whyranoid.data.constant.CollectionId.GROUP_NOTIFICATIONS_COLLECTION
6+
import com.whyranoid.data.constant.CollectionId.RUNNING_HISTORY_COLLECTION
7+
import com.whyranoid.data.constant.CollectionId.START_NOTIFICATION
8+
import com.whyranoid.data.model.FinishNotificationResponse
9+
import com.whyranoid.data.model.StartNotificationResponse
10+
import com.whyranoid.data.model.toStartNotification
11+
import com.whyranoid.domain.model.FinishNotification
12+
import com.whyranoid.domain.model.GroupNotification
13+
import com.whyranoid.domain.model.RunningHistory
14+
import com.whyranoid.domain.model.StartNotification
15+
import kotlinx.coroutines.Dispatchers
16+
import kotlinx.coroutines.FlowPreview
17+
import kotlinx.coroutines.channels.awaitClose
18+
import kotlinx.coroutines.flow.Flow
19+
import kotlinx.coroutines.flow.callbackFlow
20+
import kotlinx.coroutines.flow.flattenMerge
21+
import kotlinx.coroutines.flow.flowOf
22+
import kotlinx.coroutines.withContext
23+
import java.util.UUID
24+
import javax.inject.Inject
25+
26+
class GroupNotificationDataSourceImpl @Inject constructor(
27+
private val db: FirebaseFirestore
28+
) : GroupNotificationDataSource {
29+
30+
@OptIn(FlowPreview::class)
31+
override fun getGroupNotifications(groupId: String): Flow<List<GroupNotification>> {
32+
val startNotificationFlow = getGroupStartNotifications(groupId)
33+
val finishNotificationFlow = getGroupFinishNotifications(groupId)
34+
35+
return flowOf(
36+
startNotificationFlow,
37+
finishNotificationFlow
38+
).flattenMerge()
39+
}
40+
41+
private fun getGroupStartNotifications(groupId: String): Flow<List<GroupNotification>> =
42+
callbackFlow {
43+
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
44+
.document(groupId)
45+
.collection(START_NOTIFICATION)
46+
.addSnapshotListener { snapshot, error ->
47+
val startNotificationList = mutableListOf<GroupNotification>()
48+
snapshot?.forEach { document ->
49+
val startNotification =
50+
document.toObject(StartNotificationResponse::class.java)
51+
startNotificationList.add(startNotification.toStartNotification())
52+
}
53+
trySend(startNotificationList)
54+
}
55+
56+
awaitClose()
57+
}
58+
59+
private fun getGroupFinishNotifications(groupId: String): Flow<List<GroupNotification>> =
60+
callbackFlow {
61+
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
62+
.document(groupId)
63+
.collection(FINISH_NOTIFICATION)
64+
.addSnapshotListener { snapshot, error ->
65+
val finishNotificationList = mutableListOf<GroupNotification>()
66+
snapshot?.forEach { document ->
67+
val finishNotificationResponse =
68+
document.toObject(FinishNotificationResponse::class.java)
69+
70+
finishNotificationResponse.let { finishNotificationResponse ->
71+
db.collection(RUNNING_HISTORY_COLLECTION)
72+
.document(finishNotificationResponse.historyId)
73+
.get()
74+
.addOnSuccessListener {
75+
val runningHistory = it.toObject(RunningHistory::class.java)
76+
77+
runningHistory?.let {
78+
finishNotificationList.add(
79+
FinishNotification(
80+
uid = finishNotificationResponse.uid,
81+
runningHistory = runningHistory
82+
)
83+
)
84+
}
85+
trySend(finishNotificationList)
86+
}
87+
}
88+
}
89+
}
90+
91+
awaitClose()
92+
}
93+
94+
override suspend fun notifyRunningStart(uid: String, groupIdList: List<String>) {
95+
withContext(Dispatchers.IO) {
96+
groupIdList.forEach { groupId ->
97+
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
98+
.document(groupId)
99+
.collection(START_NOTIFICATION)
100+
.document(UUID.randomUUID().toString())
101+
.set(
102+
StartNotification(
103+
startedAt = System.currentTimeMillis(),
104+
uid = uid
105+
)
106+
)
107+
}
108+
}
109+
}
110+
111+
override suspend fun notifyRunningFinish(
112+
uid: String,
113+
runningHistory: RunningHistory,
114+
groupIdList: List<String>
115+
) {
116+
withContext(Dispatchers.IO) {
117+
groupIdList.forEach { groupId ->
118+
db.collection(GROUP_NOTIFICATIONS_COLLECTION)
119+
.document(groupId)
120+
.collection(FINISH_NOTIFICATION)
121+
.document(UUID.randomUUID().toString())
122+
.set(
123+
FinishNotificationResponse(
124+
uid = uid,
125+
historyId = runningHistory.historyId
126+
)
127+
)
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)