@@ -14,14 +14,16 @@ import com.whyranoid.data.model.UserResponse
14
14
import com.whyranoid.data.model.toGroupInfo
15
15
import com.whyranoid.data.model.toUser
16
16
import com.whyranoid.domain.model.GroupInfo
17
+ import com.whyranoid.domain.model.MoGakRunException
17
18
import com.whyranoid.domain.model.Rule
18
19
import com.whyranoid.domain.model.toRule
19
20
import kotlinx.coroutines.channels.awaitClose
20
21
import kotlinx.coroutines.flow.Flow
21
22
import kotlinx.coroutines.flow.callbackFlow
23
+ import kotlinx.coroutines.flow.combine
22
24
import kotlinx.coroutines.suspendCancellableCoroutine
23
25
import kotlinx.coroutines.tasks.await
24
- import java.util.UUID
26
+ import java.util.*
25
27
import javax.inject.Inject
26
28
import kotlin.coroutines.resume
27
29
@@ -165,32 +167,41 @@ class GroupDataSourceImpl @Inject constructor(
165
167
}
166
168
}
167
169
168
- override fun getGroupInfoFlow (uid : String , groupId : String ): Flow <GroupInfo > = callbackFlow {
169
- db.collection(GROUPS_COLLECTION )
170
- .document(groupId)
171
- .addSnapshotListener { documentSnapshot, _ ->
172
- val groupInfoResponse = documentSnapshot?.toObject(GroupInfoResponse ::class .java)
170
+ override fun getGroupInfoFlow (uid : String , groupId : String ): Flow <GroupInfo > {
171
+ return getUserResponse(uid).combine(getGroupInfoResponse(groupId)) { userResponse, groupInfoResponse ->
172
+ groupInfoResponse.toGroupInfo(
173
+ leader = userResponse.toUser(),
174
+ rules = groupInfoResponse.rules.map { it.toRule() }
175
+ )
176
+ }
177
+ }
173
178
174
- groupInfoResponse?.let {
175
- db.collection(USERS_COLLECTION )
176
- .document(uid)
177
- .addSnapshotListener { documentSnapshot, _ ->
178
- val userResponse = documentSnapshot?.toObject(UserResponse ::class .java)
179
+ private fun getGroupInfoResponse (groupId : String ): Flow <GroupInfoResponse > {
180
+ return callbackFlow {
181
+ db.collection(GROUPS_COLLECTION )
182
+ .document(groupId)
183
+ .addSnapshotListener { documentSnapshot, _ ->
184
+ trySend(
185
+ documentSnapshot?.toObject(GroupInfoResponse ::class .java)
186
+ ? : throw MoGakRunException .FileNotFoundedException
187
+ )
188
+ }
189
+ awaitClose()
190
+ }
191
+ }
179
192
180
- userResponse?.let {
181
- trySend(
182
- groupInfoResponse.toGroupInfo(
183
- leader = userResponse.toUser(),
184
- rules = groupInfoResponse.rules.map {
185
- it.toRule()
186
- }
187
- )
188
- )
189
- }
190
- }
193
+ private fun getUserResponse (uid : String ): Flow <UserResponse > {
194
+ return callbackFlow {
195
+ db.collection(USERS_COLLECTION )
196
+ .document(uid)
197
+ .addSnapshotListener { documentSnapshot, _ ->
198
+ trySend(
199
+ documentSnapshot?.toObject(UserResponse ::class .java)
200
+ ? : throw MoGakRunException .FileNotFoundedException
201
+ )
191
202
}
192
- }
193
- awaitClose()
203
+ awaitClose()
204
+ }
194
205
}
195
206
196
207
override suspend fun isDuplicatedGroupName (groupName : String ): Boolean {
0 commit comments