|
1 | 1 | package com.whyranoid.data.groupnotification
|
2 | 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 | 3 | import com.whyranoid.domain.model.GroupNotification
|
13 | 4 | 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 | 5 | 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 | 6 |
|
26 |
| -class GroupNotificationDataSource @Inject constructor( |
27 |
| - private val db: FirebaseFirestore |
28 |
| -) { |
| 7 | +interface GroupNotificationDataSource { |
29 | 8 |
|
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>> |
34 | 10 |
|
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>) |
75 | 12 |
|
76 | 13 | suspend fun notifyRunningFinish(
|
77 | 14 | uid: String,
|
78 | 15 | runningHistory: RunningHistory,
|
79 | 16 | 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 | + ) |
131 | 18 | }
|
0 commit comments