Skip to content

Try to add retry feature of live translating #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions MyLibrary/Sources/LiveTranslationFeature/LiveTranslationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -45,7 +45,7 @@ public struct LiveTranslationView: View {
}
}
}

HStack {
Spacer()
Text("Powered by", bundle: .module)
Expand All @@ -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)
Expand All @@ -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")
}
Expand Down
12 changes: 10 additions & 2 deletions MyLibrary/Sources/LiveTranslationFeature/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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):
Expand Down