Skip to content
Open
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
21 changes: 21 additions & 0 deletions IceCubesApp/Resources/Localization/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -31903,6 +31903,9 @@
}
}
}
},
"Full timeline fetch" : {

},
"Home Timeline" : {
"localizations" : {
Expand Down Expand Up @@ -56717,6 +56720,7 @@
}
},
"settings.section.other.footer" : {
"extractionState" : "stale",
"localizations" : {
"be" : {
"stringUnit" : {
Expand Down Expand Up @@ -69283,6 +69287,23 @@
}
}
},
"status.editor.media.copy-text" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "🤖 Copy text from image"
}
},
"en-GB" : {
"stringUnit" : {
"state" : "translated",
"value" : "🤖 Copy text from image"
}
}
}
},
"status.editor.media.edit-image" : {
"extractionState" : "manual",
"localizations" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import DesignSystem
import Env
import Models
import NetworkClient
import Nuke
import SwiftUI
import VisionKit

extension StatusEditor {
@MainActor
Expand All @@ -22,6 +24,7 @@ extension StatusEditor {

@State private var didAppear: Bool = false
@State private var isGeneratingDescription: Bool = false
@State private var isGeneratingText: Bool = false

@State private var showTranslateView: Bool = false
@State private var isTranslating: Bool = false
Expand All @@ -38,6 +41,7 @@ extension StatusEditor {
.focused($isFieldFocused)
if imageDescription.isEmpty {
generateButton
copyTextButton
}
#if canImport(_Translation_SwiftUI)
if #available(iOS 17.4, *), !imageDescription.isEmpty {
Expand Down Expand Up @@ -133,6 +137,25 @@ extension StatusEditor {
}
}

@ViewBuilder
private var copyTextButton: some View {
if let url = container.mediaAttachment?.url {
Button {
Task {
if let description = await generateText(url: url) {
imageDescription = description
}
}
} label: {
if isGeneratingText {
ProgressView()
} else {
Text("status.editor.media.copy-text")
}
}
}
}

@ViewBuilder
private var translateButton: some View {
Button {
Expand All @@ -156,5 +179,25 @@ extension StatusEditor {
isGeneratingDescription = false
return response?.trimmedText
}

private func generateText(url: URL) async -> String? {
isGeneratingText = true
defer {
isGeneratingText = false
}

let analyzer = ImageAnalyzer()
do {
let image = try await ImagePipeline.shared.image(for: url)
let configuration = ImageAnalyzer.Configuration([.text])

let analysis = try await analyzer.analyze(image, configuration: configuration)
return analysis.transcript
} catch {
print("Error: \(error)")
}

return nil
}
}
}