From ce0a41f76aec2036e2bf00fd9383d4e0fe3fbd84 Mon Sep 17 00:00:00 2001 From: touyou Date: Wed, 9 Apr 2025 20:41:50 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20try=20to=20add=20retry=20feature=20?= =?UTF-8?q?when=20translation=20stream=20is=20closed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LiveTranslationView.swift | 27 ++++++++++++------- .../LiveTranslationFeature/ViewModel.swift | 12 +++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/MyLibrary/Sources/LiveTranslationFeature/LiveTranslationView.swift b/MyLibrary/Sources/LiveTranslationFeature/LiveTranslationView.swift index b742dbb..423c278 100644 --- a/MyLibrary/Sources/LiveTranslationFeature/LiveTranslationView.swift +++ b/MyLibrary/Sources/LiveTranslationFeature/LiveTranslationView.swift @@ -5,17 +5,17 @@ public struct LiveTranslationView: View { let viewModel: ViewModel @State var isSelectedLanguageSheet: Bool = false @State var isShowingLastChat: Bool = false - + private let scrollContentBottomID: String = "atBottom" - + public init( roomNumber: String = ProcessInfo.processInfo.environment["LIVE_TRANSLATION_KEY"] - ?? (Bundle.main.infoDictionary?["Live translation room number"] as? String) ?? "" + ?? (Bundle.main.infoDictionary?["Live translation room number"] as? String) ?? "" ) { print(roomNumber) self.viewModel = ViewModel(roomNumber: roomNumber) } - + public var body: some View { NavigationStack { VStack { @@ -45,7 +45,7 @@ public struct LiveTranslationView: View { } } } - + HStack { Spacer() Text("Powered by", bundle: .module) @@ -66,9 +66,9 @@ public struct LiveTranslationView: View { reader.scrollTo(scrollContentBottomID, anchor: .bottom) return } - + guard isShowingLastChat else { return } - + guard new != .none else { return } withAnimation(.interactiveSpring) { reader.scrollTo(scrollContentBottomID, anchor: .center) @@ -82,12 +82,21 @@ public struct LiveTranslationView: View { } .navigationTitle(Text("Live translation", bundle: .module)) .toolbar { - ToolbarItem(placement: .navigationBarTrailing) { + if !viewModel.isConnected { + ToolbarItem(placement: .topBarLeading) { + Button { + viewModel.send(.connectChatStream) + } label: { + Image(systemName: "arrow.trianglehead.2.clockwise") + } + } + } + ToolbarItem(placement: .topBarTrailing) { Button { isSelectedLanguageSheet.toggle() } label: { let selectedLanguage = - viewModel.langSet?.langCodingKey(viewModel.selectedLangCode) ?? "" + viewModel.langSet?.langCodingKey(viewModel.selectedLangCode) ?? "" Text(selectedLanguage) Image(systemName: "globe") } diff --git a/MyLibrary/Sources/LiveTranslationFeature/ViewModel.swift b/MyLibrary/Sources/LiveTranslationFeature/ViewModel.swift index bc26f22..241646e 100644 --- a/MyLibrary/Sources/LiveTranslationFeature/ViewModel.swift +++ b/MyLibrary/Sources/LiveTranslationFeature/ViewModel.swift @@ -23,6 +23,10 @@ public final class ViewModel { var updateChatWaitingQueue: [RealTimeEntity.Chat.Response] = [] var updateTrWaitingQueue: [RealTimeEntity.Translation.Response] = [] var latestListType: RealTimeEntity.ListType? = .none + + /// # isConnected + /// connecting state with LiveTranslationService + var isConnected: Bool = false } extension ViewModel { @@ -84,8 +88,12 @@ extension ViewModel { let stream = service.chatConnection(.init(interactionKey: roomNumber)) for try await action in stream { switch action { - case .connect: break - case .disconnect: break + case .connect: + self.isConnected = true + break + case .disconnect: + self.isConnected = false + break case .peerClosed: send(.connectChatStream) case .responseChat(let chatItem):