@@ -14,6 +14,7 @@ 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
@@ -126,32 +127,44 @@ class GroupDataSource @Inject constructor(
126
127
}
127
128
}
128
129
129
- fun getGroupInfoFlow (uid : String , groupId : String ): Flow <GroupInfo > = callbackFlow {
130
- db.collection(GROUPS_COLLECTION )
131
- .document(groupId)
132
- .addSnapshotListener { documentSnapshot, _ ->
133
- val groupInfoResponse = documentSnapshot?.toObject(GroupInfoResponse ::class .java)
130
+ fun getGroupInfoFlow (uid : String , groupId : String ): Flow <GroupInfo > {
131
+ return callbackFlow {
132
+ val groupInfoResponse = getGroupInfoResponse(groupId)
133
+ val userResponse = getUserInfoResponse(uid)
134
+ val groupInfo = groupInfoResponse.toGroupInfo(
135
+ leader = userResponse.toUser(),
136
+ rules = groupInfoResponse.rules.map { it.toRule() }
137
+ )
138
+ trySend(groupInfo)
139
+ awaitClose()
140
+ }
141
+ }
134
142
135
- groupInfoResponse?.let {
136
- db.collection(USERS_COLLECTION )
137
- .document(uid)
138
- .addSnapshotListener { documentSnapshot, _ ->
139
- val userResponse = documentSnapshot?.toObject(UserResponse ::class .java)
143
+ private suspend fun getGroupInfoResponse (groupId : String ): GroupInfoResponse {
144
+ return suspendCancellableCoroutine { continuation ->
145
+ db.collection(GROUPS_COLLECTION )
146
+ .document(groupId)
147
+ .addSnapshotListener { documentSnapshot, _ ->
148
+ continuation.resume(
149
+ documentSnapshot?.toObject(GroupInfoResponse ::class .java)
150
+ ? : throw MoGakRunException .FileNotFoundedException
140
151
141
- userResponse?.let {
142
- trySend(
143
- groupInfoResponse.toGroupInfo(
144
- leader = userResponse.toUser(),
145
- rules = groupInfoResponse.rules.map {
146
- it.toRule()
147
- }
148
- )
149
- )
150
- }
151
- }
152
+ )
152
153
}
153
- }
154
- awaitClose()
154
+ }
155
+ }
156
+
157
+ private suspend fun getUserInfoResponse (uid : String ): UserResponse {
158
+ return suspendCancellableCoroutine { continuation ->
159
+ db.collection(USERS_COLLECTION )
160
+ .document(uid)
161
+ .addSnapshotListener { documentSnapshot, _ ->
162
+ continuation.resume(
163
+ documentSnapshot?.toObject(UserResponse ::class .java)
164
+ ? : throw MoGakRunException .FileNotFoundedException
165
+ )
166
+ }
167
+ }
155
168
}
156
169
157
170
suspend fun isDuplicatedGroupName (groupName : String ): Boolean {
0 commit comments