mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-11-26 10:11:00 +00:00
Add a translate button after generating image description
This commit is contained in:
parent
4f9e23296f
commit
f2cd05968e
1 changed files with 74 additions and 17 deletions
|
@ -5,6 +5,7 @@ import Shimmer
|
|||
import SwiftUI
|
||||
import Network
|
||||
|
||||
@MainActor
|
||||
struct StatusEditorMediaEditView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@Environment(Theme.self) private var theme
|
||||
|
@ -22,6 +23,9 @@ struct StatusEditorMediaEditView: View {
|
|||
@State private var didAppear: Bool = false
|
||||
@State private var isGeneratingDescription: Bool = false
|
||||
|
||||
@State private var showTranslateButton: Bool = false
|
||||
@State private var isTranslating: Bool = false
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
|
@ -30,23 +34,8 @@ struct StatusEditorMediaEditView: View {
|
|||
text: $imageDescription,
|
||||
axis: .vertical)
|
||||
.focused($isFieldFocused)
|
||||
if let url = container.mediaAttachment?.url, preferences.isOpenAIEnabled {
|
||||
Button {
|
||||
isGeneratingDescription = true
|
||||
Task {
|
||||
let client = OpenAIClient()
|
||||
let response = try await client.request(.imageDescription(image: url))
|
||||
imageDescription = response.trimmedText
|
||||
isGeneratingDescription = false
|
||||
}
|
||||
} label: {
|
||||
if isGeneratingDescription {
|
||||
ProgressView()
|
||||
} else {
|
||||
Text("status.editor.media.generate-description")
|
||||
}
|
||||
}
|
||||
}
|
||||
generateButton
|
||||
translateButton
|
||||
}
|
||||
.listRowBackground(theme.primaryBackgroundColor)
|
||||
Section {
|
||||
|
@ -118,4 +107,72 @@ struct StatusEditorMediaEditView: View {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var generateButton: some View {
|
||||
if let url = container.mediaAttachment?.url, preferences.isOpenAIEnabled {
|
||||
Button {
|
||||
Task {
|
||||
if let description = await generateDescription(url: url) {
|
||||
imageDescription = description
|
||||
let lang = preferences.serverPreferences?.postLanguage ?? Locale.current.language.languageCode?.identifier
|
||||
if lang != nil, lang != "en" {
|
||||
withAnimation {
|
||||
showTranslateButton = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
if isGeneratingDescription {
|
||||
ProgressView()
|
||||
} else {
|
||||
Text("status.editor.media.generate-description")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var translateButton: some View {
|
||||
if showTranslateButton {
|
||||
Button {
|
||||
Task {
|
||||
if let description = await translateDescription() {
|
||||
imageDescription = description
|
||||
withAnimation {
|
||||
showTranslateButton = false
|
||||
}
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
if isTranslating {
|
||||
ProgressView()
|
||||
} else {
|
||||
Text("status.action.translate")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private func generateDescription(url: URL) async -> String? {
|
||||
isGeneratingDescription = true
|
||||
let client = OpenAIClient()
|
||||
let response = try? await client.request(.imageDescription(image: url))
|
||||
isGeneratingDescription = false
|
||||
return response?.trimmedText
|
||||
}
|
||||
|
||||
private func translateDescription() async -> String? {
|
||||
isTranslating = true
|
||||
let userAPIKey = DeepLUserAPIHandler.readIfAllowed()
|
||||
let userAPIFree = UserPreferences.shared.userDeeplAPIFree
|
||||
let deeplClient = DeepLClient(userAPIKey: userAPIKey, userAPIFree: userAPIFree)
|
||||
let lang = preferences.serverPreferences?.postLanguage ?? Locale.current.language.languageCode?.identifier
|
||||
guard let lang else { return nil }
|
||||
let translation = try? await deeplClient.request(target: lang, text: imageDescription)
|
||||
isTranslating = false
|
||||
return translation?.content.asRawText
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue