@@ -13,7 +13,8 @@ public final class ViewModel {
13
13
var langSet : LanguageEntity . Response . LangSet ? = . none
14
14
var langList : [ LanguageEntity . Response . LanguageItem ] = [ ]
15
15
var roomInfo : ChatRoomEntity . Make . Response ? = . none
16
- var selectedLangCode : String = Locale . autoupdatingCurrent. language. languageCode? . identifier ?? " en "
16
+ var selectedLangCode : String =
17
+ Locale . autoupdatingCurrent. language. languageCode? . identifier ?? " en "
17
18
18
19
let service : LiveTranslationService = . init( )
19
20
@@ -99,7 +100,9 @@ extension ViewModel {
99
100
}
100
101
}
101
102
102
- package func loadTranslation( chatList: [ TranslationEntity . CompositeChatItem ] , _ dstLangCode: String ) async {
103
+ package func loadTranslation(
104
+ chatList: [ TranslationEntity . CompositeChatItem ] , _ dstLangCode: String
105
+ ) async {
103
106
await withTaskGroup ( of: Void . self) { [ weak self] group in
104
107
let chunkedArray = chatList. chunked ( into: 20 )
105
108
for array in chunkedArray {
@@ -140,8 +143,12 @@ extension ViewModel {
140
143
141
144
switch chatItem. contentData. listType {
142
145
case . update:
143
- let updateTargetList = chatItem. contentData. chatList. reduce ( [ TranslationEntity . CompositeChatItem] ( ) ) { current, next in
144
- guard let firstIndex = newChatList. firstIndex ( where: { $0. id == next. id } ) else { return current }
146
+ let updateTargetList = chatItem. contentData. chatList. reduce (
147
+ [ TranslationEntity . CompositeChatItem] ( )
148
+ ) { current, next in
149
+ guard let firstIndex = newChatList. firstIndex ( where: { $0. id == next. id } ) else {
150
+ return current
151
+ }
145
152
return current + [ newChatList [ firstIndex] ]
146
153
}
147
154
await loadTranslation ( chatList: updateTargetList, selectedLangCode)
@@ -188,7 +195,9 @@ extension ViewModel {
188
195
}
189
196
190
197
extension [ TranslationEntity . CompositeChatItem ] {
191
- fileprivate func merge( item: RealTimeEntity . Chat . Response , dstLangCode: String ) async -> [ TranslationEntity . CompositeChatItem ] {
198
+ fileprivate func merge( item: RealTimeEntity . Chat . Response , dstLangCode: String ) async
199
+ -> [ TranslationEntity . CompositeChatItem ]
200
+ {
192
201
await withCheckedContinuation { continuation in
193
202
switch item. contentData. listType {
194
203
case . append:
@@ -199,7 +208,8 @@ extension [TranslationEntity.CompositeChatItem] {
199
208
}
200
209
201
210
guard !( newItem. textForTR. isEmpty || newItem. text. isEmpty) else { continue }
202
- mutableSelf. append ( . init( item: newItem, trItem: . none, ttsData: . none, dstLangCode: dstLangCode) )
211
+ mutableSelf. append (
212
+ . init( item: newItem, trItem: . none, ttsData: . none, dstLangCode: dstLangCode) )
203
213
}
204
214
205
215
return continuation. resume ( returning: mutableSelf. suffix ( 100 ) )
@@ -211,15 +221,20 @@ extension [TranslationEntity.CompositeChatItem] {
211
221
mutableSelf. remove ( at: lastIdx)
212
222
}
213
223
214
- mutableSelf. append ( . init( item: newItem, trItem: . none, ttsData: . none, dstLangCode: dstLangCode) )
224
+ mutableSelf. append (
225
+ . init( item: newItem, trItem: . none, ttsData: . none, dstLangCode: dstLangCode) )
215
226
}
216
227
return continuation. resume ( returning: mutableSelf)
217
228
218
229
case . renew:
219
- let newArr : [ TranslationEntity . CompositeChatItem ] = item. contentData. chatList. reduce ( [ ] ) { current, next in
220
- guard !next. text. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty else { return current }
230
+ let newArr : [ TranslationEntity . CompositeChatItem ] = item. contentData. chatList. reduce ( [ ] ) {
231
+ current, next in
232
+ guard !next. text. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty else {
233
+ return current
234
+ }
221
235
let first = self . first ( where: { $0. item. id == next. id } )
222
- let new : TranslationEntity . CompositeChatItem = . init( item: next, trItem: first? . trItem, ttsData: first? . ttsData, dstLangCode: dstLangCode)
236
+ let new : TranslationEntity . CompositeChatItem = . init(
237
+ item: next, trItem: first? . trItem, ttsData: first? . ttsData, dstLangCode: dstLangCode)
223
238
224
239
return current + [ new]
225
240
}
@@ -245,8 +260,12 @@ extension [TranslationEntity.CompositeChatItem] {
245
260
return variableCurrent
246
261
}
247
262
// If the update target is not included in the current chat list (when modifying an empty chat)
248
- } else if let willAppendIndex = current. firstIndex ( where: { $0. item. timestamp > next. timestamp } ) {
249
- guard !next. text. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty else { return current }
263
+ } else if let willAppendIndex = current. firstIndex ( where: {
264
+ $0. item. timestamp > next. timestamp
265
+ } ) {
266
+ guard !next. text. trimmingCharacters ( in: . whitespacesAndNewlines) . isEmpty else {
267
+ return current
268
+ }
250
269
var variableCurrent = current
251
270
variableCurrent. insert (
252
271
. init( item: next, trItem: . none, ttsData: . none, dstLangCode: dstLangCode) ,
@@ -265,13 +284,18 @@ extension [TranslationEntity.CompositeChatItem] {
265
284
}
266
285
}
267
286
268
- fileprivate func updateTranslation( item: RealTimeEntity . Translation . Response ) async -> [ TranslationEntity . CompositeChatItem ] {
287
+ fileprivate func updateTranslation( item: RealTimeEntity . Translation . Response ) async
288
+ -> [ TranslationEntity . CompositeChatItem ]
289
+ {
269
290
await withCheckedContinuation { continuation in
270
- guard let firstIndex = self . firstIndex ( where: { $0. id == item. contentData. chatList. first? . chatID } ) else {
291
+ guard
292
+ let firstIndex = self . firstIndex ( where: { $0. id == item. contentData. chatList. first? . chatID }
293
+ )
294
+ else {
271
295
return continuation. resume ( returning: self )
272
296
}
273
297
274
- let range = firstIndex ..< ( firstIndex + item. contentData. chatList. count)
298
+ let range = firstIndex..< ( firstIndex + item. contentData. chatList. count)
275
299
var mutatedArray : [ TranslationEntity . CompositeChatItem ] = [ ]
276
300
277
301
for index in range {
0 commit comments